1. Preface
Constant buffer是我们在编写shader的时候,打交道最多的一种buffer resource了。constant表明了constant buffer中的数据,在一次draw call的执行过程中都是不变的;而在不同的draw call之间,我们可以修改其中的数据。它是我们把数据从CPU传递到GPU最常见的方法。constant buffer的概念和定义就不在这里赘述了,鄙文主要讨论如何优雅的管理constant buffer.
2. How to create and manipulate constant buffer gracefully.
Constant buffer在各种api中都有很好的支持,如在DX11中,以cbuffer为类型的闭包,可以定义一个constant buffer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
cbuffer Transforms { matrix world; matrix view_project; matrix skin[32]; }; cbuffer change_every_frame { float3 light_positoin; float4 light_color; }; float4 dummy_vec4_variable; |
cbuffer可以根据你自己对它的功能或者buffer数据改变的策略来定义。在DX11中,我通过shader reflection的接口发现,没有在cbuffer闭包中的变量,都会被放到一个名叫@Globals的constant buffer中。当然这也取决你的fxc.exe的版本。其实让我们苦恼的并不是如何使用DX或者GL等这些api创建一个constant buffer对象,而是我们为了使用constant buffer,通常情况下需要在我们的引擎或者客户端的程序代码中,创建一个内存布局和大小与GPU端统一的数据结构。例如,我们要使用 transforms和change_every_frame这两个cbuffer,我们还要定义如下C的struct,和一些额外的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
struct transform { your_matrix_type world_matrix; your_matrix_type view_project_matrix; your_skin_matrices skin_matrices[32]; }; struct change_every_frame { your_float3_type light_positoin; your_float_type pack; yourefloat4_type light_color; }; |
这是一件很让人困扰的事情。首先,如果你开发了一个新的shader并使用了一个新的cbuffer的定义,那么你不得不修改你的引擎或者客户端代码。添加新的数据结构,还有使用和更新的代码。如果是这样,我们的程序或者引擎的扩展性就太差了!其次,你得非常小心的处理constant buffer内存布局的规则,否则你的数据不会正确的传递。例如,light_position后面要更一个float作为补位。我们应该把这些交给程序自己,而不是自己来做重复的工作。在C++中,我们必须要把这些与类型相关,也就是受限于编译期的,改成到运行期当中来计算。管理的基本方法如图所示:
将一个cbuffer分成的两个部分,一个大小跟GPU中cbuffer大小一致的memory block,和一个对cbuffer各个成员的描述meta data. 如何来实现呢?在最开始,我们需要一个枚举来描述所有的基本类型,例如float,float3,float4x4,这是从编译期转向运行期的第一步。
1 2 3 4 5 6 7 8 9 10 |
enum class data_format { float_, float2, float3, float4, float2x2, float2x3, float2x4, float3x2, float3x3, float3x4, float4x2, float4x3, float4x4, int_, int2, int3, int4, uint, uint2, uint3, uint4, structured, }; |
然后,我们需要一个结构体来描述整个constant buffer,例如change_every_frame这个cbuffer,我们要描述整个buffer的大小,light_color的data format,还有相对于cbuffer头地址的偏移量等。并且还要支持在cbuffer中使用结构体和数组。所以这个结构体应该是自递归的。如下面的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
// numeric layout class numeric_layout { using sub_var_container = std::vector<numeric_layout>; static constexpr size_t sub_count = 8; public: // construct numeric_layout(data_format format, uint16_t count, uint16_t size, uint16_t offset) : format_(format) , count_(count) , size_(size) , offset_(offset) { sub_variables_.reserve(sub_count); } template <typename T> numeric_layout(large_class_wrapper<T> t) : numeric_layout( data_format::structured, 1, structure_size<T>::value, 0) { detail::init_variable_layout_from_tuple(*this, t); } // attribute access data_format format() const noexcept { return format_; } uint16_t count() const noexcept { return count_; } uint16_t size() const noexcept { return size_; } uint16_t offset() const noexcept { return offset_; } // sub variables void add_sub(data_format format, uint16_t count, uint16_t size, uint16_t offset) { //assert(data_format::structured != format_); assert(offset + size <= size_); sub_variables_.emplace_back(format, count, size, offset); } numeric_layout& operator[] (size_t index) { return sub_variables_[index]; } numeric_layout const& operator[] (size_t index) const { return sub_variables_[index]; } private: data_format format_; uint16_t count_; uint16_t size_; uint16_t offset_; sub_var_container sub_variables_; }; |
在编译shader之前,还需要多做一件事情,就是解析shader code中的cbuffer,把这些meta data都获取出来,创建好numeric_layout对象。当然,都已经解析了cbuffer,讲道理应该把整个shader codes都解析一遍,创建一个完整的effect框架。这部分的功能,我正在研究和开发中,希望能顺利完成并同大家分享。然后在渲染框架的与平台无关的代码部分,抽象一个constant buffer类型,并使用这个numeric_layout创建与平台无关的constant buffer对象。有了这个constant buffer对象,平台相关的代码就有足够多的信息正确创建设备上的cbuffer的对象了,无论是dx还是gl. 那么总体的流程如图:
在很多图形api中,对cbuffer的部分更新做的并不是很好,如只更新change_every_frame中的light_color分量。DX的UpdateSubresource无法实现,gl3.1之后有了ubo,才可以使用glSetBufferSubData来实现。在我们管理的cbuffer下,我们可以部分更新cpu中的cbuffer的memory,在一起update到gpu上来模拟这种部分更新的实现。
3. Use the powerful compile time computation of C++ to manipulate constant buffer
接下来聊聊如何利用C++新标准强大的编译期计算来方便我们创建cbuffer. 上述将编译期迁移到运行期的方法,很适合在渲染框架中使用。运行期化的代码,虽然能解决问题,但是创建的过程还是比较复杂。在写实验和测试的代码这类很小的程序的时候,上图的流程就显的笨重了。所以,我在实现numeric_layout的时候,提供了使用用户自定义类型来创建cbuffer的metadata的方法,以便小型程序使用。使用起来非常简单,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// test constant buffer struct global_cbuffer { float3 light_position; float4 ambient_color; float4 diffuse_color; float4x4 model_view; float3 light_direction; }; BOOST_FUSION_ADAPT_STRUCT( global_cbuffer, (float3, light_position) (float4, ambient_color) (float4, diffuse_color) (float4x4, model_view) (float3, light_direction) ); void test_constant_buffer() { numeric_layout_t layout = wrap_large_class<global_cbuffer>(); constant_buffer_t cbuffer{ "global cbuffer", std::move(layout) }; } |
首先定义自定义的cbuffer的结构体,跟shader code中的一模一样的结构体;然后使用boost::fusion做编译期的反射;最后使用numeric_layout的另外一个构造函数创建cbuffer的metadata,共cbuffer创建使用。大概的实现思路如下。
1. 使用large_class_wrapper<T>来避免不必要的栈内存分配。由于定义我们的constant buffer的数据结构往往是一个结构体,而我们创建numeric_layout对象并不需要这个结构体的实例或者实例的引用,只需要编译期的反射信息。我们没有必要去创建一个无用的对象,而是把类型传递给large_class_wrapper<T>类模板,让它带上类型信息,但是这个类模板本身是一个空类,大小为1,甚至会被编译期优化掉。所以这里使用large_class_wrapper<T>可以避免不比较的内存开销。
2. 然后,在使用被fusion适配成sequence之后的用户自定义类型T,对numeric_layout进行初始化。根据cbuffer的内存对齐规则,对sequence中的每一个成员类型做计算,当前成员类型计算的过程依赖上一个成员计算的结果。那么numeric_layout的构造函数调用的detail::init_variable_layout_from_tuple函数实现如下,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
namespace detail { struct offset_register { uint16_t offset; uint16_t reg; }; template <typename T> void bind_numeric(numeric_layout& layout, offset_register& helper) { using traits_t = array_traits<T>; using traits_type = typename traits_t::numeric_traits_t; // get the current register position auto reg = detail::reg_size(traits_type::format()); // if we need to begin a new four component ? auto begin_new_four_component = traits_t::count > 1 || reg + helper.reg >= 4; if (begin_new_four_component) { helper.reg = 0; helper.offset = detail::align<16>(helper.offset); } // calculate the size of current variable auto size = detail::size_of(traits_type::format(), traits_t::count); // add the container layout.add_sub(traits_type::format(), traits_t::count, size, helper.offset); // update helper object, which acts like a context of this calculation process helper.offset += size; helper.reg += reg & 0x03; // helper.reg = (helper.reg + reg) % 4 } template <typename T, size_t ... Is> void init_variable_layout_from_tuple(numeric_layout& layout, large_class_wrapper<T> tuple, std::index_sequence<Is...> seq) { offset_register helper = { 0, 0 }; using swallow_t = bool[]; swallow_t s = { (bind_numeric<type_at<T, Is>>(layout, helper), true)... }; } template <typename T> void init_variable_layout_from_tuple(numeric_layout& layout, large_class_wrapper<T> tuple) { init_variable_layout_from_tuple(layout, tuple, std::make_index_sequence<sequence_size<T>::value>{}); } } |
3. numeric_layout的构造函数,在编译期计算出了T在GPU中的大小,算法与运行期的原理基本相同,只是改写到编译期计算了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
namespace detail { template <size_t Reg, size_t Offset> struct structure_size_helper { static constexpr size_t reg = Reg; static constexpr size_t offset = Offset; }; template <typename Helper, typename ... Args> struct structure_size_impl_expand_impl; template <typename Helper, typename T> struct export_calculate_result { using traits_t = array_traits<T>; using numeric_traits_t = typename traits_t::numeric_traits_t; using helper_t = Helper; static constexpr size_t count = traits_t::count; static constexpr size_t numeric_size = count > 1 ? align<16>(numeric_traits_t::size()) * count : numeric_traits_t::size(); static constexpr size_t numeric_reg = numeric_traits_t::reg(); static constexpr bool new_four_component = (count > 1) || (helper_t::reg + numeric_reg >= 4); static constexpr size_t new_reg = new_four_component ? 0 : (helper_t::reg + numeric_reg) % 4; static constexpr size_t new_size = new_four_component ? align<16>(helper_t::offset) + numeric_size : helper_t::offset + numeric_size; }; template <typename Helper, typename First, typename ... Rests> struct structure_size_impl_expand_impl<Helper, First, Rests...> : export_calculate_result<Helper, First> { using next_helper = structure_size_helper<new_reg, new_size>; using next_type = structure_size_impl_expand_impl<next_helper, Rests...>; static constexpr size_t value = next_type::value; }; template <typename Helper, typename Last> struct structure_size_impl_expand_impl<Helper, Last> : export_calculate_result<Helper, Last> { static constexpr size_t value = align<16>(new_size); }; template <typename T, typename Indices> struct structure_size_impl; template <typename ... Args> struct structure_size_impl_expand { static constexpr size_t value = structure_size_impl_expand_impl <structure_size_helper<0,0>, Args...>::value; }; template <typename T, size_t ... Is> struct structure_size_impl<T, std::index_sequence<Is...>> { static constexpr size_t value = structure_size_impl_expand< type_at<T, Is>...>::value; }; } template <typename T> struct structure_size { static_assert(is_sequence<T>::value, "!!!"); static constexpr size_t size = sequence_size<T>::value; static constexpr size_t value = detail::structure_size_impl< T, std::make_index_sequence<size>>::value; }; |
目前还不支持struct中嵌套struct,我想不久的将来应该会支持的。仓库在这里,不过我写的很慢,连平台无关的代码都还没有写完。接下来会准备研究glslang,hlslcc,还有vulkan的SPIRV的一些库和tools的开源项目,来解决one shader all platforms的问题。
4. Tail
constant buffer曾经是我引擎开发工作当中一个比较痛苦的环节,每写一个shader,每多一个effect就得在C++代码中添加相应的数据结构和逻辑显得很DRY。而后错误的更新cbuffer招致的痛苦的调试过程也是历历在目。还有当我看到maya的cgfx如此灵活和强大功能更让我觉得cbuffer是得好好管理一下了。Powered by modern cpp and modern graphics api,希望我自己实现的渲染框架,可以在不添加一行C++代码的同时,还能高效的正确渲染新加入的effect,杜绝DRY的设计和实现。