diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2021-03-11 13:08:21 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-11 13:08:21 -0800 |
| commit | 4b74f994bf94217f174cf0fb02ed94abe62e9d7c (patch) | |
| tree | 9c6f4c771d1e962059c84047ccf96e0d891a5c80 /tools/platform | |
| parent | a07455c175db33d8d95bc5cd83738808d74cd105 (diff) | |
Change representation of initial data for textures (#1747)
* Change representation of initial data for textures
Before this change, initial data for a texture has been provided with the `ITextureResource::Data` type, where a call to `IDevice::createTexture()` would take zero or one `Data` and, if present, use it to initialize all the subresources of a texture.
The organization of `Data` was not actually quite how its own documentation comment described it (the implementations didn't agree with the comment), and while it aggressively factored out redundancies (e.g., only storing the stride for each mip level once, instead of once per subresource for large arrays), the result was that setting up a `Data` correcty was a bit confusing.
This change makes the initial data for a texture using a `SubresourceData` type that is almost identical to what D3D11 uses, so that developers are more likely to be comfortable filling it in. All of the existing implementations were easily adapted to use the new type, so it seems like a net win.
Note: Both Vulkan and D3D11 do away with the idea of initializing a texture with data as part of allocating it, and we might eventually want to do the same given the complexity that this system entails. The main reason to preserve this detail is for better compatibility with D3D11, where immutable textures/buffers need to have their data specified at creation time. It seems good to preserve the ability to have immutable resources on target APIs where this distinction could affect performance (e.g., immutable resources do not need state/transition tracking on APIs like D3D11).
* fixup: CUDA
Diffstat (limited to 'tools/platform')
| -rw-r--r-- | tools/platform/gui.cpp | 11 | ||||
| -rw-r--r-- | tools/platform/model.cpp | 30 |
2 files changed, 19 insertions, 22 deletions
diff --git a/tools/platform/gui.cpp b/tools/platform/gui.cpp index f2f9561d5..8e455cd12 100644 --- a/tools/platform/gui.cpp +++ b/tools/platform/gui.cpp @@ -217,14 +217,9 @@ GUI::GUI( desc.init2D(IResource::Type::Texture2D, Format::RGBA_Unorm_UInt8, width, height, 1); desc.setDefaults(IResource::Usage::PixelShaderResource); - - ptrdiff_t mipRowStrides[] = { ptrdiff_t(width * 4 * sizeof(unsigned char)) }; - void* subResourceData[] = { pixels }; - ITextureResource::Data initData; - initData.mipRowStrides = mipRowStrides; - initData.numMips = 1; - initData.numSubResources = 1; - initData.subResources = subResourceData; + ITextureResource::SubresourceData initData = {}; + initData.data = pixels; + initData.strideY = width * 4 * sizeof(unsigned char); auto texture = device->createTextureResource(IResource::Usage::PixelShaderResource, desc, &initData); diff --git a/tools/platform/model.cpp b/tools/platform/model.cpp index 287f80958..54c171e54 100644 --- a/tools/platform/model.cpp +++ b/tools/platform/model.cpp @@ -128,13 +128,16 @@ ComPtr<ITextureResource> loadTextureImage( // results when loading the image with stb_image. } - std::vector<void*> subresourceInitData; - std::vector<ptrdiff_t> mipRowStrides; + std::vector<ITextureResource::SubresourceData> subresourceInitData; ptrdiff_t stride = extentX * channelCount * sizeof(stbi_uc); - subresourceInitData.push_back(data); - mipRowStrides.push_back(stride); + ITextureResource::SubresourceData baseInitData; + baseInitData.data = data; + baseInitData.strideY = stride; + baseInitData.strideZ = 0; + + subresourceInitData.push_back(baseInitData); // create down-sampled images for the different mip levels bool generateMips = true; @@ -166,8 +169,13 @@ ComPtr<ITextureResource> loadTextureImage( STBIR_ALPHA_CHANNEL_NONE, STBIR_FLAG_ALPHA_PREMULTIPLIED); - subresourceInitData.push_back(newData); - mipRowStrides.push_back(newStride); + + ITextureResource::SubresourceData mipInitData; + mipInitData.data = newData; + mipInitData.strideY = newStride; + mipInitData.strideZ = 0; + + subresourceInitData.push_back(mipInitData); prevExtentX = newExtentX; prevExtentY = newExtentY; @@ -176,19 +184,13 @@ ComPtr<ITextureResource> loadTextureImage( } } - int mipCount = (int) mipRowStrides.size(); + int mipCount = (int) subresourceInitData.size(); ITextureResource::Desc desc; desc.init2D(IResource::Type::Texture2D, format, extentX, extentY, mipCount); - ITextureResource::Data initData; - initData.numSubResources = mipCount; - initData.numMips = mipCount; - initData.subResources = &subresourceInitData[0]; - initData.mipRowStrides = &mipRowStrides[0]; - auto texture = - device->createTextureResource(IResource::Usage::PixelShaderResource, desc, &initData); + device->createTextureResource(IResource::Usage::PixelShaderResource, desc, subresourceInitData.data()); free(data); |
