Jump to content

  • Log In with Google      Sign In   
  • Create Account


DX11 - Shaders - Writable Textures


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
25 replies to this topic

#1 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 18 September 2013 - 01:16 PM

Hi guys, wink.png

 

I'm researching techniques for scene voxelization, and so far, only few papers has been found, but I found a great one! So now I'm in the process of creating the shader, but a problem stroke my mind, the paper is based on OpenGl (Theory is the same), and OpenGl has completely re-writable textures, in the shader, like so:

imageStore(voxelColor, voxelPos, outColor);

// voxelColor -> 3D Texture
// voxelPos   -> xyz Position of texture
// outColor, well, the data needed to be written

How can this be achieved with Directx 11 with HLSL? (Maybe I forgot something... Like usual tongue.png )

 

Thanks, as usual!

-MIGI0027



Sponsor:

#2 phantom   Moderators   -  Reputation: 6805

Like
3Likes
Like

Posted 18 September 2013 - 01:46 PM

Pixel and Compute shaders can both write to textures if you use RWTexture objects; http://msdn.microsoft.com/en-us/library/windows/desktop/ff471511(v=vs.85).aspx

#3 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 18 September 2013 - 01:52 PM

Thanks for making me aware of that, but I thought that you could only output data from the computer shader, is it possible to write to it in the pixel shader?



#4 phantom   Moderators   -  Reputation: 6805

Like
1Likes
Like

Posted 18 September 2013 - 02:16 PM

On D3D11/SM5 hardware yes; compute and pixel are the only stages which can write out.

D3D11.1 expands UAV support to all stages so you can write from basically anywhere.

#5 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 18 September 2013 - 02:16 PM

Damn, sorry, didn't fully read your comment, sorry about that... unsure.png

 

EDIT: Not solved yet!


Edited by Migi0027, 19 September 2013 - 07:07 AM.


#6 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 19 September 2013 - 07:09 AM

Sorry to annoy you further, but I have one last question regarding this topic.

 

Q: Do you send the 3D Texture (in this case), just like a read only shader resource [DeviceContext->XSSetShaderResources(....)], or do they require a different method?

 

-MIGI0027



#7 MJP   Moderators   -  Reputation: 10281

Like
2Likes
Like

Posted 19 September 2013 - 12:09 PM

You don't use a shader resource view, you need to create an unordered access view (UAV) for the texture that you want to write to. To bind it to a compute shader you use CSSetUnorderedAccessViews, and to a pixel shader you use OMSetRenderTargetsAndUnorderedAccessViews.



#8 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 20 September 2013 - 01:23 PM

Hi again, thanks btw! happy.png

 

Another question...

 

How would one send such a resource to the GPU for reading to the PS, without setting any render targets. Here's my questions:

  • When calling OMSetRenderTargetsAndUnorderedAccessViews, if you set the backbuffer, numRTS, RTS to NULL, does it set the render targets to NULL, or does it skip the placement of the render targets.
  • Is there a way of treating the content of an UAV as a SRV?

Thanks

-MIGI0027



#9 unbird   Crossbones+   -  Reputation: 4863

Like
0Likes
Like

Posted 20 September 2013 - 01:51 PM

When calling OMSetRenderTargetsAndUnorderedAccessViews, if you set the backbuffer, numRTS, RTS to NULL, does it set the render targets to NULL, or does it skip the placement of the render targets.

Not that I have tried (never used UAVs with pixel shaders), but docs are somewhat clear.

Specify NULL to set none.

If that doesn't work, use a array with explicit NULLs.
 

Is there a way of treating the content of an UAV as a SRV?


Nope, it's a different view, you have to create one. IIRC one can create an arbitrary amount of views for any resource. You only need to make sure that the corresponding bind flag was set when creating the resource and you don't have read/write hazards. So in this case don't bind both the UAV and SRV at once. You won't need to anyway, UAVs are read and write. Not saying that it is always a good idea to do, sometimes a ping-pong approach with two resources is better or even the only possibility.

By the way: You made me curious. You don't mind posting links to that papers, please ?

Edited by unbird, 20 September 2013 - 01:53 PM.


#10 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 20 September 2013 - 01:56 PM

Unbird, thanks! (You too MJP, Phantom).

 

Paper: http://www.seas.upenn.edu/~pcozzi/OpenGLInsights/OpenGLInsights-SparseVoxelization.pdf

 

I've read one or two more, but I don't have their address right now, forgot them wacko.png ...

 

Thanks! Again...

-MIGI0027



#11 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 20 September 2013 - 02:01 PM

Btw. Why didn't they make another function of this, as they waste processing power by checking if the number of render targets is 0?

 

I know it's such a small operation, but everything counts. happy.png

 

Is there any specific reason for this?

 

-MIGI0027



#12 unbird   Crossbones+   -  Reputation: 4863

Like
0Likes
Like

Posted 20 September 2013 - 02:19 PM

Ah yeah, Crassin et.al, thanks. You're in for some challenge then if you really wanna go octrees (I for one haven't done hierarchical structures on the GPU yet). Good luck.
 
I got you additional food for thought. The GPU Pro 4 books also has a chapter about voxelization. You can save you the money: The Ph.D. thesis from Mr. Gaitatzes about it is available on his site. Nice, since papers are usually a bit... brief.

#13 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 20 September 2013 - 02:33 PM

Thanks for the link!



#14 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 21 September 2013 - 03:30 AM

An error has come up, which I am for now unable to understand (I understand it, just don't know why tongue.png ).

 

Error:

maximum ps_5_0 UAV register index (8) exceeded - note that the minimum index is 5.

Relevant shader code: (Some resources are repeated or have some weird name, but that's because I'm moving stuff around)

RWTexture3D<float> t_gi0 : register(t0); // GD: These are all faces of a cube map
RWTexture3D<float> t_gi1 : register(t1); // Though i could construct them to one final cube map
RWTexture3D<float> t_gi2 : register(t2); // But I'm too lazy to do that now...
RWTexture3D<float> t_gi3 : register(t3); // After it works I might change it
RWTexture3D<float> t_gi4 : register(t4); // These are only for reading
RWTexture3D<float> t_gi5 : register(t5); // They have already been written to...

// pos and dir are in texture space
// dir is the direction from the center of the voxel outward
// dir should be normalized
float4 voxelFetch(float3 pos, float3 dir, float lod)
{
	float4 sampleX =
		dir.x < 0.0
		? t_gi0[pos]
		: t_gi1[pos];
	
	float4 sampleY =
		dir.y < 0.0
		? t_gi2[pos]
		: t_gi3[pos];
	
	float4 sampleZ =
		dir.z < 0.0
		? t_gi4[pos]
		: t_gi5[pos];
	
	float3 sampleWeights = abs(dir);
	float invSampleMag = 1.0 / (sampleWeights.x + sampleWeights.y + sampleWeights.z + .0001);
	sampleWeights *= invSampleMag;
	
	float4 filtered = 
		sampleX * sampleWeights.x
		+ sampleY * sampleWeights.y
		+ sampleZ * sampleWeights.z;
	
	return filtered;
}

// origin, dir, and maxDist are in texture space
// dir should be normalized
// coneRatio is the cone diameter to height ratio (2.0 for 90-degree cone)
float4 voxelTraceCone(float3 origin, float3 dir, float coneRatio, float maxDist)
{
	float3 samplePos = origin;
	float4 accum = float4(0, 0, 0, 0);

	// the starting sample diameter
	float minDiameter = minVoxelDiameter;

	// push out the starting point to avoid self-intersection
	float startDist = minDiameter;
	
	float dist = startDist;

	[loop]
    [allow_uav_condition]
	while (dist <= maxDist && accum.w < 1.0)
	{
		// ensure the sample diameter is no smaller than the min
		// desired diameter for this cone (ensuring we always
		// step at least minDiameter each iteration, even for tiny
		// cones - otherwise lots of overlapped samples)
		float sampleDiameter = max(minDiameter, coneRatio * dist);
		
		// convert diameter to LOD
		// for example:
		// log2(1/256 * 256) = 0
		// log2(1/128 * 256) = 1
		// log2(1/64 * 256) = 2
		float sampleLOD = log2(sampleDiameter * minVoxelDiameterInv);
		
		float3 samplePos = origin + dir * dist;
		
		float4 sampleValue = voxelFetch(samplePos, -dir, sampleLOD);
		
		float sampleWeight = (1.0 - accum.w);
		accum += sampleValue * sampleWeight;
		
		dist += sampleDiameter;
	}
	
	// decompress color range to decode limited HDR
	accum.xyz *= 2.0;
	
	return accum;
}

#endif

Texture2D t_alphamap : register(t0);
Texture2D t_dffalpha : register(t1);
Texture2D gBuffer_Shadows : register(t2);
Texture2D t_rFront : register(t3);
Texture2D t_rBack : register(t4);

#if COMPILENORMALMAP == 1
Texture2D t_norm : register(t5);
#endif
#if COMPILESPECULARMAP == 1
Texture2D t_spec : register(t6);
#endif
#if COMPILEMASKMAP == 1
Texture2D t_mask : register(t7);
#endif

SamplerState ss;

Now what on earth did I do wrong?

-MIGI0027



#15 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 21 September 2013 - 08:53 AM

Ohh, wait, I might get it now...



#16 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 21 September 2013 - 09:26 AM

You can't tell me it's impossible to send more than 6 UAVs, or maybe I'm setting it to the wrong slots, with the wrong range.

 

OpenGL GLSL for what I'm trying to achieve:

layout(binding = 3) uniform sampler3D voxelTex[6];

HLSL:

RWTexture3D<float> t_gi[6];

wacko.png



#17 unbird   Crossbones+   -  Reputation: 4863

Like
0Likes
Like

Posted 21 September 2013 - 10:19 AM

Nope for SM5 it should be 8 UAVs. I checked with the compiler and the funny thing you are doing (at least two posts ago) is binding a read-write resource to a t# register, which would actually be a SRV. The compiler doesn't complain, it puts them into the UAV slots. Furthermore, a non RW resource goes into the SRV slots, even when marked with u# register. Again no compiler warning. It helps to look at the assembly. Example:

Buffer<float> In1 : register(u0);  // WRONG: It should be t#
RWBuffer<float> Out1: register(t0); // AGAIN WRONG: It should be u#
RWBuffer<float> Out2: register(t1);
RWBuffer<float> Out3: register(t2);
RWBuffer<float> Out4: register(t3);
RWBuffer<float> Out5: register(t4);
RWBuffer<float> Out6: register(t5);
RWBuffer<float> Out7: register(t6);
RWBuffer<float> Out8: register(t7);

[numthreads(4,4,4)]
void main(uint3 tid : SV_DispatchThreadID) 
{
    uint index = dot(tid, uint3(1, 8, 256)); // whatever
    Out1[index] = In1[index] + 
        Out2[index] +
        Out3[index] +
        Out4[index] +
        Out5[index] +
        Out6[index] +
        Out7[index] +
        Out8[index];        
}
 
This is where the slots end up:

// Generated by Microsoft (R) HLSL Shader Compiler 9.30.960.8229
//
//
//   fxc /Zi /T cs_5_0 /Fo stub.fxo /Fx stub.asm stub.fx
//
//
// Resource Bindings:
//
// Name                                 Type  Format         Dim Slot Elements
// ------------------------------ ---------- ------- ----------- ---- --------
// In1                               texture   float         buf    0        1
// Out1                                  UAV   float         buf    0        1
// Out2                                  UAV   float         buf    1        1
// Out3                                  UAV   float         buf    2        1
// Out4                                  UAV   float         buf    3        1
// Out5                                  UAV   float         buf    4        1
// Out6                                  UAV   float         buf    5        1
// Out7                                  UAV   float         buf    6        1
// Out8                                  UAV   float         buf    7        1
(Resource type texture means t# register, hence the t).
Same happens if one doesn't use the register keyword at all.

So I expect other RW-resources on your side - where you probably only read from. If so, don't use RW.

#18 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 21 September 2013 - 02:33 PM

Thanks, first time using UAVs wacko.png .

 

Though I have a problem, the u's seemed to solve some problems, the pixel shader outputs to 5 different render targets (Can be boiled down to 3-4). So then the UAVs expect to start at an index of at least u5, but the max UAV register index is 8, and I need six slots, but this doesn't obey the laws.
 

UAV registers live in the same name space as outputs, so they must be bound to at least u5, manual bind to slot u0 failed

What would be your suggestion?


Edited by Migi0027, 21 September 2013 - 02:34 PM.


#19 unbird   Crossbones+   -  Reputation: 4863

Like
0Likes
Like

Posted 21 September 2013 - 03:14 PM

Ah-ha, there's the culprit. Learned something new then - haven't used UAVs in pixel shaders so far.

 

Seems you have to tailor your setup accordingly. Multiple passes ? Combine two or more resources into one (say, use a bigger texture and "atlas"/split manually) ? How's it done in OpenGL ? Is there no such limit ?

 

For multiple passes look into both stream out (geometry shader) and append/consume buffers (special UAVs, hopefully they work in pixel shaders) maybe they can help you split the problem.

 

Admittedly, these are hints from a bird's eye view. This really sounds like a challenging problem. Look into alternatives: If something gets too cumbersome another approach is likely better than "shoehorning" it into a unsuitable API. That thesis I linked looks feasable with D3D11 (first variant using geometry shaders and three passes).



#20 MiniMigi   Crossbones+   -  Reputation: 1449

Like
0Likes
Like

Posted 21 September 2013 - 03:22 PM

*Still Thinking*

 

I hate having multiple passes, but that's just me... happy.png






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS