FX Composer issues with cgfx

Started by
4 comments, last by amirabiri 11 years, 11 months ago
I got a basic CgFX shader working in FX composer that has transformation and texturing, but I'm having some issues.. If I render it in OpenGL mode It renders fine in the perspective view, but in the material view the sample sphere doesnt show, the whole thing is black..

If I change to DirectX9 or 10 only a red wireframe mesh shows up, both in the material preview and in the perspective view.. CG is cross platform, but what about CgFX? Do I need a .fx file for rendering cg shaders in directx? If so, would I need that in my application aswell?


void mainVS(float4 position : POSITION,
float3 normal : NORMAL,
float2 texCoord : TEXCOORD0,
out float4 oPosition : POSITION,
out float2 oTexCoord : TEXCOORD0,
uniform float4x4 viewProjection,
uniform float4x4 world)
{
oPosition = mul(world, position);
oPosition = mul(viewProjection, oPosition);
oTexCoord = texCoord;
}
void mainFS(float2 texCoord : TEXCOORD0,
out float4 color : COLOR,
uniform sampler2D colorSampler)
{
color = tex2D(colorSampler, texCoord);
}

float4x4 world : World;
float4x4 viewProjection : ViewProjection;

texture colorTexture <
string ResourceName = "default_color.dds";
string UIName = "Diffuse Texture";
string ResourceType = "2D";
>;
sampler2D colorSampler = sampler_state {
Texture = <colorTexture>;
generateMipMap = true;
MinFilter = LinearMipMapLinear;
MagFilter = Linear;
WrapS = Repeat;
WrapT = Repeat;
};

technique basic_textured_dx_40
{
pass
{
FragmentProgram = compile ps_2_0 mainFS(colorSampler);
VertexProgram = compile vs_2_0 mainVS(viewProjection, world);
DepthTestEnable = true;
DepthMask = true;
}
}

technique basic_textured_ogl_40
{
pass
{
FragmentProgram = compile fp40 mainFS(colorSampler);
VertexProgram = compile vp40 mainVS(viewProjection, world);
DepthTestEnable = true;
DepthMask = true;
}
}
Advertisement
I've just come across the exact same problem myself... did you solve it?
Maybe this is because DX and OpenGL use different coordinate systems? If this is the case try to change [color=#000000][size=2]


[color=#000000][size=2]

oPosition

[color=#666600][size=2]

=

[color=#000000][size=2]

mul

[color=#666600][size=2]

(

[color=#000000][size=2]

world

[color=#666600][size=2]

,

[color=#000000][size=2]

position

[color=#666600][size=2]

);


[color=#000000][size=2]

oPosition

[color=#666600][size=2]

=

[color=#000000][size=2]

mul

[color=#666600][size=2]

(

[color=#000000][size=2]

viewProjection

[color=#666600][size=2]

,

[color=#000000][size=2]

oPosition

[color=#666600][size=2]

);





to


[color=#000000][size=2]

oPosition

[color=#666600][size=2]

=

[color=#000000][size=2]

mul

[color=#666600][size=2]

(

[color=#000000][size=2]

position

[color=#666600][size=2]

,

[color=#000000][size=2]

world

[color=#666600][size=2]

);


[color=#000000][size=2]

oPosition

[color=#666600][size=2]

=

[color=#000000][size=2]

mul

[color=#666600][size=2]

(

[color=#000000][size=2]

oPosition

[color=#666600][size=2]

,

[color=#000000][size=2]

viewProjection

[color=#666600][size=2]

);


Sry for that... just tried to copy the code from the shader code.
In short:

change the order of the parameters in the calls to "mul"
I posted the question on Stack Overflow and it doesn't seem like anyone has an answer for it there either.

I've done a lot of investigations and tried many different things, and the bottom line I think is that our hardware doesn't support certain profiles.

For example for Direct3D 10 you need shader model 4 it seems. However when I tried changing the profiles of the effect from vs_3_0/ps_3_0 to vs_4_0/ps_4_0, I got an error that my hardware doesn't support it.

I also found a forum thread on nvidia's site where a member of nvidia's staff mentioned something along the lines of FX Composer not supporting cgfx with Direct3D. I've also come across multiple forum posts all over the web (very old mostly) talking about cg dead/dying/will die, nvidia not supporting it and so on.

Also note the FX Composer FAQ, scroll down and you'll find

[indent=1]

Q: "Does FX Composer work with Cg or GLSL?"


[indent=1]

A: "We are always evaluating opportunites to provide powerful tools for developers, but have no plans to add support for other languages to FX Composer. FX Composer was built to support the large number of developers using HLSL in their applications."



So basically, I think the bottom line is this:

  • Some of the problems we are seeing are related to the hardware capabilities (or lack of them).
  • Some of the problems are related to restrictions of FX Composer.

For me since my objective is purely to learn shaders in general, the solution was to stick to HLSL and Direct3D 9. I will verify my suspicion about DirectX 10 when I have access to a newer card on another machine that I have.

There is also an interesting (old) forum thread here on gamedev: http://www.gamedev.net/topic/281349-hlsl-vs-cg-any-significant-difference/
OK I take it back in regards to DirectX 10, I managed to get it to work on the HLSL/.fx side (not Cg/.cgfx).

It seems that the .fx templates that FX Composer generates by default only provide a DirectX 9 technique. With DirectX 10, you need to use the technique10 keyword instead of technique. The technique10 keyword has different syntax. Where previously it looked like this:

technique technique1 {
pass p0 {
VertexShader = compile vs_3_0 mainVS();
PixelShader = compile ps_3_0 mainPS();
}
}


With DirectX 10 it should look like this:

technique10 technique_dx10 {
pass p0 {
SetVertexShader( CompileShader( vs_4_0, mainVS() ) );
SetPixelShader( CompileShader( ps_4_0, mainPS() ) );
}
}


With this your effect will work both on DirectX 9 and DirectX 10. It's a shame nVidia didn't make FX Composer generate both to begin with.

You can find the various bits and pieces of the technique10 syntax online (mainly on microsoft's website).

This topic is closed to new replies.

Advertisement