MCPcopy Create free account
hub / github.com/Apress/Ray-Tracing-Gems-II / ProcessTexture

Function ProcessTexture

Chapter_14/src/Textures.cpp:189–227  ·  view source on GitHub ↗

* Covert a R8G8B8A8_UNORM texture to BC7 format and generate mips. */

Source from the content-addressed store, hash-verified

187 * Covert a R8G8B8A8_UNORM texture to BC7 format and generate mips.
188 */
189 bool ProcessTexture(Texture& texture, bool generateMips = true, bool quickBC7 = true)
190 {
191 // Check if there's anything to compress
192 if (texture.mips.empty()) return false;
193
194 TextureData* texData = &texture.mips[0];
195
196 // BC7 textures must be aligned to pixel 4x4 blocks.
197 if (texData->width % 4 != 0 || texData->height % 4 != 0) {
198 MessageBox(NULL, L"Size of the input texture must be multiple of 4 for compression to work! Make sure to call FormatTexture() before this function to add necessary padding", L"Error", MB_OK);
199 return false;
200 }
201
202 Image source = {};
203 source.width = texData->width;
204 source.height = texData->height;
205 source.rowPitch = (texData->width * size_t(texData->stride));
206 source.slicePitch = (source.rowPitch * source.height);
207 source.format = DXGI_FORMAT_R8G8B8A8_UNORM;
208 source.pixels = texData->texels;
209
210 TEX_COMPRESS_FLAGS flags = TEX_COMPRESS_DEFAULT;
211 if (quickBC7) flags = TEX_COMPRESS_BC7_QUICK;
212 ScratchImage scratchBuffer;
213
214 if (generateMips) {
215 ScratchImage mips;
216 if (FAILED(DirectX::GenerateMipMaps(source, TEX_FILTER_DEFAULT, 0, mips))) return false;
217 if (FAILED(Compress(d3d11Device, mips.GetImages(), mips.GetImageCount(), mips.GetMetadata(), DXGI_FORMAT_BC7_UNORM, flags, 1.f, scratchBuffer))) return false;
218 mips.Release();
219 } else {
220 if (FAILED(Compress(d3d11Device, source, DXGI_FORMAT_BC7_UNORM, flags, 1.f, scratchBuffer))) return false;
221 }
222
223 // Format the compressed texture into our format, prepping it for use on the GPU
224 texture.isBC7Compressed = true;
225
226 return FormatCompressedTexture(scratchBuffer, texture);
227 }
228#endif
229
230 //----------------------------------------------------------------------------------------------------------

Callers 1

LoadTextureFunction · 0.85

Calls 3

FormatCompressedTextureFunction · 0.85
ReleaseMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected