]> git.tdb.fi Git - libs/gl.git/commitdiff
Use friend declarations to access OpenGL IDs of objects
authorMikko Rasa <tdb@tdb.fi>
Sat, 18 Sep 2021 12:57:24 +0000 (15:57 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 18 Sep 2021 13:20:19 +0000 (16:20 +0300)
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
source/core/commands.cpp
source/core/framebuffer.cpp
source/core/framebuffer.h
source/core/pipelinestate.cpp
source/core/program.h
source/core/sampler.h
source/core/texture.h
source/core/vertexsetup.cpp
source/core/vertexsetup.h

index 9144afaed3d1682f28efd8076988c8e815a977db..db0ff025f3b1f816283fe0bed7a696d95d7ccff9 100644 (file)
@@ -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;
index 97da5dadbe9cbc29dcfcbf22a36009decdd79072..529cd3921fa5ef0243a6a78a6ffe2be6c1f6654d 100644 (file)
@@ -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);
        }
 }
 
index a60b437eff6fbe9e4c9a1eec8e2b6012b35395af..97b5c8b1394e428bcc640339af98d58a68607462 100644 (file)
@@ -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<TextureCubeFace>(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<TextureCubeFace>(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<Texture2D *>(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<Texture2DMultisample *>(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<Texture3D *>(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<TextureCube *>(a.tex)->get_size()>>a.level, 1U);
                                h = w;
index f2fbfea40e19bca9577cd97a540d6bfcfb3b1673..2f867e6c591ec69706df22fc2a90bbeef52575f8 100644 (file)
@@ -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();
index c781b6a6413877f515ae751266ed2279206ab663..31bde278f44b9405a9cd6bad27c750a33babe80a 100644 (file)
@@ -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<int>(t.texture->get_target())!=bound_tex_targets[t.binding])
+                                               if(bound_tex_targets[t.binding] && static_cast<int>(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<const BufferBackedUniformBlock *>(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
index ecfe1a1991232e0d686c68363d7f1f9b5e8b847c..1ca146f1e998c0fd0d05221a4dab0b66a6da31e6 100644 (file)
@@ -19,6 +19,8 @@ generated with a set of standard features.
 */
 class Program
 {
+       friend class PipelineState;
+
 public:
        class Loader: public DataFile::CollectionObjectLoader<Program>
        {
@@ -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);
index 524a8fc5f95f01fbee29faa87a7f731c50b0e55d..852c971003359bb7d0f0341dd97becef7242017d 100644 (file)
@@ -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<Sampler>
        {
@@ -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 &);
 };
 
index 0377c022c30137898da1257571eba01c3dbfd543..6d474b8864ec392375041e47821ca29016d66006 100644 (file)
@@ -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<Texture>
        {
@@ -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 &);
index 0971aed1c62f08c8c2823e873bcf86ce5ba15ba4..cda38b458fbb670bca4426d2c7060e4d7786d0f2 100644 (file)
@@ -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);
        }
 
index d0ed870378034334b6935b092dcfbcae80909c80..2e483fc81768e1fcb3b4c0b15a5c91b53fd346cb 100644 (file)
@@ -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 &);