This repository was archived by the owner on Apr 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathTexture2D.cpp
More file actions
278 lines (241 loc) · 10.6 KB
/
Copy pathTexture2D.cpp
File metadata and controls
278 lines (241 loc) · 10.6 KB
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
/////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or imlied.
// See the License for the specific language governing permissions and
// limitations under the License.
/////////////////////////////////////////////////////////////////////////////////////////////
#include "Texture2D.h"
#include <assert.h>
Texture2D::Texture2D(ID3D11Device* d3dDevice,
int width, int height, DXGI_FORMAT format,
UINT bindFlags,
int mipLevels)
{
InternalConstruct(d3dDevice, width, height, format, bindFlags, mipLevels, 1, 1, 0,
D3D11_RTV_DIMENSION_TEXTURE2D, D3D11_UAV_DIMENSION_TEXTURE2D, D3D11_SRV_DIMENSION_TEXTURE2D);
}
Texture2D::Texture2D(ID3D11Device* d3dDevice,
int width, int height, DXGI_FORMAT format,
UINT bindFlags,
const DXGI_SAMPLE_DESC& sampleDesc)
{
if (sampleDesc.Count > 1) {
// UAV's can't point to multisampled resources
InternalConstruct(d3dDevice, width, height, format, bindFlags, 1, 1, sampleDesc.Count, sampleDesc.Quality,
D3D11_RTV_DIMENSION_TEXTURE2DMS, D3D11_UAV_DIMENSION_UNKNOWN, D3D11_SRV_DIMENSION_TEXTURE2DMS);
} else {
InternalConstruct(d3dDevice, width, height, format, bindFlags, 1, 1, sampleDesc.Count, sampleDesc.Quality,
D3D11_RTV_DIMENSION_TEXTURE2D, D3D11_UAV_DIMENSION_UNKNOWN, D3D11_SRV_DIMENSION_TEXTURE2D);
}
}
Texture2D::Texture2D(ID3D11Device* d3dDevice,
int width, int height, DXGI_FORMAT format,
UINT bindFlags,
int mipLevels, int arraySize)
{
InternalConstruct(d3dDevice, width, height, format, bindFlags, mipLevels, arraySize, 1, 0,
D3D11_RTV_DIMENSION_TEXTURE2DARRAY, D3D11_UAV_DIMENSION_TEXTURE2DARRAY, D3D11_SRV_DIMENSION_TEXTURE2DARRAY);
}
Texture2D::Texture2D(ID3D11Device* d3dDevice,
int width, int height, DXGI_FORMAT format,
UINT bindFlags,
int arraySize, const DXGI_SAMPLE_DESC& sampleDesc)
{
if (sampleDesc.Count > 1) {
// UAV's can't point to multisampled resources
InternalConstruct(d3dDevice, width, height, format, bindFlags, 1, arraySize, sampleDesc.Count, sampleDesc.Quality,
D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY, D3D11_UAV_DIMENSION_UNKNOWN, D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY);
} else {
InternalConstruct(d3dDevice, width, height, format, bindFlags, 1, arraySize, sampleDesc.Count, sampleDesc.Quality,
D3D11_RTV_DIMENSION_TEXTURE2DARRAY, D3D11_UAV_DIMENSION_UNKNOWN, D3D11_SRV_DIMENSION_TEXTURE2DARRAY);
}
}
void Texture2D::InternalConstruct(ID3D11Device* d3dDevice,
int width, int height, DXGI_FORMAT format,
UINT bindFlags, int mipLevels, int arraySize,
int sampleCount, int sampleQuality,
D3D11_RTV_DIMENSION rtvDimension,
D3D11_UAV_DIMENSION uavDimension,
D3D11_SRV_DIMENSION srvDimension)
{
// Initalize
mShaderResource = 0;
CD3D11_TEXTURE2D_DESC desc(
format,
width, height, arraySize, mipLevels,
bindFlags,
D3D11_USAGE_DEFAULT, 0,
sampleCount, sampleQuality,
// If they request mipmap levels, it's nice to be able to autogenerate them.
(mipLevels != 1 ? D3D11_RESOURCE_MISC_GENERATE_MIPS : 0));
d3dDevice->CreateTexture2D(&desc, 0, &mTexture);
// Update with actual mip levels, etc.
mTexture->GetDesc(&desc);
if (bindFlags & D3D11_BIND_RENDER_TARGET) {
for (int i = 0; i < arraySize; ++i) {
CD3D11_RENDER_TARGET_VIEW_DESC rtvDesc(
rtvDimension,
format,
0, // Mips
i, 1 // Array
);
ID3D11RenderTargetView* renderTargetView;
d3dDevice->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
mRenderTargetElements.push_back(renderTargetView);
}
}
if (bindFlags & D3D11_BIND_UNORDERED_ACCESS) {
// UAV's can't point to multisampled resources!
assert(uavDimension != D3D11_UAV_DIMENSION_UNKNOWN);
for (int i = 0; i < arraySize; ++i) {
CD3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc(
uavDimension,
format,
0, // Mips
i, 1 // Array
);
ID3D11UnorderedAccessView* unorderedAccessView;
d3dDevice->CreateUnorderedAccessView(mTexture, &uavDesc, &unorderedAccessView);
mUnorderedAccessElements.push_back(unorderedAccessView);
}
}
if (bindFlags & D3D11_BIND_SHADER_RESOURCE) {
// Whole resource
CD3D11_SHADER_RESOURCE_VIEW_DESC srvDesc(
srvDimension,
format,
0, desc.MipLevels, // Mips
0, desc.ArraySize // Array
);
d3dDevice->CreateShaderResourceView(mTexture, &srvDesc, &mShaderResource);
// Single elements
for (int i = 0; i < arraySize; ++i) {
CD3D11_SHADER_RESOURCE_VIEW_DESC srvElementDesc(
srvDimension,
format,
0, 1, // Mips
i, 1 // Array
);
ID3D11ShaderResourceView* shaderResourceView;
d3dDevice->CreateShaderResourceView(mTexture, &srvElementDesc, &shaderResourceView);
mShaderResourceElements.push_back(shaderResourceView);
}
}
}
Texture2D::~Texture2D()
{
for (std::size_t i = 0; i < mRenderTargetElements.size(); ++i) {
mRenderTargetElements[i]->Release();
}
for (std::size_t i = 0; i < mUnorderedAccessElements.size(); ++i) {
mUnorderedAccessElements[i]->Release();
}
for (std::size_t i = 0; i < mShaderResourceElements.size(); ++i) {
mShaderResourceElements[i]->Release();
}
if (mShaderResource) mShaderResource->Release();
mTexture->Release();
}
Depth2D::Depth2D(ID3D11Device* d3dDevice,
int width, int height,
UINT bindFlags,
bool stencil)
{
InternalConstruct(d3dDevice, width, height, bindFlags, 1, 1, 0,
D3D11_DSV_DIMENSION_TEXTURE2D, D3D11_SRV_DIMENSION_TEXTURE2D, stencil);
}
Depth2D::Depth2D(ID3D11Device* d3dDevice,
int width, int height,
UINT bindFlags,
const DXGI_SAMPLE_DESC& sampleDesc,
bool stencil)
{
if (stencil) {
InternalConstruct(d3dDevice, width, height, bindFlags, 1, sampleDesc.Count, sampleDesc.Quality,
D3D11_DSV_DIMENSION_TEXTURE2DMS, D3D11_SRV_DIMENSION_TEXTURE2DMS, stencil);
} else {
InternalConstruct(d3dDevice, width, height, bindFlags, 1, sampleDesc.Count, sampleDesc.Quality,
D3D11_DSV_DIMENSION_TEXTURE2D, D3D11_SRV_DIMENSION_TEXTURE2D, stencil);
}
}
Depth2D::Depth2D(ID3D11Device* d3dDevice,
int width, int height,
UINT bindFlags,
int arraySize,
bool stencil)
{
InternalConstruct(d3dDevice, width, height, bindFlags, arraySize, 1, 0,
D3D11_DSV_DIMENSION_TEXTURE2DARRAY, D3D11_SRV_DIMENSION_TEXTURE2DARRAY, stencil);
}
Depth2D::Depth2D(ID3D11Device* d3dDevice,
int width, int height,
UINT bindFlags,
int arraySize,
const DXGI_SAMPLE_DESC& sampleDesc,
bool stencil)
{
if (stencil) {
InternalConstruct(d3dDevice, width, height, bindFlags, arraySize, sampleDesc.Count, sampleDesc.Quality,
D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY, D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY, stencil);
} else {
InternalConstruct(d3dDevice, width, height, bindFlags, arraySize, sampleDesc.Count, sampleDesc.Quality,
D3D11_DSV_DIMENSION_TEXTURE2DARRAY, D3D11_SRV_DIMENSION_TEXTURE2DARRAY, stencil);
}
}
void Depth2D::InternalConstruct(ID3D11Device* d3dDevice,
int width, int height,
UINT bindFlags, int arraySize,
int sampleCount, int sampleQuality,
D3D11_DSV_DIMENSION dsvDimension,
D3D11_SRV_DIMENSION srvDimension,
bool stencil)
{
// Initalize
mShaderResource = 0;
CD3D11_TEXTURE2D_DESC desc(
stencil ? DXGI_FORMAT_R32G8X24_TYPELESS : DXGI_FORMAT_R32_TYPELESS,
width, height, arraySize, 1,
bindFlags,
D3D11_USAGE_DEFAULT, 0,
sampleCount, sampleQuality);
d3dDevice->CreateTexture2D(&desc, 0, &mTexture);
if (bindFlags & D3D11_BIND_DEPTH_STENCIL) {
for (int i = 0; i < arraySize; ++i) {
CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilDesc(
dsvDimension,
stencil ? DXGI_FORMAT_D32_FLOAT_S8X24_UINT : DXGI_FORMAT_D32_FLOAT,
0, // Mips
i, 1 // Array
);
ID3D11DepthStencilView* depthStencilView;
d3dDevice->CreateDepthStencilView(mTexture, &depthStencilDesc, &depthStencilView);
mDepthStencilElements.push_back(depthStencilView);
}
}
if (bindFlags & D3D11_BIND_SHADER_RESOURCE) {
CD3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceDesc(
srvDimension,
stencil ? DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS : DXGI_FORMAT_R32_FLOAT,
0, 1, // Mips
0, arraySize // Array
);
d3dDevice->CreateShaderResourceView(mTexture, &shaderResourceDesc, &mShaderResource);
}
}
Depth2D::~Depth2D()
{
for (std::size_t i = 0; i < mDepthStencilElements.size(); ++i) {
mDepthStencilElements[i]->Release();
}
if (mShaderResource) mShaderResource->Release();
mTexture->Release();
}