• 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
Foxostro

Very peculiar GLSL bug

7 posts in this topic

Hi, I'm trying to write some GLSL support into a game I'm working on so I can do PCF shadow maps. I have been able to resolve most of the problems that have come up, but this one really sticks me. I was trying to do shadow comparison in a fragment shader, but every time I include a call to shadow2D or shadow2DProj or whatnot, the shader fails to validate and will not execute. So, I looked over my shaders and couldn't see anything wrong. As a sanity check, I tried out the shaders from the PCF tutorial on GameTutorials.com. In that tutorial program the shader runs flawlessly. In my program the shader compiles, but fails to link because "Not all shaders have valid object code." I'm not exactly sure what that means. But the problem has to be in my own code. The root of the problem seems to be that either a shader will not validate or will not link if I call shadow2D, etc at any point. If I comment out calls to shadow2D, etc, then the shader will compile, link, validate, and run in my game provided there is nothing else wrong with it. Since other programs on my computer can handle shaders with these calls, the problem, again, must reside in my own code. But why shadow2D? Here is the code that I'm using to compile my shaders after I load them from file. Let me know if anything is being set up wrong. It's probably right in front of me...
bool Shader::compile(const char *vertexShader, const char *fragmentShader)
{
	int status=GL_FALSE;

	// Flush errors, error reports will concern the following calls only
	FLUSH_GL_ERROR();

// Create the shader objects
	unsigned int v = glCreateShader(GL_VERTEX_SHADER);
	unsigned int f = glCreateShader(GL_FRAGMENT_SHADER);	

// Send the source code to the driver
	glShaderSource(v, 1, (const char **)&vertexShader, NULL);
	glShaderSource(f, 1, (const char **)&fragmentShader, NULL);

// Compile the Vertex Shader
	glCompileShader(v);

	CHECK_GL_ERROR();
	glGetShaderiv(v, GL_COMPILE_STATUS, &status);
	if(status == GL_FALSE)
	{
		// Failed to compile the vertex shader
		printInfoLog(v);
		ASSERT(false, "Vertex Shader failed to compile! Check the error log for details.");
		return false;
	}

// Compile the Fragment Shader
	glCompileShader(f);

	CHECK_GL_ERROR();
	glGetShaderiv(f, GL_COMPILE_STATUS, &status);
	if(status == GL_FALSE)
	{
		// Failed to compile the fragment shader
		printInfoLog(f);
		ASSERT(false, "Fragment Shader failed to compile! Check the error log for details.");
		return false;
	}

	// Create a program name
	programHandle = glCreateProgram();
	CHECK_GL_ERROR();
	ASSERT(glIsProgram(programHandle)==GL_TRUE, "Shader::compile  ->  The program handle is not a valid handle to a GLSL shader program");

	// Attach object code to the program
	glAttachShader(programHandle, v);
	glAttachShader(programHandle, f);

	// Link the object code to finish the program
	glLinkProgram(programHandle);
	printInfoLog(programHandle);

	CHECK_GL_ERROR();
	glGetProgramiv(programHandle, GL_LINK_STATUS, &status);
	if(status == GL_FALSE)
	{
		// Failed to link correctly
		THROW("Failed to link shader");
		return false;
	}
	else
	{
		// Lastly, validate the shader to determine whether it will run at all
#ifdef _DEBUG
		glValidateProgram(programHandle);
		glGetProgramiv(programHandle, GL_VALIDATE_STATUS, &status);
		ASSERT(status==GL_TRUE, "Shader::compile  ->  The GLSL shader program could not be validated, it is garaunteed to not execute.");
#endif

		// Get the locations of the uniforms samplers for each texture unit
		for(int unit=0; unit<16; ++unit)
		{
			char buffer[32]={0};
			sprintf(buffer, "tex%i", unit);

			// If a uniform could not be found, then -1 will bestored
			texLocs[unit] = getLocation(buffer);
		}
	}

	return true;
}


-Andrew
0

Share this post


Link to post
Share on other sites
This is a shadow map shader from gametutorials.com, however, I renamed the sampler to tex1. Its not the PCF one I mentioned above, but it suffers the same error when I execute it in my program. The demo program on gametutorials.com has no problems, but in my program, while compiling the vertex shader and fragment shader causes no error, it fails to link because "not all shaders have valid object code."

Vertex Shader:

varying vec4 projCoord;

void main()
{
vec4 realPos = gl_ModelViewMatrix * gl_Vertex;

projCoord = gl_TextureMatrix[0] * realPos;
gl_FrontColor = gl_Color;

gl_Position = ftransform();
}





Fragment Shader:

uniform sampler2DShadow tex1;
varying vec4 projCoord;

void main ()
{
const float kTransparency = 0.3;
vec4 color = gl_Color;

float rValue = shadow2DProj(tex1, projCoord).r + kTransparency;
rValue = clamp(rValue, 0.0, 1.0);

vec3 coordPos = projCoord.xyz / projCoord.w;

if(coordPos.x >= 0.0 && coordPos.y >= 0.0 && coordPos.x <= 1.0 && coordPos.y <= 1.0 )
{
gl_FragColor = color * rValue;
}
else
{
gl_FragColor = color;
}
}





--------------------------------------------------------

I tried to create the simplest shader possible that still causes an error. The following shader compiles, links, but fails to validate.
Vertex Shader:

varying vec2 Texcoord;
varying vec4 projCoord;

void main()
{
// Pass down the texcoord for this vertex
Texcoord = gl_MultiTexCoord0.xy;

// Pass down the projected shadow coord
vec4 realPos = gl_ModelViewMatrix * gl_Vertex;
projCoord = gl_TextureMatrix[1] * realPos;

// Transform vertex
gl_Position = ftransform();
}




Fragment Shader:

uniform sampler2D tex0;
uniform sampler2DShadow tex1;

varying vec2 Texcoord;
varying vec4 projCoord;

void main()
{
vec4 textureColor = texture2D(tex0, Texcoord);
vec4 shadowColor = shadow2DProj(tex1, projCoord);

gl_FragColor = textureColor * shadowColor;
}





-------------------------------

I was surprised to find that this shader compiles, links, validates, and runs. However, it does not behave correctly.

Vertex Shader:

varying vec4 projCoord;

void main()
{
vec4 realPos = gl_ModelViewMatrix * gl_Vertex;

projCoord = gl_TextureMatrix[0] * realPos;

gl_Position = ftransform();
}





Fragment Shader:

uniform sampler2DShadow tex1;
varying vec4 projCoord;

void main ()
{
float rValue = shadow2DProj(tex1, projCoord).r;

gl_FragColor = vec4(rValue);
}





I would expect that if it were to run, it would color each fragment according to the shadow value at that point. Instead, it appears to project the base map texture, which is not even read in this shader. The fragment shader above behaves identically to this other fragment shader:


uniform sampler2D tex0;
varying vec4 projCoord;

void main ()
{
float rValue = texture2DProj(tex0, projCoord).r;

gl_FragColor = vec4(rValue);
}





---------------------------------

If it affects anything:

* Every time an object is about to be drawn, my program attempts to bind texture unit 0 to a uniform called tex0, texture unit 1 to tex1, texture unit 2 to tex2, etc.

* Each object has its own shadow map and FFP shadows work perfectly. Right now I'm only trying to cast the shadow from one object with these shaders.

---------------------------------

-Thank you

EDIT: sloppy copy/paste

[Edited by - Foxostro on January 29, 2006 1:19:27 PM]
0

Share this post


Link to post
Share on other sites
Got nothing? :(

Am I building my shaders correctly? It looks good to me, but I might be missing something.

Could some other GL state that I have set elsewhere in the program interfere with GLSL?
0

Share this post


Link to post
Share on other sites
SOLVED

glValidateProgram lies.
If I ignore the fact that "If validation is successful, program is guaranteed to execute given the current state. Otherwise, program is guaranteed to not execute." and try to execute the shader anyway, then it will execute.


When there are "if" statements, the shader may compile with no errors, but they will cause it to fail to link. Does this mean my Radeon 9800XT doesn't support branching statements?


Quote:

I would expect that if it were to run, it would color each fragment according to the shadow value at that point. Instead, it appears to project the base map texture, which is not even read in this shader. The fragment shader above behaves identically to this other fragment shader [...]

I was setting a sampler-uniform wrong for that particular test.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Foxostro
When there are "if" statements, the shader may compile with no errors, but they will cause it to fail to link. Does this mean my Radeon 9800XT doesn't support branching statements?


As far as I know, dynamic branching is only supported on shader model 3 hardware...
0

Share this post


Link to post
Share on other sites
But you would think it would give you a more descriptive error when I used an "if" :b Its not even that I can't use "if" ever, I can use it when comparing a float against a compile time constant or a literal constant. I do that twice in my phong shader...

And I still don't get glValidateProgram :/
0

Share this post


Link to post
Share on other sites
glValidateProgram doesnt lie, it does precisly what it is ment todo, you are just failing to take into account a certain fact : it returns true or false depending on the whole OpenGL state at the time of calling.

Now, you are using a shadow lookup, however the GLSL state tracking at that point says that texture unit 0 is a standard texture unit and not in shadow mapping mode, thus the outcome of the shader may or may not be valid, therefore glValidateProgram returns false, as it is ment todo.
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