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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
// Copyright 2014 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#include "texture.h"
#include "util.h"
#include "noise.h"
#include "DDSTextureLoader.h"
#include <stdint.h>
#include <sstream>
static void WaitForAll(ID3D12Device* device, ID3D12CommandQueue* queue)
{
// Kind of ugly, but yeah...
ID3D12Fence* fence = nullptr;
ThrowIfFailed(device->CreateFence( 0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)));
auto eventHandle = CreateEvent(NULL, FALSE, FALSE, NULL);
queue->Signal(fence, 1);
fence->SetEventOnCompletion(1, eventHandle);
WaitForSingleObject(eventHandle, INFINITE);
CloseHandle(eventHandle);
fence->Release();
}
void GenerateMips2D_XXXX8(D3D11_SUBRESOURCE_DATA* subresources, size_t widthLevel0, size_t heightLevel0, size_t mipLevels)
{
for (size_t m = 1; m < mipLevels; ++m) {
auto rowPitchSrc = subresources[m - 1].SysMemPitch;
const BYTE* dataSrc = (BYTE*)subresources[m - 1].pSysMem;
auto rowPitchDst = subresources[m].SysMemPitch;
BYTE* dataDst = (BYTE*)subresources[m].pSysMem;
auto width = widthLevel0 >> m;
auto height = heightLevel0 >> m;
// Iterating byte-wise is simpler in this case (pulls apart color nicely)
// Not optimized at all, obviously...
for (size_t y = 0; y < height; ++y) {
auto rowSrc0 = (dataSrc + (y*2+0)*rowPitchSrc);
auto rowSrc1 = (dataSrc + (y*2+1)*rowPitchSrc);
auto rowDst = (dataDst + (y )*rowPitchDst);
for (size_t x = 0; x < width; ++x) {
for (size_t comp = 0; comp < 4; ++comp) {
uint32_t c = rowSrc0[x*8+comp+0];
c += rowSrc0[x*8+comp+4];
c += rowSrc1[x*8+comp+0];
c += rowSrc1[x*8+comp+4];
c = c / 4;
assert(c < 256);
rowDst[4*x+comp] = (byte)c;
}
}
}
}
}
void FillNoise2D_RGBA8(D3D11_SUBRESOURCE_DATA* subresources, size_t width, size_t height, size_t mipLevels,
float seed, float persistence, float noiseScale, float noiseStrength,
float redScale, float greenScale, float blueScale)
{
NoiseOctaves<4> textureNoise(persistence);
// Level 0
for (size_t y = 0; y < height; ++y) {
uint32_t* row = (uint32_t*)((BYTE*)subresources[0].pSysMem + y*subresources[0].SysMemPitch);
for (size_t x = 0; x < width; ++x) {
auto c = textureNoise((float)x*noiseScale, (float)y*noiseScale, seed);
c = std::max(0.0f, std::min(1.0f, (c - 0.5f) * noiseStrength + 0.5f));
int32_t cr = (int32_t)(c * redScale);
int32_t cg = (int32_t)(c * greenScale);
int32_t cb = (int32_t)(c * blueScale);
assert(cr >= 0 && cr < 256);
assert(cg >= 0 && cg < 256);
assert(cb >= 0 && cb < 256);
row[x] = (cr) << 16 | (cg) << 8 | (cb) << 0;
}
}
if (mipLevels > 1)
GenerateMips2D_XXXX8(subresources, width, height, mipLevels);
}
void InitializeTexture2D(
ID3D12Device* device, ID3D12CommandQueue* cmdQueue,
ID3D12Resource* texture, const D3D12_RESOURCE_DESC* desc, UINT bytesPerPixel,
const D3D11_SUBRESOURCE_DATA* initialData,
D3D12_RESOURCE_STATES stateAfter)
{
// Pull some data
auto format = desc->Format;
UINT width = (UINT)desc->Width;
UINT height = desc->Height;
UINT arraySize = desc->DepthOrArraySize;
UINT mipLevels = desc->MipLevels;
// Pow2 mip chain!
assert(mipLevels == 1 || ((width & (width-1)) == 0 && (height & (height-1)) == 0));
std::vector<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> placedUpload;
UINT64 totalSize = 0;
for (UINT a = 0; a < arraySize; ++a) {
for (UINT m = 0; m < mipLevels; ++m) {
D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed = {};
placed.Footprint.Format = format;
placed.Footprint.Width = width >> m; // TODO: Handle mip sizes properly!
placed.Footprint.Height = height >> m; // TODO: Handle mip sizes properly!
placed.Footprint.Depth = 1;
placed.Footprint.RowPitch = Align<UINT>(placed.Footprint.Width * bytesPerPixel, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
placed.Offset = totalSize;
totalSize = Align<UINT64>(placed.Offset + (size_t{placed.Footprint.RowPitch} * size_t{placed.Footprint.Height}),
D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT);
placedUpload.push_back(placed);
}
}
ID3D12Resource* uploadBuffer = nullptr;
ThrowIfFailed(device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Buffer(totalSize),
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(&uploadBuffer)
));
BYTE *baseData = nullptr;
ThrowIfFailed(uploadBuffer->Map(0, nullptr, reinterpret_cast<void**>(&baseData)));
// Fill in data (RGBA8)
for (UINT a = 0; a < arraySize; ++a) {
for (UINT m = 0; m < mipLevels; ++m) {
auto subresource = a * mipLevels + m;
BYTE* dataSrc = (BYTE*)initialData[subresource].pSysMem;
auto rowPitchSrc = initialData[subresource].SysMemPitch;
auto placed = &placedUpload[subresource];
BYTE* dataDst = baseData + placed->Offset;
auto rowPitchDst = placed->Footprint.RowPitch;
UINT width_mip = placed->Footprint.Width;
UINT height_mip = placed->Footprint.Height;
for (UINT y = 0; y < height_mip; ++y) {
memcpy(dataDst + y*size_t{rowPitchDst}, dataSrc + y*size_t{rowPitchSrc}, size_t{bytesPerPixel} * width_mip);
}
}
}
// Create some new resources for initialization
ID3D12GraphicsCommandList* cmdLst = nullptr;
ID3D12CommandAllocator* cmdAlloc = nullptr;
ThrowIfFailed(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmdAlloc)));
ThrowIfFailed(device->CreateCommandList(1, D3D12_COMMAND_LIST_TYPE_DIRECT, cmdAlloc, nullptr, IID_PPV_ARGS(&cmdLst)));
{
ResourceBarrier rb;
rb.AddTransition(texture, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST);
rb.Submit(cmdLst);
}
// Copy data from each subresource into texture
for (size_t s = 0; s < placedUpload.size(); ++s) {
CD3DX12_TEXTURE_COPY_LOCATION dest(texture, static_cast<UINT>(s));
CD3DX12_TEXTURE_COPY_LOCATION src(uploadBuffer, placedUpload[s]);
cmdLst->CopyTextureRegion(
&dest, 0, 0, 0,
&src, nullptr);
}
{
ResourceBarrier rb;
rb.AddTransition(texture, D3D12_RESOURCE_STATE_COPY_DEST, stateAfter);
rb.Submit(cmdLst);
}
ThrowIfFailed(cmdLst->Close());
cmdQueue->ExecuteCommandLists(1, reinterpret_cast<ID3D12CommandList*const*>(&cmdLst));
// Kind of ugly... but yeah
WaitForAll(device, cmdQueue);
SafeRelease(&uploadBuffer);
SafeRelease(&cmdLst);
SafeRelease(&cmdAlloc);
}
HRESULT CreateTexture2DFromDDS_XXXX8(
ID3D12Device* device, ID3D12CommandQueue* cmdQueue,
ID3D12Resource** texture,
const char* fileName,
DXGI_FORMAT format,
D3D12_RESOURCE_STATES stateAfter )
{
BYTE* heapData = nullptr;
DDS_HEADER* header = nullptr;
BYTE* bitData = nullptr;
UINT bitSize = 0;
std::wostringstream wfileName;
wfileName << fileName;
HRESULT hr = LoadTextureDataFromFile(wfileName.str().c_str(), &heapData, &header, &bitData, &bitSize);
if (FAILED(hr)) {
delete[] heapData;
return hr;
}
unsigned int arraySize = 1;
if (header->dwCaps2 & DDS_CUBEMAP) {
assert((header->dwCaps2 & DDS_CUBEMAP_ALLFACES ) == DDS_CUBEMAP_ALLFACES);
arraySize = 6;
}
// We only support XXXX8_UNORM[_SRGB] atm...
if (format != DXGI_FORMAT_B8G8R8A8_UNORM && format != DXGI_FORMAT_R8G8B8A8_UNORM &&
format != DXGI_FORMAT_B8G8R8A8_UNORM_SRGB && format != DXGI_FORMAT_R8G8B8A8_UNORM_SRGB) {
return E_NOTIMPL;
}
unsigned int mipLevels = header->dwMipMapCount;
if (mipLevels == 0) mipLevels = 1; // Hacky... but good enough for now
// TODO: lots of stuff is not checked... just don't be stupid, this is hacky sample code after all :)
auto desc = CD3DX12_RESOURCE_DESC::Tex2D(
format, header->dwWidth, header->dwHeight,
(UINT16)arraySize, (UINT16)mipLevels );
ThrowIfFailed(device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&desc,
D3D12_RESOURCE_STATE_COMMON,
nullptr,
IID_PPV_ARGS(texture)
));
std::vector<D3D11_SUBRESOURCE_DATA> initialData(size_t{desc.MipLevels} * size_t{arraySize});
BYTE* srcBits = bitData;
const BYTE *endBits = bitData + bitSize;
for (UINT a = 0; a < arraySize; ++a) {
for (UINT m = 0; m < desc.MipLevels; ++m) {
auto width = (UINT)desc.Width >> m;
auto height = desc.Height >> m;
auto subresource = a * desc.MipLevels + m;
auto rowPitch = 4 * width;
auto bytes = rowPitch * height;
assert(srcBits + bytes <= endBits);
initialData[subresource].pSysMem = (void*)srcBits;
initialData[subresource].SysMemPitch = rowPitch;
initialData[subresource].SysMemSlicePitch = bytes;
srcBits += bytes;
}
}
InitializeTexture2D(device, cmdQueue, *texture, &desc, 4, initialData.data(), stateAfter);
delete[] heapData;
return S_OK;
}
|