From 2e09b4f72f06537431151fe8b2574e1aa886ad48 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 18 Sep 2021 15:57:24 +0300 Subject: [PATCH] Use friend declarations to access OpenGL IDs of objects This seems neater than exposing get_id() methods to the public. Texture2D::AsyncLoader still uses get_id() on the PBO for now, since it's not reasonably possible to name that class as a friend. --- source/core/buffer.h | 3 +++ source/core/commands.cpp | 8 ++++---- source/core/framebuffer.cpp | 34 ++++++++++++++++------------------ source/core/framebuffer.h | 5 +++-- source/core/pipelinestate.cpp | 19 +++++++++---------- source/core/program.h | 4 ++-- source/core/sampler.h | 4 ++-- source/core/texture.h | 5 ++--- source/core/vertexsetup.cpp | 8 ++++---- source/core/vertexsetup.h | 4 ++-- 10 files changed, 47 insertions(+), 47 deletions(-) diff --git a/source/core/buffer.h b/source/core/buffer.h index 9144afae..db0ff025 100644 --- a/source/core/buffer.h +++ b/source/core/buffer.h @@ -25,6 +25,9 @@ UniformBlock classes contain built-in support for buffers. */ class Buffer { + friend class PipelineState; + friend class VertexSetup; + private: unsigned id; unsigned size; diff --git a/source/core/commands.cpp b/source/core/commands.cpp index 97da5dad..529cd392 100644 --- a/source/core/commands.cpp +++ b/source/core/commands.cpp @@ -54,17 +54,17 @@ void Commands::resolve_multisample(Framebuffer &target, BufferBits buffers) unsigned height = min(source->get_height(), target.get_height()); if(ARB_direct_state_access) - glBlitNamedFramebuffer(source->get_id(), target.get_id(), 0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST); + glBlitNamedFramebuffer(source->id, target.id, 0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST); else { - glBindFramebuffer(GL_READ_FRAMEBUFFER, source->get_id()); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target.get_id()); + glBindFramebuffer(GL_READ_FRAMEBUFFER, source->id); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target.id); target.refresh(); glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST); - glBindFramebuffer(GL_FRAMEBUFFER, source->get_id()); + glBindFramebuffer(GL_FRAMEBUFFER, source->id); } } diff --git a/source/core/framebuffer.cpp b/source/core/framebuffer.cpp index a60b437e..97b5c8b1 100644 --- a/source/core/framebuffer.cpp +++ b/source/core/framebuffer.cpp @@ -139,24 +139,23 @@ void Framebuffer::update() const const Attachment &attch = attachments[i]; if(attch.tex) { - GLenum type = attch.tex->get_target(); if(ARB_direct_state_access) { - if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0) - glNamedFramebufferTexture(id, gl_attach_point, attch.tex->get_id(), attch.level); + if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0) + glNamedFramebufferTexture(id, gl_attach_point, attch.tex->id, attch.level); else - glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->get_id(), attch.level, attch.layer); + glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->id, attch.level, attch.layer); } - else if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE) - glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, type, attch.tex->get_id(), attch.level); + else if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE) + glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level); else if(attch.layer<0) - glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->get_id(), attch.level); - else if(type==GL_TEXTURE_2D_ARRAY) - glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->get_id(), attch.level, attch.layer); - else if(type==GL_TEXTURE_3D) - glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, type, attch.tex->get_id(), attch.level, attch.layer); - else if(type==GL_TEXTURE_CUBE_MAP) - glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast(attch.layer)), attch.tex->get_id(), attch.level); + glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level); + else if(attch.tex->target==GL_TEXTURE_2D_ARRAY) + glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level, attch.layer); + else if(attch.tex->target==GL_TEXTURE_3D) + glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level, attch.layer); + else if(attch.tex->target==GL_TEXTURE_CUBE_MAP) + glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast(attch.layer)), attch.tex->id, attch.level); } else if(ARB_direct_state_access) glNamedFramebufferTexture(id, gl_attach_point, 0, 0); @@ -206,28 +205,27 @@ void Framebuffer::check_size() for(Attachment &a: attachments) if(a.tex) { - GLenum type = a.tex->get_target(); unsigned w = 0; unsigned h = 0; - if(type==GL_TEXTURE_2D) + if(a.tex->target==GL_TEXTURE_2D) { Texture2D *tex = static_cast(a.tex); w = max(tex->get_width()>>a.level, 1U); h = max(tex->get_height()>>a.level, 1U); } - else if(type==GL_TEXTURE_2D_MULTISAMPLE) + else if(a.tex->target==GL_TEXTURE_2D_MULTISAMPLE) { Texture2DMultisample *tex = static_cast(a.tex); w = tex->get_width(); h = tex->get_height(); } - else if(type==GL_TEXTURE_3D || type==GL_TEXTURE_2D_ARRAY) + else if(a.tex->target==GL_TEXTURE_3D || a.tex->target==GL_TEXTURE_2D_ARRAY) { Texture3D *tex = static_cast(a.tex); w = max(tex->get_width()>>a.level, 1U); h = max(tex->get_height()>>a.level, 1U); } - else if(type==GL_TEXTURE_CUBE_MAP) + else if(a.tex->target==GL_TEXTURE_CUBE_MAP) { w = max(static_cast(a.tex)->get_size()>>a.level, 1U); h = w; diff --git a/source/core/framebuffer.h b/source/core/framebuffer.h index f2fbfea4..2f867e6c 100644 --- a/source/core/framebuffer.h +++ b/source/core/framebuffer.h @@ -62,6 +62,9 @@ the GL_EXT_framebuffer_blit extension. */ class Framebuffer { + friend class Commands; + friend class PipelineState; + private: struct Attachment { @@ -139,8 +142,6 @@ public: void refresh() const { if(dirty) update(); } - unsigned get_id() const { return id; } - void set_debug_name(const std::string &); static Framebuffer &system(); diff --git a/source/core/pipelinestate.cpp b/source/core/pipelinestate.cpp index c781b6a6..31bde278 100644 --- a/source/core/pipelinestate.cpp +++ b/source/core/pipelinestate.cpp @@ -173,7 +173,7 @@ void PipelineState::apply(unsigned mask) const { if(mask&FRAMEBUFFER) { - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->get_id() : 0); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0); if(framebuffer) { framebuffer->refresh(); @@ -201,11 +201,11 @@ void PipelineState::apply(unsigned mask) const } if(mask&SHPROG) - glUseProgram(shprog ? shprog->get_id() : 0); + glUseProgram(shprog ? shprog->id : 0); if(mask&VERTEX_SETUP) { - glBindVertexArray(vertex_setup ? vertex_setup->get_id() : 0); + glBindVertexArray(vertex_setup ? vertex_setup->id : 0); if(vertex_setup) { static Require _req(MSP_primitive_restart); @@ -255,19 +255,18 @@ void PipelineState::apply(unsigned mask) const if(t.texture && t.sampler) { if(ARB_direct_state_access) - glBindTextureUnit(t.binding, t.texture->get_id()); + glBindTextureUnit(t.binding, t.texture->id); else { glActiveTexture(GL_TEXTURE0+t.binding); - if(bound_tex_targets[t.binding] && static_cast(t.texture->get_target())!=bound_tex_targets[t.binding]) + if(bound_tex_targets[t.binding] && static_cast(t.texture->target)!=bound_tex_targets[t.binding]) glBindTexture(bound_tex_targets[t.binding], 0); - glBindTexture(t.texture->get_target(), t.texture->get_id()); + glBindTexture(t.texture->target, t.texture->id); } - bound_tex_targets[t.binding] = t.texture->get_target(); + bound_tex_targets[t.binding] = t.texture->target; - glBindSampler(t.binding, t.sampler->get_id()); - t.sampler->refresh(); + glBindSampler(t.binding, t.sampler->id); } t.changed = false; @@ -284,7 +283,7 @@ void PipelineState::apply(unsigned mask) const if(u.binding>=0) { const BufferBackedUniformBlock *block = static_cast(u.block); - glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, block->get_buffer()->get_id(), block->get_offset(), block->get_data_size()); + glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, block->get_buffer()->id, block->get_offset(), block->get_data_size()); bound_uniform_blocks[u.binding] = 1; } else diff --git a/source/core/program.h b/source/core/program.h index ecfe1a19..1ca146f1 100644 --- a/source/core/program.h +++ b/source/core/program.h @@ -19,6 +19,8 @@ generated with a set of standard features. */ class Program { + friend class PipelineState; + public: class Loader: public DataFile::CollectionObjectLoader { @@ -168,8 +170,6 @@ public: const AttributeInfo &get_attribute_info(const std::string &) const; int get_attribute_location(const std::string &) const; - unsigned get_id() const { return id; } - void set_debug_name(const std::string &); private: void set_stage_debug_name(unsigned, Stage); diff --git a/source/core/sampler.h b/source/core/sampler.h index 524a8fc5..852c9710 100644 --- a/source/core/sampler.h +++ b/source/core/sampler.h @@ -63,6 +63,8 @@ wrapping is applied. The default for all directions is REPEAT. */ class Sampler { + friend class PipelineState; + public: class Loader: public DataFile::ObjectLoader { @@ -152,8 +154,6 @@ public: void refresh() const { if(dirty_params) update(); } - unsigned get_id() const { return id; } - void set_debug_name(const std::string &); }; diff --git a/source/core/texture.h b/source/core/texture.h index 0377c022..6d474b88 100644 --- a/source/core/texture.h +++ b/source/core/texture.h @@ -21,6 +21,8 @@ can be used for texture minification; see the Sampler class for details. */ class Texture: public Resource { + friend class Framebuffer; + friend class PipelineState; protected: class Loader: public DataFile::CollectionObjectLoader { @@ -92,9 +94,6 @@ public: with the defined storage. Semantics depend on the type of texture. */ virtual void image(const Graphics::Image &, unsigned = 0) = 0; - GLenum get_target() const { return target; } - unsigned get_id() const { return id; } - virtual std::uint64_t get_data_size() const { return 0; } void set_debug_name(const std::string &); diff --git a/source/core/vertexsetup.cpp b/source/core/vertexsetup.cpp index 0971aed1..cda38b45 100644 --- a/source/core/vertexsetup.cpp +++ b/source/core/vertexsetup.cpp @@ -137,9 +137,9 @@ void VertexSetup::update() const if(dirty&INDEX_BUFFER) { if(direct) - glVertexArrayElementBuffer(id, index_buffer->get_id()); + glVertexArrayElementBuffer(id, index_buffer->id); else - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer->get_id()); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer->id); } dirty = 0; @@ -150,14 +150,14 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding if(!direct) { Buffer::unbind_scratch(); - glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->get_id()); + glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->id); } const VertexFormat &fmt = array.get_format(); unsigned stride = fmt.stride(); if(direct) { - glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride); + glVertexArrayVertexBuffer(id, binding, array.get_buffer()->id, 0, stride); glVertexArrayBindingDivisor(id, binding, divisor); } diff --git a/source/core/vertexsetup.h b/source/core/vertexsetup.h index d0ed8703..2e483fc8 100644 --- a/source/core/vertexsetup.h +++ b/source/core/vertexsetup.h @@ -16,6 +16,8 @@ objects. Intended for internal use. */ class VertexSetup { + friend class PipelineState; + private: enum ComponentMask { @@ -57,8 +59,6 @@ private: public: void refresh() const { if(dirty) update(); } - unsigned get_id() const { return id; } - void unload(); void set_debug_name(const std::string &); -- 2.45.2