Compile Shader From Memory

Started by
3 comments, last by Endurion 11 years, 1 month ago

I have started making my own shader loader and of course it's not working! When I read from the shader file i separate the hlsl code and my own code into different strings and when i try to compile the shader code with D3DX11CompileFromMemory() I get this error message:
"error X3000: syntax error: unexpected end of file"

I have put a null terminator in the end of the string but it doesn't work...
So anyone know the reason for this problem?

Code:


std::fstream file(path, std::ios::in);
std::string shaderCode = "", tempBuffer;

if(file.is_open())
{
        while(!file.eof())
        {
            //file.get(tempBuffer);
            file >> tempBuffer;

            if(tempBuffer == "ShaderInformation")
            {
                int i = 0;
                i += 0;
            }
            else
            {
                shaderCode += " " + tempBuffer;
            }
        }
}

shaderCode += '\0';

HRESULT hr;
ID3D10Blob *vsBlob, *psBlob, *errorBlob;
	
//Dummy hlsl code
const char temp[] = 
"Texture2D colormap;"
"Texture2D lightmap;"

"SamplerState fullscreenQuadSampler;"

"struct VertexInput"
"{"
	"float3 position : POSITION0;"
	"float2 uv : TEXCOORD0;"
"};"

"struct PixelInput"
"{"
	"float4 position : SV_Position;"
	"float2 uv : TEXCOORD0;"
"};"

"PixelInput VS(VertexInput input)"
"{"
	"PixelInput output;"

	"output.position = float4(input.position, 1.0f);"
	"float2 halfPixel = float2(0.5f / 800.0f, 0.5f / 600.0f);"
	"output.uv = input.uv;// + halfPixel;"

	"return output;"
"}"

"float4 PS(PixelInput input) : SV_Target"
"{"
	"float3 diffuseColor = colormap.Sample(fullscreenQuadSampler, input.uv).rgb;"
	"float4 lightData = lightmap.Sample(fullscreenQuadSampler, input.uv);"
   
	"float3 diffuseLight = lightData.rgb;"
	"float specularLight = lightData.a;"

	"return float4(diffuseColor * diffuseLight + specularLight, 1.0f);"
"}"
"\0";		
	
hr = D3DX11CompileFromMemory(temp,
		sizeof(temp),
		"FinalComposition.hlsl",
		NULL,
		NULL,
		"VS",
		"vs_4_0",
		D3D10_SHADER_PACK_MATRIX_ROW_MAJOR,
		NULL,
		NULL,
		&vsBlob,
		&errorBlob,
		NULL);


if(hr != S_OK)
{
	if(!errorBlob)
		return false;

	char msg[20000];
	strcpy_s(msg, sizeof(msg), (char*)errorBlob->GetBufferPointer());
	OutputDebugStringA(msg);
	MessageBoxA(GetDesktopWindow(), msg, "VertexShader Compilation Failed!", MB_OK | MB_ICONERROR);
	errorBlob->Release();

	return false;
}

EnJOJ Gaming
Advertisement

There's probably another \0 character in the shader code before the end of file.

Why don't you try saving the shader in a text file, and open it inside the program and load the file to a buffer, and then try to compile it?

Try setting the file-name to NULL when you specify the code in memory.

Or try using D3DCompile instead. There's probably no reason to go through the D3DX11 function in this case.

I think the problem is that each "" pair in the HLSL dummy buffer is being treated as a single string, so each "" pair will have a \0 char at the end, hence the shader will stop compiling at the end of the first line.

Try this instead:






//Dummy hlsl code
    const char temp[] = {
                        "Texture2D colormap;\
                        Texture2D lightmap;\
                        \
                        SamplerState fullscreenQuadSampler;\
                        \
                        struct VertexInput\
                        {\
	                        float3 position : POSITION0;\
	                        float2 uv : TEXCOORD0;\
                        };\
                        \
                        struct PixelInput\
                        {\
	                        float4 position : SV_Position;\
	                        float2 uv : TEXCOORD0;\
                        };\
                        \
                        PixelInput VS(VertexInput input)\
                        {\
	                        PixelInput output;\
                            \
	                        output.position = float4(input.position, 1.0f);\
	                        float2 halfPixel = float2(0.5f / 800.0f, 0.5f / 600.0f);\
	                        output.uv = input.uv;// + halfPixel;\
                            \
	                        return output;\
                        }\
                        \
                        float4 PS(PixelInput input) : SV_Target\
                        {\
	                        float3 diffuseColor = colormap.Sample(fullscreenQuadSampler, input.uv).rgb;\
	                        float4 lightData = lightmap.Sample(fullscreenQuadSampler, input.uv);\
    \
	                        float3 diffuseLight = lightData.rgb;\
	                        float specularLight = lightData.a;\
                            \
	                        return float4(diffuseColor * diffuseLight + specularLight, 1.0f);\
                        }"
    };

Using the \ character at the end of the line indicates that the string continues on the next line, and there's no need to put each line in a "" pair.

Also, you could try what Erik Rufelt said. I don't use D3D so i don't know about it, but your problem, at least to me, seemed to be with the temp HLSL code.

I think the problem is that each "" pair in the HLSL dummy buffer is being treated as a single string, so each "" pair will have a \0 char at the end, hence the shader will stop compiling at the end of the first line.

No, it isn't. The compiler will simply concatenate the text blocks into one string.


Also, you do not have to add a \0 at the end, the compiler inserts one on its own. This may actually be your problem.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement