]> git.tdb.fi Git - libs/gl.git/commitdiff
Generate storage offsets for default block uniforms internally
authorMikko Rasa <tdb@tdb.fi>
Fri, 29 Dec 2023 08:21:21 +0000 (10:21 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 29 Dec 2023 08:24:55 +0000 (10:24 +0200)
Intel drivers treat locations as abstract identifiers, so a 4×4 matrix
only consumes a single location.

source/backends/opengl/pipelinestate_backend.cpp
source/backends/opengl/program_backend.cpp
source/backends/opengl/program_backend.h
source/core/reflectdata.h
source/core/uniformblock.cpp

index 51be6151da148622f4e031b67f8cb1f3d29a1ba1..90cbc9942e80e81f05b140119dc30be84169b090 100644 (file)
@@ -133,7 +133,7 @@ void OpenGLPipelineState::apply() const
                                        {
                                                const char *data = static_cast<const char *>(r.block->get_data_pointer());
                                                for(const Program::UniformCall &call: self.shprog->uniform_calls)
-                                                       call.func(call.location, call.size, data+call.location*16);
+                                                       call.func(call.location, call.size, data+call.offset);
                                        }
                                }
                                else if(r.type==PipelineState::STORAGE_BLOCK)
index 994373f3f0268d97267559e8e0f498c12c64c227..ecf6de0704de56dc35168b2d7fa646390125366c 100644 (file)
@@ -361,11 +361,13 @@ void OpenGLProgram::query_uniforms()
 
        ReflectData::BlockInfo &default_block = rd.blocks.emplace_back();
 
+       size_t offset = 0;
        for(ReflectData::UniformInfo &u: rd.uniforms)
                if(!u.block)
                {
                        u.location = glGetUniformLocation(id, u.name.c_str());
                        u.block = &default_block;
+                       u.offset = offset;
                        u.array_stride = get_type_size(u.type);
                        if(is_matrix(u.type))
                                u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
@@ -373,8 +375,11 @@ void OpenGLProgram::query_uniforms()
 
                        if(is_image(u.type) && u.location>=0)
                                glGetUniformiv(id, u.location, &u.binding);
+
+                       offset += u.array_size*get_type_size(u.type);
                }
 
+       default_block.data_size = offset;
        default_block.sort_uniforms();
        default_block.update_layout_hash();
 }
@@ -558,14 +563,8 @@ void OpenGLProgram::finalize_uniforms()
                                        func = &uniform_matrix_wrapper<float, glUniformMatrix4x3fv>;
 
                                if(func)
-                                       uniform_calls.emplace_back(u->location, u->array_size, func);
+                                       uniform_calls.emplace_back(u->location, u->array_size, u->offset, func);
                        }
-
-               if(i->data_size<=0)
-               {
-                       const ReflectData::UniformInfo &last = *i->uniforms.back();
-                       i->data_size = last.location*16+last.array_size*get_type_size(last.type);
-               }
        }
 }
 
index 1b383f82d4c31c560721052502cce05312d33687..52f30524f6bbf6440c41526f0ed92fd81f2e9329 100644 (file)
@@ -36,9 +36,10 @@ protected:
 
                unsigned location;
                unsigned size;
+               unsigned offset;
                FuncPtr func;
 
-               UniformCall(unsigned l, unsigned s, FuncPtr f): location(l), size(s), func(f) { }
+               UniformCall(unsigned l, unsigned s, unsigned o, FuncPtr f): location(l), size(s), offset(o), func(f) { }
        };
 
        unsigned id = 0;
index 4b0cbd2ce82c65ecb96984898c462596564c4485..1fc60d28431d7828fa3bd32a7771c116c34f6feb 100644 (file)
@@ -29,11 +29,8 @@ struct ReflectData
        {
                std::string name;
                const BlockInfo *block = nullptr;
-               union
-               {
-                       int location = -1;
-                       unsigned offset;
-               };
+               int location = -1;
+               unsigned offset = 0;
                unsigned array_size = 0;
                unsigned array_stride = 0;
                unsigned matrix_stride = 0;
index b4af61a2b64d34eeb8d97cd86aaeb92a28cade0e..3f0497d5cebc30c2f2a1026a49aac392b2204944 100644 (file)
@@ -21,40 +21,30 @@ void UniformBlock::store(const ReflectData::UniformInfo &info, size_t array_size
 {
        array_size = min(array_size, max<size_t>(info.array_size, 1U));
 
-       size_t store_offset;
-       bool packed;
-       if(info.block->bind_point==ReflectData::DEFAULT_BLOCK)
+       bool packed = true;
+       if(info.block->bind_point!=ReflectData::DEFAULT_BLOCK)
        {
-               if(info.location<0)
-                       return;
-
-               store_offset = info.location*16;
-               packed = true;
-       }
-       else
-       {
-               store_offset = info.offset;
                if(array_size!=1 && info.array_stride!=get_type_size(info.type))
                        packed = false;
                else if(is_matrix(info.type))
                        packed = (info.matrix_stride==get_type_size(get_matrix_column_type(info.type)));
-               else
-                       packed = true;
        }
+       else if(info.location<0)
+               return;
 
-       char *store_ptr = data.data()+store_offset;
+       char *store_ptr = data.data()+info.offset;
        const char *value_ptr = static_cast<const char *>(value);
        if(packed)
        {
                size_t value_size = array_size*get_type_size(info.type);
-               check_store_range(store_offset, value_size);
+               check_store_range(info.offset, value_size);
                copy(value_ptr, value_ptr+value_size, store_ptr);
        }
        else if(is_matrix(info.type))
        {
                unsigned col_size = get_type_size(get_matrix_column_type(info.type));
                unsigned cols = get_type_size(info.type)/col_size;
-               check_store_range(store_offset, (array_size-1)*info.array_stride+(cols-1)*info.matrix_stride+col_size);
+               check_store_range(info.offset, (array_size-1)*info.array_stride+(cols-1)*info.matrix_stride+col_size);
                for(unsigned i=0; i<array_size; ++i)
                {
                        char *elem_ptr = store_ptr;
@@ -70,7 +60,7 @@ void UniformBlock::store(const ReflectData::UniformInfo &info, size_t array_size
        else
        {
                unsigned elem_size = get_type_size(info.type);
-               check_store_range(store_offset, (array_size-1)*info.array_stride+elem_size);
+               check_store_range(info.offset, (array_size-1)*info.array_stride+elem_size);
                for(unsigned i=0; i<array_size; ++i)
                {
                        copy(value_ptr, value_ptr+elem_size, store_ptr);