Search code examples
directxshaderhlsl

Modify global texture3d variable in HLSL


How would I go about making a global texture3d variable which I can modify in the geometry shader (HLSL)?

The size would be 64x64x64.

I would fill it up once in the first renderpass and use it in the second renderpass.

Any help appreciated. Preferably using D10.

EDIT:

I'm making a marching cube terrain shader by following this https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch01.html guide (See paragraph 1.4). I got the geometry shader done it's just that I don't have normals and the guide suggests that I can create them by storing data in texture3D but I don't know how I can put data in a texture3D. (The guide is using DX10).


Solution

  • You are probably thinking of a compute shader. In this case, you would declare your texture as an RWTexture3D in the compute shader, and then sample it in your render pass as a Texture3D. There are many resources/tutorials/examples on the internet about compute shaders to help you get started.

    You cannot modify textures with a geometry shader in D3D11. The only thing that a geometry shader can do is generate primitives. However, as @Adam Miles answer states, it is possible to write the RWTexture3D in a geometry shader in D3D11.1. While this is feasible, unless you are also generating primitives with your geometry shader, it probably makes most sense to do it in a compute shader.

    If you are working in D3D 10.x via the D3D11 API, you can still use compute shaders, although you cannot use an RWTexture3D type. You would have to view it as an RWByteAddressBuffer, and index it yourself which would be slightly awkward. You don't necessarily need to generate texture data within a shader, you can write into the texture with the CPU, by mapping it and writing the data directly. See ID3D10Texture3D::Map for more information.