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

Render to Texture within HLSL

2 posts in this topic

Hello,

 

I've been looking all over the web for the past week to discover whether it's possible to render the result of a pass to a texture to use it in a second pass. Especially this part creates the impression it should be possible:

http://books.google.nl/books?id=5FAWBK9g-wAC&pg=PA97&lpg=PA97&dq=hlsl+rendercolortarget&source=bl&ots=UWTa8puvst&sig=J5Ov8F357RsccYhh_ftRT4E7sZw&hl=nl&sa=X&ei=bjTEUcnkLoqQ7Aad3IDIAQ&ved=0CC8Q6AEwAA%20-#v=onepage&q=hlsl%20rendercolortarget&f=false

The idea of setting the result of a pass to a texture to use it again is very interesting for a lot of things (in this case I need it for gaussian blur), but I can't seem to get it to work.

 

The effect file I'm using is for post-processing only and has two textures: ScreenContent and HorBlurTexture.

Furthermore I used the following passes:

pass HorBlurPass  <
    string Script = "RenderColorTarget0=HorBlurTexture;"
    				"Draw=Buffer;";
 > 
{
    PixelShader = compile ps_2_0 GaussianShaderHorizontal();
}
pass VerBlurPass  <
    string Script = "RenderColorTarget0=;"
				"Draw=Buffer;";
  > 
{
    PixelShader = compile ps_2_0 GaussianShaderVertical();
}

There are two theories I have at the moment:

Because it's Post-processing all textures are automatically set to the given Screen (which was rendered earlier)

RenderColorTarget does not work as I think it does.

 

Can anybody help me?

 

Thanks in advance,

0

Share this post


Link to post
Share on other sites

I'm using effect annotations to declaratively define render target textures in my shaders. The engine code parses these annotations when loading the effects and creates textures dynamically that are feed back to the shader through pEffect->SetTexture().

 

If you look at my blur filter you will find these annotations as RENDERCOLORTARGET in the code below.

texture g_inputTexture;  // Input texture to the filter

float2 outputTextureSize : VIEWPORTPIXELSIZE;

texture g_pass2Texture : RENDERCOLORTARGET
<
	float2 ViewportRatio = { 1.0, 1.0 };
>;

texture g_pass3Texture : RENDERCOLORTARGET
<
	float2 ViewportRatio = { 1.0, 1.0 };
>;

float Spread <
    string UIName = "Spread";
> = 0.1f;

// Vertex shader /////////////////////////////////////////

struct AppData {
	float3 Position		: POSITION;
	float2 UV			: TEXCOORD0;
};

struct VertData {
	float4 HPosition	: POSITION;
	float2 UV			: TEXCOORD0;
	float2 PixelDelta	: TEXCOORD1;
};

VertData VS_Common(AppData IN)
{
	VertData OUT;

	OUT.UV = IN.UV.xy;

	OUT.HPosition = float4(IN.Position.x, IN.Position.y, 0, 1.0);

	float dx = 1.0 / outputTextureSize.x;
	float dy = 1.0 / outputTextureSize.y;
    OUT.PixelDelta = float2(dx, dy);

	return OUT;
}

// Pixel shader /////////////////////////////////////////

sampler Sampler1 = sampler_state
{
	Texture   = (g_inputTexture);
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	BorderColor = float4(0, 0, 0, 0);
	AddressU  = CLAMP;
	AddressV  = CLAMP;
};

sampler Sampler2 = sampler_state
{
	Texture   = (g_pass2Texture);
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	BorderColor = float4(0, 0, 0, 0);
	AddressU  = CLAMP;
	AddressV  = CLAMP;
};

sampler Sampler3 = sampler_state
{
	Texture   = (g_pass3Texture);
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	BorderColor = float4(0, 0, 0, 0);
	AddressU  = CLAMP;
	AddressV  = CLAMP;
};

static const int g_cKernelSize = 13;

static const float BlurWeights[g_cKernelSize] =
{
	0.002216,
	0.008764,
	0.026995,
	0.064759,
	0.120985,
	0.176033,
	0.199471,
	0.176033,
	0.120985,
	0.064759,
	0.026995,
	0.008764,
	0.002216,
};

float4 PS_HBlur(uniform sampler Sampler,
                uniform float SpreadMultiplier,
                float2 uv : TEXCOORD0,
                float2 pixelDelta : TEXCOORD1) : COLOR
{
	float4 Color = 0;

	float SpreadAmount = Spread * SpreadMultiplier;
	for (int i = 0; i < g_cKernelSize; i++) {
		Color += tex2D(Sampler, uv + float2(-SpreadAmount*pixelDelta.x*(i-6), 0)) * BlurWeights[i];
	}

    return Color;
}

float4 PS_VBlur(uniform sampler Sampler,
                uniform float SpreadMultiplier,
                float2 uv : TEXCOORD0,
                float2 pixelDelta : TEXCOORD1) : COLOR
{
	float4 color = 0;

	float SpreadAmount = Spread * SpreadMultiplier;
	for (int i = 0; i < g_cKernelSize; i++) {
		color += tex2D(Sampler, uv + float2(0, -SpreadAmount*pixelDelta.y*(i-6))) * BlurWeights[i];
	}
	
	return color;
}

// Technique /////////////////////////////////////////

technique FilterTechnique
{
	pass HBlur1 <
		string RenderTarget0 = "g_pass2Texture";
	>
	{
		// Setup render states
		ZWriteEnable     = FALSE;
		ZEnable          = FALSE;
		AlphaBlendEnable = FALSE;

		// Shaders
		VertexShader = compile vs_1_1 VS_Common();
		PixelShader  = compile ps_2_0 PS_HBlur(Sampler1, 8);
	}

	pass VBlur1 <
		string RenderTarget0 = "g_pass3Texture";
	>
	{
		// Setup render states
		ZWriteEnable     = FALSE;
		ZEnable          = FALSE;
		AlphaBlendEnable = FALSE;

		// Shaders
		VertexShader = compile vs_1_1 VS_Common();
		PixelShader  = compile ps_2_0 PS_VBlur(Sampler2, 8);
	}

	pass HBlur2 <
		string RenderTarget0 = "g_pass2Texture";
	>
	{
		// Setup render states
		ZWriteEnable     = FALSE;
		ZEnable          = FALSE;
		AlphaBlendEnable = FALSE;

		// Shaders
		VertexShader = compile vs_1_1 VS_Common();
		PixelShader  = compile ps_2_0 PS_HBlur(Sampler3, 4);
	}

	pass VBlur2 <
		string RenderTarget0 = "g_pass3Texture";
	>
	{
		// Setup render states
		ZWriteEnable     = FALSE;
		ZEnable          = FALSE;
		AlphaBlendEnable = FALSE;

		// Shaders
		VertexShader = compile vs_1_1 VS_Common();
		PixelShader  = compile ps_2_0 PS_VBlur(Sampler2, 4);
	}

	pass HBlur3 <
		string RenderTarget0 = "g_pass2Texture";
	>
	{
		// Setup render states
		ZWriteEnable     = FALSE;
		ZEnable          = FALSE;
		AlphaBlendEnable = FALSE;

		// Shaders
		VertexShader = compile vs_1_1 VS_Common();
		PixelShader  = compile ps_2_0 PS_HBlur(Sampler3, 2);
	}

	pass VBlur3 <
		string RenderTarget0 = "g_pass3Texture";
	>
	{
		// Setup render states
		ZWriteEnable     = FALSE;
		ZEnable          = FALSE;
		AlphaBlendEnable = FALSE;

		// Shaders
		VertexShader = compile vs_1_1 VS_Common();
		PixelShader  = compile ps_2_0 PS_VBlur(Sampler2, 2);
	}

	pass HBlur4 <
		string RenderTarget0 = "g_pass2Texture";
	>
	{
		// Setup render states
		ZWriteEnable     = FALSE;
		ZEnable          = FALSE;
		AlphaBlendEnable = FALSE;

		// Shaders
		VertexShader = compile vs_1_1 VS_Common();
		PixelShader  = compile ps_2_0 PS_HBlur(Sampler3, 1);
	}

	pass VBlur4 <
		string RenderTarget0 = "";
	>
	{
		// Setup render states
		ZWriteEnable     = FALSE;
		ZEnable          = FALSE;
		AlphaBlendEnable = FALSE;

		// Shaders
		VertexShader = compile vs_1_1 VS_Common();
		PixelShader  = compile ps_2_0 PS_VBlur(Sampler2, 1);
	}
}

The ViewportRatio parameter I use as a scaling parameter when creating the textures - for effects that for example want lower resolution intermediate textures.

 

What you are looking for is the enumeration of effect annotations in code similar to this:

void GXEffectFilter::ParseEffectAnnotations(LPD3DXEFFECT pEffect)
{
	// Parse parameter annotations
	D3DXHANDLE			paramHandle;
	D3DXPARAMETER_DESC	paramDesc;

	unsigned int index = 0;
	while ((paramHandle = pEffect->GetParameter(NULL, index)) != NULL) {
		if (pEffect->GetParameterDesc(paramHandle, &paramDesc) == S_OK) {
			// Iterate through all associated annotations
			FLOAT viewportRatio[2] = {1, 1};
			for (unsigned int a = 0; a < paramDesc.Annotations; a++) {
				D3DXHANDLE hAnnot = pEffect->GetAnnotation(paramHandle, a);
				// Get annotation description
				D3DXPARAMETER_DESC annotDesc;
				if (pEffect->GetParameterDesc(hAnnot, &annotDesc) == S_OK) {
					if (_stricmp(annotDesc.Name, "ViewportRatio") == 0) {
						pEffect->GetFloatArray(hAnnot, viewportRatio, 2);
					}
[...]
				}
			}
			// Handle creation of textures
			if (paramDesc.Type == D3DXPT_TEXTURE &&
				paramDesc.Semantic != NULL && _stricmp(paramDesc.Semantic, "RENDERCOLORTARGET") == 0)
			{
				RegisterInternalTexture(index, viewportRatio);
			}
[...]
		}
		index++;
	}
}

Then handle the creation of the dynamic texture in the RegisterInternalTexture() call and set the created texture back to the shader by calling

		pEffect->SetTexture(pEffect->GetParameter(NULL, parameterIndex), pTexture);

When you later is about the render each pass in your shader you will have to parse the Pass annotations and find "RenderTarget0" and use pD3DDevice->SetRenderTarget() with the texture you have defined in your shader pass.

Edited by Kjell Andersson
1

Share this post


Link to post
Share on other sites

First of all, thank you for the reply. The loads of code really helps a lot.

I'm not really sure what you're doing in the second coding piece though. Maybe it's because I'm experienced with C# instead of C++ (that's C++ right)?

Can you perhaps explain the second part a bit more so I could succesfully translate it to C#?

 

To clarify, this is how I currently draw the texture:

//PostProEffect is the Effect
//renderTarget is the 2DTexture
//graphics is the GraphicsDevice

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullClockwise, PostProEffect);
spriteBatch.Draw(renderTarget, new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), Color.White);
spriteBatch.End();
Edited by Dutcher
0

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