Пример #1
0
	HRESULT ShaderBase::processLoadedShaderBlob( ID3DBlob *shaderSource )
	{
		HRESULT res = build( shaderSource );
		if ( FAILED(res) )
		{
			std::cout << "Error (ShaderBase::processLoadedShaderBlob): Error creating shader" << std::endl;
			return res;
		}


		// setup buffers using the shader reflection
		ID3D11ShaderReflection *reflector;
		res = D3DReflect( shaderSource->GetBufferPointer(), shaderSource->GetBufferSize(),
							IID_ID3D11ShaderReflection, (void**)&reflector );
		if ( FAILED( res ) )
		{
			std::cout << "Error (ShaderBase::processLoadedShaderBlob): Error creating shader reflection" << std::endl;
			return res;
		}


		D3D11_SHADER_DESC shaderDesc;
		ZeroMemory( &shaderDesc, sizeof( D3D11_SHADER_DESC ) );
		res = reflector->GetDesc( &shaderDesc );
		if ( FAILED( res ) )
		{
			std::cout << "Error (ShaderBase::processLoadedShaderBlob): Error creating shader reflection description" << std::endl;
			reflector->Release();
			return res;
		}


		for ( UINT i = 0U; i < shaderDesc.ConstantBuffers; ++i )
		{
			ID3D11ShaderReflectionConstantBuffer *constBuffReflection = reflector->GetConstantBufferByIndex( i );
			D3D11_SHADER_BUFFER_DESC shaderBuffDesc;
			constBuffReflection->GetDesc( &shaderBuffDesc );

			//TODO: these can be put on the stack
			ConstantBuffer *cBuff = new ConstantBuffer( m_dxDev, i, shaderBuffDesc.Name );
			cBuff->create( shaderBuffDesc.Size );
			for ( UINT j = 0U; j < shaderBuffDesc.Variables; ++j )
			{
				ID3D11ShaderReflectionVariable *variableRefl = constBuffReflection->GetVariableByIndex( j );
				D3D11_SHADER_VARIABLE_DESC shaderVarDesc;
				variableRefl->GetDesc( &shaderVarDesc );

				cBuff->addVariableDefinition( shaderVarDesc.Name, shaderVarDesc.StartOffset, shaderVarDesc.Size );
			}

			m_constBuffers.push_back( cBuff );
			m_dxConstBuffers.push_back( cBuff->getDxBufferPtr() );
		}

		reflector->Release();

		return S_OK;
	}
Пример #2
0
    static void dx11_fill_constant_table(ShaderConstantTable& out_constants, ShaderConstantTable& out_samplers, const ShaderCode& bytecode)
    {
        out_constants.clear();
        out_samplers.clear();

        ID3D11ShaderReflection* refl = NULL; 
        D3DReflect( bytecode.data(), bytecode.size(), IID_ID3D11ShaderReflection, (void**)&refl);

        if( refl )
        {
            HRESULT hr = S_OK;
            D3D11_SHADER_DESC refl_desc;
            hr = refl->GetDesc(&refl_desc);

            for( uint32 i=0; i<refl_desc.ConstantBuffers; ++i )
            {
                ID3D11ShaderReflectionConstantBuffer* cb = refl->GetConstantBufferByIndex(i);

                D3D11_SHADER_BUFFER_DESC sb_desc;
                cb->GetDesc(&sb_desc);

                for( uint32 j=0; j<sb_desc.Variables; ++j )
                {
                    ID3D11ShaderReflectionVariable* var = cb->GetVariableByIndex(j);
                    D3D11_SHADER_VARIABLE_DESC var_desc;
                    var->GetDesc(&var_desc);

                    ShaderConstantDescr scd;
                    scd.name = var_desc.Name;
                    scd.register_index = var_desc.StartOffset/16;
                    scd.register_count = var_desc.Size/16;

                    out_constants.push_back(scd);
                }
            }

            for( uint32 i=0; i<refl_desc.BoundResources; ++i )
            {
                D3D11_SHADER_INPUT_BIND_DESC desc;
                refl->GetResourceBindingDesc(i, &desc);
                if( desc.Type == D3D10_SIT_SAMPLER )
                {
                    ShaderConstantDescr scd;
                    scd.name = desc.Name;
                    scd.register_index = desc.BindPoint;
                    scd.register_count = desc.BindCount;
                }
            }

            refl->Release();
        }
    }
void D3D11RendererVariableManager::loadConstantVariables(const void* pResource,
                                                         ID3DBlob* pShader,
                                                         ShaderTypeKey typeKey,
                                                         ID3D11ShaderReflection* pReflection,
                                                         MaterialVariables* pVariables,
                                                         PxU32* pVariableBufferSize)
{
	D3D11_SHADER_DESC desc;
	pReflection->GetDesc(&desc);

	for (PxU32 i = 0; i < desc.ConstantBuffers; ++i)
	{
		D3D11_BUFFER_DESC cbDesc;
		cbDesc.CPUAccessFlags = (mBindMode == BIND_MAP) ? D3D11_CPU_ACCESS_WRITE : 0;
		cbDesc.Usage          = (mBindMode == BIND_MAP) ? D3D11_USAGE_DYNAMIC    : D3D11_USAGE_DEFAULT;
		cbDesc.BindFlags      = D3D11_BIND_CONSTANT_BUFFER;
		cbDesc.MiscFlags      = 0;

		ID3D11ShaderReflectionConstantBuffer* pConstBuffer = pReflection->GetConstantBufferByIndex(i);

		D3D11_SHADER_BUFFER_DESC sbDesc;
		pConstBuffer->GetDesc(&sbDesc);
		cbDesc.ByteWidth = sbDesc.Size;

		D3D11ConstantBuffer* pCB = NULL;
		// Check to see if the constant buffer's name has been specified as s shared buffer
		if (mSharedBufferNames.find(sbDesc.Name) != mSharedBufferNames.end())
		{
			pCB = loadSharedBuffer(typeKey, pConstBuffer, sbDesc, cbDesc);
		}
		else if (pVariables && pVariableBufferSize)
		{
			pCB = loadBuffer(*pVariables, *pVariableBufferSize, typeKey, pConstBuffer, sbDesc, cbDesc);
		}
		else
		{
			RENDERER_ASSERT(0, "Only materials support non-shared variables.");
		}

		RENDERER_ASSERT(pCB, "Error creating shared buffer");
		if (pCB)
		{
			mResourceToBuffers[typeKey][pResource].push_back(pCB);
		}
	}
}
void ShaderBase::InitUniforms(void* s, size_t size)
{
    ID3D11ShaderReflection* ref = NULL;
    D3DReflect(s, size, IID_ID3D11ShaderReflection, (void**) &ref);
    ID3D11ShaderReflectionConstantBuffer* buf = ref->GetConstantBufferByIndex(0);
    D3D11_SHADER_BUFFER_DESC bufd;
    if (FAILED(buf->GetDesc(&bufd)))
    {
        UniformsSize = 0;
        if (UniformData)
        {
            OVR_FREE(UniformData);
            UniformData = 0;
        }
        return;
    }

    for(unsigned i = 0; i < bufd.Variables; i++)
    {
        ID3D11ShaderReflectionVariable* var = buf->GetVariableByIndex(i);
        if (var)
        {
            D3D11_SHADER_VARIABLE_DESC vd;
            if (SUCCEEDED(var->GetDesc(&vd)))
            {
                Uniform u;
                u.Name = vd.Name;
                u.Offset = vd.StartOffset;
                u.Size = vd.Size;
                UniformInfo.PushBack(u);
            }
        }
    }

    UniformsSize = bufd.Size;
    UniformData = (unsigned char*)OVR_ALLOC(bufd.Size);
}
Пример #5
0
void AGN::ShaderDX11::getConstantBufferDesc(D3D11_SHADER_BUFFER_DESC*& out_constantBufferDecs, int& out_count)
{	
	std::vector<D3D11_SHADER_BUFFER_DESC> constantBufferDescList;

	// get constant buffer info
	for (uint32_t i = 0; i < m_shaderReflectionDesc->ConstantBuffers; i++)
	{
		D3D11_SHADER_BUFFER_DESC constantBufferDesc;

		ID3D11ShaderReflectionConstantBuffer* buffer = m_shaderReflection->GetConstantBufferByIndex(i);
		
		HRESULT hr = buffer->GetDesc(&constantBufferDesc);
		
		if (constantBufferDesc.Type == D3D_CT_CBUFFER)
		{
			constantBufferDescList.push_back(constantBufferDesc);
		}
	}

	out_count = (int)constantBufferDescList.size();
	out_constantBufferDecs = new D3D11_SHADER_BUFFER_DESC[out_count];

	memcpy(out_constantBufferDecs, constantBufferDescList.data(), sizeof(D3D11_SHADER_BUFFER_DESC) * constantBufferDescList.size());
}
Пример #6
0
void Shader::ReflectConstantBufferLayouts(ID3D11ShaderReflection* sRefl, EShaderStage type)
{
	D3D11_SHADER_DESC desc;
	sRefl->GetDesc(&desc);

	unsigned bufSlot = 0;
	for (unsigned i = 0; i < desc.ConstantBuffers; ++i)
	{
		ConstantBufferLayout bufferLayout;
		bufferLayout.buffSize = 0;
		ID3D11ShaderReflectionConstantBuffer* pCBuffer = sRefl->GetConstantBufferByIndex(i);
		pCBuffer->GetDesc(&bufferLayout.desc);

		// load desc of each variable for binding on buffer later on
		for (unsigned j = 0; j < bufferLayout.desc.Variables; ++j)
		{
			// get variable and type descriptions
			ID3D11ShaderReflectionVariable* pVariable = pCBuffer->GetVariableByIndex(j);
			D3D11_SHADER_VARIABLE_DESC varDesc;
			pVariable->GetDesc(&varDesc);
			bufferLayout.variables.push_back(varDesc);

			ID3D11ShaderReflectionType* pType = pVariable->GetType();
			D3D11_SHADER_TYPE_DESC typeDesc;
			pType->GetDesc(&typeDesc);
			bufferLayout.types.push_back(typeDesc);

			// accumulate buffer size
			bufferLayout.buffSize += varDesc.Size;
		}
		bufferLayout.stage = type;
		bufferLayout.bufSlot = bufSlot;
		++bufSlot;
		m_CBLayouts.push_back(bufferLayout);
	}
}
Пример #7
0
// --------------------------------------------------------
// Loads the specified shader and builds the variable table using shader
// reflection.  This must be a separate step from the constructor since
// we can't invoke derived class overrides in the base class constructor.
//
// shaderFile - A "wide string" specifying the compiled shader to load
// 
// Returns true if shader is loaded properly, false otherwise
// --------------------------------------------------------
bool ISimpleShader::LoadShaderFile(LPCWSTR shaderFile)
{
	// Load the shader to a blob and ensure it worked
	ID3DBlob* shaderBlob = 0;
	HRESULT hr = D3DReadFileToBlob(shaderFile, &shaderBlob);
	if (hr != S_OK)
	{
		return false;
	}

	// Create the shader - Calls an overloaded version of this abstract
	// method in the appropriate child class
	shaderValid = CreateShader(shaderBlob);
	if (!shaderValid)
	{
		shaderBlob->Release();
		return false;
	}

	// Set up shader reflection to get information about
	// this shader and its variables,  buffers, etc.
	ID3D11ShaderReflection* refl;
	D3DReflect(
		shaderBlob->GetBufferPointer(),
		shaderBlob->GetBufferSize(),
		IID_ID3D11ShaderReflection,
		(void**)&refl);
	
	// Get the description of the shader
	D3D11_SHADER_DESC shaderDesc;
	refl->GetDesc(&shaderDesc);

	// Create an array of constant buffers
	constantBufferCount = shaderDesc.ConstantBuffers;
	constantBuffers = new SimpleConstantBuffer[constantBufferCount];
	
	// Handle bound resources (like shaders and samplers)
	unsigned int resourceCount = shaderDesc.BoundResources;
	for (unsigned int r = 0; r < resourceCount; r++)
	{
		// Get this resource's description
		D3D11_SHADER_INPUT_BIND_DESC resourceDesc;
		refl->GetResourceBindingDesc(r, &resourceDesc);

		// Check the type
		switch (resourceDesc.Type)
		{
		case D3D_SIT_TEXTURE: // A texture resource
			textureTable.insert(std::pair<std::string, unsigned int>(resourceDesc.Name, resourceDesc.BindPoint));
			break;

		case D3D_SIT_SAMPLER: // A sampler resource
			samplerTable.insert(std::pair<std::string, unsigned int>(resourceDesc.Name, resourceDesc.BindPoint));
			break;
		}
	}

	// Loop through all constant buffers
	for (unsigned int b = 0; b < constantBufferCount; b++)
	{
		// Get this buffer
		ID3D11ShaderReflectionConstantBuffer* cb =
			refl->GetConstantBufferByIndex(b);
		
		// Get the description of this buffer
		D3D11_SHADER_BUFFER_DESC bufferDesc;
		cb->GetDesc(&bufferDesc);

		// Get the description of the resource binding, so
		// we know exactly how it's bound in the shader
		D3D11_SHADER_INPUT_BIND_DESC bindDesc;
		refl->GetResourceBindingDescByName(bufferDesc.Name, &bindDesc);
		
		// Set up the buffer and put its pointer in the table
		constantBuffers[b].BindIndex = bindDesc.BindPoint;
		cbTable.insert(std::pair<std::string, SimpleConstantBuffer*>(bufferDesc.Name, &constantBuffers[b]));

		// Create this constant buffer
		D3D11_BUFFER_DESC newBuffDesc;
		newBuffDesc.Usage = D3D11_USAGE_DEFAULT;
		newBuffDesc.ByteWidth = bufferDesc.Size;
		newBuffDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
		newBuffDesc.CPUAccessFlags = 0;
		newBuffDesc.MiscFlags = 0;
		newBuffDesc.StructureByteStride = 0;
		device->CreateBuffer(&newBuffDesc, 0, &constantBuffers[b].ConstantBuffer);

		// Set up the data buffer for this constant buffer
		constantBuffers[b].LocalDataBuffer = new unsigned char[bufferDesc.Size];
		ZeroMemory(constantBuffers[b].LocalDataBuffer, bufferDesc.Size);

		// Loop through all variables in this buffer
		for (unsigned int v = 0; v < bufferDesc.Variables; v++)
		{
			// Get this variable
			ID3D11ShaderReflectionVariable* var =
				cb->GetVariableByIndex(v);
			
			// Get the description of the variable
			D3D11_SHADER_VARIABLE_DESC varDesc;
			var->GetDesc(&varDesc);

			// Create the variable struct
			SimpleShaderVariable varStruct;
			varStruct.ConstantBufferIndex = b;
			varStruct.ByteOffset = varDesc.StartOffset;
			varStruct.Size = varDesc.Size;
			
			// Get a string version
			std::string varName(varDesc.Name);

			// Add this variable to the table
			varTable.insert(std::pair<std::string, SimpleShaderVariable>(varName, varStruct));
		}
	}

	// All set
	refl->Release();
	shaderBlob->Release();
	return true;
}
Пример #8
0
void ShaderInfo::ReflectShader(ID3D10Blob* shader)
{
    D3D11_SHADER_DESC sd;

    D3DReflect(shader->GetBufferPointer(), shader->GetBufferSize(),
               IID_ID3D11ShaderReflection, (void**)&m_reflector);

    m_reflector->GetDesc(&sd);
    shaderDesc = sd;

    for(UINT i = 0; i < sd.InputParameters; i++)
    {
        //Mask gives info on how many components
        D3D11_SIGNATURE_PARAMETER_DESC inputparam_desc;
        m_reflector->GetInputParameterDesc(i, &inputparam_desc);
        m_inputParams.push_back(inputparam_desc);

    }
    for(UINT i = 0; i < sd.OutputParameters; i++)
    {
        D3D11_SIGNATURE_PARAMETER_DESC outputparam_desc;
        m_reflector->GetOutputParameterDesc(i, &outputparam_desc);
        m_outputParams.push_back(outputparam_desc);
    }
    for(UINT i = 0; i < sd.BoundResources; i++)
    {
        D3D11_SHADER_INPUT_BIND_DESC resource_desc; //Bind point is the register location
        m_reflector->GetResourceBindingDesc(i, &resource_desc);

        if(resource_desc.Type == D3D_SIT_CBUFFER)
        {
            Cbuffer* cbuffer = new Cbuffer();
            cbuffer->input_bind_desc = resource_desc;

            ID3D11ShaderReflectionConstantBuffer* cb =
                m_reflector->GetConstantBufferByName(cbuffer->input_bind_desc.Name);

            cb->GetDesc(&cbuffer->buffer_desc);

            for(UINT j = 0; j < cbuffer->buffer_desc.Variables; j++)
            {
                Variable* var = new Variable();

                ID3D11ShaderReflectionVariable* variable =
                    cb->GetVariableByIndex(j);

                variable->GetDesc(&var->var);

                ID3D11ShaderReflectionType* type =
                    variable->GetType();

                type->GetDesc(&var->type);

                cbuffer->vars.push_back(var);

                //register cbuff vars with ShaderVars object
// 				switch(var->type.Class) {
// 				case D3D_SVC_MATRIX_COLUMNS:
// 					if(var->type.Rows == 4 && var->type.Columns == 4) {
// 						cbp->AddMatrix(var->var.Name);
// 					}
// 					else if(var->type.Rows == 3 && var->type.Columns == 3) {
//
// 					}
// 					break;
// 				case D3D_SVC_VECTOR:
// 					if(var->type.Columns == 4) {
// 						cbp->AddVector(var->var.Name);
// 					}
// 					else if(var->type.Columns == 3) {
//
// 					}
// 					else if(var->type.Columns == 2) {
//
// 					}
// 					break;
// 				case D3D_SVC_SCALAR:
// 					if(var->type.Type == D3D_SVT_FLOAT) {
// 						cbp->AddScalar(var->var.Name);
// 					}
// 					break;
// 				default:
// 					break;
// 				}



            }
            m_cbuffers.push_back(cbuffer);
        }
        else if(resource_desc.Type == D3D_SIT_SAMPLER)
        {
            m_samplers.push_back(resource_desc);
        }
        else if(resource_desc.Type == D3D_SIT_TEXTURE)
        {
            m_textures.push_back(resource_desc);
        }
        else
        {
            m_resources.push_back(resource_desc);
        }
    }
}
Пример #9
0
bool compileHLSLShaderDx11(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
{
	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;

	ID3D11ShaderReflection* reflect = NULL;
	hr = D3DReflect(code->GetBufferPointer()
		, code->GetBufferSize()
		, IID_ID3D11ShaderReflection
		, (void**)&reflect
		);
	if (FAILED(hr) )
	{
		fprintf(stderr, "Error: 0x%08x\n", (uint32_t)hr);
		return false;
	}

	D3D11_SHADER_DESC desc;
	hr = reflect->GetDesc(&desc);
	if (FAILED(hr) )
	{
		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:");
	uint8_t numAttrs = 0;
	uint16_t attrs[bgfx::Attrib::Count];

	if (profile[0] == 'v') // Only care about input semantic on vertex shaders
	{
		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)
			{
				attrs[numAttrs] = bgfx::attribToId(ris.m_attr);
				++numAttrs;
			}
		}
	}

	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);
	}

	uint16_t size = 0;

	for (uint32_t ii = 0; ii < bx::uint32_min(1, desc.ConstantBuffers); ++ii)
	{
		ID3D11ShaderReflectionConstantBuffer* cbuffer = reflect->GetConstantBufferByIndex(ii);
		D3D11_SHADER_BUFFER_DESC bufferDesc;
		hr = cbuffer->GetDesc(&bufferDesc);

		size = (uint16_t)bufferDesc.Size;

		if (SUCCEEDED(hr) )
		{
			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);
				if (SUCCEEDED(hr) )
				{
					D3D11_SHADER_TYPE_DESC constDesc;
					hr = type->GetDesc(&constDesc);
					if (SUCCEEDED(hr) )
					{
						UniformType::Enum uniformType = findUniformTypeDx11(constDesc);

						if (UniformType::Count != uniformType
						&&  0 != (varDesc.uFlags & D3D_SVF_USED) )
						{
							Uniform un;
							un.name = varDesc.Name;
							un.type = uniformType;
							un.num = constDesc.Elements;
							un.regIndex = varDesc.StartOffset;
							un.regCount = BX_ALIGN_16(varDesc.Size)/16;
							uniforms.push_back(un);

							BX_TRACE("\t%s, %d, size %d, flags 0x%08x, %d"
								, varDesc.Name
								, varDesc.StartOffset
								, varDesc.Size
								, varDesc.uFlags
								, uniformType
								);
						}
						else
						{
							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);
		if (SUCCEEDED(hr) )
		{
			//			if (bindDesc.Type == D3D_SIT_SAMPLER)
			{
				BX_TRACE("\t%s, %d, %d, %d"
					, bindDesc.Name
					, bindDesc.Type
					, bindDesc.BindPoint
					, bindDesc.BindCount
					);
			}
		}
	}

	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
			);
	}

	{
		ID3DBlob* stripped;
		hr = D3DStripShader(code->GetBufferPointer()
			, code->GetBufferSize()
			, D3DCOMPILER_STRIP_REFLECTION_DATA
			| D3DCOMPILER_STRIP_TEST_BLOBS
			, &stripped
			);

		if (SUCCEEDED(hr) )
		{
			code->Release();
			code = stripped;
		}
	}

	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);

	bx::write(_writer, numAttrs);
	bx::write(_writer, attrs, numAttrs*sizeof(uint16_t) );

	bx::write(_writer, size);

	if (_cmdLine.hasArg('\0', "disasm") )
	{
		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 != reflect)
	{
		reflect->Release();
	}

	if (NULL != errorMsg)
	{
		errorMsg->Release();
	}

	code->Release();

	return true;
}
Пример #10
0
    bool DXShader::VInitFromFile(File* file)
    {
        
        if (!this->VInitShader(file))
            return false;
            
        if (!m_shaderReflection)
        {
            //Reflect shader info
            D3DReflect(m_shaderBlob->GetBufferPointer(),
                m_shaderBlob->GetBufferSize(),
                __uuidof(ID3D11ShaderReflection),
                reinterpret_cast<void**>(&m_shaderReflection));
        }

        //Get description of the shader
        D3D11_SHADER_DESC shaderDesc;
        m_shaderReflection->GetDesc(&shaderDesc);

        //Create array of constant buffers
        m_cbCount = shaderDesc.ConstantBuffers;
        m_cbArray = new ConstantBuffer[m_cbCount];

        //Handle bound resources (shaders or samples)
        size_t resourceCnt = shaderDesc.BoundResources;
        for (size_t i = 0; i < resourceCnt; i++)
        {
            //Get the resource description
            D3D11_SHADER_INPUT_BIND_DESC resourceDesc;
            m_shaderReflection->GetResourceBindingDesc(i, &resourceDesc);

            //Check what type
            switch (resourceDesc.Type)
            {
            case D3D_SIT_TEXTURE:
                m_texTable.insert(std::pair<std::string, size_t>(resourceDesc.Name, resourceDesc.BindPoint));
                break;

            case D3D_SIT_SAMPLER:
                m_sampTable.insert(std::pair<std::string, size_t>(resourceDesc.Name, resourceDesc.BindPoint));
                break;

            case D3D_SIT_STRUCTURED:
                m_texTable.insert(std::pair<std::string, size_t>(resourceDesc.Name, resourceDesc.BindPoint));
                break;
            }
        }

        //Loop over all constant buffers
        for (size_t i = 0; i < m_cbCount; i++)
        {
            //Get this buffer
            ID3D11ShaderReflectionConstantBuffer* cb =
                m_shaderReflection->GetConstantBufferByIndex(i);

            //Get description of buffer
            D3D11_SHADER_BUFFER_DESC bufferDesc;
            cb->GetDesc(&bufferDesc);

            //Get the description of the resource binding
            //so we know how its bound to shader
            D3D11_SHADER_INPUT_BIND_DESC bindDesc;
            m_shaderReflection->GetResourceBindingDescByName(bufferDesc.Name, &bindDesc);

            //set up buffer and put its pointer into the table
            m_cbArray[i].BindIndex = bindDesc.BindPoint;
            m_cbTable.insert(std::pair<std::string, ConstantBuffer*>(bufferDesc.Name, &m_cbArray[i]));
        
            //Create this constant buffer
            D3D11_BUFFER_DESC _newBufferDesc;
            _newBufferDesc.Usage = D3D11_USAGE_DEFAULT;
            _newBufferDesc.ByteWidth = bufferDesc.Size;
            _newBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
            _newBufferDesc.CPUAccessFlags = 0;
            _newBufferDesc.MiscFlags = 0;
            _newBufferDesc.StructureByteStride = 0;
            m_device->CreateBuffer(&_newBufferDesc, 0, &m_cbArray[i].Buffer);
        
            //setup the data buffer for the cb
            m_cbArray[i].LocalDataBuffer = new BYTE[bufferDesc.Size];
            ZeroMemory(m_cbArray[i].LocalDataBuffer, bufferDesc.Size);

            //Iterate over all variables in this buffer
            for (size_t v = 0; v < bufferDesc.Variables; v++)
            {
                //Get this variables
                ID3D11ShaderReflectionVariable* var =
                    cb->GetVariableByIndex(v);

                //Get variable description
                D3D11_SHADER_VARIABLE_DESC varDesc;
                var->GetDesc(&varDesc);

                //Create the variable struct
                ShaderVariable variable;
                variable.ConstantBufferIndex = i;
                variable.ByteOffset = varDesc.StartOffset;
                variable.Size = varDesc.Size;
                
                //Get a string representation
                std::string varName = varDesc.Name;

                //Add variable to table
                m_svTable.insert(std::pair<std::string, ShaderVariable>(varName, variable));
            }
        }

//ReleaseCOM(m_shaderReflection);
        //ReleaseCOM(m_shaderBlob);

        return true;
    }
Пример #11
0
bool getReflectionDataD3D11(ID3DBlob* _code, bool _vshader, UniformArray& _uniforms, uint8_t& _numAttrs, uint16_t* _attrs, uint16_t& _size, UniformNameList& unusedUniforms)
{
    ID3D11ShaderReflection* reflect = NULL;
    HRESULT hr = D3DReflect(_code->GetBufferPointer()
                            , _code->GetBufferSize()
                            , s_compiler->IID_ID3D11ShaderReflection
                            , (void**)&reflect
                           );
    if (FAILED(hr) )
    {
        fprintf(stderr, "Error: D3DReflect failed 0x%08x\n", (uint32_t)hr);
        return false;
    }

    D3D11_SHADER_DESC desc;
    hr = reflect->GetDesc(&desc);
    if (FAILED(hr) )
    {
        fprintf(stderr, "Error: ID3D11ShaderReflection::GetDesc failed 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:");

    if (_vshader) // Only care about input semantic on vertex shaders
    {
        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)
            {
                _attrs[_numAttrs] = bgfx::attribToId(ris.m_attr);
                ++_numAttrs;
            }
        }
    }

    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);
    }

    for (uint32_t ii = 0, num = bx::uint32_min(1, desc.ConstantBuffers); ii < num; ++ii)
    {
        ID3D11ShaderReflectionConstantBuffer* cbuffer = reflect->GetConstantBufferByIndex(ii);
        D3D11_SHADER_BUFFER_DESC bufferDesc;
        hr = cbuffer->GetDesc(&bufferDesc);

        _size = (uint16_t)bufferDesc.Size;

        if (SUCCEEDED(hr) )
        {
            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);
                if (SUCCEEDED(hr) )
                {
                    D3D11_SHADER_TYPE_DESC constDesc;
                    hr = type->GetDesc(&constDesc);
                    if (SUCCEEDED(hr) )
                    {
                        UniformType::Enum uniformType = findUniformType(constDesc);

                        if (UniformType::Count != uniformType
                                &&  0 != (varDesc.uFlags & D3D_SVF_USED) )
                        {
                            Uniform un;
                            un.name = varDesc.Name;
                            un.type = uniformType;
                            un.num = constDesc.Elements;
                            un.regIndex = varDesc.StartOffset;
                            un.regCount = BX_ALIGN_16(varDesc.Size) / 16;
                            _uniforms.push_back(un);

                            BX_TRACE("\t%s, %d, size %d, flags 0x%08x, %d (used)"
                                     , varDesc.Name
                                     , varDesc.StartOffset
                                     , varDesc.Size
                                     , varDesc.uFlags
                                     , uniformType
                                    );
                        }
                        else
                        {
                            if (0 == (varDesc.uFlags & D3D_SVF_USED) )
                            {
                                unusedUniforms.push_back(varDesc.Name);
                            }

                            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);
        if (SUCCEEDED(hr) )
        {
            if (D3D_SIT_SAMPLER == bindDesc.Type)
            {
                BX_TRACE("\t%s, %d, %d, %d"
                         , bindDesc.Name
                         , bindDesc.Type
                         , bindDesc.BindPoint
                         , bindDesc.BindCount
                        );

                const char * end = strstr(bindDesc.Name, "Sampler");
                if (NULL != end)
                {
                    Uniform un;
                    un.name.assign(bindDesc.Name, (end - bindDesc.Name) );
                    un.type = UniformType::Enum(BGFX_UNIFORM_SAMPLERBIT | UniformType::Int1);
                    un.num = 1;
                    un.regIndex = bindDesc.BindPoint;
                    un.regCount = bindDesc.BindCount;
                    _uniforms.push_back(un);
                }
            }
        }
    }

    if (NULL != reflect)
    {
        reflect->Release();
    }

    return true;
}
//-------------------------------------------------------------------------------
// @ IvConstantTableD3D11::Create()
//-------------------------------------------------------------------------------
// Get the constant table for this shader
//-------------------------------------------------------------------------------
IvConstantTableD3D11*
IvConstantTableD3D11::Create(ID3D11Device* device, ID3DBlob* code)
{
    ID3D11ShaderReflection* pReflector = nullptr;
    D3DReflect(code->GetBufferPointer(), code->GetBufferSize(),
        IID_ID3D11ShaderReflection, (void**)&pReflector);

    // first get constant table
    IvConstantTableD3D11* table = new IvConstantTableD3D11();

    ID3D11ShaderReflectionConstantBuffer* globalConstants =
        pReflector->GetConstantBufferByName("$Globals");
    D3D11_SHADER_BUFFER_DESC constantBufferDesc = { 0 };
    HRESULT hr = globalConstants->GetDesc(&constantBufferDesc);
    if (FAILED(hr))
    {
        // no constants in this shader
        goto get_textures;
    }

    table->mBacking = new char[constantBufferDesc.Size];
    table->mBackingSize = constantBufferDesc.Size;
    if (nullptr == table->mBacking)
    {
        delete table;
        pReflector->Release();
        return nullptr;
    }

    for (unsigned int i = 0; i < constantBufferDesc.Variables; ++i)
    {
        ID3D11ShaderReflectionVariable* var = globalConstants->GetVariableByIndex(i);
        D3D11_SHADER_VARIABLE_DESC varDesc;
        if (FAILED(var->GetDesc(&varDesc)))
        {
            delete table;
            pReflector->Release();
            return nullptr;
        }
        ID3D11ShaderReflectionType* type = var->GetType();
        D3D11_SHADER_TYPE_DESC typeDesc;
        if (FAILED(type->GetDesc(&typeDesc)))
        {
            delete table;
            pReflector->Release();
            return nullptr;
        }

        IvConstantDesc constantDesc;
        constantDesc.mOffset = table->mBacking + varDesc.StartOffset;
        if (typeDesc.Class == D3D_SVC_SCALAR && typeDesc.Type == D3D_SVT_FLOAT)
        {
            constantDesc.mType = IvUniformType::kFloatUniform;
        }
        else if (typeDesc.Class == D3D_SVC_VECTOR && typeDesc.Type == D3D_SVT_FLOAT
            && typeDesc.Columns == 3)
        {
            constantDesc.mType = IvUniformType::kFloat3Uniform;
        }
        else if (typeDesc.Class == D3D_SVC_VECTOR && typeDesc.Type == D3D_SVT_FLOAT
            && typeDesc.Columns == 4)
        {
            constantDesc.mType = IvUniformType::kFloat4Uniform;
        }
        else if ((typeDesc.Class == D3D_SVC_MATRIX_ROWS || typeDesc.Class == D3D_SVC_MATRIX_COLUMNS)
            && typeDesc.Type == D3D_SVT_FLOAT
            && typeDesc.Rows == 4 && typeDesc.Columns == 4)
        {
            constantDesc.mType = IvUniformType::kFloatMatrix44Uniform;
        }
        else
        {
            // unsupported uniform type
            delete table;
            pReflector->Release();
            return nullptr;
        }
        constantDesc.mCount = typeDesc.Elements != 0 ? typeDesc.Elements : 1;

        table->mConstants[varDesc.Name] = constantDesc;
    }

    D3D11_BUFFER_DESC cbDesc;
    cbDesc.ByteWidth = constantBufferDesc.Size;
    cbDesc.Usage = D3D11_USAGE_DYNAMIC;
    cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    cbDesc.MiscFlags = 0;
    cbDesc.StructureByteStride = 0;

    if (FAILED(device->CreateBuffer(&cbDesc, nullptr, &table->mBuffer)))
    {
        delete table;
        pReflector->Release();
        return nullptr;
    }

get_textures:
    // now get any textures and samplers
    D3D11_SHADER_DESC shaderDesc = { 0 };
    hr = pReflector->GetDesc(&shaderDesc);
    if (FAILED(hr))
    {
        pReflector->Release();
        return table;
    }
    int resourceCount = shaderDesc.BoundResources;

    for (int i = 0; i < resourceCount; ++i) 
    {
        D3D11_SHADER_INPUT_BIND_DESC bindingDesc = { 0 };
        pReflector->GetResourceBindingDesc(i, &bindingDesc);
        if (bindingDesc.Type == D3D_SIT_TEXTURE && bindingDesc.ReturnType == D3D_RETURN_TYPE_FLOAT
            && bindingDesc.Dimension == D3D_SRV_DIMENSION_TEXTURE2D && bindingDesc.NumSamples == -1
            && bindingDesc.BindCount == 1)
        {
            IvConstantDesc& constantDesc = table->mConstants[bindingDesc.Name];
            constantDesc.mType = IvUniformType::kTextureUniform;
            constantDesc.mTextureSlot = bindingDesc.BindPoint;
        }
        else if (bindingDesc.Type == D3D_SIT_SAMPLER && bindingDesc.BindCount == 1)
        {
            char strippedName[256];
            const char* samplerKeyword = strstr(bindingDesc.Name, "Sampler");
            strncpy(strippedName, bindingDesc.Name, samplerKeyword - bindingDesc.Name);
            strippedName[samplerKeyword - bindingDesc.Name] = '\0';
            IvConstantDesc& constantDesc = table->mConstants[strippedName];
            constantDesc.mType = IvUniformType::kTextureUniform;
            constantDesc.mSamplerSlot = bindingDesc.BindPoint;
        }
        else if (bindingDesc.Type == D3D_SIT_CBUFFER && !_strnicmp(bindingDesc.Name, "$Globals", 8))
        {
            // ignore this, we handled it above
        }
        else
        {
            // unsupported resource type
            delete table;
            pReflector->Release();
            return nullptr;
        }
    }

    pReflector->Release();

    return table;
}
    //=====================================================================================================================
    static void ParseShaderReflection( ID3D11ShaderReflection* pReflection, 
                                       uint nStage,
                                       std::vector<VariableRef>& vars,
                                       std::vector<TextureRef>& textures,
                                       std::vector<SamplerRef>& samplers,
                                       std::vector<ConstantBuffer>& cbs )                         
    {
        D3D11_SHADER_DESC desc;
        pReflection->GetDesc(&desc);
       
        // yank constant buffers
        for( uint i=0; i<desc.ConstantBuffers; i++ )
        {
            D3D11_SHADER_BUFFER_DESC desc;
            ID3D11ShaderReflectionConstantBuffer* pCB = pReflection->GetConstantBufferByIndex(0) ;
            pCB->GetDesc(&desc);
            
            D3D11_SHADER_INPUT_BIND_DESC bind;
            pReflection->GetResourceBindingDescByName( desc.Name, &bind );

            ConstantBuffer cb;
            cb.nShaderStage      = nStage;
            cb.nSlotIndex  = bind.BindPoint;
            cb.nBufferSize = (desc.Size + 15) & ~15;
            cbs.push_back(cb);

            // yank constant buffer variables
            for( uint v=0; v<desc.Variables; v++ )
            {
                ID3D11ShaderReflectionVariable* pVar = pCB->GetVariableByIndex(v);
                D3D11_SHADER_VARIABLE_DESC desc;
                pVar->GetDesc(&desc);

                VariableRef var;
                var.nShaderStage = nStage;
                var.nCBufferIndex = cbs.size()-1;
                var.nCBufferOffset = desc.StartOffset;
                var.nVariableSize = desc.Size;
                var.nName = crcOfString(desc.Name);
                vars.push_back(var);
            }

        }
        
        // yank sampler, texture bindings
        for( uint i=0; i<desc.BoundResources; i++ )
        {
            D3D11_SHADER_INPUT_BIND_DESC inputDesc;
            pReflection->GetResourceBindingDesc(i, &inputDesc );
            switch( inputDesc.Type )
            {
            case D3D_SIT_SAMPLER:
                {
                    SamplerRef ref;
                    ref.nName = crcOfString( inputDesc.Name );
                    ref.nSlotIndex = inputDesc.BindPoint;
                    ref.nShaderStage = nStage;
                    samplers.push_back(ref);
                }
                break;

            case D3D_SIT_TBUFFER:
            case D3D_SIT_TEXTURE:
            case D3D_SIT_STRUCTURED:
            case D3D_SIT_BYTEADDRESS:
                {
                    TextureRef ref;
                    ref.nName = crcOfString(inputDesc.Name);
                    ref.nSlotIndex = inputDesc.BindPoint;
                    ref.nShaderStage = nStage;
                    textures.push_back(ref);
                }
                break;
            }
        }      
    }
Пример #14
0
int main(void)
{
    char const *verbose_var = getenv("D3D4LINUX_VERBOSE");
    if (!verbose_var || *verbose_var != '1')
        dup2(open("/dev/null", O_WRONLY), STDERR_FILENO);

    HMODULE lib = LoadLibrary("d3dcompiler_43.dll");

    /* Ensure stdout is in binary mode */
    setmode(fileno(stdout), O_BINARY);
    setmode(fileno(stdin), O_BINARY);

    int syscall = read_integer();
    int marker = 0;

    if (syscall == D3D4LINUX_COMPILE)
    {
        HRESULT (*compile)(void const *pSrcData, size_t SrcDataSize,
                           char const *pFileName,
                           D3D_SHADER_MACRO const *pDefines,
                           ID3DInclude *pInclude,
                           char const *pEntrypoint, char const *pTarget,
                           uint32_t Flags1, uint32_t Flags2,
                           ID3DBlob **ppCode, ID3DBlob **ppErrorMsgs);
        compile = (decltype(compile))GetProcAddress(lib, "D3DCompile");

        /* This is a D3DCompile() call */
        std::string shader_source = read_string();

        int has_filename = (int)read_integer();
        std::string shader_file;
        if (has_filename)
            shader_file = read_string();

        std::string shader_main = read_string();
        std::string shader_type = read_string();
        uint32_t flags1 = (uint32_t)read_integer();
        uint32_t flags2 = (uint32_t)read_integer();
        marker = (int)read_integer();
        if (marker != D3D4LINUX_FINISHED)
            goto error;

        ID3DBlob *shader_blob = nullptr, *error_blob = nullptr;
        HRESULT ret = compile(shader_source.c_str(), shader_source.size(),
                              shader_file.c_str(),
                              nullptr, /* unimplemented */
                              nullptr, /* unimplemented */
                              shader_main.c_str(),
                              shader_type.c_str(),
                              flags1, flags2, &shader_blob, &error_blob);
        fprintf(stderr, "[D3D4LINUX] D3DCompile([%d bytes], \"%s\", ?, ?, \"%s\", \"%s\", %04x, %04x ) = 0x%08x\n",
                (int)shader_source.size(), has_filename ? shader_file.c_str() : "(nullptr)", shader_main.c_str(), shader_type.c_str(),
                flags1, flags2, (int)ret);

        write_integer(ret);
        write_blob(shader_blob);
        write_blob(error_blob);
        write_integer(D3D4LINUX_FINISHED);

        if (shader_blob)
            shader_blob->Release();
        if (error_blob)
            error_blob->Release();
    }
    else if (syscall == D3D4LINUX_REFLECT)
    {
        HRESULT (*reflect)(void const *pSrcData,
                           size_t SrcDataSize,
                           REFIID pInterface,
                           void **ppReflector);
        reflect = (decltype(reflect))GetProcAddress(lib, "D3DReflect");

        std::vector<uint8_t> *data = read_data();
        int iid_code = read_integer();
        marker = (int)read_integer();
        if (marker != D3D4LINUX_FINISHED)
            goto error;

        char const *iid_name = "";
        IID iid;
        switch (iid_code)
        {
        case D3D4LINUX_IID_SHADER_REFLECTION:
            iid = IID_ID3D11ShaderReflection;
            iid_name = "IID_ID3D11ShaderReflection";
            break;
        default:
            goto error;
        }

        void *object;
        HRESULT ret = reflect(data ? data->data() : nullptr,
                              data ? data->size() : 0,
                              iid, &object);
        fprintf(stderr, "[D3D4LINUX] D3DReflect([%d bytes], %s) = 0x%08x\n",
                data ? (int)data->size() : 0, iid_name, (int)ret);

        write_integer(ret);

        if (iid_code == D3D4LINUX_IID_SHADER_REFLECTION)
        {
            D3D11_SIGNATURE_PARAMETER_DESC param_desc;
            D3D11_SHADER_INPUT_BIND_DESC bind_desc;
            D3D11_SHADER_VARIABLE_DESC variable_desc;
            D3D11_SHADER_BUFFER_DESC buffer_desc;
            D3D11_SHADER_DESC shader_desc;

            ID3D11ShaderReflection *reflector = (ID3D11ShaderReflection *)object;

            /* Serialise D3D11_SHADER_DESC */
            reflector->GetDesc(&shader_desc);
            write_raw(&shader_desc, sizeof(shader_desc));
            write_string(shader_desc.Creator);

            /* Serialize all InputParameterDesc */
            for (uint32_t i = 0; i < shader_desc.InputParameters; ++i)
            {
                reflector->GetInputParameterDesc(i, &param_desc);
                write_raw(&param_desc, sizeof(param_desc));
                write_string(param_desc.SemanticName);
            }

            /* Serialize all OutParameterDesc */
            for (uint32_t i = 0; i < shader_desc.OutputParameters; ++i)
            {
                reflector->GetOutputParameterDesc(i, &param_desc);
                write_raw(&param_desc, sizeof(param_desc));
                write_string(param_desc.SemanticName);
            }

            /* Serialize all ResourceBindingDesc */
            for (uint32_t i = 0; i < shader_desc.BoundResources; ++i)
            {
                reflector->GetResourceBindingDesc(i, &bind_desc);
                write_raw(&bind_desc, sizeof(bind_desc));
                write_string(bind_desc.Name);
            }

            /* Serialize all ConstantBuffer */
            for (uint32_t i = 0; i < shader_desc.ConstantBuffers; ++i)
            {
                ID3D11ShaderReflectionConstantBuffer *cbuffer
                        = reflector->GetConstantBufferByIndex(i);

                /* Serialize D3D11_SHADER_BUFFER_DESC */
                cbuffer->GetDesc(&buffer_desc);
                write_raw(&buffer_desc, sizeof(buffer_desc));
                write_string(buffer_desc.Name);

                /* Serialize all Variable */
                for (uint32_t j = 0; j < buffer_desc.Variables; ++j)
                {
                    ID3D11ShaderReflectionVariable *var
                            = cbuffer->GetVariableByIndex(j);

                    /* Serialize D3D11_SHADER_VARIABLE_DESC */
                    var->GetDesc(&variable_desc);
                    write_raw(&variable_desc, sizeof(variable_desc));
                    write_string(variable_desc.Name);
                    write_integer(variable_desc.DefaultValue ? 1 : 0);
                    if (variable_desc.DefaultValue)
                        write_raw(variable_desc.DefaultValue, variable_desc.Size);
                }
            }
        }

        write_integer(D3D4LINUX_FINISHED);

        delete data;
    }
    else if (syscall == D3D4LINUX_STRIP)
    {
        HRESULT (*strip)(void const *pShaderBytecode,
                         size_t BytecodeLength,
                         uint32_t uStripFlags,
                         ID3DBlob **ppStrippedBlob);
        strip = (decltype(strip))GetProcAddress(lib, "D3DStripShader");

        std::vector<uint8_t> *data = read_data();
        uint32_t flags = (uint32_t)read_integer();
        marker = (int)read_integer();
        if (marker != D3D4LINUX_FINISHED)
            goto error;

        ID3DBlob *strip_blob = nullptr;
        HRESULT ret = strip(data ? data->data() : nullptr,
                            data ? data->size() : 0,
                            flags, &strip_blob);
        fprintf(stderr, "[D3D4LINUX] D3DStripShader([%d bytes], %04x) = 0x%08x\n",
                data ? (int)data->size() : 0, flags, (int)ret);

        write_integer(ret);
        write_blob(strip_blob);
        write_integer(D3D4LINUX_FINISHED);

        if (strip_blob)
            strip_blob->Release();
    }
    else if (syscall == D3D4LINUX_DISASSEMBLE)
    {
        HRESULT (*disas)(void const *pSrcData,
                         size_t SrcDataSize,
                         uint32_t Flags,
                         char const *szComments,
                         ID3DBlob **ppDisassembly);
        disas = (decltype(disas))GetProcAddress(lib, "D3DDisassemble");

        std::vector<uint8_t> *data = read_data();
        uint32_t flags = (uint32_t)read_integer();
        int has_comments = (int)read_integer();
        std::string comments;
        if (has_comments)
            comments = read_string();
        marker = (int)read_integer();
        if (marker != D3D4LINUX_FINISHED)
            goto error;

        ID3DBlob *disas_blob = nullptr;
        HRESULT ret = disas(data ? data->data() : nullptr,
                            data ? data->size() : 0,
                            flags,
                            has_comments ? comments.c_str() : nullptr,
                            &disas_blob);
        fprintf(stderr, "[D3D4LINUX] D3DDisassemble([%d bytes], %04x, %s) = 0x%08x\n",
                data ? (int)data->size() : 0, flags, has_comments ? "[comments]" : "(nullptr)", (int)ret);

        write_integer(ret);
        write_blob(disas_blob);
        write_integer(D3D4LINUX_FINISHED);

        if (disas_blob)
            disas_blob->Release();
    }

    return EXIT_SUCCESS;

error:
    fprintf(stderr, "[D3D4LINUX] Bad message received: %08x %08x\n", syscall, marker);
}