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

Making a texture render to itself

11 posts in this topic

Hi everyone,

 

I'm working with post-processing in HLSL, i.e. I pass a texture to my effect file containing the back buffer and I then render a sprite using my shader to create various effects. I am doing Gaussian blur right now which means that I need to use multiple steps:

 

- Render scene to texture

- Render a sprite with the texture blurred horizontally

- Render a sprite with the texture blurred vertically

 

I've seen a few tutorials which seem to imply that you should render the back buffer to one texture (let's call it t_A), then render the horizontal blur to another separate texture (which I'll call t_B), and then render the final sprite by sampling t_B to perform the vertical blur. However, I decided to try and do it with only one texture, and so my program renders the scene to the texture, then passes the texture straight into the shader to perform the horizontal blur, then immediately passes it straight back in to do the final vertical blur, all without changing the render target (and all within the same BeginScene() and EndScene() pair).

 

So my question is: is it safe to make a texture sample itself in my shader while also rendering onto itself? Of course I pass the texture into the shader before I start drawing again, so I guess it's stored in there, but there must be a reason why the tutorials are using different textures to do different parts of the post-processing.

 

Thanks!

1

Share this post


Link to post
Share on other sites

Heh, it works when I try it! Here's a horizontal blur, vertical blur and greyscale effect rendered with just one render target:

 

[attachment=19967:rendering_bw_blur.png]

 

Here's what I'm doing:

 

1. Set the offscreen texture as the render target, clear it, begin scene

2. Render the scene in colour, un-blurred, to the offscreen target

3. Immediately send the offscreen texture to the shader and render a sprite by sampling the texture to add the horizontal blur

4. Repeat for the vertical blur

5. End the scene and set the back buffer as the render target

6. Render a sprite to the back buffer by sampling the double-blurred texture using the greyscale shader

7. Present the back buffer

 

...should this be happening?? tongue.png

 

EDIT: I get this though when I run in debug mode:

 

Direct3D9: (WARN) :Can not render to a render target that is also used as a texture. A render target was detected as bound, but couldn't detect if texture was actually used in rendering.

 

Does this mean that it's actually using a different target? Funny how the effect still happens...

Edited by george7378
0

Share this post


Link to post
Share on other sites

I'd suspect that the driver is detecting the potentially trouble-causing situation and behind your back it's making a copy of the render target to use as the source texture. If that's the case you get correct rendering results on your machine, but there's no guarantee it'll work on any other driver/hardware and the copy means you get sub-optimal performance.

 

Just a guess though.

1

Share this post


Link to post
Share on other sites

Typically if you try to bind a texture as a render target and it is bound as input in the same time, the binding will be released. 

You should enable debugging to see if there are any errors produced (there should be).

 

Cheers!

1

Share this post


Link to post
Share on other sites

In this situation, I think it's better to create two textures. One for SRV and one for RTV. After you render into RTV, you can copy RTV to SRV with copyresource. It's should be fast as it's GPU to GPU. Also SRV and RTV should be set to default, so no CPU writing/reading.

1

Share this post


Link to post
Share on other sites


In this situation, I think it's better to create two textures. One for SRV and one for RTV. After you render into RTV, you can copy RTV to SRV with copyresource. It's should be fast as it's GPU to GPU. Also SRV and RTV should be set to default, so no CPU writing/reading.

 

Copying between resources isn't necessary. You may use the same texture as render target and pixel shader resource, just not at the same time.

 

Cheers!

1

Share this post


Link to post
Share on other sites

It will work on *some* GPUs as long as you only sample from the current pixel (so greyscale filter is possible, but a blur filter is a potential race condition).
However, this is not possible on ALL GPUs, so D3D/GL are forced to disallow it and call it undefined behavior. Don't do it if you want your game to work on any PC configuration besides your current one...

Edited by Hodgman
1

Share this post


Link to post
Share on other sites

You are perhaps not combining the ouput of first horizontal pass, and you only sample original texture in creating vertical pass. Blending them will result in a blur, but not the exact gausian blur, since that demands the vertical dissorted texture to be sampled to generate horizontal one. But if result is satisfying and you are not doing an university assignment you can stick to it, since it is more optimized in terms of memory resources and state changes.

1

Share this post


Link to post
Share on other sites

I've changed it so that I do the ping-pong rendering between two different textures. It's very little extra effort but it means that I can be guaranteed safe results (and I don't get the warning signs any more). When I render properly I get exactly the same result with the Gaussian blur as I I do with the single texture, so the GPU must be making a separate copy. Thanks for the help!

2

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