0
votes

I am wondering how to actually sample the data I am passing to the shader file. I am using two methods, is it the same for both? Are there any resources online for me to actually read up on this sort of thing?

Compiling at 5.0 but the version number does not matter so much.


I have two methods to pass the data.

The first;

    for( UINT row = 0; row < textureDesc.Height; row++ )
    {
        UINT rowStart = row * mappedResource.RowPitch;
        for( UINT col = 0; col < textureDesc.Width; col++ )
        {
            //width * number of channels (r,g,b,a)
            UINT colStart = col * 4;
            pTexels[rowStart + colStart + 0] = 10.0f; // Red
            pTexels[rowStart + colStart + 1] = 10.0f; // Green
            pTexels[rowStart + colStart + 2] = 255.0f; // Blue
            pTexels[rowStart + colStart + 3] = 255.0f; // Alpha
        }
    }

The second;

    float elements[416][416];
    int elementsCount = 416*416;

    for( int i = 0; i < 416; i++ )
    {
        for( int k = 0; k < 416; k++ )
        {
            elements[i][k] = 0;
        }
    }

    memcpy(mappedResource.pData, elements, sizeof(float) * elementsCount);
1
It seems you create the texture manually, if you want to simplify the process, why not same it as file and load it by DirectX?zdd
@Dino (1) Sampling in shader does not cares about how exactly you copying data into texture. So if you're copying same data different ways it's all the same for HLSL. (2) It's not quite clear what you're asking to read up. You need just sample texture? Then use Texture.Sample(Sampler, texcoord). You need sampling tutorial? There is a plenty on google. Please, precise your question to get meaningful answers.Ivan Aksamentov - Drop
zdd I need to create it manually, it is updated every frame. @Drop I can simply just sample it and it will know that it contains a single float? Even if those floats are set using memcpy? My question really is how to access the data in the shader file. So to get element[x][y] it would be float val = Texture.Sample(Texture, texcoord)? I am just not sure how the GPU will interpret the data in the texture or what it is expecting so I am not sure how to read it in the shader file.Questioning
Sampling is a different than array access in many ways. If you'd looked the link above, you could see that Texture.Sample(Texture, texcoord) will return value of type depending on DXGI_FORMAT used when texture was created. There are some issues when using textures as arrays. If you need somewhat like C array in HLSL side, use StructuredBuffers (requires SM 5.0, limited on SM 4.0 ).Ivan Aksamentov - Drop
@Drop I replied before I read the link, I've got it reading the data I pass to the texture I just forgot to reply back. Are StructuredBuffers more efficient than sampling textures being used as arrays? - As a side note thanks for all the help.Questioning

1 Answers

1
votes

Seems that I missed an important part of all of this.

When creating a texture, in the texture description, the Format is the type that will be returned when the object is sampled. Many thanks to Drop for the help.