From 3b98e13c823d4cb7e4d2d4d14e8440b44bc71f91 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 17 Nov 2021 16:49:52 +0200 Subject: [PATCH] Adjust access to main class members from backend classes --- .../backends/opengl/framebuffer_backend.cpp | 15 ++++---- .../backends/opengl/pipelinestate_backend.cpp | 38 +++++++++---------- source/backends/opengl/sampler_backend.cpp | 22 +++++------ source/backends/opengl/texture1d_backend.cpp | 20 +++++----- source/backends/opengl/texture2d_backend.cpp | 22 +++++------ .../opengl/texture2dmultisample_backend.cpp | 16 +++----- source/backends/opengl/texture3d_backend.cpp | 24 +++++------- .../backends/opengl/texturecube_backend.cpp | 18 ++++----- .../backends/opengl/vertexsetup_backend.cpp | 14 ++++--- 9 files changed, 88 insertions(+), 101 deletions(-) diff --git a/source/backends/opengl/framebuffer_backend.cpp b/source/backends/opengl/framebuffer_backend.cpp index de8a9c6e..18566ac3 100644 --- a/source/backends/opengl/framebuffer_backend.cpp +++ b/source/backends/opengl/framebuffer_backend.cpp @@ -76,23 +76,24 @@ void OpenGLFramebuffer::require_layered() void OpenGLFramebuffer::resize_system(unsigned w, unsigned h) { - Framebuffer *self = static_cast(this); - self->width = w; - self->height = h; + Framebuffer &self = *static_cast(this); + self.width = w; + self.height = h; } void OpenGLFramebuffer::update(unsigned mask) const { - const FrameFormat &format = static_cast(this)->format; + const Framebuffer &self = *static_cast(this); + vector color_bufs; - color_bufs.reserve(format.size()); + color_bufs.reserve(self.format.size()); unsigned i = 0; - for(FrameAttachment a: format) + for(FrameAttachment a: self.format) { GLenum gl_attach_point = get_gl_attachment(a); if(mask&(1<(this)->attachments[i]; + const Framebuffer::Attachment &attch = self.attachments[i]; if(attch.tex) { if(ARB_direct_state_access) diff --git a/source/backends/opengl/pipelinestate_backend.cpp b/source/backends/opengl/pipelinestate_backend.cpp index 91789701..7cd65400 100644 --- a/source/backends/opengl/pipelinestate_backend.cpp +++ b/source/backends/opengl/pipelinestate_backend.cpp @@ -56,11 +56,11 @@ void OpenGLPipelineState::apply() const void OpenGLPipelineState::apply(unsigned mask) const { - const PipelineState *self = static_cast(this); + const PipelineState &self = *static_cast(this); if(mask&PipelineState::FRAMEBUFFER) { - const Framebuffer *framebuffer = self->framebuffer; + const Framebuffer *framebuffer = self.framebuffer; glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0); if(framebuffer) { @@ -71,15 +71,15 @@ void OpenGLPipelineState::apply(unsigned mask) const if(mask&(PipelineState::VIEWPORT|PipelineState::FRAMEBUFFER)) { - if(const Rect *viewport = self->viewport) + if(const Rect *viewport = self.viewport) glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height); - else if(const Framebuffer *framebuffer = self->framebuffer) + else if(const Framebuffer *framebuffer = self.framebuffer) glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height()); } if(mask&PipelineState::SCISSOR) { - if(const Rect *scissor = self->scissor) + if(const Rect *scissor = self.scissor) { glEnable(GL_SCISSOR_TEST); glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height); @@ -90,9 +90,9 @@ void OpenGLPipelineState::apply(unsigned mask) const if(mask&PipelineState::SHPROG) { - glUseProgram(self->shprog ? self->shprog->id : 0); + glUseProgram(self.shprog ? self.shprog->id : 0); - unsigned ncd = (self->shprog ? self->shprog->get_n_clip_distances() : 0); + unsigned ncd = (self.shprog ? self.shprog->get_n_clip_distances() : 0); if(ncd!=n_clip_distances) { for(unsigned i=0; (iuniform_blocks) + for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks) if(u.changed || mask==~0U) { if(u.block) @@ -118,10 +118,10 @@ void OpenGLPipelineState::apply(unsigned mask) const glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size()); bound_uniform_blocks[u.binding] = 1; } - else if(self->shprog) + else if(self.shprog) { const char *data = static_cast(u.block->get_data_pointer()); - for(const Program::UniformCall &call: self->shprog->uniform_calls) + for(const Program::UniformCall &call: self.shprog->uniform_calls) call.func(call.location, call.size, data+call.location*16); } } @@ -132,7 +132,7 @@ void OpenGLPipelineState::apply(unsigned mask) const if(mask&PipelineState::TEXTURES) { - for(const PipelineState::BoundTexture &t: self->textures) + for(const PipelineState::BoundTexture &t: self.textures) if(t.changed || mask==~0U) { if(t.texture && t.sampler) @@ -159,7 +159,7 @@ void OpenGLPipelineState::apply(unsigned mask) const if(mask&PipelineState::VERTEX_SETUP) { - const VertexSetup *vertex_setup = self->vertex_setup; + const VertexSetup *vertex_setup = self.vertex_setup; glBindVertexArray(vertex_setup ? vertex_setup->id : 0); if(vertex_setup) { @@ -179,12 +179,12 @@ void OpenGLPipelineState::apply(unsigned mask) const if(mask&PipelineState::FACE_CULL) { - glFrontFace(self->front_face==CLOCKWISE ? GL_CW : GL_CCW); + glFrontFace(self.front_face==CLOCKWISE ? GL_CW : GL_CCW); - if(self->face_cull!=NO_CULL && self->front_face!=NON_MANIFOLD) + if(self.face_cull!=NO_CULL && self.front_face!=NON_MANIFOLD) { glEnable(GL_CULL_FACE); - glCullFace(self->face_cull==CULL_FRONT ? GL_FRONT : GL_BACK); + glCullFace(self.face_cull==CULL_FRONT ? GL_FRONT : GL_BACK); } else glDisable(GL_CULL_FACE); @@ -192,7 +192,7 @@ void OpenGLPipelineState::apply(unsigned mask) const if(mask&PipelineState::DEPTH_TEST) { - const DepthTest *depth_test = self->depth_test; + const DepthTest *depth_test = self.depth_test; if(depth_test && depth_test->enabled) { glEnable(GL_DEPTH_TEST); @@ -206,7 +206,7 @@ void OpenGLPipelineState::apply(unsigned mask) const if(mask&PipelineState::STENCIL_TEST) { - const StencilTest *stencil_test = self->stencil_test; + const StencilTest *stencil_test = self.stencil_test; if(stencil_test && stencil_test->enabled) { glEnable(GL_STENCIL_TEST); @@ -219,7 +219,7 @@ void OpenGLPipelineState::apply(unsigned mask) const if(mask&PipelineState::BLEND) { - const Blend *blend = self->blend; + const Blend *blend = self.blend; if(blend && blend->enabled) { glEnable(GL_BLEND); @@ -237,7 +237,7 @@ void OpenGLPipelineState::apply(unsigned mask) const } last_applied = this; - self->changes &= ~mask; + self.changes &= ~mask; } void OpenGLPipelineState::clear() diff --git a/source/backends/opengl/sampler_backend.cpp b/source/backends/opengl/sampler_backend.cpp index 8198e40f..31dcc6bc 100644 --- a/source/backends/opengl/sampler_backend.cpp +++ b/source/backends/opengl/sampler_backend.cpp @@ -46,26 +46,26 @@ bool OpenGLSampler::check_anisotropic(bool require) void OpenGLSampler::update(unsigned mask) const { - const Sampler *self = static_cast(this); + const Sampler &self = *static_cast(this); if(mask&Sampler::MIN_FILTER) - glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, get_gl_filter(self->min_filter)); + glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, get_gl_filter(self.min_filter)); if(mask&Sampler::MAG_FILTER) - glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, get_gl_filter(self->mag_filter)); + glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, get_gl_filter(self.mag_filter)); if(mask&Sampler::MAX_ANISOTROPY) - glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, self->max_anisotropy); + glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, self.max_anisotropy); if(mask&Sampler::WRAP_S) - glSamplerParameteri(id, GL_TEXTURE_WRAP_S, get_gl_wrap(self->wrap_s)); + glSamplerParameteri(id, GL_TEXTURE_WRAP_S, get_gl_wrap(self.wrap_s)); if(mask&Sampler::WRAP_T) - glSamplerParameteri(id, GL_TEXTURE_WRAP_T, get_gl_wrap(self->wrap_t)); + glSamplerParameteri(id, GL_TEXTURE_WRAP_T, get_gl_wrap(self.wrap_t)); if(mask&Sampler::WRAP_R) - glSamplerParameteri(id, GL_TEXTURE_WRAP_R, get_gl_wrap(self->wrap_r)); + glSamplerParameteri(id, GL_TEXTURE_WRAP_R, get_gl_wrap(self.wrap_r)); if(mask&Sampler::BORDER_COLOR) - glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &self->border_color.r); + glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &self.border_color.r); if(mask&Sampler::COMPARE) { - glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, (self->compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE)); - if(self->compare) - glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, get_gl_predicate(self->cmp_func)); + glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, (self.compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE)); + if(self.compare) + glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, get_gl_predicate(self.cmp_func)); } } diff --git a/source/backends/opengl/texture1d_backend.cpp b/source/backends/opengl/texture1d_backend.cpp index b8424872..8e33d278 100644 --- a/source/backends/opengl/texture1d_backend.cpp +++ b/source/backends/opengl/texture1d_backend.cpp @@ -16,8 +16,7 @@ OpenGLTexture1D::OpenGLTexture1D(): void OpenGLTexture1D::allocate() { - unsigned width = static_cast(this)->width; - unsigned levels = static_cast(this)->levels; + const Texture1D &self = *static_cast(this); if(!id) create(); @@ -26,22 +25,22 @@ void OpenGLTexture1D::allocate() if(ARB_texture_storage) { if(ARB_direct_state_access) - glTextureStorage1D(id, levels, gl_fmt, width); + glTextureStorage1D(id, self.levels, gl_fmt, self.width); else { bind_scratch(); - glTexStorage1D(target, levels, gl_fmt, width); + glTexStorage1D(target, self.levels, gl_fmt, self.width); } } else { bind_scratch(); - glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1); + glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, self.levels-1); GLenum comp = get_gl_components(get_components(storage_fmt)); GLenum type = get_gl_type(get_component_type(storage_fmt)); - for(unsigned i=0; i(this)->get_level_size(i); + unsigned lv_size = self.get_level_size(i); glTexImage1D(target, i, gl_fmt, lv_size, 0, comp, type, 0); } } @@ -67,12 +66,11 @@ size_t OpenGLTexture1D::get_data_size() const if(!id) return 0; - unsigned width = static_cast(this)->width; - unsigned levels = static_cast(this)->levels; + const Texture1D &self = *static_cast(this); - size_t level_size = width*get_pixel_size(storage_fmt); + size_t level_size = self.width*get_pixel_size(storage_fmt); size_t total_size = level_size; - for(unsigned i=0; i>=2) + for(unsigned i=0; i>=2) total_size += level_size; return total_size; } diff --git a/source/backends/opengl/texture2d_backend.cpp b/source/backends/opengl/texture2d_backend.cpp index 45ba78cc..54c82280 100644 --- a/source/backends/opengl/texture2d_backend.cpp +++ b/source/backends/opengl/texture2d_backend.cpp @@ -41,9 +41,7 @@ OpenGLTexture2D::OpenGLTexture2D(): void OpenGLTexture2D::allocate() { - unsigned width = static_cast(this)->width; - unsigned height = static_cast(this)->height; - unsigned levels = static_cast(this)->levels; + const Texture2D &self = *static_cast(this); if(!id) create(); @@ -52,22 +50,22 @@ void OpenGLTexture2D::allocate() if(ARB_texture_storage) { if(ARB_direct_state_access) - glTextureStorage2D(id, levels, gl_fmt, width, height); + glTextureStorage2D(id, self.levels, gl_fmt, self.width, self.height); else { bind_scratch(); - glTexStorage2D(target, levels, gl_fmt, width, height); + glTexStorage2D(target, self.levels, gl_fmt, self.width, self.height); } } else { bind_scratch(); - glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1); + glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, self.levels-1); GLenum comp = get_gl_components(get_components(storage_fmt)); GLenum type = get_gl_type(get_component_type(storage_fmt)); - for(unsigned i=0; i(this)->get_level_size(i); + auto lv_size = self.get_level_size(i); glTexImage2D(target, i, gl_fmt, lv_size.x, lv_size.y, 0, comp, type, 0); } } @@ -105,13 +103,11 @@ uint64_t OpenGLTexture2D::get_data_size() const if(!id) return 0; - unsigned width = static_cast(this)->width; - unsigned height = static_cast(this)->height; - unsigned levels = static_cast(this)->levels; + const Texture2D &self = *static_cast(this); - size_t level_size = width*height*get_pixel_size(format); + size_t level_size = self.width*self.height*get_pixel_size(format); size_t total_size = level_size; - for(unsigned i=0; i>=2) + for(unsigned i=0; i>=2) total_size += level_size; return total_size; } diff --git a/source/backends/opengl/texture2dmultisample_backend.cpp b/source/backends/opengl/texture2dmultisample_backend.cpp index e6ee7614..f0675044 100644 --- a/source/backends/opengl/texture2dmultisample_backend.cpp +++ b/source/backends/opengl/texture2dmultisample_backend.cpp @@ -15,9 +15,7 @@ OpenGLTexture2DMultisample::OpenGLTexture2DMultisample(): void OpenGLTexture2DMultisample::allocate() { - unsigned width = static_cast(this)->width; - unsigned height = static_cast(this)->height; - unsigned samples = static_cast(this)->samples; + const Texture2DMultisample &self = *static_cast(this); if(!id) create(); @@ -26,27 +24,25 @@ void OpenGLTexture2DMultisample::allocate() if(ARB_texture_storage_multisample) { if(ARB_direct_state_access) - glTextureStorage2DMultisample(id, samples, gl_fmt, width, height, false); + glTextureStorage2DMultisample(id, self.samples, gl_fmt, self.width, self.height, false); else { bind_scratch(); - glTexStorage2DMultisample(target, samples, gl_fmt, width, height, false); + glTexStorage2DMultisample(target, self.samples, gl_fmt, self.width, self.height, false); } } else { bind_scratch(); - glTexImage2DMultisample(target, samples, gl_fmt, width, height, false); + glTexImage2DMultisample(target, self.samples, gl_fmt, self.width, self.height, false); } apply_swizzle(); } size_t OpenGLTexture2DMultisample::get_data_size() const { - unsigned width = static_cast(this)->width; - unsigned height = static_cast(this)->height; - unsigned samples = static_cast(this)->samples; - return width*height*get_pixel_size(format)*samples; + const Texture2DMultisample &self = *static_cast(this); + return self.width*self.height*get_pixel_size(format)*self.samples; } } // namespace GL diff --git a/source/backends/opengl/texture3d_backend.cpp b/source/backends/opengl/texture3d_backend.cpp index 961dd7e7..f446e653 100644 --- a/source/backends/opengl/texture3d_backend.cpp +++ b/source/backends/opengl/texture3d_backend.cpp @@ -21,10 +21,7 @@ OpenGLTexture3D::OpenGLTexture3D(unsigned t): void OpenGLTexture3D::allocate() { - unsigned width = static_cast(this)->width; - unsigned height = static_cast(this)->height; - unsigned depth = static_cast(this)->depth; - unsigned levels = static_cast(this)->levels; + const Texture3D &self = *static_cast(this); if(!id) create(); @@ -33,11 +30,11 @@ void OpenGLTexture3D::allocate() if(ARB_texture_storage) { if(ARB_direct_state_access) - glTextureStorage3D(id, levels, gl_fmt, width, height, depth); + glTextureStorage3D(id, self.levels, gl_fmt, self.width, self.height, self.depth); else { bind_scratch(); - glTexStorage3D(target, levels, gl_fmt, width, height, depth); + glTexStorage3D(target, self.levels, gl_fmt, self.width, self.height, self.depth); } } else @@ -45,12 +42,12 @@ void OpenGLTexture3D::allocate() bind_scratch(); GLenum comp = get_gl_components(get_components(storage_fmt)); GLenum type = get_gl_type(get_component_type(storage_fmt)); - for(unsigned i=0; i(this)->get_level_size(i); + auto lv_size = self.get_level_size(i); glTexImage3D(target, i, gl_fmt, lv_size.x, lv_size.y, lv_size.z, 0, comp, type, 0); } - glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1); + glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, self.levels-1); } apply_swizzle(); @@ -79,14 +76,11 @@ size_t OpenGLTexture3D::get_data_size() const if(!id) return 0; - unsigned width = static_cast(this)->width; - unsigned height = static_cast(this)->height; - unsigned depth = static_cast(this)->depth; - unsigned levels = static_cast(this)->levels; + const Texture3D &self = *static_cast(this); - size_t level_size = width*height*depth*get_pixel_size(format); + size_t level_size = self.width*self.height*self.depth*get_pixel_size(format); size_t total_size = level_size; - for(unsigned i=0; i>=2) + for(unsigned i=0; i>=2) total_size += level_size; return total_size; } diff --git a/source/backends/opengl/texturecube_backend.cpp b/source/backends/opengl/texturecube_backend.cpp index 1cc3f837..142e343d 100644 --- a/source/backends/opengl/texturecube_backend.cpp +++ b/source/backends/opengl/texturecube_backend.cpp @@ -28,8 +28,7 @@ OpenGLTextureCube::OpenGLTextureCube(): void OpenGLTextureCube::allocate() { - unsigned size = static_cast(this)->size; - unsigned levels = static_cast(this)->levels; + const TextureCube &self = *static_cast(this); if(!id) create(); @@ -38,11 +37,11 @@ void OpenGLTextureCube::allocate() if(ARB_texture_storage) { if(ARB_direct_state_access) - glTextureStorage2D(id, levels, gl_fmt, size, size); + glTextureStorage2D(id, self.levels, gl_fmt, self.size, self.size); else { bind_scratch(); - glTexStorage2D(target, levels, gl_fmt, size, size); + glTexStorage2D(target, self.levels, gl_fmt, self.size, self.size); } } else @@ -50,13 +49,13 @@ void OpenGLTextureCube::allocate() bind_scratch(); GLenum comp = get_gl_components(get_components(storage_fmt)); GLenum type = get_gl_type(get_component_type(storage_fmt)); - for(unsigned i=0; i(this)->get_level_size(i); for(unsigned j=0; j<6; ++j) glTexImage2D(get_gl_cube_face(j), i, gl_fmt, lv_size, lv_size, 0, comp, type, 0); } - glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1); + glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, self.levels-1); } apply_swizzle(); @@ -80,12 +79,11 @@ size_t OpenGLTextureCube::get_data_size() const if(!id) return 0; - unsigned size = static_cast(this)->size; - unsigned levels = static_cast(this)->levels; + const TextureCube &self = *static_cast(this); - size_t level_size = size*size*get_pixel_size(storage_fmt); + size_t level_size = self.size*self.size*get_pixel_size(storage_fmt); size_t total_size = level_size; - for(unsigned i=0; i>=2) + for(unsigned i=0; i>=2) total_size += level_size; return total_size; } diff --git a/source/backends/opengl/vertexsetup_backend.cpp b/source/backends/opengl/vertexsetup_backend.cpp index f3243107..6dc97411 100644 --- a/source/backends/opengl/vertexsetup_backend.cpp +++ b/source/backends/opengl/vertexsetup_backend.cpp @@ -51,15 +51,17 @@ void OpenGLVertexSetup::update(unsigned mask) const { static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding; + const VertexSetup &self = *static_cast(this); + if(mask&VertexSetup::VERTEX_ARRAY) - update_vertex_array(*static_cast(this)->vertex_array, 0, 0, direct); + update_vertex_array(*self.vertex_array, 0, 0, direct); if(mask&VertexSetup::INSTANCE_ARRAY) - update_vertex_array(*static_cast(this)->inst_array, 1, 1, direct); + update_vertex_array(*self.inst_array, 1, 1, direct); if(mask&VertexSetup::INDEX_BUFFER) { - unsigned buf_id = static_cast(this)->index_buffer->id; + unsigned buf_id = self.index_buffer->id; if(direct) glVertexArrayElementBuffer(id, buf_id); else @@ -129,17 +131,19 @@ void OpenGLVertexSetup::unload() } else { + const VertexSetup &self = *static_cast(this); + glBindVertexArray(id); glBindBuffer(GL_ARRAY_BUFFER, 0); - for(VertexAttribute a: static_cast(this)->vertex_format) + for(VertexAttribute a: self.vertex_format) if(!is_padding(a)) { unsigned sem = get_attribute_semantic(a); glDisableVertexAttribArray(sem); glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0); } - for(VertexAttribute a: static_cast(this)->inst_format) + for(VertexAttribute a: self.inst_format) if(!is_padding(a)) { unsigned sem = get_attribute_semantic(a); -- 2.43.0