18 std::ifstream
input(filename);
21 WriteLog(
"Cannot open file " + filename);
30 source += line +
"\n";
49 unsigned char const* bytecode)
51 ID3DBlob* compiledCode =
nullptr;
52 HRESULT hr = D3DCreateBlob(numBytes, &compiledCode);
55 WriteLog(
"CreateFromBytecode failed to create blob.");
59 Memcpy(compiledCode->GetBufferPointer(), bytecode, numBytes);
62 if (!
ReflectShader(name, entry, target, compiledCode, shader))
67 compiledCode->Release();
74 unsigned int compileFlags)
77 if (type ==
"vs_" || type ==
"gs_" || type ==
"ps_" || type ==
"cs_")
79 ID3DBlob* compiledCode =
CompileShader(name, source, entry, target,
80 compileFlags, defines);
88 if (!
ReflectShader(name, entry, target, compiledCode, shader))
93 compiledCode->Release();
98 WriteLog(
"Invalid target " + target);
108 ID3DInclude* include = D3D_COMPILE_STANDARD_FILE_INCLUDE;
109 ID3DBlob* compiledCode =
nullptr;
110 ID3DBlob* errors =
nullptr;
114 auto const& definitions = defines.
Get();
115 std::vector<D3D_SHADER_MACRO> localDefinitions(definitions.size() + 2);
118 #if defined(GTE_USE_MAT_VEC) 123 localDefinitions.front().Name = umvName.c_str();
124 localDefinitions.front().Definition = umvValue.c_str();
125 for (
size_t i = 0; i < definitions.size(); ++i)
127 localDefinitions[i + 1].Name = definitions[i].first.c_str();
128 localDefinitions[i + 1].Definition = definitions[i].second.c_str();
130 localDefinitions.back().Name =
nullptr;
131 localDefinitions.back().Definition =
nullptr;
133 if (!(compileFlags & D3DCOMPILE_PACK_MATRIX_ROW_MAJOR)
134 && !(compileFlags & D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR))
138 #if defined(GTE_USE_ROW_MAJOR) 139 compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
141 compileFlags |= D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR;
145 HRESULT hr = D3DCompile(source.c_str(), source.length(), name.c_str(),
146 localDefinitions.data(), include, entry.c_str(), target.c_str(),
147 compileFlags, 0, &compiledCode, &errors);
155 static_cast<char const*
>(errors->GetBufferPointer());
159 size_t const length = strlen(message);
160 std::wstring output = L
"";
161 for (
size_t i = 0; i <
length; ++i)
163 output +=
static_cast<wchar_t>(message[i]);
166 OutputDebugString(output.c_str());
177 static_cast<char const*
>(errors->GetBufferPointer());
178 size_t const length = strlen(message);
182 std::wstring output = L
"";
183 for (
size_t i = 0; i <
length; ++i)
185 output +=
static_cast<wchar_t>(message[i]);
188 OutputDebugString(output.c_str());
195 WriteLog(
"D3DCompile error, hr = " + std::to_string(hr));
211 void const*
buffer = compiledCode->GetBufferPointer();
212 size_t numBytes = compiledCode->GetBufferSize();
217 WriteLog(
"D3DReflect error, hr = " + std::to_string(hr));
239 UINT numX, numY, numZ;
240 reflector->GetThreadGroupSize(&numX, &numY, &numZ);
245 reflector->Release();
252 HRESULT hr = reflector->GetDesc(&desc);
255 WriteLog(
"reflector->GetDesc error, hr = " + std::to_string(hr));
265 for (
UINT i = 0; i < numInputs; ++i)
268 HRESULT hr = reflector->GetInputParameterDesc(i, &spDesc);
271 WriteLog(
"reflector->GetInputParameterDesc error, hr = " +
283 for (
UINT i = 0; i < numOutputs; ++i)
286 HRESULT hr = reflector->GetOutputParameterDesc(i, &spDesc);
289 WriteLog(
"reflector->GetOutputParameterDesc error, hr = " +
301 for (
UINT i = 0; i < numCBuffers; ++i)
306 HRESULT hr = cbuffer->GetDesc(&cbDesc);
309 WriteLog(
"reflector->GetConstantBufferByIndex error, hr = " +
315 hr = reflector->GetResourceBindingDescByName(cbDesc.Name, &resDesc);
318 WriteLog(
"reflector->GetResourceBindingDescByName error, hr = " +
323 if (cbDesc.Type == D3D_CT_CBUFFER)
325 std::vector<HLSLBaseBuffer::Member> members;
331 if (resDesc.BindCount == 1)
337 for (
UINT j = 0; j < resDesc.BindCount; ++j)
343 else if (cbDesc.Type == D3D_CT_TBUFFER)
345 std::vector<HLSLBaseBuffer::Member> members;
351 if (resDesc.BindCount == 1)
357 for (
UINT j = 0; j < resDesc.BindCount; ++j)
360 cbDesc.Size, members));
364 else if (cbDesc.Type == D3D_CT_RESOURCE_BIND_INFO)
366 std::vector<HLSLBaseBuffer::Member> members;
372 if (resDesc.BindCount == 1)
378 for (
UINT j = 0; j < resDesc.BindCount; ++j)
386 WriteLog(
"Interface pointers are not yet supported in GTEngine.");
405 for (
UINT i = 0; i < numResources; ++i)
408 HRESULT hr = reflector->GetResourceBindingDesc(i, &resDesc);
411 WriteLog(
"reflector->GetResourceBindingDesc error, hr = " + std::to_string(hr));
415 if (resDesc.Type == D3D_SIT_CBUFFER
416 || resDesc.Type == D3D_SIT_TBUFFER)
421 resDesc.Type == D3D_SIT_TEXTURE
422 || resDesc.Type == D3D_SIT_UAV_RWTYPED)
426 if (resDesc.BindCount == 1)
432 for (
UINT j = 0; j < resDesc.BindCount; ++j)
440 if (resDesc.BindCount == 1)
446 for (
UINT j = 0; j < resDesc.BindCount; ++j)
453 else if (resDesc.Type == D3D_SIT_SAMPLER)
455 if (resDesc.BindCount == 1)
461 for (
UINT j = 0; j < resDesc.BindCount; ++j)
468 resDesc.Type == D3D_SIT_BYTEADDRESS
469 || resDesc.Type == D3D_SIT_UAV_RWBYTEADDRESS)
471 if (resDesc.BindCount == 1)
477 for (
UINT j = 0; j < resDesc.BindCount; ++j)
491 if (resDesc.BindCount == 1)
497 for (
UINT j = 0; j < resDesc.BindCount; ++j)
510 unsigned int numVariables,
511 std::vector<HLSLBaseBuffer::Member>& members)
513 for (
UINT i = 0; i < numVariables; ++i)
519 HRESULT hr = var->GetDesc(&varDesc);
522 WriteLog(
"var->GetDesc error, hr = " + std::to_string(hr));
527 hr = varType->GetDesc(&varTypeDesc);
530 WriteLog(
"varType->GetDesc error, hr = " + std::to_string(hr));
543 if (!
GetTypes(varType, varTypeDesc.Members, stype))
548 members.push_back(std::make_pair(svar, stype));
556 for (
UINT i = 0; i < numMembers; ++i)
559 char const* memTypeName = rtype->GetMemberTypeName(i);
560 std::string memName(memTypeName ? memTypeName :
"");
562 HRESULT hr = memType->GetDesc(&memTypeDesc);
565 WriteLog(
"memType->GetDesc error, hr = " + std::to_string(hr));
571 GetTypes(memType, memTypeDesc.Members, child);
578 return dim == D3D_SRV_DIMENSION_TEXTURE1DARRAY
579 || dim == D3D_SRV_DIMENSION_TEXTURE2DARRAY
580 || dim == D3D_SRV_DIMENSION_TEXTURE2DMSARRAY
581 || dim == D3D_SRV_DIMENSION_TEXTURECUBE
582 || dim == D3D_SRV_DIMENSION_TEXTURECUBEARRAY;
587 std::ofstream logfile(
"hlslshaderfactory.log");
590 logfile << message.c_str() << std::endl;
static bool IsTextureArray(D3D_SRV_DIMENSION dim)
void SetDescription(D3D_SHADER_TYPE_DESC const &desc)
unsigned int numConstantBuffers
unsigned int numOutputParameters
unsigned int numBoundResources
D3D_SHADER_VERSION_TYPE shaderType
void SetTarget(std::string const &target)
void InsertOutput(HLSLParameter const ¶meter)
static bool ReflectShader(std::string const &name, std::string const &entry, std::string const &target, ID3DBlob *compiledCode, HLSLShader &shader)
#define D3D_SHADER_TYPE_DESC
static bool GetInputs(ID3DShaderReflection *reflector, HLSLShader &shader)
std::vector< std::pair< std::string, std::string > > const & Get() const
static HLSLShader CreateFromBytecode(std::string const &name, std::string const &entry, std::string const &target, size_t numBytes, unsigned char const *bytecode)
void SetEntry(std::string const &entry)
#define D3D_SIGNATURE_PARAMETER_DESC
#define ID3DShaderReflectionType
HLSLShaderType & GetChild(unsigned int i)
GLuint const GLchar * name
#define D3D_SHADER_VARIABLE_DESC
void SetDescription(D3D_SHADER_DESC const &desc)
static bool GetVariables(ID3DShaderReflectionConstantBuffer *cbuffer, unsigned int numVariables, std::vector< HLSLBaseBuffer::Member > &members)
#define D3D_SHADER_BUFFER_DESC
void SetCompiledCode(size_t numBytes, void const *buffer)
void InsertInput(HLSLParameter const ¶meter)
static HLSLShader CreateFromFile(std::string const &filename, std::string const &entry, std::string const &target, ProgramDefines const &defines, unsigned int compileFlags)
GLsizei GLsizei GLchar * source
static bool GetOutputs(ID3DShaderReflection *reflector, HLSLShader &shader)
GLsizei const GLchar *const * string
void SetNumThreads(unsigned int numXThreads, unsigned int numYThreads, unsigned int numZThreads)
#define D3D_SHADER_INPUT_BIND_DESC
#define IID_ID3DShaderReflection
void SetName(std::string const &name)
static ID3DBlob * CompileShader(std::string const &name, std::string const &source, std::string const &entry, std::string const &target, unsigned int compileFlags, ProgramDefines const &defines)
GLuint GLsizei const GLchar * message
Description const & GetDescription() const
void SetDescription(D3D_SHADER_VARIABLE_DESC const &desc)
std::string const & GetName() const
#define ID3DShaderReflectionConstantBuffer
GLuint GLsizei GLsizei * length
#define ID3DShaderReflectionVariable
static HLSLShader CompileAndReflect(std::string const &name, std::string const &source, std::string const &entry, std::string const &target, ProgramDefines const &defines, unsigned int compileFlags)
unsigned int numInputParameters
GLenum GLenum GLenum input
void Memcpy(void *target, void const *source, size_t count)
static bool GetCBuffers(ID3DShaderReflection *reflector, HLSLShader &shader)
static bool GetTypes(ID3DShaderReflectionType *rtype, unsigned int numMembers, HLSLShaderType &stype)
void Insert(HLSLConstantBuffer const &cbuffer)
typedef UINT(WINAPI *PFNWGLGETGPUIDSAMDPROC)(UINT maxCount
static bool GetBoundResources(ID3DShaderReflection *reflector, HLSLShader &shader)
static HLSLShader CreateFromString(std::string const &name, std::string const &source, std::string const &entry, std::string const &target, ProgramDefines const &defines, unsigned int compileFlags)
static void WriteLog(std::string const &message)
void SetName(std::string const &name)
#define ID3DShaderReflection
GLint GLint GLsizei GLint GLenum GLenum type
static bool GetDescription(ID3DShaderReflection *reflector, HLSLShader &shader)