86 ComPtr<ID3D11ShaderReflection> reflection;
87 HRESULT hr = D3DReflect(code, size, IID_PPV_ARGS(&reflection));
88 if (FAILED(hr))
return Error(
"reflect native shader", hr);
89 D3D11_SHADER_DESC desc{};
90 if (FAILED(hr = reflection->GetDesc(&desc)))
return hr;
91 for (UINT i = 0; i < desc.BoundResources; ++i)
93 D3D11_SHADER_INPUT_BIND_DESC binding{};
94 if (FAILED(hr = reflection->GetResourceBindingDesc(i, &binding)))
return hr;
95 if (binding.BindCount != 1)
return Error(
"resource arrays require explicit binding: " + std::string(binding.Name), E_NOTIMPL);
96 if (binding.Type == D3D_SIT_TEXTURE) stage.
textures.push_back({binding.Name, binding.BindPoint, binding.Dimension});
97 else if (binding.Type == D3D_SIT_SAMPLER) stage.
samplers.push_back({binding.Name, binding.BindPoint});
98 else if (binding.Type == D3D_SIT_CBUFFER)
100 auto* reflected = reflection->GetConstantBufferByName(binding.Name);
101 D3D11_SHADER_BUFFER_DESC bufferDesc{};
102 if (FAILED(hr = reflected->GetDesc(&bufferDesc)))
return hr;
104 buffer.
slot = binding.BindPoint;
105 buffer.
defaults.resize(bufferDesc.Size);
106 for (UINT j = 0; j < bufferDesc.Variables; ++j)
108 auto* reflectedVar = reflected->GetVariableByIndex(j);
109 D3D11_SHADER_VARIABLE_DESC variableDesc{};
110 D3D11_SHADER_TYPE_DESC typeDesc{};
111 if (FAILED(hr = reflectedVar->GetDesc(&variableDesc)) || FAILED(hr = reflectedVar->GetType()->GetDesc(&typeDesc)))
return hr;
112 if (!(variableDesc.uFlags & D3D_SVF_USED))
continue;
113 if (typeDesc.Class == D3D_SVC_STRUCT || typeDesc.Class == D3D_SVC_OBJECT)
114 return Error(
"unsupported constant type: " + std::string(variableDesc.Name), E_NOTIMPL);
115 buffer.
variables.push_back({variableDesc.Name, variableDesc.StartOffset, variableDesc.Size,
116 typeDesc.Elements, typeDesc.Rows, typeDesc.Columns, typeDesc.Class});
117 if (variableDesc.DefaultValue)
118 memcpy(buffer.
defaults.data() + variableDesc.StartOffset, variableDesc.DefaultValue, variableDesc.Size);
123 D3D11_BUFFER_DESC native{};
124 native.ByteWidth = bufferDesc.Size; native.Usage = D3D11_USAGE_DYNAMIC;
125 native.BindFlags = D3D11_BIND_CONSTANT_BUFFER; native.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
126 if (FAILED(hr = device->CreateBuffer(&native,
nullptr, &buffer.
buffer)))
return Error(
"create constant buffer", hr);
127 stage.
constants.push_back(std::move(buffer));
129 else return Error(
"unsupported shader resource: " + std::string(binding.Name), E_NOTIMPL);
135 const void* psCode,
size_t psSize,
const D3D11_INPUT_ELEMENT_DESC* elements, UINT count, UINT packedColors)
139 if (!device || !vsCode || !vsSize || !psCode || !psSize || (count && !elements))
return E_INVALIDARG;
140 UINT validPackedColors = 0;
141 for (UINT i = 0; i < count; ++i)
142 if (elements[i].SemanticName && !_stricmp(elements[i].SemanticName,
"COLOR") && elements[i].SemanticIndex < 32
143 && elements[i].Format == DXGI_FORMAT_R8G8B8A8_UNORM) validPackedColors |= 1u << elements[i].SemanticIndex;
144 if (packedColors & ~validPackedColors)
return Error(
"packed color mask requires a matching R8G8B8A8 COLOR attribute", E_INVALIDARG);
146 ComPtr<ID3D11ShaderReflection> vsReflection, psReflection;
147 HRESULT hr = D3DReflect(vsCode, vsSize, IID_PPV_ARGS(&vsReflection));
148 if (FAILED(hr))
return Error(
"reflect vertex signature", hr);
149 if (FAILED(hr = D3DReflect(psCode, psSize, IID_PPV_ARGS(&psReflection))))
return Error(
"reflect pixel signature", hr);
150 D3D11_SHADER_DESC vsDesc{}, psDesc{};
151 if (FAILED(hr = vsReflection->GetDesc(&vsDesc)) || FAILED(hr = psReflection->GetDesc(&psDesc)))
return hr;
152 for (UINT i = 0; i < psDesc.InputParameters; ++i)
154 D3D11_SIGNATURE_PARAMETER_DESC input{};
155 if (FAILED(hr = psReflection->GetInputParameterDesc(i, &input)))
return hr;
156 if (!input.ReadWriteMask || input.SystemValueType == D3D_NAME_SAMPLE_INDEX
157 || input.SystemValueType == D3D_NAME_PRIMITIVE_ID || input.SystemValueType == D3D_NAME_IS_FRONT_FACE)
continue;
158 bool matched =
false;
159 for (UINT j = 0; j < vsDesc.OutputParameters; ++j)
161 D3D11_SIGNATURE_PARAMETER_DESC output{};
162 if (FAILED(hr = vsReflection->GetOutputParameterDesc(j, &output)))
return hr;
163 if (_stricmp(input.SemanticName, output.SemanticName) == 0 && input.SemanticIndex == output.SemanticIndex)
164 matched = input.Register == output.Register && input.ComponentType == output.ComponentType
165 && (input.ReadWriteMask & output.Mask) == input.ReadWriteMask;
167 if (!matched)
return Error(
"vertex/pixel signature mismatch: " + std::string(input.SemanticName)
168 + std::to_string(input.SemanticIndex), E_INVALIDARG);
170 hr = device->CreateVertexShader(vsCode, vsSize,
nullptr, &
vertex);
171 if (FAILED(hr))
return Error(
"create vertex shader", hr);
172 if (FAILED(hr = device->CreatePixelShader(psCode, psSize,
nullptr, &
pixel)))
return Error(
"create pixel shader", hr);
173 if (count && FAILED(hr = device->CreateInputLayout(elements, count, vsCode, vsSize, &
layout)))
return Error(
"create input layout", hr);
176 bool needsColorSwizzle =
false, hasColorSwizzle =
false;
177 for (UINT i = 0; i < vsDesc.InputParameters; ++i)
179 D3D11_SIGNATURE_PARAMETER_DESC input{};
180 if (FAILED(hr = vsReflection->GetInputParameterDesc(i, &input)))
return hr;
181 if (!_stricmp(input.SemanticName,
"COLOR") && input.SemanticIndex < 32 && input.ReadWriteMask
182 && (packedColors & (1u << input.SemanticIndex))) needsColorSwizzle =
true;
185 if (variable.name ==
"_risePackedColorMask") hasColorSwizzle =
true;
186 if (needsColorSwizzle && !hasColorSwizzle)
return Error(
"vertex shader lacks requested packed-color conversion", E_INVALIDARG);
187 status =
"native vertex/pixel program ready";
196 for (
auto& buffer : stage.
constants)
for (
auto& variable : buffer.variables)
198 const auto found = parameters.
values.find(variable.name);
199 variable.source = found == parameters.
values.end() ? nullptr : &found->second;
201 for (
auto& binding : stage.
textures)
203 auto found = parameters.
textures.find(binding.name);
205 if (found == parameters.
textures.end() && binding.name.size() > 6 && binding.name.compare(binding.name.size() - 6, 6,
"Volume") == 0)
206 found = parameters.
textures.find(binding.name.substr(0, binding.name.size() - 6));
207 binding.texture = found == parameters.
textures.end() ? nullptr : &found->second;
209 for (
auto& binding : stage.
samplers)
211 const auto found = parameters.
samplers.find(binding.name);
212 binding.sampler = found == parameters.
samplers.end() ? nullptr : std::addressof(found->second);
218 auto& next = buffer.packed;
219 if (buffer.packedVersion != parameters.
version)
223 buffer.packedVersion = UINT64_MAX;
224 for (
auto& variable : buffer.variables)
226 if (isVertex && variable.name ==
"_risePackedColorMask")
228 if (variable.size !=
sizeof(
bgraColorMask))
return Error(
"invalid packed color constant", E_INVALIDARG);
232 const auto* found = variable.source;
233 const uint64_t revision = found ? found->version : 0;
234 if (variable.packedVersion == revision)
continue;
239 memcpy(next.data() + variable.offset, buffer.defaults.data() + variable.offset, variable.size);
240 variable.packedVersion = revision;
continue;
242 const auto& source = found->data;
243 const UINT elements = (std::max)(1u, variable.elements);
244 const UINT valueBytes = variable.rows * variable.columns *
sizeof(float);
245 if (source.size() >
size_t(elements) * valueBytes || source.size() % valueBytes)
246 return Error(
"constant size mismatch: " + variable.name, E_INVALIDARG);
247 memcpy(next.data() + variable.offset, buffer.defaults.data() + variable.offset, variable.size);
250 const bool matrix = variable.typeClass == D3D_SVC_MATRIX_ROWS || variable.typeClass == D3D_SVC_MATRIX_COLUMNS;
251 const UINT vectors = matrix ? (variable.typeClass == D3D_SVC_MATRIX_ROWS ? variable.rows : variable.columns) : 1;
252 const UINT stride = vectors * 16;
253 for (UINT i = 0; i < source.size() / valueBytes; ++i)
255 auto* destination = next.data() + variable.offset + (variable.elements ? stride * i : 0);
256 const auto* input = source.data() + valueBytes * i;
257 if (!matrix) memcpy(destination, input, valueBytes);
258#if defined(_M_IX86) || defined(_M_X64)
259 else if (variable.rows == 4 && variable.columns == 4)
264 if (variable.typeClass == D3D_SVC_MATRIX_ROWS) memcpy(destination,input,64);
267 auto a=_mm_loadu_ps(
reinterpret_cast<const float*
>(input));
268 auto b=_mm_loadu_ps(
reinterpret_cast<const float*
>(input+16));
269 auto c=_mm_loadu_ps(
reinterpret_cast<const float*
>(input+32));
270 auto d=_mm_loadu_ps(
reinterpret_cast<const float*
>(input+48));
271 _MM_TRANSPOSE4_PS(a,b,c,d);
272 _mm_storeu_ps(
reinterpret_cast<float*
>(destination),a);
273 _mm_storeu_ps(
reinterpret_cast<float*
>(destination+16),b);
274 _mm_storeu_ps(
reinterpret_cast<float*
>(destination+32),c);
275 _mm_storeu_ps(
reinterpret_cast<float*
>(destination+48),d);
279 else for (UINT row = 0; row < variable.rows; ++row)
for (UINT column = 0; column < variable.columns; ++column)
281 const UINT offset = variable.typeClass == D3D_SVC_MATRIX_ROWS ? row * 16 + column * 4 : column * 16 + row * 4;
282 memcpy(destination + offset, input + (row * variable.columns + column) * 4, 4);
285 variable.packedVersion = revision;
287 buffer.packedVersion = parameters.
version;
289 if (next != buffer.data)
291 D3D11_MAPPED_SUBRESOURCE mapped{};
292 const HRESULT hr = context->Map(buffer.buffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped);
293 if (FAILED(hr))
return Error(
"upload constant buffer", hr);
294 memcpy(mapped.pData, next.data(), next.size());
295 context->Unmap(buffer.buffer.Get(), 0);
299 auto* resource = buffer.buffer.Get();
300 if (isVertex) context->VSSetConstantBuffers(buffer.slot, 1, &resource);
301 else context->PSSetConstantBuffers(buffer.slot, 1, &resource);
305 ID3D11ShaderResourceView* textureResources[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT];
306 UINT textureFirst=0,textureCount=0;
307 const auto bindTextures=[&]() {
308 if(!textureCount)
return;
309 if(isVertex)context->VSSetShaderResources(textureFirst,textureCount,textureResources);
310 else context->PSSetShaderResources(textureFirst,textureCount,textureResources);
313 for (
const auto& binding : stage.
textures)
315 if(textureCount && binding.slot!=textureFirst+textureCount)bindTextures();
316 if(!textureCount)textureFirst=binding.slot;
317 ID3D11ShaderResourceView* resource = binding.texture ? binding.texture->resource.Get() :
nullptr;
320 if (UINT(binding.texture->dimension) != UINT(binding.dimension))
323 bindTextures();
return Error(
"texture dimension mismatch: " + binding.name, E_INVALIDARG);
326 textureResources[textureCount++]=resource;
329 ID3D11SamplerState* samplerResources[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT];
330 UINT samplerFirst=0,samplerCount=0;
331 const auto bindSamplers=[&]() {
332 if(!samplerCount)
return;
333 if(isVertex)context->VSSetSamplers(samplerFirst,samplerCount,samplerResources);
334 else context->PSSetSamplers(samplerFirst,samplerCount,samplerResources);
337 for (
const auto& binding : stage.
samplers)
339 if(samplerCount && binding.slot!=samplerFirst+samplerCount)bindSamplers();
340 if(!samplerCount)samplerFirst=binding.slot;
341 if (!binding.sampler || !*binding.sampler)
343 bindSamplers();
return Error(
"missing sampler: " + binding.name, E_INVALIDARG);
345 samplerResources[samplerCount++]=binding.sampler->Get();