Thread: Phong Pixel Shader for PS1.3

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Phong Pixel Shader for PS1.3

    I'm looking for some direction about implemetning a phong shader for PS1.3. I always get an error that the shader is too complex. Gouraud lighting works fine, but phong gets too complex.

    Even in PS1.3 shouldn't you be able to write such a basic thing as a phong shader? Has anyone done this (real phong, not a vertex-interpolated phong)? Links?

    Im using HLSL.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    It should be fairly straightforward. I imagine you are doing something unnecessary. Post up your effect file and I'll take a look.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    By to complex, do you mean it's yelling at you because the shader is to long? I'm pretty sure there's a way you can force it to load a shader of any size, but you'll take performance hits because of cache misses and such. Just a thought.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Yeah it's too long, removing one or two parts of it makes it compile properly.
    How do you force load a shader then?

    Vertex shader:
    Code:
    struct VS_IN
    {
      float4 Position : POSITION;
      float3 Normal : NORMAL;
      float2 TexturePosition : TEXCOORD;
    };
    
    struct VS_OUT
    {
      float4 Position : POSITION;
      float2 TexturePosition : TEXCOORD0;
      float3 Light : TEXCOORD1;
      float3 Normal : TEXCOORD2;
      float3 View : TEXCOORD3;
    };
    
    float4x4 WorldMatrix;
    float4x4 ViewMatrix;
    float4x4 ProjectionMatrix;
    float4x4 WorldViewProjectionMatrix;
    float4 CameraPosition;
    float4 CameraDirection;
    float4 AmbientColor;
    float4 DiffuseColor;
    float4 SpecularColor;
    
    VS_OUT main(VS_IN In)
    {
      VS_OUT Out;
    
      float3 WorldPosition = normalize(mul(In.Position, WorldMatrix));
    
      Out.Position = mul(In.Position, WorldViewProjectionMatrix);
      Out.TexturePosition = In.TexturePosition;
      Out.Light = (-CameraDirection + 1) / 2;
      Out.Normal = (normalize(mul(In.Normal, WorldMatrix)) + 1) / 2;
      Out.View = (normalize(CameraPosition - WorldPosition) + 1) / 2;
    
      return Out;
    }
    Pixel shader:
    Code:
    struct PS_IN
    {
      float4 Position : POSITION;
      float2 TexturePosition : TEXCOORD0;
      float3 Light : TEXCOORD1;
      float3 Normal : TEXCOORD2;
      float3 View : TEXCOORD3;
    };
    
    struct PS_OUT
    {
      float4 Color : COLOR;
    };
    
    float4x4 WorldMatrix;
    float4x4 ViewMatrix;
    float4x4 ProjectionMatrix;
    float4x4 WorldViewProjectionMatrix;
    float4 CameraPosition;
    float4 CameraDirection;
    float4 AmbientColor;
    float4 DiffuseColor;
    float4 SpecularColor;
    
    sampler2D Texture;
    
    PS_OUT main(PS_IN In)
    {
      PS_OUT Out;
    
      float3 Light = 2 * In.Light - 1;
      float3 Normal = 2 * In.Normal - 1;
      float3 View = 2 * In.View - 1;
      float DiffuseFactor = dot(Light, Normal);
      float3 Reflect = normalize(2 * DiffuseFactor * Normal - Light);
      float4 SpecularFactor = pow(dot(Reflect, View), 8);
    
      Out.Color = AmbientColor + (DiffuseColor * DiffuseFactor) + (SpecularColor * SpecularFactor);
      Out.Color.a = 1.0f;
    
      return Out;
    }
    (doesn't consider the texture... yet...)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    To be honest I'm not sure how to "force load" a shader. I never actually did it, but recall reading about it when playing with CG a while back.

    These shaders aren't even that amazingly large. Maybe your video card is a bit low caliber (no offense). Aside from that, you can take out a lot of those variables you declare. You've got a few matricies you never use (unless they will come around later). I'm also not sure if that will help at all, but worth a shot.

    Would you care to explain why you're doing a lot of (vector + 1) / 2 in your vertex shader, then the inverse in your pixel shader?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    As I said in the topic and the first post this is for pixel shader v1.3 so yes it's very limited (my current gfc card doesn't support higher, plus I want as many as possible to be able to run it).

    The +1 /2 thingy is to prevent clamping of the values (due to the low PS version). Removing them give very odd effects...

    As for the globals yes some are unneccessary and will be removed at a later time. I doubt they reduce so much efficiency though...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    By the silence I assume no one here knows how to do this?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I'm sorry, but I believe you're right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing pixel colour
    By redruby147 in forum C Programming
    Replies: 11
    Last Post: 06-09-2009, 05:28 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. Shader hell
    By VirtualAce in forum Game Programming
    Replies: 4
    Last Post: 08-18-2006, 09:21 PM
  4. Replies: 6
    Last Post: 11-12-2005, 11:57 AM
  5. Pixel Shaders.
    By Cheeze-It in forum Game Programming
    Replies: 1
    Last Post: 05-21-2002, 01:16 AM