Diligent Engine API Reference: D:/GitHub/DiligentEngine/DiligentCore/Graphics/GraphicsEngineD3DBase/include/D3DViewDescConversionImpl.h Source File

Diligent Engine API Reference

D3DViewDescConversionImpl.h
1 /* Copyright 2015-2018 Egor Yusov
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12 *
13 * In no event and under no legal theory, whether in tort (including negligence),
14 * contract, or otherwise, unless required by applicable law (such as deliberate
15 * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16 * liable for any damages, including any direct, indirect, special, incidental,
17 * or consequential damages of any character arising as a result of this License or
18 * out of the use or inability to use the software (including but not limited to damages
19 * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20 * all other commercial damages or losses), even if such Contributor has been advised
21 * of the possibility of such damages.
22 */
23
24 #pragma once
25
28
30
31 #include "DXGITypeConversions.h"
32
33 namespace Diligent
34 {
35 template<typename D3D_SHADER_RESOURCE_VIEW_DESC>
36 void TextureViewDesc_to_D3D_SRV_DESC(const TextureViewDesc& SRVDesc, D3D_SHADER_RESOURCE_VIEW_DESC &d3dSRVDesc, Uint32 SampleCount)
37 {
38 memset(&d3dSRVDesc, 0, sizeof(d3dSRVDesc));
39 d3dSRVDesc.Format = TexFormatToDXGI_Format(SRVDesc.Format, BIND_SHADER_RESOURCE);
40
41 switch (SRVDesc.TextureDim)
42 {
43 case RESOURCE_DIM_TEX_1D:
44 d3dSRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE1D;
45 d3dSRVDesc.Texture1D.MipLevels = SRVDesc.NumMipLevels;
46 d3dSRVDesc.Texture1D.MostDetailedMip = SRVDesc.MostDetailedMip;
47 break;
48
49 case RESOURCE_DIM_TEX_1D_ARRAY:
50 d3dSRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE1DARRAY;
51 d3dSRVDesc.Texture1DArray.ArraySize = SRVDesc.NumArraySlices;
52 d3dSRVDesc.Texture1DArray.FirstArraySlice = SRVDesc.FirstArraySlice;
53 d3dSRVDesc.Texture1DArray.MipLevels = SRVDesc.NumMipLevels;
54 d3dSRVDesc.Texture1DArray.MostDetailedMip = SRVDesc.MostDetailedMip;
55 break;
56
57 case RESOURCE_DIM_TEX_2D:
58 if( SampleCount > 1 )
59 {
60 d3dSRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2DMS;
61 d3dSRVDesc.Texture2DMS.UnusedField_NothingToDefine = 0;
62 }
63 else
64 {
65 d3dSRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2D;
66 d3dSRVDesc.Texture2D.MipLevels = SRVDesc.NumMipLevels;
67 d3dSRVDesc.Texture2D.MostDetailedMip = SRVDesc.MostDetailedMip;
68 }
69 break;
70
71 case RESOURCE_DIM_TEX_2D_ARRAY:
72 if( SampleCount > 1 )
73 {
74 d3dSRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2DMSARRAY;
75 d3dSRVDesc.Texture2DMSArray.ArraySize = SRVDesc.NumArraySlices;
76 d3dSRVDesc.Texture2DMSArray.FirstArraySlice = SRVDesc.FirstArraySlice;
77 }
78 else
79 {
80 d3dSRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2DARRAY;
81 d3dSRVDesc.Texture2DArray.ArraySize = SRVDesc.NumArraySlices;
82 d3dSRVDesc.Texture2DArray.FirstArraySlice = SRVDesc.FirstArraySlice;
83 d3dSRVDesc.Texture2DArray.MipLevels = SRVDesc.NumMipLevels;
84 d3dSRVDesc.Texture2DArray.MostDetailedMip = SRVDesc.MostDetailedMip;
85 }
86 break;
87
88 case RESOURCE_DIM_TEX_3D:
89 d3dSRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE3D;
90 d3dSRVDesc.Texture3D.MipLevels = SRVDesc.NumMipLevels;
91 d3dSRVDesc.Texture3D.MostDetailedMip = SRVDesc.MostDetailedMip;
92 break;
93
94 case RESOURCE_DIM_TEX_CUBE:
95 d3dSRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURECUBE;
96 d3dSRVDesc.TextureCube.MipLevels = SRVDesc.NumMipLevels;
97 d3dSRVDesc.TextureCube.MostDetailedMip = SRVDesc.MostDetailedMip;
98 break;
99
100 case RESOURCE_DIM_TEX_CUBE_ARRAY:
101 d3dSRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURECUBEARRAY;
102 d3dSRVDesc.TextureCubeArray.MipLevels = SRVDesc.NumMipLevels;
103 d3dSRVDesc.TextureCubeArray.MostDetailedMip = SRVDesc.MostDetailedMip;
104 d3dSRVDesc.TextureCubeArray.First2DArrayFace = SRVDesc.FirstArraySlice;
105 d3dSRVDesc.TextureCubeArray.NumCubes = SRVDesc.NumArraySlices / 6;
106 break;
107
108 default:
109 UNEXPECTED( "Unexpected view type" );
110 }
111 }
112
113 template<typename D3D_RENDER_TARGET_VIEW_DESC>
114 void TextureViewDesc_to_D3D_RTV_DESC(const TextureViewDesc& RTVDesc, D3D_RENDER_TARGET_VIEW_DESC &d3dRTVDesc, Uint32 SampleCount)
115 {
116 memset(&d3dRTVDesc, 0, sizeof(d3dRTVDesc));
117 d3dRTVDesc.Format = TexFormatToDXGI_Format(RTVDesc.Format, BIND_RENDER_TARGET);
118
119 switch(RTVDesc.TextureDim)
120 {
121 case RESOURCE_DIM_TEX_1D:
122 d3dRTVDesc.ViewDimension = D3D_RTV_DIMENSION_TEXTURE1D;
123 d3dRTVDesc.Texture1D.MipSlice = RTVDesc.MostDetailedMip;
124 break;
125
126 case RESOURCE_DIM_TEX_1D_ARRAY:
127 d3dRTVDesc.ViewDimension = D3D_RTV_DIMENSION_TEXTURE1DARRAY;
128 d3dRTVDesc.Texture1DArray.ArraySize = RTVDesc.NumArraySlices;
129 d3dRTVDesc.Texture1DArray.FirstArraySlice = RTVDesc.FirstArraySlice;
130 d3dRTVDesc.Texture1DArray.MipSlice = RTVDesc.MostDetailedMip;
131 break;
132
133
134 case RESOURCE_DIM_TEX_2D:
135 if( SampleCount > 1 )
136 {
137 d3dRTVDesc.ViewDimension = D3D_RTV_DIMENSION_TEXTURE2DMS;
138 d3dRTVDesc.Texture2DMS.UnusedField_NothingToDefine = 0;
139 }
140 else
141 {
142 d3dRTVDesc.ViewDimension = D3D_RTV_DIMENSION_TEXTURE2D;
143 d3dRTVDesc.Texture2D.MipSlice = RTVDesc.MostDetailedMip;
144 }
145 break;
146
147 case RESOURCE_DIM_TEX_2D_ARRAY:
148 if( SampleCount > 1 )
149 {
150 d3dRTVDesc.ViewDimension = D3D_RTV_DIMENSION_TEXTURE2DMSARRAY;
151 d3dRTVDesc.Texture2DMSArray.ArraySize = RTVDesc.NumArraySlices;
152 d3dRTVDesc.Texture2DMSArray.FirstArraySlice = RTVDesc.FirstArraySlice;
153 }
154 else
155 {
156 d3dRTVDesc.ViewDimension = D3D_RTV_DIMENSION_TEXTURE2DARRAY;
157 d3dRTVDesc.Texture2DArray.ArraySize = RTVDesc.NumArraySlices;
158 d3dRTVDesc.Texture2DArray.FirstArraySlice = RTVDesc.FirstArraySlice;
159 d3dRTVDesc.Texture2DArray.MipSlice = RTVDesc.MostDetailedMip;
160 }
161 break;
162
163 case RESOURCE_DIM_TEX_3D:
164 d3dRTVDesc.ViewDimension = D3D_RTV_DIMENSION_TEXTURE3D;
165 d3dRTVDesc.Texture3D.FirstWSlice = RTVDesc.FirstDepthSlice;
166 d3dRTVDesc.Texture3D.WSize = RTVDesc.NumDepthSlices;
167 d3dRTVDesc.Texture3D.MipSlice = RTVDesc.MostDetailedMip;
168 break;
169
170 default:
171 UNEXPECTED( "Unexpected view type" );
172 }
173 }
174
175 template<typename D3D_DEPTH_STENCIL_VIEW_DESC>
176 void TextureViewDesc_to_D3D_DSV_DESC(const TextureViewDesc& DSVDesc, D3D_DEPTH_STENCIL_VIEW_DESC &d3dDSVDesc, Uint32 SampleCount)
177 {
178 memset(&d3dDSVDesc, 0, sizeof(d3dDSVDesc));
179 d3dDSVDesc.Format = TexFormatToDXGI_Format(DSVDesc.Format, BIND_DEPTH_STENCIL);
180
181 switch(DSVDesc.TextureDim)
182 {
183 case RESOURCE_DIM_TEX_1D:
184 d3dDSVDesc.ViewDimension = D3D_DSV_DIMENSION_TEXTURE1D;
185 d3dDSVDesc.Texture1D.MipSlice = DSVDesc.MostDetailedMip;
186 break;
187
188 case RESOURCE_DIM_TEX_1D_ARRAY:
189 d3dDSVDesc.ViewDimension = D3D_DSV_DIMENSION_TEXTURE1DARRAY;
190 d3dDSVDesc.Texture1DArray.ArraySize = DSVDesc.NumArraySlices;
191 d3dDSVDesc.Texture1DArray.FirstArraySlice = DSVDesc.FirstArraySlice;
192 d3dDSVDesc.Texture1DArray.MipSlice = DSVDesc.MostDetailedMip;
193 break;
194
195
196 case RESOURCE_DIM_TEX_2D:
197 if( SampleCount > 1 )
198 {
199 d3dDSVDesc.ViewDimension = D3D_DSV_DIMENSION_TEXTURE2DMS;
200 d3dDSVDesc.Texture2DMS.UnusedField_NothingToDefine = 0;
201 }
202 else
203 {
204 d3dDSVDesc.ViewDimension = D3D_DSV_DIMENSION_TEXTURE2D;
205 d3dDSVDesc.Texture2D.MipSlice = DSVDesc.MostDetailedMip;
206 }
207 break;
208
209 case RESOURCE_DIM_TEX_2D_ARRAY:
210 if( SampleCount > 1 )
211 {
212 d3dDSVDesc.ViewDimension = D3D_DSV_DIMENSION_TEXTURE2DMSARRAY;
213 d3dDSVDesc.Texture2DMSArray.ArraySize = DSVDesc.NumArraySlices;
214 d3dDSVDesc.Texture2DMSArray.FirstArraySlice = DSVDesc.FirstArraySlice;
215 }
216 else
217 {
218 d3dDSVDesc.ViewDimension = D3D_DSV_DIMENSION_TEXTURE2DARRAY;
219 d3dDSVDesc.Texture2DArray.ArraySize = DSVDesc.NumArraySlices;
220 d3dDSVDesc.Texture2DArray.FirstArraySlice = DSVDesc.FirstArraySlice;
221 d3dDSVDesc.Texture2DArray.MipSlice = DSVDesc.MostDetailedMip;
222 }
223 break;
224
225 case RESOURCE_DIM_TEX_3D:
226 LOG_ERROR_AND_THROW("Depth stencil views are not supported for 3D textures");
227 break;
228
229 default:
230 UNEXPECTED( "Unexpected view type" );
231 }
232 }
233
234 template<typename D3D_UNORDERED_ACCESS_VIEW_DESC>
235 void TextureViewDesc_to_D3D_UAV_DESC(const TextureViewDesc& UAVDesc, D3D_UNORDERED_ACCESS_VIEW_DESC &d3dUAVDesc)
236 {
237 memset(&d3dUAVDesc, 0, sizeof(d3dUAVDesc));
238 d3dUAVDesc.Format = TexFormatToDXGI_Format(UAVDesc.Format, BIND_UNORDERED_ACCESS);
239
240 switch(UAVDesc.TextureDim)
241 {
242 case RESOURCE_DIM_TEX_1D:
243 d3dUAVDesc.ViewDimension = D3D_UAV_DIMENSION_TEXTURE1D;
244 d3dUAVDesc.Texture1D.MipSlice = UAVDesc.MostDetailedMip;
245 break;
246
247 case RESOURCE_DIM_TEX_1D_ARRAY:
248 d3dUAVDesc.ViewDimension = D3D_UAV_DIMENSION_TEXTURE1DARRAY;
249 d3dUAVDesc.Texture1DArray.ArraySize = UAVDesc.NumArraySlices;
250 d3dUAVDesc.Texture1DArray.FirstArraySlice = UAVDesc.FirstArraySlice;
251 d3dUAVDesc.Texture1DArray.MipSlice = UAVDesc.MostDetailedMip;
252 break;
253
254 case RESOURCE_DIM_TEX_2D:
255 d3dUAVDesc.ViewDimension = D3D_UAV_DIMENSION_TEXTURE2D;
256 d3dUAVDesc.Texture2D.MipSlice = UAVDesc.MostDetailedMip;
257 break;
258
259 case RESOURCE_DIM_TEX_2D_ARRAY:
260 d3dUAVDesc.ViewDimension = D3D_UAV_DIMENSION_TEXTURE2DARRAY;
261 d3dUAVDesc.Texture2DArray.ArraySize = UAVDesc.NumArraySlices;
262 d3dUAVDesc.Texture2DArray.FirstArraySlice = UAVDesc.FirstArraySlice;
263 d3dUAVDesc.Texture2DArray.MipSlice = UAVDesc.MostDetailedMip;
264 break;
265
266 case RESOURCE_DIM_TEX_3D:
267 d3dUAVDesc.ViewDimension = D3D_UAV_DIMENSION_TEXTURE3D;
268 d3dUAVDesc.Texture3D.FirstWSlice = UAVDesc.FirstDepthSlice;
269 d3dUAVDesc.Texture3D.WSize = UAVDesc.NumDepthSlices;
270 d3dUAVDesc.Texture3D.MipSlice = UAVDesc.MostDetailedMip;
271 break;
272
273 default:
274 UNEXPECTED( "Unexpected view type" );
275 }
276 }
277
278 template<typename D3D_UNORDERED_ACCESS_VIEW_DESC>
279 void BufferViewDesc_to_D3D_SRV_DESC(const BufferDesc &BuffDesc, const BufferViewDesc& SRVDesc, D3D_UNORDERED_ACCESS_VIEW_DESC &d3dSRVDesc)
280 {
281 if( SRVDesc.ByteOffset != 0 )
282 {
283 VERIFY( BuffDesc.Mode == BUFFER_MODE_STRUCTURED, "Non-zero byte offset is only supported for structured buffers" );
284 }
285
286 memset( &d3dSRVDesc, 0, sizeof( d3dSRVDesc ) );
287 const auto &BuffFmt = BuffDesc.Format;
288 if( BuffDesc.Mode == BUFFER_MODE_FORMATTED )
289 d3dSRVDesc.Format = TypeToDXGI_Format( BuffFmt.ValueType, BuffFmt.NumComponents, BuffFmt.IsNormalized );
290
291 VERIFY( SRVDesc.ViewType == BUFFER_VIEW_SHADER_RESOURCE, "Incorrect view type: shader resource is expected" );
292 VERIFY( (SRVDesc.ByteOffset % BuffDesc.ElementByteStride) == 0, "Byte offest is not multiple of element byte stride" );
293 VERIFY( (SRVDesc.ByteWidth % BuffDesc.ElementByteStride) == 0, "Byte width is not multiple of element byte stride" );
294 d3dSRVDesc.Buffer.FirstElement = SRVDesc.ByteOffset / BuffDesc.ElementByteStride;
295 d3dSRVDesc.Buffer.NumElements = SRVDesc.ByteWidth / BuffDesc.ElementByteStride;
296 d3dSRVDesc.ViewDimension = D3D_SRV_DIMENSION_BUFFER;
297 }
298
299 template<typename D3D_UNORDERED_ACCESS_VIEW_DESC>
300 void BufferViewDesc_to_D3D_UAV_DESC(const BufferDesc &BuffDesc, const BufferViewDesc& UAVDesc, D3D_UNORDERED_ACCESS_VIEW_DESC &d3dUAVDesc)
301 {
302 if( UAVDesc.ByteOffset != 0 )
303 {
304 VERIFY( BuffDesc.Mode == BUFFER_MODE_STRUCTURED, "Non-zero byte offset is only supported for structured buffers" );
305 }
306
307 memset( &d3dUAVDesc, 0, sizeof(d3dUAVDesc) );
308 const auto &BuffFmt = BuffDesc.Format;
309 if( BuffDesc.Mode == BUFFER_MODE_FORMATTED )
310 d3dUAVDesc.Format = TypeToDXGI_Format( BuffFmt.ValueType, BuffFmt.NumComponents, BuffFmt.IsNormalized );
311
312 VERIFY( UAVDesc.ViewType == BUFFER_VIEW_UNORDERED_ACCESS, "Incorrect view type: unordered access is expected" );
313 VERIFY( (UAVDesc.ByteOffset % BuffDesc.ElementByteStride) == 0, "Byte offest is not multiple of element byte stride" );
314 VERIFY( (UAVDesc.ByteWidth % BuffDesc.ElementByteStride) == 0, "Byte width is not multiple of element byte stride" );
315 d3dUAVDesc.Buffer.FirstElement = UAVDesc.ByteOffset / BuffDesc.ElementByteStride;
316 d3dUAVDesc.Buffer.NumElements = UAVDesc.ByteWidth / BuffDesc.ElementByteStride;
317 d3dUAVDesc.ViewDimension = D3D_UAV_DIMENSION_BUFFER;
318 }
319 }
Diligent::RESOURCE_DIM_TEX_CUBE
Cube-map texture.
Definition: GraphicsTypes.h:181
Diligent::RESOURCE_DIM_TEX_2D
Two-dimensional texture.
Definition: GraphicsTypes.h:178
Diligent::BIND_RENDER_TARGET
A texture can be bound as a render target.
Definition: GraphicsTypes.h:71
Diligent::RESOURCE_DIM_TEX_1D
One-dimensional texture.
Definition: GraphicsTypes.h:176
Diligent::RESOURCE_DIM_TEX_CUBE_ARRAY
Cube-map array texture.
Definition: GraphicsTypes.h:182
Diligent::BIND_SHADER_RESOURCE
A buffer or a texture can be bound as a shader resource.
Definition: GraphicsTypes.h:68
Diligent
Graphics engine namespace.
Definition: AdaptiveFixedBlockAllocator.h:30
Diligent::BUFFER_VIEW_UNORDERED_ACCESS
A buffer view will define an unordered access view that will be used for unordered read/write operati...
Definition: GraphicsTypes.h:230
Diligent::BUFFER_MODE_STRUCTURED
Structured buffer.
Definition: Buffer.h:50
Diligent::BUFFER_MODE_FORMATTED
Formated buffer.
Definition: Buffer.h:47
Diligent::BIND_DEPTH_STENCIL
A texture can be bound as a depth-stencil target.
Definition: GraphicsTypes.h:72
Diligent::RESOURCE_DIM_TEX_1D_ARRAY
One-dimensional texture array.
Definition: GraphicsTypes.h:177
Diligent::BIND_UNORDERED_ACCESS
A buffer or a texture can be bound as an unordered access view.
Definition: GraphicsTypes.h:73
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:39
Diligent::BUFFER_VIEW_SHADER_RESOURCE
A buffer view will define a shader resource view that will be used as the source for the shader read ...
Definition: GraphicsTypes.h:226
Diligent::RESOURCE_DIM_TEX_3D
Three-dimensional texture.
Definition: GraphicsTypes.h:180
Diligent::RESOURCE_DIM_TEX_2D_ARRAY
Two-dimensional texture array.
Definition: GraphicsTypes.h:179