• 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
Tim Coolman

Storing non-color data in texture from pixel shader

6 posts in this topic

I am using a pixel shader to put some data into a texture. Typically, with a [i]float4[/i] formatted texture, you would output RGBA color data to the texture where each color component is a 0.0 - 1.0 float value. I'm trying to use the pixel shader to store non-color data. This texture is not meant for display. Instead, once the texture is filled, I convert the texture texels to a different binary format using a compute shader (due to the nature of the data, it makes sense for me to output this data with a pixel shader). When outputting to the texture from my pixel shader, I would like to store some [i]uint[/i] values instead of floats in the Y, Z, W components. So here is an example of how I'm trying to return from the pixel shader:
[source lang="cpp"]
return float4(floatValue, asfloat(firstUintValue), asfloat(secondUintValue), asfloat(thirdUintValue));
[/source]
I do this because I don't want to cast the [i]uint[/i] values to [i]float[/i], but rather maintain their binary equivalent.

However, when I read from the texture using my compute shader and convert these values back to [i]uint[/i] using the [i]asuint(texel.Y)[/i] function, they do not seem to be the same value I attempted to store in the first place. Actually, most the time I seem to get ZERO values out of this.

I know that I have supplied my compute shader with the texture as a shader resource properly, because I am able to retrieve the X component of the texels, which you'll notice above was a regular float (between 0.0 and 1.0).

Does the pixel shader require output to be 0.0 - 1.0 floats and do automatic adjustments otherwise?

Thanks you for your time and assistance.
0

Share this post


Link to post
Share on other sites
The behavior depends on the format modifer of the DXGI format that you use for the render target. The different format modifiers and their behavior are listed in the documentation for the DXGI_FORMAT enumeration. In your case anything except for a 32-bit FLOAT format will mess up your values, because conversions will happen.

The easiest thing for you to do would be to just use two render targets: one that has a single FLOAT or UNORM channel for your floating-point value, and another that uses a UINT format so that you can just store your values as float's and uint's with no conversions necessary.
1

Share this post


Link to post
Share on other sites
Have you considered writing to a UAV in your pixel shader? This should help you have a common way to reference the resource, and you can directly use the appropriate type in your shader code.
0

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1352764056' post='5000386']
In your case anything except for a 32-bit FLOAT format will mess up your values, because conversions will happen.
[/quote]

What kind of conversions? I just figured that since I was using the asfloat() function to store my UINT values, the texture would accept it as a float - how would the texture know the difference that it is actually a binary representation of a UINT? Unless the texture requires that the value be a value color-component value between 0.0 and 1.0.

[quote name='Jason Z' timestamp='1352771199' post='5000418']
Have you considered writing to a UAV in your pixel shader?
[/quote]

I'll have to think about this. The reason I'm doing it this way is because I actually am storing graphical data - I still take advantage of the way the pixel shader projects the data onto my texture using transformation matrices, and I also need it to take care of depth buffering and resolution. However, I don't care about color - instead I have other data to keep track of, which is why I was trying to use the color-component values to store other information.
0

Share this post


Link to post
Share on other sites
[quote name='Tim Coolman' timestamp='1352908869' post='5000943']
What kind of conversions?
[/quote]

The kind of conversions specified in the [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb173059%28v=vs.85%29.aspx"]documentation for the DXGI_FORMAT enumeration[/url].

[quote name='Tim Coolman' timestamp='1352908869' post='5000943']
I just figured that since I was using the asfloat() function to store my UINT values, the texture would accept it as a float - how would the texture know the difference that it is actually a binary representation of a UINT?
[/quote]

It won't know that it's a UINT, which can be a problem. The hardware will assume it's a 32-bit floating point value, and will treat it as such. If any format conversions are applied, it will apply floating point operations to your output value which will be totally invalid. For instance it might clamp to [0, 1] and convert to an unsigned integer, which will result in a bunch of garbage when you sample it an attempt to cast the value as an integer.
1

Share this post


Link to post
Share on other sites
Okay. The values I'd like to store consist of one float and 3 uint values. Think using [b]DXGI_FORMAT_R32G32B32A32_TYPELESS[/b] instead of [b]DXGI_FORMAT_R32G32B32A32_FLOAT [/b]would prevent unexpected conversions from occurring?
0

Share this post


Link to post
Share on other sites
Well, after trying a few other ways to do this, I put it back to how I had it and... now it works! Magic. I have no idea what changed since my first attempt, but is now working as I'd originally expected. I apologize, as I feel like I wasted your time with this question. But now using a [b]DXGI_FORMAT_R32G32B32A32_FLOAT[/b] texture, I'm able to store UINT values using asfloat() and asuint() to convert back and forth between Pixel and Compute shaders.
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