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

Is it possible to dynamically allocate memory in HLSL?

5 posts in this topic

Hello there, I am working on this graphics engine and I am now learning how to implement lights into the engine using HLSL. I've come across several tutorials that teach you how to create directional lights, multiple point lights, etc, everything works fine but, I want to create a shader that is as generic as possible, and I've encountered a problem with that.

Lets say that I have a structure in HLSL that looks like this:

[code]struct PointLight
{
float4 LightColor;
float3 LightPosition;
}; [/code]

And lets also say that I created a level for my game that uses X amount of point lights.

Is there any way to create an X amount of PointLight instances in HLSL during runtime? Is it possible to do something like: PointLight arrayOfLights = new PointLight[X]; ?

Another question, what is the best way in D3D10 to pass variables from the program to a structure member in a shader?

Example:

I want to pass this

D3DXVECTOR3 LPosition(0.0f, 0.0f, 0.0f);

Into this

PointLightInstance.LightPosition;

How should I do this?

Thanks in advance
0

Share this post


Link to post
Share on other sites
You definitely can't dynamically allocate memory in HLSL. But you also don't need to. What you can do is declare a constant buffer in your shader containing an array of your "PointLight" structures with size equal to some maximum number of lights (you can go for something big like 128 if you'd like), and then have another variable in your constant buffer containing the actual number of lights that you want rendered. So your shader would look something like this:
[code]
struct PointLight
{
float4 LightColor;
float3 LightPosition;
};

static const uint MaxLights = 128;

cbuffer Lights : register(cb0)
{
PointLight Lights[MaxLights];
uint NumLights;
}

float4 PSMain(in PSInput input) : SV_Target0
{
float3 lighting = 0.0f;
for(uint i = 0; i < NumLights; ++i)
lighting += ApplyPointLight(Lights[i]);
}
[/code]

As far as the C++ side of things go, the easiest way to manage this is to have a C++ structure that exactly matches the memory layout of your constant buffer. So you can declare a C++ version of your PointLight struct, as well as a C++ version of the entire constant buffer structure. The one thing that you need to watch out for is in HLSL your PointLight structure will get padded out to some multiple of 16 bytes (float4 size), so you'll need to do the same in C++. Something like this should work:
[code]
struct PointLight
{
D3DXVECTOR4 LightColor;
D3DXVECTOR3 LightPosition;
float _Padding;
};

static const UINT32 MaxLights = 128;

struct LightConstantBuffer
{
PointLight Lights[MaxLights];
UINT32 NumLights;
};
[/code]

Then all you need to do is create a dynamic constant buffer resource with the appropriate size (sizeof(LightConstantBuffer), rounded up to the next multiple of 16 bytes) during initialization, then each frame you can Map it to fill it with the current lights + number of lights for each draw call. To fill it you can just cast the void pointer you get back from Map to the type LightConstantBuffer*, and then you can just set all of the values. And don't forget to bind the constant buffer to the pixel shader stage as well.
1

Share this post


Link to post
Share on other sites
Hi!

The term „dynamic memory allocation“ caught my attention. In your case it is not needed, true, but if you stumble upon the case where you would like to allocate memory in shaders, there is a nice workaround.

First, you have to preallocate enough memory (you can’t get around this, since dynamic allocations are not possible.)
You could do so by creating a structured buffer that has room for enough elements. Structured buffers can have an internal counter, which is (and that’s interesting) faster than a plain atomic counter. You can use the current value of the counter as address in the structured buffer. If you’d like to add a new element, you can simply increment the counter and thereby get a new exclusive address. This works without problems across multiple threads (although it is a little bottleneck) and is currently only available in pixel shaders and compute shaders. Dx11.1 will add it to the other shader stages as well.

By the way, you can build linked lists with that, too. The [url="http://www.yakiimo3d.com/2010/07/19/dx11-order-independent-transparency/"]order-independent transparency demos[/url], [url="http://wwwcg.in.tum.de/research/research/publications/2011/view-dependent-integral-lines.html"]screen contribution blending[/url] and [url="http://software.intel.com/en-us/articles/deferred-rendering-for-current-and-future-rendering-pipelines/"]tile-based deferred shading[/url] do exactly that.

Declaration in the HLSL:
[CODE]struct DataElement {
int someData;
};
RWStructuredBuffer< DataElement > myBuffer : register( u0 );[/CODE]

And then the usage:
[CODE]// prepare the data element to write
DataElement element;
element.someData = 1;

// get address
uint nAddress= myBuffer.IncrementCounter();

// write the data element
myBuffer[ nAddress ] = element;[/CODE]

Cheers! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
1

Share this post


Link to post
Share on other sites
Thanks both of you, this helped me a lot.

Also, a noob question. Do the constant buffers always get padded out to a multiple of the size of the biggest member of the buffer?

If I had this in the shader

[code]struct PointLight
{
float4 LightColor;
float2 SomeData;
};[/code]

The code in C++ would be like this, am I right?

[code]struct PointLight
{
D3DXVECTOR4 LColor;
D3DXVECTOR2 SomeData;
D3DXVECTOR2 Padding;
};[/code]
0

Share this post


Link to post
Share on other sites
It works that way for structures declared in constant buffers, yes. For variables that aren't structures the packing rules are a bit more complex. See [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb509632%28v=vs.85%29.aspx"]this[/url] for more info.
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