• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Johannes1991

Compile Shader From Memory

4 posts in this topic

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

 

 

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0