• 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
Aleks Le

Directx Shader calcualtions.

6 posts in this topic

Hi everyone.

I am currently trying to re-implement a project i have created form CPU side to GPU side.
I am trying to use Directx11 Compute Shader.

The problem:
I am trying to calculate the center of mass of a model using the vertex list.
Currently i have an input buffer that holds all the vertices. and an output buffer, that should hold the center of mass position.

 

Currently i am using a test vector that has 4 vertices.(for testing).

 

I dispatch one group in each direction. Dispatch(1,1,1)
In each group i have numthreds[4,1,1] .

 

Basically i am trying to calculate the sum of all vertex positions, and store in in single float3 variable.

I am running into a problem: race condition, as i understand this is due to the code being parallel , and trying to write into the same variable at the same time, which might cause miscalculations.

 

Thank you all!


 

Edited by AlexRus
0

Share this post


Link to post
Share on other sites

Having numthreads be [numverts, 1, 1] is not going to be viable going forward, when the number of verts is unbounded. Probably what you'll want to do is have a set number of threads, (probably a multiple of 64, for example, so you fully utilize your wavefronts) and then split your verts into 64*n groups. One for each thread.

Each thread can add up its vert positions, then write the result to a unique location in the group shared mem. Since each thread is writing to a separate location, there will be no race condition.

Then at the very end, you can wait on a synchronization barrier (AllMemoryBarrier) to make sure each thread has written its result. Then one of the threads can add up all the results that are stored in the shared mem and write the final output.

 

Alternately: you could use InterlockedAdd in your current compute shader instead of +=. (not for floats, though)

Edited by Samith
0

Share this post


Link to post
Share on other sites

How many vertices do you expect from a real usecase? Will there be multiple independent meshes, for which you want to compute the centers?

 

What you tried to do only works with atomic operations, and even that should be avoided. What you need is a proper "reduction" algorithm. In my experience, butterfly networks work great on the GPU for these kinds of things. But the specifics depend heavily on how many vertices per model and how many models in parallel you want to process.

0

Share this post


Link to post
Share on other sites

Ok, inside a project i might have a few models, but on compute shader the idea currently is to process one at a time, I do understand that i am limited with the amount of threads per group, and i should split it into multiple groups. Currently the project has only 1 model , combination of groups and the number of threads in them is not the current issue i have some ideas about how that should be implemented.

i took a look at the InterlockedAdd , and it does not support floats =/.

Can you please elaborate on " then write the result to a unique location in the group shared mem." sorry i never worked with shaders =/

Also is there any difference in using numthreds[1000,1,1] and numthreads[10,10,10] (performance wise), since they are all running in parallel anyway ( AFAIK).

sorry if i am asking simple questions, just trying to get my head around this.

 

0

Share this post


Link to post
Share on other sites


i took a look at the InterlockedAdd , and it does not support floats =/.

 

Derp, my bad.

 


Can you please elaborate on " then write the result to a unique location in the group shared mem." sorry i never worked with shaders =/

 

In your code screenshot you have a line that says "groupshared unsigned int shared_data". That's shared memory for your group. Why I was proposing is having something like "groupshared float accumulation[NUM_THREADS];" and then each thread would write its results into accumulation[threadID], which means no two threads would be writing to the same location in memory, thus no race conditions.

0

Share this post


Link to post
Share on other sites

Ok, i think i understand what is happening, but im still running into the main problem, how would i sum them all up? =/ 
my head is not thinking today... i tried "for looping it" with a condition, didn't work out. 

0

Share this post


Link to post
Share on other sites
Simplest way?
Check thread id; if thread id = 0 then loop over the results, add them up and write them out.

Probably the better performing way; Do a reduction in stages.
If you have, for example, 64 outputs from the first pass then during the summing phase;
- Thread 0 adds result[0] + result[1]
- Thread 1 adds result[2] + result[3]
- Thread 2 adds result[4] + result[5]
and up to thread 32 (rest do nothing)

then repeat, each time halving the thread count doing the summing; your final calculation will be the result.
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