Skip to content

Commit 34ad169

Browse files
committed
[D3D] Unified shader reflection between D3D11 and D3D12.
Moved major shader reflection code into templates in DXCommon static library.
1 parent ebeaceb commit 34ad169

File tree

4 files changed

+450
-602
lines changed

4 files changed

+450
-602
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* DXShaderReflection.cpp
3+
*
4+
* Copyright (c) 2015 Lukas Hermanns. All rights reserved.
5+
* Licensed under the terms of the BSD 3-Clause license (see LICENSE.txt).
6+
*/
7+
8+
#include "DXShaderReflection.h"
9+
10+
11+
namespace LLGL
12+
{
13+
14+
15+
ShaderResourceReflection* FetchOrInsertResource(
16+
ShaderReflection& outReflection,
17+
const char* name,
18+
const ResourceType type,
19+
std::uint32_t slot)
20+
{
21+
/* Fetch resource from list */
22+
for (ShaderResourceReflection& resource : outReflection.resources)
23+
{
24+
if (resource.binding.type == type &&
25+
resource.binding.slot == slot &&
26+
resource.binding.name.compare(name) == 0)
27+
{
28+
return (&resource);
29+
}
30+
}
31+
32+
/* Allocate new resource and initialize parameters */
33+
outReflection.resources.resize(outReflection.resources.size() + 1);
34+
ShaderResourceReflection* ref = &(outReflection.resources.back());
35+
{
36+
ref->binding.name = std::string(name);
37+
ref->binding.type = type;
38+
ref->binding.slot = slot;
39+
}
40+
return ref;
41+
}
42+
43+
UniformType MakeUniformVectorType(UniformType baseType, UINT elements)
44+
{
45+
if (elements >= 1 && elements <= 4)
46+
return static_cast<UniformType>(static_cast<int>(baseType) + (elements - 1));
47+
else
48+
return UniformType::Undefined;
49+
}
50+
51+
UniformType MakeUniformMatrixType(UniformType baseType, UINT rows, UINT cols)
52+
{
53+
if (rows < 2 || cols < 2)
54+
{
55+
if (baseType == UniformType::Float2x2)
56+
return MakeUniformVectorType(UniformType::Float1, std::max<int>(rows, cols));
57+
}
58+
else if (rows <= 4 && cols <= 4)
59+
return static_cast<UniformType>(static_cast<int>(baseType) + (rows - 2)*3 + (cols - 2));
60+
return UniformType::Undefined;
61+
}
62+
63+
UniformType MapD3DShaderScalarTypeToUniformType(D3D_SHADER_VARIABLE_TYPE type)
64+
{
65+
switch (type)
66+
{
67+
case D3D_SVT_BOOL: return UniformType::Bool1;
68+
case D3D_SVT_FLOAT: /*pass*/
69+
case D3D_SVT_FLOAT16: return UniformType::Float1;
70+
case D3D_SVT_INT: return UniformType::Int1;
71+
case D3D_SVT_UINT: return UniformType::UInt1;
72+
default: return UniformType::Undefined;
73+
}
74+
}
75+
76+
UniformType MapD3DShaderVectorTypeToUniformType(D3D_SHADER_VARIABLE_TYPE type, UINT elements)
77+
{
78+
switch (type)
79+
{
80+
case D3D_SVT_BOOL: return MakeUniformVectorType(UniformType::Bool1, elements);
81+
case D3D_SVT_FLOAT: /*pass*/
82+
case D3D_SVT_FLOAT16: return MakeUniformVectorType(UniformType::Float1, elements);
83+
case D3D_SVT_INT: return MakeUniformVectorType(UniformType::Int1, elements);
84+
case D3D_SVT_UINT: return MakeUniformVectorType(UniformType::UInt1, elements);
85+
default: return UniformType::Undefined;
86+
}
87+
}
88+
89+
UniformType MapD3DShaderMatrixTypeToUniformType(D3D_SHADER_VARIABLE_TYPE type, UINT rows, UINT cols)
90+
{
91+
switch (type)
92+
{
93+
case D3D_SVT_FLOAT: /*pass*/
94+
case D3D_SVT_FLOAT16: return MakeUniformMatrixType(UniformType::Float2x2, rows, cols);
95+
default: return UniformType::Undefined;
96+
}
97+
}
98+
99+
100+
} // /namespace LLGL
101+
102+
103+
104+
// ================================================================================

0 commit comments

Comments
 (0)