Cross-platform rendering framework attempt-texture management

Source: Internet
Author: User
Tags traits

Textures are important resources for renderers and are relatively simple resources. This article discusses the management of texture resources in detail.

As mentioned in the resource management overview, a resource is a bunch of memory and CPU and GPU access rights. Texture management on top of resource management, you are responsible for how a bunch of memory constructs a texture object and tells the renderer how to use the platform-related texture object. Below, we begin to discuss in detail.

1. Texture Resources

first, the texture resource is the resource that the GPU can use. It differs from the buffer resource in that, in the interpolation calculation of neighboring pixels, textures are simpler and much faster than buffer because of the corresponding hardware implementations. the texture resource is literally like a Sotu, but it is not limited to two-dimensional pixels, but also one and three-dimensional. For example, cut in practise Rendering and computation with Direct3D One

In addition, there are generally mipmap in addition to textures as RenderTarget and depthstencil. The raw data of the texture is the 0th level of the mipmap, and the mipmap size of each downward level is half the previous level. These basic concepts will appear in some graphic books, not here.

Some texture resources, in addition to Mipmap, are also a texture array (texutre array). Cube Map is a very special texture array whose array size is always 6,cube Map array, which is always a multiple of 6. The Texture3d array is temporarily not supported by various APIs for storage size reasons.

Texture arrays and mipmap are generally managed uniformly, using the concept of subresource. Such as

For example, a texture1d array object with a texture size of 8 pixels and an array size of 3, whose subresource is numbered once by the first texture's mipmap No. 0 level to the 3rd level of the first, and then from the second level mipmap No. 0 level.

The management of textures also has a very important content, is the pixel format. Pixel format is compressed and uncompressed, the compressed pixel format is mainly in the bc1-7 bit represents the lossy compression format, because of its storage in 4x4 pixels, it will affect its subresource real physical size. Such as

Uncompressed textures when generating mipmap, the texture size is always the same as the amount of physical memory occupied, such as the texture on the left. BC compresses the texture, its texture has a virtual size, you need to align the size of the texture to 4, so it actually occupies more memory size. For more information, see document HTTPS://MSDN.MICROSOFT.COM/EN-US/LIBRARY/WINDOWS/DESKTOP/BB694531 (v=vs.85). aspx

In summary, textures need to be managed as follows. Texture type, pixel format, texture size and subresource.

2. Design

In managing the type of texture, the method shown in the previous article is to give the corresponding value in the scoped enumeration type of object_type, and then review

namespace pipeline{    enumclass  object_type    {        texture_1d,        texture_2d,        Texture_3d,        Texture_cube,        Texture_1d_array,        Texture_2d_array,        Texture_cube_array,        Texture_rt,        TEXTURE_DP,    };}

Pixel format, the same applies to a scoped enumeration type.

namespace pipeline{    enumclass  pixel_format    {        unknown,        rgba_32,         // .......         bc_1,        //... ..     };}

The following first gives the implementation of the texture texture template, and then to discuss in detail.

namespacepipeline{Template<typename impl>structTexture_traits;
struct Subresource
{
uint16_t widht;
uint16_t height;
uint16_t depth;
uint16_t array_size;
uint16_t Mip_level;
size_t Row_pitch;
size_t Slice_pitch;
byte* ptr;
};
Template<typename impl>classTexture: PublicResource<texture<impl>> { Public: usingBase_type = resource<texture<policy>>; usingThis_type =Texture;usingSubresource_constainer = std::vector<subresource>; usingTraits_type = texture_traits<impl>; protected: Texture (string Const&name, Pixel_format format, uint16_t width, uint16_t height, uint16_t depth, uint16_t array_size,BOOLhas_mipmap) Noexcept:base_type (name), Width_ (width), height_ (height) , Depth_ (depth), array_size_ (size), HAS_MIPMAP_ (Has_mipmap), Subresources_ () { } Public: Template<typename = std::enable_if_t<traits_type::is_texture_1d () >>voidResize (uint16_t widht) noexcept {width_=widht; } template<typename = std::enable_if_t<traits_type::is_texture_2d () >>voidResize (uint16_t widht, uint16_t height) noexcept {width_=widht; Height_=height; } template<typename = std::enable_if_t<traits_type::is_texture_3d () >>voidResize (uint16_t widht, uint16_t height, uint16_t depth) noexcept {width_=widht; Height_=height; Depth_=Depth} template<typename = Std::enable_if_t<traits_type::is_texture_array () >>voidset_array_size (uint16_t array_size) noexcept {array_size_= Array_size *Traits_type::array_size_unit (); } voidSet_pixel_format (Pixel_format pixel_format) {Traits_type::validate_format (Pixel_format); Pixel_format_=Pixel_format; } uint16_t width ()Constnoexcept {returnWidth_; } uint16_t height ()Constnoexcept {returnHeight_; } uint16_t Depth ()Constnoexcept {returnDepth_; } uint16_t array_size ()Constnoexcept {returnArray_size_; } BOOL&mipmap () noexcept {returnHAS_MIPMAP_; } BOOLMipmap ()Constnoexcept {returnHAS_MIPMAP_; } voidconstruct () {Detail::subresource_generator Gen {pixel_format_, width_, Height_, Depth_, Array_size_, HAS_MIPMAP_}; Data_=Std::move (Gen.data ()); Size_=data_.size (); Subresources_=Std::move (Gen.subresource ()); } decltype (Auto) begin () noexcept {returnSubresources_.begin (); } decltype (Auto) end () noexcept {returnSubresources_.end (); } Private: Pixel_format pixel_format_; uint16_t Width_; uint16_t Height_; uint16_t Depth_; uint16_t Array_size_; BOOLHAS_MIPMAP_; Subresource_constainer Subresources_; };}

The reason to use the template here is to make it possible to write dry code.

Anatomy of the Resize function. This function has three points of knowledge.

1. Default Tempalte parameter & Template member function overload resolution;

2. Substitution failure is no an error (SFINAE); Http://en.cppreference.com/w/cpp/language/sfinae

3. std::enable_if; http://www.boost.org/doc/libs/1_59_0/libs/core/doc/html/core/enable_if.html

If the resize function is implemented in Texture_2d,texture_rt,texture_2d_array and so on, the code is dry, and texture_1d cannot resize the height component. Here is the use of a number of meta-programming techniques, through the template traits mode to do the compilation period of reflection, with enable_if help us in the compile time resolution resize function is valid. In short, it is texture_1d if you try to use an overload of the resize two parameter, the compiler will make an error. In this way, the code only needs to be implemented once in the texture base class to ensure that no duplicate code is present and the security of the compile period is guaranteed. Similar functions.

The Set_pixel_format function assigns the current pixel format as valid task to the traits to complete. For example, TEXTURE_3D cannot be a BC series format, TEXTURE_RT and TEXTURE_DP have a specific pixel format.

Since texture is a complex object (complex object) with an STL container as a member variable, the RAII pattern is not appropriate, so the function that sets the parameter is only the assignment of some member variable, and the construct function is the function that actually creates it. The construct function allocates memory for texture and populates subresource. Detail::subresource_generator is an auxiliary class that passes all the parameters required for texture creation, and it completes the memory allocation and padding Subresource for texture in the constructor. For the sake of exception security (exceptional safe), all the creation is inside the helper class, and after creation, use move semantics to efficiently move the created memory and subresource information to the texture corresponding member. If an exception occurs during the creation process, the state of the texture does not change, and the helper class object can perfectly guarantee that the resource will not be compromised. The design of the auxiliary class is given below.

Write it here today and fill it out tomorrow.

Cross-platform rendering framework attempt-texture management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.