Archived

This topic is now archived and is closed to further replies.

ahzab33

DX8.1 shaders vs. HLSL

Recommended Posts

ahzab33    122
Hi. I recently downloaded some HLSL tutorials on my laptop which has a Radeon 9000 GPU. Two of them ran fine in HW mode while the rest only ran fine in SW mode. They ran fine on my Radeon 9500 system though. The tutorials that ran fine demonstratedAmbient & Diffuse lighting. The others demonstrated Phong lighting, Specular lighting, Diffuse + Specular Bump Mappping, etc. After examining the code, I realized that the reason the more complex lighting demos were''nt working was because they surpassed the DX8.1 instruction limit. My questions are: Is it possible to use HLSL efficiently on DX8.1 cards or is it exclusive to DX9? Is it possible to implement Phong lighting using PS_1_x in HLSL without surpassing the instruction limit?

Share this post


Link to post
Share on other sites
poly-gone    148
The other HLSL examples probably used ps 2.0 and hence didn''t work on your 9000 VPU (think ATI calls them VPU).

Phong lighting can be done with ps 1.x but on ps 2.0 is much easier and shorter. But if you want to stick with a high-level language, use Cg instead of HLSL. You can use that with DX 8.1 as well.

Share this post


Link to post
Share on other sites
Muhammad Haggag    1358
quote:
After examining the code, I realized that the reason the more complex lighting demos were''nt working was because they surpassed the DX8.1 instruction limit.

It''s not accurate to say "DX8.1 instruction limit", it''s the shader models that have different instruction limits.

quote:
The other HLSL examples probably used ps 2.0 and hence didn''t work on your 9000 VPU (think ATI calls them VPU).

Yes, they do call them that.

quote:
Phong lighting can be done with ps 1.x but on ps 2.0 is much easier and shorter

All the phong lighting implementations I''ve seen are so simple, and are easily done in ps1.1
The problem with ps1.1 is that you can''t combine phong lighting with anything else (as far as I recall: No dependent texture reads, 8 instructions)

Share this post


Link to post
Share on other sites
EvilDecl81    360
Its possible to target ps_1_x shaders with HLSL, however due to the limits of these models only simple shaders will work. ps_1_4 is much better then ps_1_1-3, but it is still somewhat limited (the SDK update about to be realeased does a better job with 1_4 so you might want to check it out).

If the shaders are simple, they should compile. Otherwise, they probablly will not.

Share this post


Link to post
Share on other sites
ahzab33    122
All of that makes great sense, but help me understand this. All of the code compiled just fine. It was when I ran the executable that I noticed the problems. For example, the specular lighting shader makes the teapot look all white in HW mode, but in REF mode it looks fine. I looked at the .fx file for the shader and I noticed that it said:

// -------------------------------------------------------------
// Diffuse and specular shader using Phong specular
//
// Copyright (c) 2003 Wolfgang F. Engel (wolf@direct3d.net)
// All rights reserved.
// -------------------------------------------------------------

// -------------------------------------------------------------
// variables that are provided by the application
// -------------------------------------------------------------
float4x4 matWorldViewProj;
float4x4 matWorld;
float4 vecLightDir;
float4 vecEye;

// -------------------------------------------------------------
// Output channels
// -------------------------------------------------------------
struct VS_OUTPUT
{
float4 Pos : POSITION;
float3 Light : TEXCOORD0;
float3 Norm : TEXCOORD1;
float3 View : TEXCOORD2;
};

// -------------------------------------------------------------
// vertex shader function (input channels)
// -------------------------------------------------------------
VS_OUTPUT VS(float4 Pos : POSITION, float3 Normal : NORMAL)
{
VS_OUTPUT Out = (VS_OUTPUT)0;
Out.Pos = mul(Pos, matWorldViewProj); // transform Position
Out.Light = vecLightDir; // L
// float3 PosWorld = normalize(mul(Pos, matWorld));
Out.View = vecEye; // - PosWorld; // V
Out.Norm = mul(Normal, matWorld); // N

return Out;
}

// -------------------------------------------------------------
// Pixel Shader (input channels):output channel
// -------------------------------------------------------------
float4 PS(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1,
float3 View : TEXCOORD2) : COLOR
{
float4 diffuse = { 1.0f, 0.0f, 0.0f, 1.0f};
float4 ambient = { 0.1f, 0.0f, 0.0f, 1.0f};

float3 Normal = normalize(Norm);
float3 LightDir = normalize(Light);
float3 ViewDir = normalize(View);
float4 diff = saturate(dot(Normal, LightDir)); // diffuse component

// R = 2 * (N.L) * N – L
float3 Reflect = normalize(2 * diff * Normal - LightDir);
float4 specular = pow(saturate(dot(Reflect, ViewDir)), 8); // R.V^n

// I = Acolor + Dcolor * N.L + (R.V)n
return ambient + diffuse * diff + specular;
}


// -------------------------------------------------------------
//
// -------------------------------------------------------------
technique TShader
{
pass P0
{
// compile shaders
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PS();
}
}

I noticed that the PixelShader was being compiled as for PS_2_0. I changed it to PS_1_4 and it compiled, but crashed when ran. You would think that this code could be compiled into PS_1_4. It doesn''t look too complicated.

Somebody said to use Cg instead of HLSL, but i''ve read that Cg and HLSL are actually the same language and that it was co-developed by Microsoft & nVidia. The only difference is that HLSL is compiled only for Microsoft profiles while Cg can be compiled for both Microsoft profiles & OpenGL profiles.

By the way, if any of you wants to see the code, it can be downloaded at http://www.gamasutra.com/features/20030418/engel_01.shtml

Thanks for all of your advice

Share this post


Link to post
Share on other sites
EvilDecl81    360
It does not crash on the SDK update,(which should be out in a few weeks).

However, you cannot do normilization on these models. Which is why this won''t compile (I beleive you got a line # no?). I''d suggest making some kind of dependent texture read. Normilization requirets recipcal square root. THis doesn''t exist in these shader models.

Additionally, you cannot do a pow on 8.1 targets. The only reason why this works is HLSL expands it out into muls.








Share this post


Link to post
Share on other sites