Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 26 Nov 2008
Offline Last Active Yesterday, 08:13 AM

#5255133 D3D12 Best Practices

Posted by TiagoCosta on 02 October 2015 - 07:52 AM

Thought I shouldn't start a new topic as it is kind of generic DX12 question: if I put my CBV in the root signature, does the driver version the data automatically, i.e. I wouldn't need a per-frame constant buffer resource?




You will still need a per frame constant buffer resource. You just won't need a per frame entry in a descriptor table for that CBV.


The only way to not need a constant buffer resources is to store constants directly inside the root signature, but you have very limited root signature memory, so you won't be able to store everything in the root signature.

#5253613 DirectCompute - set numthreads from application code (or read from shader).

Posted by TiagoCosta on 23 September 2015 - 02:46 AM

You can use ID3D11ShaderReflection::GetThreadGroupSize to get the thread group size of a compute shader in C++.


I do it in my engine right after creating the compute shader and store the thread group size together with the compute shader pointer in a struct, so I always have access to the info when I want the use the shader. 

#5241204 Cheap but realistic lighting engine: Is it a good idea?

Posted by TiagoCosta on 18 July 2015 - 08:14 AM

You haven't described anything new wink.png but if you came up with that on your own, great!


Many engines and games use lightmaps, just google a bit.


You also, basically, described reflective shadow maps (also here).


So yes, it will work but you will have to deal with the limitations. Eg: How will you handle reflections?

#5238418 Uses for unordered access views?

Posted by TiagoCosta on 05 July 2015 - 04:31 AM

There are many possible uses:


1 - Update particle systems on the GPU. Read from previous frame state (ConsumeBuffer), run simulation for the particle and append to the current frame (AppendBuffer). Draw using draw indirect. You can also do this in the geometry shaders (try both and see which is faster)

     This also works for other types of geometry like vegetation, water simulation, finding pixels that need bokeh, cloth simulations etc.


2 - Calculate per tile light lists (forward+ rendering) or tiled deferred lighting (tile culling and lighting runs on compute shaders). The results are stored in UAV that are then read as SRVs.


3 - Some full screen effects might benefit from compute shaders groupshared memory, so you'll use UAVs.


4 - and many more, just google a bit.


Check out this GDC presentation.

#5236830 Dealing with different collision responses in an entity-component system

Posted by TiagoCosta on 25 June 2015 - 03:46 PM

This is how I would do it:
The CollisionComponent should only contain data related to the physics system (is this a rigid body, a trigger region?, friction, restitution, etc)
PhysicSystem: does all physics related math and outputs a list of collisions that occurred.

struct Collision
    handle object_a;
    handle object_b;
    //.... other collision info

CollisionHandlerSystem (this is a high level game specific system):
Takes the list of collisions (from above) as input and does the necessary processing depending on the types of objects involved in the collisions.
This is where you would handle the logic you mentioned. This system could generate a list of messages that could be consumed by other systems, like "destroy entity X", "damage entity Y", etc (good for multi threading) or talk directly with other systems (careful with race conditions).
You could also create another component (CollisionCallbackComponent) and system (or the CollisionHandlerSystem could do it) to call entity specific collision callbacks.
This way you could efficiently handle global game logic, and still provide support of custom callbacks.

#5229741 What the difference between these two books? which one would you recommend?

Posted by TiagoCosta on 18 May 2015 - 07:49 PM

I don't think there is any up-to-date book on how to manage "scene graph, mesh, animation classes, etc".


-There's a chapter in GPU Pro 3 (Chapter V.4 Designing a Data-Driven Renderer) that might be helpful (it's short).

-Game Engine Architecture chapter on Animation is really good (including some actual code) and should be enough as a start of an engine animation system.

-If you want to learn rendering techniques start by the (free) GPU Gems books.


Regarding "scene graph management, etc", you'll be better served by reading other engine source code, the hundreds of topics about this in the forums and conference presentations (check GDC Vault, Frostbite, and other engine/companies websites).


There's a recent presentation about Destiny’s Multi-threaded Renderer Architecture. It probably isn't easy for a beginner to understand everything but read it carefully (and multiple times).


Horde3D source code is small and easy to follow (not the best architecture for current gen but it's a good start).


Google "Data Oriented Programming" and read the Bitsquid blog


More useful link here

#5228880 Want to create a cloud in 3d array

Posted by TiagoCosta on 13 May 2015 - 07:44 PM

EDIT: Turns out I misread the question. Check answers below


You can render it using Volume Rendering techniques.


IMO, the easiest way is to put that data in a 3D texture and ray march in the pixel shader.

For each pixel, compute the pixel's view direction and sample the 3D texture at N steps from the near to the far plane.


You can also convert the 3D array into a distance field to improve performance, etc.



Another method

#5227509 Shader and shader buffer management resources

Posted by TiagoCosta on 06 May 2015 - 08:23 AM

The only book I know that (kind of) covers this topic is GPU Pro 3 (Chapter V.4 Designing a Data-Driven Renderer).


There's also a lot of topics on GameDev.net about this (look for Hodgmanreplies):


Link 1 (you should probably read this one carefully)

Link 2

Link 3

Link 4

Link 5

Link 6

Link 7

Link 8

Link 9

Link 10

Link 11

Link 12

Link 13


You should find lots of info in those links.

#5225453 Direct3D 12 documentation is now public

Posted by TiagoCosta on 25 April 2015 - 09:43 AM

By the way what is the purpose of the D3D12_DSV_READ_ONLY_DEPTH and D3D12_DSV_READ_ONLY_STENCIL flags in the Depth Stencil View ?


It can be useful for some effects like unbird mentioned.

Example: Rendering light bounding volumes in Deferred rendering. You bind a read-only DSV to enable depth testing and a SRV of the same depth texture that the shader will sample to reconstruct the pixels position.

#5225129 Going out of bounds in a rwtexture

Posted by TiagoCosta on 23 April 2015 - 03:59 PM

Out of bounds UAV reads return 0s and writes result in No-Ops(nothing being written). More info here (slide 16)


So you shouldn't run into any problems.


I think the only case you have to be careful is appending to a AppendStructuredBuffer because out of bounds doesn't write but still increases the internal counter.

#5224774 Some basic help on fxc (compiling shaders)

Posted by TiagoCosta on 21 April 2015 - 04:10 PM

You're incorrectly specifying the 'defines', the command should have a /D NAME=VALUE for each define (the =VALUE part is optional)
So it should be:

fxc /T ps_3_0 /E VS_function /Fo comptst.fxc /D AMBIENT_HEMI /D FOGGING /D USENORMALMAP /D SPECULAR CR_ushader_v1.fx

If your .fx file contains a vertex shader and a pixel shader you have to compile it twice, once with the target vs_3_0 and the entry point 'your_vs_function_name', and a second time with ps_3_0 and entry point 'your_ps_fuction_name'
That's why your example works with vs_3_0 and not with ps_3_0, you're not setting the correct entry point name for the pixel shader.

#5222489 C++ cant find a match for 16 bit float and how to convert 32 bit float to 16...

Posted by TiagoCosta on 10 April 2015 - 02:04 PM

I've been using this code written by Mike Acton from Insomniac to convert between float and *half*.


You might need to change inline to __inline or the extension from .c to .cpp when working with visual studio.


You can use a union to convert float to/from uint32_t:


union Helper
    float f;
    uint32_t u;
Helper helper;
helper.f = 5.0f;

uint16_t h = half_from_float(helper.u);
//and back
helper.u = half_to_float(h);
float y = helper.f;

#5217391 Render only to depth buffer

Posted by TiagoCosta on 18 March 2015 - 11:34 AM

Yes, you just have to set the render targets to NULL but bind the correct depth stencil target.

You don't even have to bind a pixel shader (set it to NULL too). If you've alpha tested geometry bind a pixel shader that discards the pixels that fail the alpha test and return void.

#5213065 Minimizing resource switching when sorting front to back

Posted by TiagoCosta on 26 February 2015 - 05:17 AM

First of all, the most important thing is to sort non-opaque (translucent) objects back-to-front, otherwise they'll (probably) be rendered incorrectly.


For opaque objects you'll have to find the best option for you specific project.


Option 1: Sort by depth front-to-back;


Option 2: Sort by "material"


Option 3: a mix of (1) and (2) - put "material id" in the higher bits and depth in the lower bits, this way you minimize resource switches and still benefit from occlusion tests (early z) in objects using the same resources. 


Option 4: a mix of (1) and (2) - do a rough sort by depth (basically you round down the distances) and then sort each bucket by material (not really two separate sorts, just put the depth in higher bits).


More info at: http://aras-p.info/blog/2014/01/16/rough-sorting-by-depth/




Object A (distance 12)

Object B (distance 25)

Object C (distance 17)


If you round down the distances and sort by depth:


Object A (distance 10)

Object C (distance 10)

Object B (distance 20)


You can also sort A and C by material, and they're drawn before B


Depending on your project this might be an improvement (maybe you have many objects using the same resources close to each other), but you have to profile different methods to find what works best for you.

#5212893 Using a 2D Texture for Point-Light Shadows

Posted by TiagoCosta on 25 February 2015 - 05:08 AM

Haven't tried it but should work: http://www.nvidia.com/object/cube_map_ogl_tutorial.html Near the end search for the "Mapping Texture Coordinates to Cube Map Faces" section


EDIT: Btw you can also google "Mapping Texture Coordinates to Cube Map Faces" to find more info