• 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

Constant Buffer usage

3 posts in this topic

I went over some of my code with a colleague yesterday and he was quite surprised by how I manage my constant buffers.
Except for a few rare and very specific situations, I only use 2 "global" constant buffers.

A per-frame buffer, which contains data which only needs to be updated once per frame.
[source lang="cpp"]cbuffer PerFrameCB : register (b0)
float4x4 CameraView : packoffset( c0.x);
float4x4 CameraProjection : packoffset( c4.x);
float4 CameraPosition : packoffset( c8.x);
float4 SunDirection : packoffset( c9.x);
float2 ViewportSize : packoffset(c10.x);

And another buffer used for everything else.
This buffer is 1kb in size, and is updated whenever new data is needed, which is multiple times per frame.
Both of these buffers are always bound to the registers b0 and b1 for all shader stages.

I've been told that this is the "wrong" way to do it. I'm supposed to split this up into individual constant buffers.

However I don't understand why that is the case.
If I split my current b1 constant buffer into X buffers, not only do I still need to update these buffers, but I'll also need to bind a new constant buffer whenever new data is needed.

I don't see how my method is wrong, but I'm a little paranoid when I hear such claims because I am self-taught.
So I figured it's better to ask than potentially doing something wrong.

Hyu Edited by Hyunkel

Share this post

Link to post
Share on other sites
A per-frame cbuffer is OK to use for constants that really only change once per frame. Using a single large cbuffer for everything else is always advised against - [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ee416643%28v=vs.85%29.aspx#constant_buffers"]Microsoft advise against it[/url], the [url="http://developer.amd.com/gpu_assets/The%20A%20to%20Z%20of%20DX10%20Performance.pps"]hardware vendors[/url] [url="http://developer.download.nvidia.com/presentations/2008/GDC/GDC08-D3DDay-Performance.pdf"]advise against it[/url], and I'm going to advise against it.

The reason why is because every time you need to update even a single constant, you need to upload the entire cbuffer to the GPU. So if you're drawing a sprite that only needs 4 float3s updated, that entire 1kb cbuffer needs to go up. If you're drawing hundreds of these sprites - ouch!

The general usage is that you have a "per-object" constant buffer, which may (but doesn't have to be) different for each object type, and which only contains the constants needed to draw each object type. So a mesh cbuffer will contain a matrix and some animation info, a sprite cbuffer will contain info needed to billboard the sprite, etc. Sort your objects by type for drawing, select the cbuffer to use and bind it (add some state filtering to your engine here), then map the cbuffer with discard, update, unmap, draw.

Don't worry too much about the overhead of switching constant buffers - this is a very lightweight operation, certainly far more lightweight than uploading a full 1kb buffer multiple times per frame.

Share this post

Link to post
Share on other sites
The GPU and CPU aren't synchronised -- usually the GPU is running about a whole frame behind the CPU. This means that when you use D3D to tell the GPU to do something, you're really just putting a command into a queue that it will get to later.
[code]CPU:| Draw A | Draw B |
GPU: | Draw A | Draw B | [/code]This has a big impact on how GPU-side resources are managed.
Let's say that above, A and B both use the same cbuffer, but it's updated before each draw call.
When you ask to update the buffer for [B], the CPU has to wait until your [A] command has been completed, otherwise it will be drawn incorrectly, and then after [A] has been drawn, it will allow you to map the buffer and draw [B], so you end up with a big stall, like:
[code]CPU:| Update | Draw A | | Update | Draw B |
GPU: | Draw A | | Draw B | [/code]
If your GPU driver is really clever, then instead of stalling, it can actually allocate 2kb space instead of 1kb space, and although it looks like you've got 1 cbuffer, it's actually allocated two different ones internally! This is basically the same as if you'd gone and made multiple cbuffers yourself, but you're relying on the GPU driver to save you instead of writing safe code yourself:
[code]CPU:| Update 1 | Draw A | Update 2 | Draw B |
GPU: | Draw A using 1 | Draw B using 2 | [/code]

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