Jump to content

  • Log In with Google      Sign In   
  • Create Account


Compile Shader From Memory


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Johannes1991   Members   -  Reputation: 433

Like
0Likes
Like

Posted 28 February 2013 - 10:33 AM

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

Sponsor:

#2 __SKYe   Members   -  Reputation: 1010

Like
0Likes
Like

Posted 28 February 2013 - 12:46 PM

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?



#3 Erik Rufelt   Crossbones+   -  Reputation: 3365

Like
1Likes
Like

Posted 28 February 2013 - 07:53 PM

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.



#4 __SKYe   Members   -  Reputation: 1010

Like
0Likes
Like

Posted 28 February 2013 - 11:10 PM

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.


Edited by __SKYe, 28 February 2013 - 11:13 PM.


#5 Endurion   Crossbones+   -  Reputation: 3463

Like
0Likes
Like

Posted 28 February 2013 - 11:51 PM

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>




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS