Archived

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

superpig

Cg/Shader concerns

Recommended Posts

''lo all, I was just pondering some shader stuff... Is speed/length of a shader program a massive concern? I see some fairly long programs in the Cg toolkit... Is there any way of getting world-space XYZ coordinates through to a pixel shader? Given that the standard coordinates sent to the pixel shader are screen coordinates, I figure you''d either have to unproject them (eek) or somehow pass them through as ''extra parameters'' in the fragment structure. With that second thing, it''s like this - say I''m trying to apply an effect, on a per-pixel basis, depending on distance from a point in 3D space. Like say a texture blend - I''ve got an arbitrary object, and for each pixel of it I test to see if that pixel is within a given sphere (distance from a point). If it''s within, I use one texture; if not, I use the other. How''s that doable? Superpig - saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.

Share this post


Link to post
Share on other sites
Unfortunately, I''ve only programmed in assembler shaders, but I can hopefully give some answers.

quote:
Original post by superpig

Is there any way of getting world-space XYZ coordinates through to a pixel shader? Given that the standard coordinates sent to the pixel shader are screen coordinates, I figure you''d either have to unproject them (eek) or somehow pass them through as ''extra parameters'' in the fragment structure.



Send the world-space coordinates of the vertices from your vertex shader as texture coordinates. They will be interpolated so you''ll have the right coordinates in the pixel shader. Here''s a sample vertex shader:


vs_1_1

dcl_position v0 ;model-space position of vertex
;c0-c3 contains the world-space transformation matrix, transposed
;c4-c7 contains the world+view+projection transformation, transposed

m4x4 oT0, v0, c0
m4x4 oPos, v0, c4


quote:

With that second thing, it''s like this - say I''m trying to apply an effect, on a per-pixel basis, depending on distance from a point in 3D space. Like say a texture blend - I''ve got an arbitrary object, and for each pixel of it I test to see if that pixel is within a given sphere (distance from a point). If it''s within, I use one texture; if not, I use the other. How''s that doable?



Again, I don''t know about CG, but on an assembler pixel shader with the cmp instruction this is pretty trivial. Here''s some sample code:


ps_2_0

dcl_2d s0 ;texture 1
dcl_2d s1 ;texture 2
dcl t0.xyzw ;world-space coordinates
dcl t1.xy ;the actual texture coordinates for sampling

;c0.x - distance threshold
;c1 - the point
sub r0, t0, c1
dp3 r0, r0, r0
rcp r0.x, r0.w ;r0.x now contains the distance
texld r1, t1, s0 ;sample to r1 from first texture
texld r2, t1, s1 ;sample to r2 from second texture
sub r0.x, r0.x, c0.x
cmp r0, r0.x, r1, r2
;if (distance - threshold) > 0, choose the first texture, otherwise, choose the second one.
mov oC0, r0


I hope this helps.

Share this post


Link to post
Share on other sites
quote:
Original post by Painless
Send the world-space coordinates of the vertices from your vertex shader as texture coordinates. They will be interpolated so you''ll have the right coordinates in the pixel shader.


GENIOUS! I don''t even need a vertex shader that way - I can just set up all the 3d texture coordinates ahead of time. Dude, you rock

About the first question, though - how long would a shader need to be before speed started becoming an issue?

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.

Share this post


Link to post
Share on other sites