2014-12-03 23:16:20 -05:00
|
|
|
/*
|
2015-01-01 18:04:46 -05:00
|
|
|
* Copyright 2011-2015 Branimir Karadzic. All rights reserved.
|
2014-12-03 23:16:20 -05:00
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shaderc.h"
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
#if SHADERC_CONFIG_HLSL
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-05-26 15:08:12 -04:00
|
|
|
#define INITGUID
|
2014-12-03 23:16:20 -05:00
|
|
|
#include <d3dcompiler.h>
|
2015-03-31 18:09:43 -04:00
|
|
|
#include <d3d11shader.h>
|
|
|
|
|
|
|
|
#ifndef D3D_SVF_USED
|
|
|
|
# define D3D_SVF_USED 2
|
|
|
|
#endif // D3D_SVF_USED
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
struct CTHeader
|
|
|
|
{
|
|
|
|
uint32_t Size;
|
|
|
|
uint32_t Creator;
|
|
|
|
uint32_t Version;
|
|
|
|
uint32_t Constants;
|
|
|
|
uint32_t ConstantInfo;
|
|
|
|
uint32_t Flags;
|
|
|
|
uint32_t Target;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CTInfo
|
|
|
|
{
|
|
|
|
uint32_t Name;
|
|
|
|
uint16_t RegisterSet;
|
|
|
|
uint16_t RegisterIndex;
|
|
|
|
uint16_t RegisterCount;
|
|
|
|
uint16_t Reserved;
|
|
|
|
uint32_t TypeInfo;
|
|
|
|
uint32_t DefaultValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CTType
|
|
|
|
{
|
|
|
|
uint16_t Class;
|
|
|
|
uint16_t Type;
|
|
|
|
uint16_t Rows;
|
|
|
|
uint16_t Columns;
|
|
|
|
uint16_t Elements;
|
|
|
|
uint16_t StructMembers;
|
|
|
|
uint32_t StructMemberInfo;
|
|
|
|
};
|
|
|
|
|
2014-12-03 23:16:20 -05:00
|
|
|
struct RemapInputSemantic
|
|
|
|
{
|
|
|
|
bgfx::Attrib::Enum m_attr;
|
|
|
|
const char* m_name;
|
|
|
|
uint8_t m_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const RemapInputSemantic s_remapInputSemantic[bgfx::Attrib::Count+1] =
|
|
|
|
{
|
|
|
|
{ bgfx::Attrib::Position, "POSITION", 0 },
|
|
|
|
{ bgfx::Attrib::Normal, "NORMAL", 0 },
|
|
|
|
{ bgfx::Attrib::Tangent, "TANGENT", 0 },
|
|
|
|
{ bgfx::Attrib::Bitangent, "BITANGENT", 0 },
|
|
|
|
{ bgfx::Attrib::Color0, "COLOR", 0 },
|
|
|
|
{ bgfx::Attrib::Color1, "COLOR", 1 },
|
|
|
|
{ bgfx::Attrib::Indices, "BLENDINDICES", 0 },
|
|
|
|
{ bgfx::Attrib::Weight, "BLENDWEIGHT", 0 },
|
|
|
|
{ bgfx::Attrib::TexCoord0, "TEXCOORD", 0 },
|
|
|
|
{ bgfx::Attrib::TexCoord1, "TEXCOORD", 1 },
|
|
|
|
{ bgfx::Attrib::TexCoord2, "TEXCOORD", 2 },
|
|
|
|
{ bgfx::Attrib::TexCoord3, "TEXCOORD", 3 },
|
|
|
|
{ bgfx::Attrib::TexCoord4, "TEXCOORD", 4 },
|
|
|
|
{ bgfx::Attrib::TexCoord5, "TEXCOORD", 5 },
|
|
|
|
{ bgfx::Attrib::TexCoord6, "TEXCOORD", 6 },
|
|
|
|
{ bgfx::Attrib::TexCoord7, "TEXCOORD", 7 },
|
|
|
|
{ bgfx::Attrib::Count, "", 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
const RemapInputSemantic& findInputSemantic(const char* _name, uint8_t _index)
|
|
|
|
{
|
|
|
|
for (uint32_t ii = 0; ii < bgfx::Attrib::Count; ++ii)
|
|
|
|
{
|
|
|
|
const RemapInputSemantic& ris = s_remapInputSemantic[ii];
|
|
|
|
if (0 == strcmp(ris.m_name, _name)
|
2014-12-05 22:17:59 -05:00
|
|
|
&& ris.m_index == _index)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
return ris;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s_remapInputSemantic[bgfx::Attrib::Count];
|
|
|
|
}
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
struct UniformRemap
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
UniformType::Enum id;
|
|
|
|
D3D_SHADER_VARIABLE_CLASS paramClass;
|
|
|
|
D3D_SHADER_VARIABLE_TYPE paramType;
|
|
|
|
uint8_t columns;
|
|
|
|
uint8_t rows;
|
|
|
|
};
|
|
|
|
|
2015-04-13 01:26:18 -04:00
|
|
|
static const UniformRemap s_uniformRemap[7] =
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
2015-05-28 18:27:00 -04:00
|
|
|
{ UniformType::Int1, D3D_SVC_SCALAR, D3D_SVT_INT, 0, 0 },
|
|
|
|
{ UniformType::Vec4, D3D_SVC_VECTOR, D3D_SVT_FLOAT, 0, 0 },
|
|
|
|
{ UniformType::Mat3, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 3, 3 },
|
|
|
|
{ UniformType::Mat4, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 4, 4 },
|
2014-12-03 23:16:20 -05:00
|
|
|
};
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
UniformType::Enum findUniformType(const D3D11_SHADER_TYPE_DESC& constDesc)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
2015-04-13 01:26:18 -04:00
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(s_uniformRemap); ++ii)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
2015-04-13 01:26:18 -04:00
|
|
|
const UniformRemap& remap = s_uniformRemap[ii];
|
2014-12-03 23:16:20 -05:00
|
|
|
|
|
|
|
if (remap.paramClass == constDesc.Class
|
2014-12-05 22:17:59 -05:00
|
|
|
&& remap.paramType == constDesc.Type)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
if (D3D_SVC_MATRIX_COLUMNS != constDesc.Class)
|
|
|
|
{
|
|
|
|
return remap.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remap.columns == constDesc.Columns
|
2014-12-05 22:17:59 -05:00
|
|
|
&& remap.rows == constDesc.Rows)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
return remap.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UniformType::Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t s_optimizationLevelDx11[4] =
|
|
|
|
{
|
|
|
|
D3DCOMPILE_OPTIMIZATION_LEVEL0,
|
|
|
|
D3DCOMPILE_OPTIMIZATION_LEVEL1,
|
|
|
|
D3DCOMPILE_OPTIMIZATION_LEVEL2,
|
|
|
|
D3DCOMPILE_OPTIMIZATION_LEVEL3,
|
|
|
|
};
|
|
|
|
|
2015-04-11 15:40:31 -04:00
|
|
|
typedef std::vector<std::string> UniformNameList;
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
bool getReflectionDataDx9(ID3DBlob* _code, UniformArray& _uniforms)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
2015-04-10 21:00:46 -04:00
|
|
|
// see reference for magic values: https://msdn.microsoft.com/en-us/library/ff552891(VS.85).aspx
|
|
|
|
const uint32_t D3DSIO_COMMENT = 0x0000FFFE;
|
|
|
|
const uint32_t D3DSIO_END = 0x0000FFFF;
|
|
|
|
const uint32_t D3DSI_OPCODE_MASK = 0x0000FFFF;
|
|
|
|
const uint32_t D3DSI_COMMENTSIZE_MASK = 0x7FFF0000;
|
|
|
|
const uint32_t CTAB_CONSTANT = MAKEFOURCC('C','T','A','B');
|
|
|
|
|
|
|
|
// parse the shader blob for the constant table
|
|
|
|
const size_t codeSize = _code->GetBufferSize();
|
|
|
|
const uint32_t* ptr = (const uint32_t*)_code->GetBufferPointer();
|
2015-04-11 00:24:25 -04:00
|
|
|
const uint32_t* end = (const uint32_t*)( (const uint8_t*)ptr + codeSize);
|
2015-04-10 21:00:46 -04:00
|
|
|
const CTHeader* header = NULL;
|
|
|
|
|
|
|
|
ptr++; // first byte is shader type / version; skip it since we already know
|
|
|
|
|
|
|
|
while (ptr < end && *ptr != D3DSIO_END)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
2015-04-10 21:00:46 -04:00
|
|
|
uint32_t cur = *ptr++;
|
2015-04-11 00:24:25 -04:00
|
|
|
if ( (cur & D3DSI_OPCODE_MASK) != D3DSIO_COMMENT)
|
|
|
|
{
|
2015-04-10 21:00:46 -04:00
|
|
|
continue;
|
2015-04-11 00:24:25 -04:00
|
|
|
}
|
2015-04-10 21:00:46 -04:00
|
|
|
|
|
|
|
// try to find CTAB comment block
|
|
|
|
uint32_t commentSize = (cur & D3DSI_COMMENTSIZE_MASK) >> 16;
|
|
|
|
uint32_t fourcc = *ptr;
|
|
|
|
if (fourcc == CTAB_CONSTANT)
|
|
|
|
{
|
|
|
|
// found the constant table data
|
|
|
|
header = (const CTHeader*)(ptr + 1);
|
|
|
|
uint32_t tableSize = (commentSize - 1) * 4;
|
2015-04-11 00:24:25 -04:00
|
|
|
if (tableSize < sizeof(CTHeader) || header->Size != sizeof(CTHeader) )
|
2015-04-10 21:00:46 -04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Invalid constant table data\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
// this is a different kind of comment section, so skip over it
|
|
|
|
ptr += commentSize - 1;
|
2014-12-03 23:16:20 -05:00
|
|
|
}
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
if (!header)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
2015-04-10 21:00:46 -04:00
|
|
|
fprintf(stderr, "Error: Could not find constant table data\n");
|
|
|
|
return false;
|
2014-12-03 23:16:20 -05:00
|
|
|
}
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
const uint8_t* headerBytePtr = (const uint8_t*)header;
|
|
|
|
const char* creator = (const char*)(headerBytePtr + header->Creator);
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
BX_TRACE("Creator: %s 0x%08x", creator, header->Version);
|
|
|
|
BX_TRACE("Num constants: %d", header->Constants);
|
|
|
|
BX_TRACE("# cl ty RxC S By Name");
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
const CTInfo* ctInfoArray = (const CTInfo*)(headerBytePtr + header->ConstantInfo);
|
2015-04-11 00:24:25 -04:00
|
|
|
for (uint32_t ii = 0; ii < header->Constants; ++ii)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
2015-04-10 21:00:46 -04:00
|
|
|
const CTInfo& ctInfo = ctInfoArray[ii];
|
|
|
|
const CTType& ctType = *(const CTType*)(headerBytePtr + ctInfo.TypeInfo);
|
|
|
|
const char* name = (const char*)(headerBytePtr + ctInfo.Name);
|
|
|
|
|
|
|
|
BX_TRACE("%3d %2d %2d [%dx%d] %d %s[%d] c%d (%d)"
|
|
|
|
, ii
|
|
|
|
, ctType.Class
|
|
|
|
, ctType.Type
|
|
|
|
, ctType.Rows
|
|
|
|
, ctType.Columns
|
|
|
|
, ctType.StructMembers
|
|
|
|
, name
|
|
|
|
, ctType.Elements
|
|
|
|
, ctInfo.RegisterIndex
|
|
|
|
, ctInfo.RegisterCount
|
|
|
|
);
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
D3D11_SHADER_TYPE_DESC desc;
|
|
|
|
desc.Class = (D3D_SHADER_VARIABLE_CLASS)ctType.Class;
|
|
|
|
desc.Type = (D3D_SHADER_VARIABLE_TYPE)ctType.Type;
|
|
|
|
desc.Rows = ctType.Rows;
|
|
|
|
desc.Columns = ctType.Columns;
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
UniformType::Enum type = findUniformType(desc);
|
|
|
|
if (UniformType::Count != type)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
2015-04-10 21:00:46 -04:00
|
|
|
Uniform un;
|
|
|
|
un.name = '$' == name[0] ? name + 1 : name;
|
|
|
|
un.type = type;
|
2015-04-10 22:01:42 -04:00
|
|
|
un.num = (uint8_t)ctType.Elements;
|
2015-04-10 21:00:46 -04:00
|
|
|
un.regIndex = ctInfo.RegisterIndex;
|
|
|
|
un.regCount = ctInfo.RegisterCount;
|
|
|
|
_uniforms.push_back(un);
|
2014-12-03 23:16:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
return true;
|
|
|
|
}
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-11 15:40:31 -04:00
|
|
|
bool getReflectionDataDx11(ID3DBlob* _code, bool _vshader, UniformArray& _uniforms, uint8_t& _numAttrs, uint16_t* _attrs, uint16_t& _size, UniformNameList& unusedUniforms)
|
2015-04-10 21:00:46 -04:00
|
|
|
{
|
2014-12-03 23:16:20 -05:00
|
|
|
ID3D11ShaderReflection* reflect = NULL;
|
2015-04-10 21:00:46 -04:00
|
|
|
HRESULT hr = D3DReflect(_code->GetBufferPointer()
|
|
|
|
, _code->GetBufferSize()
|
2015-04-05 14:24:59 -04:00
|
|
|
, IID_ID3D11ShaderReflection
|
2014-12-03 23:16:20 -05:00
|
|
|
, (void**)&reflect
|
|
|
|
);
|
2015-04-11 00:24:25 -04:00
|
|
|
if (FAILED(hr) )
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: 0x%08x\n", (uint32_t)hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D11_SHADER_DESC desc;
|
|
|
|
hr = reflect->GetDesc(&desc);
|
2015-04-11 00:24:25 -04:00
|
|
|
if (FAILED(hr) )
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x\n", (uint32_t)hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
|
|
|
|
BX_TRACE("Num constant buffers: %d", desc.ConstantBuffers);
|
|
|
|
|
|
|
|
BX_TRACE("Input:");
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
if (_vshader) // Only care about input semantic on vertex shaders
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
for (uint32_t ii = 0; ii < desc.InputParameters; ++ii)
|
|
|
|
{
|
|
|
|
D3D11_SIGNATURE_PARAMETER_DESC spd;
|
|
|
|
reflect->GetInputParameterDesc(ii, &spd);
|
|
|
|
BX_TRACE("\t%2d: %s%d, vt %d, ct %d, mask %x, reg %d"
|
|
|
|
, ii
|
|
|
|
, spd.SemanticName
|
|
|
|
, spd.SemanticIndex
|
|
|
|
, spd.SystemValueType
|
|
|
|
, spd.ComponentType
|
|
|
|
, spd.Mask
|
|
|
|
, spd.Register
|
|
|
|
);
|
|
|
|
|
|
|
|
const RemapInputSemantic& ris = findInputSemantic(spd.SemanticName, spd.SemanticIndex);
|
|
|
|
if (ris.m_attr != bgfx::Attrib::Count)
|
|
|
|
{
|
2015-04-10 21:00:46 -04:00
|
|
|
_attrs[_numAttrs] = bgfx::attribToId(ris.m_attr);
|
|
|
|
++_numAttrs;
|
2014-12-03 23:16:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BX_TRACE("Output:");
|
|
|
|
for (uint32_t ii = 0; ii < desc.OutputParameters; ++ii)
|
|
|
|
{
|
|
|
|
D3D11_SIGNATURE_PARAMETER_DESC spd;
|
|
|
|
reflect->GetOutputParameterDesc(ii, &spd);
|
|
|
|
BX_TRACE("\t%2d: %s%d, %d, %d", ii, spd.SemanticName, spd.SemanticIndex, spd.SystemValueType, spd.ComponentType);
|
|
|
|
}
|
|
|
|
|
2015-04-13 01:26:18 -04:00
|
|
|
for (uint32_t ii = 0, num = bx::uint32_min(1, desc.ConstantBuffers); ii < num; ++ii)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
ID3D11ShaderReflectionConstantBuffer* cbuffer = reflect->GetConstantBufferByIndex(ii);
|
|
|
|
D3D11_SHADER_BUFFER_DESC bufferDesc;
|
|
|
|
hr = cbuffer->GetDesc(&bufferDesc);
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
_size = (uint16_t)bufferDesc.Size;
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-11 00:24:25 -04:00
|
|
|
if (SUCCEEDED(hr) )
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
BX_TRACE("%s, %d, vars %d, size %d"
|
|
|
|
, bufferDesc.Name
|
|
|
|
, bufferDesc.Type
|
|
|
|
, bufferDesc.Variables
|
|
|
|
, bufferDesc.Size
|
|
|
|
);
|
|
|
|
|
|
|
|
for (uint32_t jj = 0; jj < bufferDesc.Variables; ++jj)
|
|
|
|
{
|
|
|
|
ID3D11ShaderReflectionVariable* var = cbuffer->GetVariableByIndex(jj);
|
|
|
|
ID3D11ShaderReflectionType* type = var->GetType();
|
|
|
|
D3D11_SHADER_VARIABLE_DESC varDesc;
|
|
|
|
hr = var->GetDesc(&varDesc);
|
2015-04-11 00:24:25 -04:00
|
|
|
if (SUCCEEDED(hr) )
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
D3D11_SHADER_TYPE_DESC constDesc;
|
|
|
|
hr = type->GetDesc(&constDesc);
|
2015-04-11 00:24:25 -04:00
|
|
|
if (SUCCEEDED(hr) )
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
2015-04-10 21:00:46 -04:00
|
|
|
UniformType::Enum uniformType = findUniformType(constDesc);
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-03 02:31:28 -04:00
|
|
|
if (UniformType::Count != uniformType
|
2015-04-11 00:24:25 -04:00
|
|
|
&& 0 != (varDesc.uFlags & D3D_SVF_USED) )
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
Uniform un;
|
|
|
|
un.name = varDesc.Name;
|
2015-04-03 02:31:28 -04:00
|
|
|
un.type = uniformType;
|
2014-12-03 23:16:20 -05:00
|
|
|
un.num = constDesc.Elements;
|
|
|
|
un.regIndex = varDesc.StartOffset;
|
2015-04-10 21:00:46 -04:00
|
|
|
un.regCount = BX_ALIGN_16(varDesc.Size) / 16;
|
|
|
|
_uniforms.push_back(un);
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-13 15:54:45 -04:00
|
|
|
BX_TRACE("\t%s, %d, size %d, flags 0x%08x, %d (used)"
|
2014-12-03 23:16:20 -05:00
|
|
|
, varDesc.Name
|
|
|
|
, varDesc.StartOffset
|
|
|
|
, varDesc.Size
|
|
|
|
, varDesc.uFlags
|
2015-04-03 02:31:28 -04:00
|
|
|
, uniformType
|
2014-12-03 23:16:20 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-13 15:54:45 -04:00
|
|
|
if (0 == (varDesc.uFlags & D3D_SVF_USED) )
|
|
|
|
{
|
2015-04-11 15:40:31 -04:00
|
|
|
unusedUniforms.push_back(varDesc.Name);
|
2015-04-13 15:54:45 -04:00
|
|
|
}
|
2015-04-11 15:40:31 -04:00
|
|
|
|
2014-12-03 23:16:20 -05:00
|
|
|
BX_TRACE("\t%s, unknown type", varDesc.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BX_TRACE("Bound:");
|
|
|
|
for (uint32_t ii = 0; ii < desc.BoundResources; ++ii)
|
|
|
|
{
|
|
|
|
D3D11_SHADER_INPUT_BIND_DESC bindDesc;
|
|
|
|
|
|
|
|
hr = reflect->GetResourceBindingDesc(ii, &bindDesc);
|
2015-04-11 00:24:25 -04:00
|
|
|
if (SUCCEEDED(hr) )
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
// if (bindDesc.Type == D3D_SIT_SAMPLER)
|
|
|
|
{
|
|
|
|
BX_TRACE("\t%s, %d, %d, %d"
|
|
|
|
, bindDesc.Name
|
|
|
|
, bindDesc.Type
|
|
|
|
, bindDesc.BindPoint
|
|
|
|
, bindDesc.BindCount
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
if (NULL != reflect)
|
|
|
|
{
|
|
|
|
reflect->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-11 23:52:05 -04:00
|
|
|
bool compileHLSLShader(bx::CommandLine& _cmdLine, uint32_t _d3d, const std::string& _code, bx::WriterI* _writer, bool _firstPass)
|
2015-04-10 21:00:46 -04:00
|
|
|
{
|
|
|
|
BX_TRACE("DX11");
|
|
|
|
|
|
|
|
const char* profile = _cmdLine.findOption('p', "profile");
|
|
|
|
if (NULL == profile)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Shader profile must be specified.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool debug = _cmdLine.hasArg('\0', "debug");
|
|
|
|
|
|
|
|
uint32_t flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
|
|
|
|
flags |= debug ? D3DCOMPILE_DEBUG : 0;
|
|
|
|
flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DCOMPILE_AVOID_FLOW_CONTROL : 0;
|
|
|
|
flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DCOMPILE_NO_PRESHADER : 0;
|
|
|
|
flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DCOMPILE_PARTIAL_PRECISION : 0;
|
|
|
|
flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DCOMPILE_PREFER_FLOW_CONTROL : 0;
|
|
|
|
flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY : 0;
|
|
|
|
|
|
|
|
bool werror = _cmdLine.hasArg('\0', "Werror");
|
|
|
|
|
|
|
|
if (werror)
|
|
|
|
{
|
|
|
|
flags |= D3DCOMPILE_WARNINGS_ARE_ERRORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t optimization = 3;
|
|
|
|
if (_cmdLine.hasArg(optimization, 'O') )
|
|
|
|
{
|
|
|
|
optimization = bx::uint32_min(optimization, BX_COUNTOF(s_optimizationLevelDx11)-1);
|
|
|
|
flags |= s_optimizationLevelDx11[optimization];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
BX_TRACE("Profile: %s", profile);
|
|
|
|
BX_TRACE("Flags: 0x%08x", flags);
|
|
|
|
|
|
|
|
ID3DBlob* code;
|
|
|
|
ID3DBlob* errorMsg;
|
|
|
|
|
|
|
|
// Output preprocessed shader so that HLSL can be debugged via GPA
|
|
|
|
// or PIX. Compiling through memory won't embed preprocessed shader
|
|
|
|
// file path.
|
|
|
|
std::string hlslfp;
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
hlslfp = _cmdLine.findOption('o');
|
|
|
|
hlslfp += ".hlsl";
|
|
|
|
writeFile(hlslfp.c_str(), _code.c_str(), (int32_t)_code.size() );
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT hr = D3DCompile(_code.c_str()
|
|
|
|
, _code.size()
|
|
|
|
, hlslfp.c_str()
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, "main"
|
|
|
|
, profile
|
|
|
|
, flags
|
|
|
|
, 0
|
|
|
|
, &code
|
|
|
|
, &errorMsg
|
|
|
|
);
|
|
|
|
if (FAILED(hr)
|
|
|
|
|| (werror && NULL != errorMsg) )
|
|
|
|
{
|
|
|
|
const char* log = (char*)errorMsg->GetBufferPointer();
|
|
|
|
|
|
|
|
int32_t line = 0;
|
|
|
|
int32_t column = 0;
|
|
|
|
int32_t start = 0;
|
|
|
|
int32_t end = INT32_MAX;
|
|
|
|
|
|
|
|
if (2 == sscanf(log, "(%u,%u):", &line, &column)
|
|
|
|
&& 0 != line)
|
|
|
|
{
|
|
|
|
start = bx::uint32_imax(1, line-10);
|
|
|
|
end = start + 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
printCode(_code.c_str(), line, start, end);
|
|
|
|
fprintf(stderr, "Error: 0x%08x %s\n", (uint32_t)hr, log);
|
|
|
|
errorMsg->Release();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniformArray uniforms;
|
|
|
|
uint8_t numAttrs = 0;
|
|
|
|
uint16_t attrs[bgfx::Attrib::Count];
|
|
|
|
uint16_t size = 0;
|
|
|
|
|
|
|
|
if (_d3d == 9)
|
|
|
|
{
|
2015-04-11 00:24:25 -04:00
|
|
|
if (!getReflectionDataDx9(code, uniforms) )
|
2015-04-11 23:52:05 -04:00
|
|
|
{
|
2015-04-10 21:00:46 -04:00
|
|
|
return false;
|
2015-04-11 23:52:05 -04:00
|
|
|
}
|
2015-04-10 21:00:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-11 15:40:31 -04:00
|
|
|
UniformNameList unusedUniforms;
|
|
|
|
if (!getReflectionDataDx11(code, profile[0] == 'v', uniforms, numAttrs, attrs, size, unusedUniforms) )
|
2015-04-11 23:52:05 -04:00
|
|
|
{
|
2015-04-10 21:00:46 -04:00
|
|
|
return false;
|
2015-04-11 23:52:05 -04:00
|
|
|
}
|
2015-04-11 15:40:31 -04:00
|
|
|
|
2015-04-11 23:52:05 -04:00
|
|
|
if (_firstPass
|
|
|
|
&& unusedUniforms.size() > 0)
|
2015-04-11 15:40:31 -04:00
|
|
|
{
|
2015-04-11 16:11:16 -04:00
|
|
|
const size_t strLength = strlen("uniform");
|
|
|
|
|
2015-04-11 15:40:31 -04:00
|
|
|
// first time through, we just find unused uniforms and get rid of them
|
2015-04-13 01:26:18 -04:00
|
|
|
std::string output;
|
|
|
|
LineReader reader(_code.c_str() );
|
2015-04-11 15:40:31 -04:00
|
|
|
while (!reader.isEof() )
|
|
|
|
{
|
|
|
|
std::string line = reader.getLine();
|
2015-04-22 22:43:19 -04:00
|
|
|
for (UniformNameList::iterator it = unusedUniforms.begin(), itEnd = unusedUniforms.end(); it != itEnd; ++it)
|
2015-04-11 15:40:31 -04:00
|
|
|
{
|
|
|
|
size_t index = line.find("uniform ");
|
|
|
|
if (index == std::string::npos)
|
2015-04-13 15:54:45 -04:00
|
|
|
{
|
2015-04-11 15:40:31 -04:00
|
|
|
continue;
|
2015-04-13 15:54:45 -04:00
|
|
|
}
|
2015-04-11 15:40:31 -04:00
|
|
|
|
|
|
|
// matching lines like: uniform u_name;
|
|
|
|
// we want to replace "uniform" with "static" so that it's no longer
|
2015-04-13 17:45:14 -04:00
|
|
|
// included in the uniform blob that the application must upload
|
2015-04-11 15:40:31 -04:00
|
|
|
// we can't just remove them, because unused functions might still reference
|
|
|
|
// them and cause a compile error when they're gone
|
2015-04-13 15:54:45 -04:00
|
|
|
if (!!bx::findIdentifierMatch(line.c_str(), it->c_str() ) )
|
2015-04-11 15:40:31 -04:00
|
|
|
{
|
2015-04-11 16:11:16 -04:00
|
|
|
line = line.replace(index, strLength, "static");
|
2015-04-11 15:40:31 -04:00
|
|
|
unusedUniforms.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 01:26:18 -04:00
|
|
|
output += line;
|
2015-04-11 15:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// recompile with the unused uniforms converted to statics
|
2015-04-13 01:26:18 -04:00
|
|
|
return compileHLSLShader(_cmdLine, _d3d, output.c_str(), _writer, false);
|
2015-04-11 15:40:31 -04:00
|
|
|
}
|
2015-04-10 21:00:46 -04:00
|
|
|
}
|
|
|
|
|
2014-12-03 23:16:20 -05:00
|
|
|
uint16_t count = (uint16_t)uniforms.size();
|
|
|
|
bx::write(_writer, count);
|
|
|
|
|
|
|
|
uint32_t fragmentBit = profile[0] == 'p' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
|
|
|
|
for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
|
|
|
|
{
|
|
|
|
const Uniform& un = *it;
|
|
|
|
uint8_t nameSize = (uint8_t)un.name.size();
|
|
|
|
bx::write(_writer, nameSize);
|
|
|
|
bx::write(_writer, un.name.c_str(), nameSize);
|
|
|
|
uint8_t type = un.type|fragmentBit;
|
|
|
|
bx::write(_writer, type);
|
|
|
|
bx::write(_writer, un.num);
|
|
|
|
bx::write(_writer, un.regIndex);
|
|
|
|
bx::write(_writer, un.regCount);
|
|
|
|
|
|
|
|
BX_TRACE("%s, %s, %d, %d, %d"
|
|
|
|
, un.name.c_str()
|
|
|
|
, getUniformTypeName(un.type)
|
|
|
|
, un.num
|
|
|
|
, un.regIndex
|
|
|
|
, un.regCount
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-12-05 23:28:15 -05:00
|
|
|
{
|
|
|
|
ID3DBlob* stripped;
|
|
|
|
hr = D3DStripShader(code->GetBufferPointer()
|
|
|
|
, code->GetBufferSize()
|
2015-03-31 18:09:43 -04:00
|
|
|
, D3DCOMPILER_STRIP_REFLECTION_DATA
|
2014-12-05 23:28:15 -05:00
|
|
|
| D3DCOMPILER_STRIP_TEST_BLOBS
|
|
|
|
, &stripped
|
|
|
|
);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr) )
|
|
|
|
{
|
|
|
|
code->Release();
|
|
|
|
code = stripped;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 23:16:20 -05:00
|
|
|
uint16_t shaderSize = (uint16_t)code->GetBufferSize();
|
|
|
|
bx::write(_writer, shaderSize);
|
|
|
|
bx::write(_writer, code->GetBufferPointer(), shaderSize);
|
|
|
|
uint8_t nul = 0;
|
|
|
|
bx::write(_writer, nul);
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
if (_d3d > 9)
|
|
|
|
{
|
|
|
|
bx::write(_writer, numAttrs);
|
2015-04-11 00:24:25 -04:00
|
|
|
bx::write(_writer, attrs, numAttrs*sizeof(uint16_t) );
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
bx::write(_writer, size);
|
|
|
|
}
|
2014-12-03 23:16:20 -05:00
|
|
|
|
2015-04-11 00:24:25 -04:00
|
|
|
if (_cmdLine.hasArg('\0', "disasm") )
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
|
|
|
ID3DBlob* disasm;
|
|
|
|
D3DDisassemble(code->GetBufferPointer()
|
|
|
|
, code->GetBufferSize()
|
|
|
|
, 0
|
|
|
|
, NULL
|
|
|
|
, &disasm
|
|
|
|
);
|
|
|
|
|
|
|
|
if (NULL != disasm)
|
|
|
|
{
|
|
|
|
std::string disasmfp = _cmdLine.findOption('o');
|
|
|
|
disasmfp += ".disasm";
|
|
|
|
|
|
|
|
writeFile(disasmfp.c_str(), disasm->GetBufferPointer(), (uint32_t)disasm->GetBufferSize() );
|
|
|
|
disasm->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != errorMsg)
|
|
|
|
{
|
|
|
|
errorMsg->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
code->Release();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2015-04-11 23:52:05 -04:00
|
|
|
bool compileHLSLShader(bx::CommandLine& _cmdLine, uint32_t _d3d, const std::string& _code, bx::WriterI* _writer, bool _firstPass)
|
2014-12-03 23:16:20 -05:00
|
|
|
{
|
2015-04-11 23:52:05 -04:00
|
|
|
BX_UNUSED(_cmdLine, _d3d, _code, _writer, _firstPass);
|
2014-12-03 23:16:20 -05:00
|
|
|
fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-10 21:00:46 -04:00
|
|
|
#endif // SHADERC_CONFIG_HLSL
|