bool FShaderProgramInfo::Compile(ID3DBlob** CodeBlob) const { TVector<D3D_SHADER_MACRO> NullTerminatedDefines; NullTerminatedDefines.Resize(ShaderDefines.Size() + 1); for (size_t i = 0; i < ShaderDefines.Size(); i++) { NullTerminatedDefines[i] = ShaderDefines[i]; } size_t LastIndex = NullTerminatedDefines.Size() - 1; NullTerminatedDefines[LastIndex].Name = NullTerminatedDefines[LastIndex].Definition = nullptr; ID3DBlob* ErrorBlob = nullptr; HRESULT Result = D3DCompileFromFile(SourcePath.c_str(), NullTerminatedDefines.Data(), D3D_COMPILE_STANDARD_FILE_INCLUDE, EntryPoint.c_str(), ShaderTarget.c_str(), 0, 0, CodeBlob, &ErrorBlob); if (Result != S_OK) { OutputDebugString("ERROR COMPILING SHADER: "); if (ErrorBlob != nullptr) { OutputDebugString((LPCSTR)ErrorBlob->GetBufferPointer()); ErrorBlob->Release(); } } return Result == S_OK; }
TVector(TVector<T> & oth) { capacity = oth.capacity; size = 0; buf = new T[capacity]; for (int i = 0; i < oth.Size(); ++i) Push_back(oth[i]); }
HRESULT FMaterial::CreateInputLayoutDescFromVertexShaderSignature(ID3D11ShaderReflection* VertexShaderReflection, ID3DBlob* VertexShaderBlob, ID3D11Device* Device) { // Get shader info D3D11_SHADER_DESC ShaderDescription; VertexShaderReflection->GetDesc(&ShaderDescription); // Read input layout description from shader info TVector<D3D11_INPUT_ELEMENT_DESC> InputLayoutDesc; for (size_t i = 0; i < ShaderDescription.InputParameters; i++) { D3D11_SIGNATURE_PARAMETER_DESC ParamDescription; VertexShaderReflection->GetInputParameterDesc(i, &ParamDescription); // fill out input element desc D3D11_INPUT_ELEMENT_DESC ElementDesc; ElementDesc.SemanticName = ParamDescription.SemanticName; ElementDesc.SemanticIndex = ParamDescription.SemanticIndex; ElementDesc.InputSlot = 0; ElementDesc.AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; ElementDesc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; ElementDesc.InstanceDataStepRate = 0; // determine DXGI format if (ParamDescription.Mask == 1) { if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_UINT32) ElementDesc.Format = DXGI_FORMAT_R32_UINT; else if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_SINT32) ElementDesc.Format = DXGI_FORMAT_R32_SINT; else if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_FLOAT32) ElementDesc.Format = DXGI_FORMAT_R32_FLOAT; } else if (ParamDescription.Mask <= 3) { if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_UINT32) ElementDesc.Format = DXGI_FORMAT_R32G32_UINT; else if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_SINT32) ElementDesc.Format = DXGI_FORMAT_R32G32_SINT; else if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_FLOAT32) ElementDesc.Format = DXGI_FORMAT_R32G32_FLOAT; } else if (ParamDescription.Mask <= 7) { if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_UINT32) ElementDesc.Format = DXGI_FORMAT_R32G32B32_UINT; else if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_SINT32) ElementDesc.Format = DXGI_FORMAT_R32G32B32_SINT; else if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_FLOAT32) ElementDesc.Format = DXGI_FORMAT_R32G32B32_FLOAT; } else if (ParamDescription.Mask <= 15) { if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_UINT32) ElementDesc.Format = DXGI_FORMAT_R32G32B32A32_UINT; else if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_SINT32) ElementDesc.Format = DXGI_FORMAT_R32G32B32A32_SINT; else if (ParamDescription.ComponentType == D3D_REGISTER_COMPONENT_FLOAT32) ElementDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; } if (strcmp(ParamDescription.SemanticName, "COLOR") == 0) { // Special case: colour is actually a r8b8g8a8_unorm. ElementDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; } //save element desc InputLayoutDesc.Add(ElementDesc); } // Special case here: // If we're using SV_VertexID only then we don't actually need an input layout. if (InputLayoutDesc.Size() == 1 && strcmp(InputLayoutDesc[0].SemanticName, "SV_VertexID") != -1) { return S_OK; } // Try to create Input Layout HRESULT hr = Device->CreateInputLayout(&InputLayoutDesc[0], InputLayoutDesc.Size(), VertexShaderBlob->GetBufferPointer(), VertexShaderBlob->GetBufferSize(), &InputLayout); return hr; }