September 2015

15:30
Mon
28
Sep 2015

Direct3D 12 - Watch out for non-uniform resource index!

DirectX 12 allows us to use arrays of resources (descriptor tables) and refer to them from a shader code by an index. The index can be constant or variable. But there is a big trap waiting for shader developers doing that! Microsoft just updated their public documentation in this regard (see Resource Binding in HLSL), so now I can talk about it.

In D3D12, resource index is expected to be uniform (convergent) by default. It means the index, even if dynamic, should be the same across whole draw call (all vertices/pixels/etc.). For example, it can be a result of some calculations, which depend on parameters coming from a constant buffer. Example code:

Texture2D<float4> textures[16] : register(t0);
SamplerState samp : register(s0);
struct SConstantBuffer
{
    uint MaterialId;
};
ConstantBuffer<SConstantBuffer> cb : register(b0);

float4 PS(
    in float2 texCoords : TEXCOORD0 ) : SV_Target
{
    uint materialId = cb.MaterialId;
    return textures[materialId].Sample(samp, texCoords);
}

Why is it this way? That is because on low level, GPU-s are made of SIMD processors. Each of its small processors executes shader instructions over multiple "SIMD threads" (or "warps", or "wavefronts", however you call them), not a single (scalar) value. Because of that, knowing that the resource index will always be the same on all SIMD threads can result in some optimizations and more efficient execution.

Resource indices in Direct3D 12 actually can be non-uniform (divergent) - different in every vertex/pixel/etc., like when they are result of some calculations based on vertex attributes or pixel position. But they must be then surrounded by a special, "pseudo"-function in HLSL, called NonUniformResourceIndex. For example:

Texture2D<float4> textures[16] : register(t0);
SamplerState samp : register(s0);

float4 PS(
    in float2 texCoords : TEXCOORD0,
    in uint materialId : MATERIAL_ID ) : SV_Target
{
    return textures[NonUniformResourceIndex(materialId)].Sample(samp, texCoords);
}

This may look a bit unintuitive, so I expect many developers will make the mistake and omit this function. If you use a non-uniform value as resource index, but forget to mark it with NonUniformResourceIndex, HLSL compiler probably won't warn you about it. It may even work in some cases and on some GPU-s, while it will give invalid (undefined) results on others. So similarly to the issue with reduced precision in normalize/length operations in GLSL, this is a thing to be careful with when coding your shaders in HLSL using new Shader Model 5.1.

Comments (2) | Tags: directx | Author: Adam Sawicki | Share

20:31
Wed
16
Sep 2015

Syntax of C++ Templates - Comparison

I noticed recently how C++ syntax differs when you define a class template, template instantiation, specialization or partial specialization. Here is an example:

// Template class definition.
template<typename T>
class Class1
{
public:
    void Print() { printf("Class1 template\n"); }
};

// Template class instantiation.
template
class Class1<int>;

// Template class specialization.
template<>
class Class1<double>
{
public:
    void Print() { printf("Class1 specialization for double\n"); }
};

// Template class partial specialization.
template<typename T>
class Class1<T*>
{
public:
    void Print() { printf("Class1 partial specialization for pointers\n"); }
};

Having these classes defined, following code fragment:

Class1<int> objInt;
objInt.Print();
Class1<double> objDouble;
objDouble.Print();
Class1<int*> objPointer;
objPointer.Print();

Produces following output:

Class1 template
Class1 specialization for double
Class1 partial specialization for pointers

Here is how the syntax can be compared:

Comments (3) | Tags: c++ | Author: Adam Sawicki | Share

22:10
Tue
15
Sep 2015

Unicode w Visual C++ - My Old Article

I just noticed that my old article "Unicode w Visual C++" (in Polish) was a dead link and restored it back online:

Unicode w Visual C++

It may not be "politically correct" if you believe in what they say in UTF-8 Everywhere manifesto, but I think the information that I have put there are still relevant and useful despite being 7 years old.

Comments (1) | Tags: c++ visual studio | Author: Adam Sawicki | Share

23:44
Mon
14
Sep 2015

Music Visualizations - Plans for the Future

Last weekend I was showing my music visualizations on two parties. First one was in a club, projected on flat screen, while the second one was an open-air party (my 7th and last one in this summer season :) with the image projected on trees. For this one I had to prepare something different - simple, contrast shapes and single color only, so it could be clearly visible. Here is a small video:

It doesn't look like this because I wanted it exactly this way or because that was my "artistic vision", but just because showing some rotating images downloaded from the Internet and blending transformed feedback from previous frame was the easiest way to start with something interestingly looking.

Now I have tons of ideas to improve this program as soon as I find some free time. Next to some small technical tasks like refactoring code or simply adding new graphical effects, I plan following big TODO-s (with no particular order decided yet):

Comments (2) | Tags: events music psytrance vj | Author: Adam Sawicki | Share

STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2017 Adam Sawicki
Copyright © 2004-2017 Adam Sawicki