Rethinking the shaders

Jack Claw was in development between 2006-2008 and was released in the Humble Frozenbyte Bundle for the community to play and build upon.
alt_turo
Posts: 195
Joined: Mon Dec 13, 2010 11:06 am

Rethinking the shaders

Postby alt_turo » Tue May 24, 2011 2:31 pm

Currently shaders are written in low-level asm. As a result they are hard to understand and change. The OpenGL shaders are buggy and reflection doesn't work. Because there's no preprocessor or sane control flow structures they also suffer from combinatorial explosion.

So the whole thing should be rewritten in something sane. First we need to decide what something sane is.

1. Pure Cg
Write shaders in Nvidia Cg and use Cg library to compile and load.

Pros:
-Single set of shaders for all architectures.

Cons:
-Poorly supported on ATI
-Does not allow access to latest advanced features of OpenGL. Not sure about Direct3D 10/11
-Long compile time, not sure if precompilation works.

2. HLSL/Cg mix
Use Microsoft HLSL compiler to compile shaders to bytecode and load without Cg library. Use Cg library on OpenGL. This is the approach used by Trine.

Pros:
-Precompiled shaders on D3D

Cons:
-Cg and HLSL are almost but not exactly similar. There are cases where they differ so in practice we'd need two sets of shaders.
-Still no precompilation for OpenGL
-All other drawbacks of Cg

3. HLSL/GLSL mix.
Use HLSL on D3D and GLSL on OpenGL.

Pros:
-Access to all the latest features of OpenGL 3/4.
-GLSL has better support on ATI than Cg

Cons:
-Two entirely different sets of shaders. Need work to keep them in sync
-OpenGL shaders need to be linked to shader objects OR we need support for separate shader objects extension which is not supported on older cards and probably not MESA drivers.


My personal preference is for the third option. Please discuss.
Turo Lamminen
Alternative Games

User avatar
StevenT
Posts: 29
Joined: Wed Apr 13, 2011 7:23 pm

Re: Rethinking the shaders

Postby StevenT » Tue May 24, 2011 4:24 pm

I like to see HLSL and GLSL for shaders. While it would require basically implementing everything twice, the advantages of better supporting all video cards/platforms plus being able to take advantage of all the features on both OpenGL and Direct3D seems like a good choice.

However the Cg option might be the more sensible option if we have a limited amount of people. It wouldn't be great for those ATI people though.

chrisvarns
Posts: 5
Joined: Tue May 24, 2011 4:25 pm

Re: Rethinking the shaders

Postby chrisvarns » Tue May 24, 2011 4:47 pm

In terms of architectures, it limits you to x86 from what I've read.

Edit: referring to cg.
Last edited by chrisvarns on Tue May 24, 2011 9:36 pm, edited 1 time in total.

User avatar
Urfoex
Posts: 50
Joined: Fri Apr 15, 2011 11:14 am

Re: Rethinking the shaders

Postby Urfoex » Tue May 24, 2011 5:11 pm

I'm not into shader-programming. But as I understood GLSL is the shader-language for OpenGL and HLSL for Direct3D. So if we would stick to OpenGL in the whole project and for all platforms we wouldn't need D3D and so wouldn't need HLSL.
Am I wrong?

What is the benefit of having also D3D and not just OGL?
Wouldn't removing D3D reduce complexity and workload a bit because then there would only be one?
+-----------------------------------------------------------------\
| Debian testing 64Bit on
| * AMD Phenom x4 905e (4x2500Mhz)
| * 6GB Ram
| * AMD/ATI Radeon HD4770 (fglrx)
+-----------------------------------------------------------------/

User avatar
StevenT
Posts: 29
Joined: Wed Apr 13, 2011 7:23 pm

Re: Rethinking the shaders

Postby StevenT » Tue May 24, 2011 5:44 pm

There can be performance boosts on Windows from using Direct3D since graphics drivers (particularly drivers for cheaper, low end cards) on that OS are better optimised for it.

alt_turo
Posts: 195
Joined: Mon Dec 13, 2010 11:06 am

Re: Rethinking the shaders

Postby alt_turo » Tue May 24, 2011 5:49 pm

Urfoex wrote:I'm not into shader-programming. But as I understood GLSL is the shader-language for OpenGL and HLSL for Direct3D.

Correct.

So if we would stick to OpenGL in the whole project and for all platforms we wouldn't need D3D and so wouldn't need HLSL.
Am I wrong?

Also correct.

What is the benefit of having also D3D and not just OGL?
Wouldn't removing D3D reduce complexity and workload a bit because then there would only be one?

A bit but not as much as you would think.

Direct3D and especially OpenGL are rather low-level APIs. In practically all cases you want to build slightly higher-level abstractions on top of them even when you aren't concerned about portability. And if you have an abstraction it's better to make it portable.

Then there's the boneheaded IHVs issue. For ATI OpenGL is a second-class citizen and there are sometimes bugs which can go unfixed for long periods of time. On older Intels OpenGL is almost entirely nonfunctional whereas Direct3D works.

As for our specific case we already have a Direct3D renderer. It's also slightly more functional (reflections, shadow bugs) than the OpenGL version so we should maintain it rather than throw it away.
Turo Lamminen
Alternative Games

chrisvarns
Posts: 5
Joined: Tue May 24, 2011 4:25 pm

Re: Rethinking the shaders

Postby chrisvarns » Tue May 24, 2011 9:34 pm

I agree with your third option alt_turo.

For clarity, at the moment it seems as though the consensus is towards maintaining a GLSL and HLSL shader path for the OpenGL and D3D render pipeline respectively?

dublindan
Posts: 8
Joined: Wed Apr 20, 2011 6:16 am

Re: Rethinking the shaders

Postby dublindan » Thu Jun 02, 2011 10:15 am

I only know GLSL, so my vote is for that. From what I've seen, HLSL isn't all that different from GLSL.

alt_turo
Posts: 195
Joined: Mon Dec 13, 2010 11:06 am

Re: Rethinking the shaders

Postby alt_turo » Fri Jun 03, 2011 12:40 pm

dublindan wrote:I only know GLSL, so my vote is for that. From what I've seen, HLSL isn't all that different from GLSL.

Eh, what? They are vastly different. Let's compare. This is GLSL:

Code: Select all

uniform mat4 world;
uniform mat4 viewProjection;

attribute vec4 position;
attribute vec2 texcoord;

varying vec2 texcoordout;

void main(void)
{
   texcoordout = texcoord;
   gl_Position = viewProjection * world * position;
}


And this is Cg which is almost but not quite identical with HLSL:

Code: Select all

struct MD5VertexOutput
{
   float4 position : POSITION;
   float2 texcoord : TEXCOORD0;
};


MD5VertexOutput main(
  float4 position : POSITION
, float2 texcoord : TEXCOORD0
, uniform mat4x4 world
, uniform mat4x4 viewProjection
)
{
   MD5VertexOutput OUT;

   OUT.position = mul(mul(viewProjection, world), position);
   OUT.texcoord = texcoord;

   return OUT;
}


These two shaders do the exact same thing. See the difference?

Interesting fact: Cg compiler accepts GLSL as input so it would be theoretically possible to use GLSL on Direct3D. I have never tried this however and it's very hacky. So unless we decide to go Cg all the way we pretty much need two separate sets of shaders.
Turo Lamminen
Alternative Games


Return to “Jack Claw Feedback & Development”

Who is online

Users browsing this forum: No registered users and 3 guests