• 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

Simple point light fragment shader very slow

7 posts in this topic

I have a deferred rendering system, and I am testing at full 1920x1080 and noticing that a particular fragment shader is causing significant slowdown (they all seem oddly slow, but this one seems the most significant). My shader is based on the OGL-Dev tutorials for deferred rendering.

The purpose of this shader is to calculate the added light within a light's area of effect. There are 3 large point lights in my scene, that cover everything in view. So essentially this shader is executing 3 times for every pixel on the screen.

I measured the difference in frame time between running this shader, and then replacing the shader's main function with a simple "FragColor = vec4(1,0,0,1); //red". The total difference in time (for my engine to render an entire frame) is 4-5 milliseconds. If I'm shooting for 60 fps, that's already 25+% of my full frame render time, which seems kind of crazy (I removed shadow calculation and blur before taking these measurements).

Here is my shader:

#version 420

layout (location = 0) out vec4 FragColor;

struct BaseLight
    vec3  Color;
    float AmbientIntensity;
    float DiffuseIntensity;

struct Attenuation
    float Constant;
    float Linear;
    float Exp;

struct PointLight
    BaseLight Base;
    vec3 Position;
    Attenuation Atten;

uniform sampler2D gPositionMap;
uniform sampler2D gColorMap;
uniform sampler2D gNormalMap;
uniform sampler2D gShadowMap;
uniform PointLight gPointLight; 
uniform vec3 gEyeWorldPos;
uniform float gMatSpecularIntensity;
uniform float gSpecularPower;
uniform int gLightType;
uniform vec2 gScreenSize;

uniform mat4 gWVP;
uniform mat4 gVP;
uniform mat4 gView;

vec4 CalcLightInternal(BaseLight Light,
					   vec3 LightDirection,
					   vec3 WorldPos,
					   vec3 Normal, float shadowFactor )
    vec4 AmbientColor = vec4(Light.Color, 1.0f) * Light.AmbientIntensity;
    float DiffuseFactor = dot(Normal, -LightDirection);

	float shadowFactorOrig = shadowFactor;

    vec4 DiffuseColor  = vec4(0, 0, 0, 0);
    vec4 SpecularColor = vec4(0, 0, 0, 0);

    if (DiffuseFactor > 0) {

		DiffuseColor = vec4(0,1,0,1);
        DiffuseColor =  vec4(Light.Color, 1.0f)  * DiffuseFactor ;

        vec3 VertexToEye = normalize(gEyeWorldPos - WorldPos);
        vec3 LightReflect = normalize(reflect(LightDirection, Normal));
        float SpecularFactor = dot(VertexToEye, LightReflect);
        SpecularFactor = pow(SpecularFactor, gSpecularPower);
        if (SpecularFactor > 0) {
            SpecularColor = vec4(Light.Color, 1.0f) * gMatSpecularIntensity * SpecularFactor;

	return   (AmbientColor + shadowFactor*(DiffuseColor+SpecularColor));


vec4 CalcPointLight(vec3 WorldPos, vec3 Normal)
    vec3 LightDirection = WorldPos - gPointLight.Position;
    float Distance = length(LightDirection);
    LightDirection = normalize(LightDirection);

    vec4 Color = CalcLightInternal(gPointLight.Base, LightDirection, WorldPos, Normal,1.0f);

    float Attenuation =  gPointLight.Atten.Constant +
                         gPointLight.Atten.Linear * Distance +
                         gPointLight.Atten.Exp * Distance * Distance;

    Attenuation = min(1.0, Attenuation);

    return Color / Attenuation;

vec2 CalcTexCoord()
    return gl_FragCoord.xy / gScreenSize;

void main()
    vec2 TexCoord = CalcTexCoord();
	vec3 WorldPos = texture(gPositionMap, TexCoord).xyz;
	vec3 Color = texture(gColorMap, TexCoord).xyz;
	vec3 Normal = texture(gNormalMap, TexCoord).xyz;
	Normal = normalize(Normal);
    FragColor =  CalcPointLight(WorldPos, Normal); 


I am on a fairly fast HP machine with plenty of memory and a NVIDIA Quadro K1100M graphics card. Also, I already checked the VSync to make sure my render times are not forcing my render times to be multiples of 16ms. 

This shader does not contain an inordinate amount of looping or branching. It is executing about 3 times per pixel. Should this really be adding 4-5 milliseconds to my render times? 

Any ideas for what could be causing this would be much appreciated.



Share this post

Link to post
Share on other sites

Aside from what Hodgman said, your branches are doing more harm than good:

if( DiffuseFactor > 0 )
   if( SpecularFactor > 0 )

Just do DiffuseFactor = max( 0, DiffuseFactor ); and same for the SpecularFactor.


Also note those 4ms do not necessarily have to scale linearly with the number of objects. The number of covered pixels affects a lot; and Early Z testing can also amortize the cost a lot.


Share this post

Link to post
Share on other sites

I tried replacing the conditionals with the "max()" function. Marginal improvement, maybe 1 ms less frame time. Something, but doesn't quite give the full story I think.

I double checked, but no, no Intel graphics hardware on this machine. 

I did notice this  https://www.techpowerup.com/gpudb/2430/quadro-k1100m which states:

"We recommend the NVIDIA Quadro K1100M for gaming with highest details at resolutions up to, and including, 1024x768."

I am wondering if this card just can't handle those resolutions... still seems unacceptably slow


Share this post

Link to post
Share on other sites

Thanks for the analysis. That's a relief that I can (at least partially) blame my hardware!

I guess this calls into question whether or not I want to optimize for this machine or aim for a stronger graphics card. I am shooting for a game that doesn't have to be on a souped up gaming rig, but also doesn't need to run on a dinosaur... something mid range is what I'd like.

I could potentially switch over to my other laptop, which has this card: https://www.techpowerup.com/gpudb/1490/geforce-gt-525m

Could I expect much more out of this one? I see its production status is "End of Life'.

Maybe its just time for a hardware upgrade... 


Share this post

Link to post
Share on other sites

Posted (edited)

You can also just aim for 30Hz 720p or similarly low resolutions on those older cards, and leave the 60Hz 1080p goal to newer hardware.


I like that! I get way way better performance at 720p and I think 30hz should be very achievable. 

I consider my issue solved. Very much appreciate the help!

Edited by gearifysoftware

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