How to access a Texture inside of a Vertex Shader. SharpDX 4.2

by Ninekorn   Last Updated April 15, 2019 09:13 AM

This is a continuation of the question found here:

vertex manipulation in instanced shader hlsl SHARPDX 4.2

Now that I was able to modify the positions of the vertex in a Vertex Shader through Vertex Bindings, I need to be able to nullify the triangles/vertex that aren't needed in the scene. In order to do that, I had multiple options:

Option 1: Create a ByteMap and send the data through a constant buffer inside of a geometry shader. The ByteMap wouldn't consist of the same ByteMap as I am sending through the vertexShader. It would consist again of 0's and 1's but each vertex NOT at an extremity of a chunk would be nullified in a Geometry shader, if that is even possible. I have tried this option and it failed as I do not know how to properly send the byteMap through a constant buffer.

Option 2: Create a Texture1D and access the Texture1D from within the VertexShader and THEN transfer the code to a Geometry shader if it works. I am currently trying this iteration but I am failing and need help.

Here's what I got so far:

THIS CODE IS CREATING THE TEXTURE1D with the BYTEMAP.

                    int totalWidth = SC_Globals.tinyChunkWidth * SC_Globals.tinyChunkHeight * SC_Globals.tinyChunkDepth * SC_Globals.numberOfInstancesPerObjectInWidth * SC_Globals.numberOfInstancesPerObjectInHeight * SC_Globals.numberOfInstancesPerObjectInDepth;

                    Texture1DDescription desc = new Texture1DDescription();
                    desc.Width = totalWidth;
                    desc.MipLevels = 1;
                    desc.ArraySize = 1;
                    desc.Format = Format.B8G8R8A8_UNorm;
                    desc.Usage = ResourceUsage.Default;
                    desc.BindFlags = BindFlags.ShaderResource;
                    desc.CpuAccessFlags = CpuAccessFlags.None;
                    desc.OptionFlags = ResourceOptionFlags.None;

                    int[] byteMap = chunkdat.arrayOfSomeMap.SelectMany(a => a).ToArray();
                    byte[] arrayOfBytes = new byte[byteMap.Length];

                    for (int i = 0; i < byteMap.Length; i++)
                    {
                        arrayOfBytes[i] = (byte)byteMap[i];
                    }

                    int rowPitch = 4 * desc.Width;
                    DataStream dataStream = new DataStream(rowPitch, true, true);
                    dataStream.WriteRange(arrayOfBytes, 0, arrayOfBytes.Length);
                    DataBox data = new DataBox(dataStream.DataPointer, rowPitch, rowPitch);

                    DataBox[] arrayOfBoxes = new DataBox[1];
                    arrayOfBoxes[0] = data;

                    Texture1D mapTexture = new Texture1D(device, desc, arrayOfBoxes);

                    textureView = new ShaderResourceView(device, mapTexture);

Inside of the Vertex Shader I use this:

  int x = input.indexPos.x; 
	int y = input.indexPos.y; 
	int z = input.indexPos.z;
  
	int currentIndex = x + (tinyChunkWidth * (y + (tinyChunkHeight * z)));
  
  int mainX = input.instancePosition1.x % 4;
	int mainY = input.instancePosition1.y % 4;
	int mainZ = input.instancePosition1.z % 4;

	int indexMain  = mainX + mapWidth *( mainY + mapHeight * mainZ);
	int indexTotal = (indexMain * 64) + (currentIndex);

	float colordata = mapTexture[indexTotal]; // doesn't work
	float colordata  = mapTexture.Load(indexTotal);  // doesn't work
  

I don't know why I cannot get the colorData out of the texture inside of the VertexShader. Can anyone help?

Thank you, ninekorn

EDIT: This doesn't work either: https://stackoverflow.com/questions/8514695/can-you-look-sample-a-texture-in-a-vertex-shader

Those examples on Microsoft Website don't work either: https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl-to-samplelevel

I am coding in SharpDX 4.2 but I am trying to get answers from everywhere. I tried tex2Dlod and it doesn't work either. tex1Dlod neither. https://forum.unity.com/threads/how-to-sample-a-texture-in-vertex-shader.513816/

Tags : c# sharpdx


Related Questions


Updated March 20, 2017 18:13 PM

Updated June 15, 2017 22:13 PM

Updated July 11, 2015 16:05 PM

Updated January 31, 2017 14:05 PM

Updated June 19, 2017 20:13 PM