• 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
viX0026

Some Problem when trying to implement ParallaxMapping

2 posts in this topic

Hi, guys, I'm trying to implement parallax mapping in hlsl, I use the algorithm in "ParallaxOcclusionMapping" in D3D Sample, But I always get a complie error X3511: Unable to unroll loop, loop does not appear to terminate in a timely manner (1024 iterations). I have tried on RenderMonkey and D3D9, neither of them works. Here is my code(simplified, I just implemented the height profile-ray intersection):

struct PS_INPUT
{
    float2 texCoord    : TEXCOORD0;
    float3 vLightDirTS : TEXCOORD1;
    float3 vViewDirTS  : TEXCOORD2;
    float3 vNormalWS   : TEXCOORD3;
    float3 vViewDirWS  : TEXCOORD4;
};


float4 CalculateLighting(float3 vViewTS, float3 vLightTS, float2 texCoord)
{
    float3 vNormalTS = normalize(tex2D(tNormalHeightMap, texCoord).xyz * 2 - 1);
    float4 cBaseColor = tex2D(tBase, texCoord);
    float4 cDiffuse = saturate(dot(vLightTS, vNormalTS)) * g_LightDiffuse;
    float4 cFinal = (g_LightAmbient + cDiffuse) * cBaseColor;
    return cFinal;
}

float4 PM_PS( VS_OUTPUT In ) : COLOR0
{ 
    float2 vOffsetTS = float2(In.vViewDirTS.x, In.vViewDirTS.y) / In.vViewDirTS.z;
    float3 vViewTS   = normalize(In.vViewDirTS);   // view direction in tangent space
    float3 vViewWS   = normalize(In.vViewDirWS);   // view direction in world space
    float3 vLightTS  = normalize(In.vLightDirTS);  // light direction in tangent space
    float3 vNormalWS = normalize(In.vNormalWS);    // normal in world space

    vOffsetTS *= g_fHeightMapScale;
    int NumSteps = (int)lerp(g_MaxSample, g_MinSample, max(dot(vNormalWS, vViewWS), 0.0f));
    float fStepSize = 1.0f / (float)NumSteps;
    float fCurrHeight = 0.0f;
    float fPrevHeight = 1.0f;
    float fCurrBound  = 1.0f;
    float fHeight = 0.0f;

    int nStepIndex = 0;
    float2 vTexOffsetPerStep = vOffsetTS * fStepSize;
    float2 vCurrTexCoord = In.texCoord;

	while(nStepIndex < NumSteps)   // height profile-ray intersection
	{
		vCurrTexCoord -= vTexOffsetPerStep;
		fCurrHeight = tex2D(tNormalHeightMap, vCurrTexCoord);
		fCurrBound -= fStepSize;
		if(fCurrHeight > fCurrHeight)   // we have found the intersection point
		{
			float delta1 = fCurrHeight - fCurrBound;
			float delta2 = fCurrBound + fStepSize - fPrevHeight;
			float alpha = delta1 / (delta1 + delta2);
			fHeight = 1.0f - lerp(fCurrHeight, fPrevHeight, alpha);  // calculate height approximately
			nStepIndex = NumSteps + 1;
		}
		else
		{
			fPrevHeight = fCurrHeight;
			nStepIndex++;
		}
	}

    float2 vParallaxOffset = vOffsetTS * fHeight;
    float2 texCoord = In.texCoord - vParallaxOffset;
   
    float4 cFinalColor = CalculateLighting(In.vViewDirTS, In.vLightDirTS, texCoord);
    return cFinalColor;
}

I compiled the "ParallaxOcclusionMapping" in D3D Sample, everything is OK. But mine couldn't work. I can't figure it out how to solve this problem.

Edited by viX0026
1

Share this post


Link to post
Share on other sites

Next time please post the entire shader, since what you posted doesn't include the textures and  constants that you use.

Anyway the problem is that you're using tex2D inside a loop, which isn't allowed since the hardware can't compute gradients for mipmapping inside of dynamic flow control (since neighboring pixels in a 2x2 quad might take different paths at a branch). You should compute the gradients outside the loop and pass them into tex2Dgrad, like the sample does. The reason the compiler isn't giving the right error message is because it's trying to be too clever for its own good. When you try to use gradient operations inside a branch or loop, it will try to either pull the operation out of the loop/branch or flatten/unroll the branch. In your particular case it tried to unroll, but it failed to do it since the loop is meant to be dynamic. If you force the compiler to try to use a dynamic loop instead of unrolling it by putting [loop] above the while loop, it gives the correct error message.

BTW once you fix that you'll also want to change "if(fCurrHeight > fCurrHeight)" to "if(fCurrHeight > fCurrBound)", otherwise it's never going to take that branch and the whole thing will get optimized away.

Edited by MJP
2

Share this post


Link to post
Share on other sites

Next time please post the entire shader, since what you posted doesn't include the textures and  constants that you use.

Anyway the problem is that you're using tex2D inside a loop, which isn't allowed since the hardware can't compute gradients for mipmapping inside of dynamic flow control (since neighboring pixels in a 2x2 quad might take different paths at a branch). You should compute the gradients outside the loop and pass them into tex2Dgrad, like the sample does. The reason the compiler isn't giving the right error message is because it's trying to be too clever for its own good. When you try to use gradient operations inside a branch or loop, it will try to either pull the operation out of the loop/branch or flatten/unroll the branch. In your particular case it tried to unroll, but it failed to do it since the loop is meant to be dynamic. If you force the compiler to try to use a dynamic loop instead of unrolling it by putting [loop] above the while loop, it gives the correct error message.

BTW once you fix that you'll also want to change "if(fCurrHeight > fCurrHeight)" to "if(fCurrHeight > fCurrBound)", otherwise it's never going to take that branch and the whole thing will get optimized away.

 

Thanks, that works ! But I'm a little bit confused about gradient operation in dynamic flow. I read the "Flow Control Limitations" in MSDN. This is how it describes:

 

"When flow control is present in a shader, the result of a gradient calculation requested inside a given branch path is ambiguous when adjacent pixels may execute separate flow control paths. Therefore, it is deemed illegal to use any pixel shader operation that requests a gradient calculation to occur at a location that is inside a flow control construct which could vary across pixels for a given primitive being rasterized."

 

I couldn't imagine how the error occurs when there are some gradient operations inside a dynamic flow. Can you show me some examples about this ? Thanks again !

Edited by viX0026
1

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