• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Why can't i use tex2D in loop with HLSL?

4 posts in this topic


When i want to use tex2D in loop in PixelShader, the compiler outputs errors:

1.warning X3553: can't use gradient instructions in loops with break, forcing loop to unroll.
2.error X3511: Unable to unroll loop, loop does not appear to terminate in a timely manner (1024 iterations).

I can't understand it.

Share this post

Link to post
Share on other sites
with googling around i found 2 answers:

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]OK, this is a bit difficult to explain. First, the reason why the second one works, is because it is automatically unrolled. You loop a constant amount of iteration, so the compiler can silently unroll without side effects.[/left][/size][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]Now, the reason the first doesn't work as expected, is due to the fact that derivatives (as used by a gradient instruction, such as tex2D) are undefined within a conditional statement (such as the one implicitly used by the loop).[/left][/size][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]You can solve this by either not using derivatives at all (ie. supplying a constant LOD through tex2DLod rather than the tex2D) or by manually computing the derivatives outside of the loop, and supplying them explicitly. If you only want to blur a screen aligned rectangle, then the best method is the former one.[/left][/size][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][/quote][/left][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]resource: [/left][/size][/font][/color][url="http://www.gamedev.net/topic/526047-bizzare-shader-error/"]http://www.gamedev.net/topic/526047-bizzare-shader-error/[/url]


The compiler needs to prove that the loop will terminate within 1024
iterations to target the ps_2_0 profile. Since your index value comes from
a global, no range information can be infered beyond the 32-bit int type.
You can force this by doing something like:

int x = gNum % 1023;
for( int i = 0; i < x; i++ ) ...
resource: [url="http://www.ureader.com/msg/146533.aspx"]http://www.ureader.com/msg/146533.aspx[/url]

hope this helps :)

Share this post

Link to post
Share on other sites
float4 ps_POM( float2 iTex : TEXCOORD0,
float3 iLightTS : TEXCOORD1,
float3 iViewTS : TEXCOORD2,
float2 iDisplaceTS : TEXCOORD3,
float3 iViewWS : TEXCOORD4,
float3 iNormalWS : TEXCOORD5 ) : COLOR
iLightTS = normalize( iLightTS );
iViewTS = normalize( iViewTS );
iViewWS = normalize( iViewWS );
iNormalWS = normalize( iNormalWS );
float2 BigTexCoord = iTex * TEXTURE_DIM;
float2 ddxBig, ddyBig, ddxSmall, ddySmall;
ddxBig = ddx( BigTexCoord );
ddyBig = ddy( BigTexCoord );
ddxSmall = ddx( iTex );
ddySmall = ddy( iTex );
float2 Delta = ddxBig * ddxBig + ddyBig * ddyBig;
float fMaxTexDelta = max( Delta.x, Delta.y );
float fMipLevel = max( 0, 0.5f * log2( fMaxTexDelta ) );
float fOcclusionShadow = 1;
//if the mesh is close enough to view, implement POM
float fNumSamples = lerp( MAX_SAMPLES, MIN_SAMPLES, dot( iNormalWS, iViewWS) );
float fStepSize = 1.0f / fNumSamples;
float2 fStepDisplace = fStepSize * iDisplaceTS;
float fCurrentSample = 0;
float fCurrentBound = 1.0f;
float fCurrentHeight = 0;
float fPrevHeight = tex2D( g_samNMH, iTex ).a;
float fPrevBound = 1.0f;
while( fCurrentSample < fNumSamples )
iTex -= fStepDisplace;
fCurrentBound -= fStepSize;
[b][color=#ff0000]fCurrentHeight = tex2D( g_samNMH, iTex ).a;[/color][/b]
if( fCurrentBound < fCurrentHeight )
fCurrentSample = fNumSamples + 1;
fPrevHeight = fCurrentHeight;
fPrevBound = fCurrentBound;
float fHeightDelta1 = fPrevBound - fPrevHeight;
float fHeightDelta2 = fCurrentHeight - fCurrentBound;
float fIntersectionStepFraction = fHeightDelta2 / ( fHeightDelta1 + fHeightDelta2 );
iTex += fIntersectionStepFraction * fStepDisplace;

float2 LightTS = iLightTS.xy * HEIGHT_SCALING;
float sh0 = tex2Dgrad( g_samNMH, iTex, ddxSmall, ddySmall ).a;
float shA = ( tex2Dgrad( g_samNMH, iTex + LightTS * 0.88f, ddxSmall, ddySmall ).a - sh0 - 0.88 ) *
float sh9 = ( tex2Dgrad( g_samNMH, iTex + LightTS * 0.77f, ddxSmall, ddySmall ).a - sh0 - 0.77 ) *
float sh8 = ( tex2Dgrad( g_samNMH, iTex + LightTS * 0.66f, ddxSmall, ddySmall ).a - sh0 - 0.66 ) *
float sh7 = ( tex2Dgrad( g_samNMH, iTex + LightTS * 0.55f, ddxSmall, ddySmall ).a - sh0 - 0.55 ) *
float sh6 = ( tex2Dgrad( g_samNMH, iTex + LightTS * 0.44f, ddxSmall, ddySmall ).a - sh0 - 0.44 ) *
float sh5 = ( tex2Dgrad( g_samNMH, iTex + LightTS * 0.33f, ddxSmall, ddySmall ).a - sh0 - 0.33 ) *
float sh4 = ( tex2Dgrad( g_samNMH, iTex + LightTS * 0.22f, ddxSmall, ddySmall ).a - sh0 - 0.22 ) *

fOcclusionShadow = 1 - max( max( max( max( max( max( shA, sh9 ), sh8 ), sh7), sh6 ), sh5), sh4 );

fOcclusionShadow = fOcclusionShadow * 0.3 + 0.4;

//fOcclusionShadow = 1;
return GetIlluminance( iTex, iLightTS, iViewTS, fOcclusionShadow );

The red bold line is the error line.

Share this post

Link to post
Share on other sites
Thank you, Ghosrath.

I get it.
GPUs shade a 'quad' of 4 pixels at a time and the gradients used for texture fetches come from the finite differences calculated from adjacent pairs of pixels. This is how the GPU is able to generate partial derivatives even for arbitrary expressions in the pixel shader. The tex2Dgrad function can be useful if you can calculate more accurate analytical derivatives for the values you are passing in as texture coordinates.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0