Confusion about using textures

Started by
8 comments, last by matt77hias 6 years, 5 months ago

So I'm a little confused about how to use textures in DirectX

I'm currently loading a image from a file, which creates a ID3D11Texture2D but is this the final step to actually use the texture?

Looking at this tutorial I see that there is something called a ID3D11ShaderResourceView. What is the purpose of this? It looks like thats how they send the texture to the shader, but is it possible to just bind the ID3D11Texture2D and use it directly?

If not does this mean I need to create a ID3D11ShaderResourceView for each texture I load?

 

Advertisement

Constant buffer, vertex buffer, index buffers and textures are all resources in D3D11. For constant, vertex and index buffers, you can only bind them in a specific way to the pipeline. Textures are much more versatile. So after creating the resource, you need to create a view (or even multiple views) in order to specify how you are going to use the texture resource. You have shader resource views (all shader stages), unordered access views (compute shader stage and ouput merger stage), render target views (output merger stage) and depth-stencil views (output merger stage). Furthermore you also need to specify the region (which region of the underlying texture) and type (how are you going to interpret the underlying texture), etc.

48 minutes ago, noodleBowl said:

If not does this mean I need to create a ID3D11ShaderResourceView for each texture I load?

You need one view at least to bind and thus use a texture in the pipeline.

🧙

5 hours ago, matt77hias said:

You need one view at least to bind and thus use a texture in the pipeline.

Is there a function to set the texture the view is going to consider?

I'm just seeing CreateShaderResourceView which requires a ID3D11Resource which I believe is the ID3D11Texture2D I create

A view is permanently bound to a single resource (the one that you pass to Create*View). It's very common to create a shader resource view right after the texture resource is created, and hold onto both of them together. If you need other views (render target view, unordered access view) you can also create these right after the texture is created.

7 hours ago, noodleBowl said:

Is there a function to set the texture the view is going to consider?

I'm just seeing CreateShaderResourceView which requires a ID3D11Resource which I believe is the ID3D11Texture2D I create

ID3D11Texture2D is a subclass of ID3D11Resource

🧙

8 hours ago, noodleBowl said:

Is there a function to set the texture the view is going to consider?

I'm just seeing CreateShaderResourceView which requires a ID3D11Resource which I believe is the ID3D11Texture2D I create

If you want to create a shader resource view (SRV) for your complete Texture2D and your Texture2D is not typeless, just pass a nullptr for the pointer to the D3D11_SHADER_RESOURCE_VIEW_DESC. So you basically pass a pointer to your Texture2D and an address for your SRV.

Note that if you follow the tutorials of RasterTek, you already faced something very similar: the creation of your depth buffer texture and depth stencil view (DSV). Similar for the back buffer (although this is different from creating a Texture2D since the swap chain creates the resource for you) and the render target view (RTV).

🧙

13 hours ago, matt77hias said:

If you want to create a shader resource view (SRV) for your complete Texture2D and your Texture2D is not typeless, just pass a nullptr for the pointer to the D3D11_SHADER_RESOURCE_VIEW_DESC. So you basically pass a pointer to your Texture2D and an address for your SRV.

I think we ended up talking about 2 different things. To me that looks like what I need to do to create a shader resource view

I thought when you said

On 10/25/2017 at 2:08 PM, matt77hias said:

You need one view at least to bind and thus use a texture in the pipeline.

I was under the impression I could do something like


//Loads the textures and returns a ID3D11Texture2D
ID3D11Texture2D* textureTruck = loadTexture("truck.png");
ID3D11Texture2D* textureBoat = loadTexture("boat.png");

//Create the shader resource view
graphicsDevice->device->CreateShaderResourceView(textureTruck, NULL, &shaderResourceView);

//Later on when I want to use a texture (Don't think this is possible. Totally made up)
graphicsDevice->device->ShaderResourceViewSetTexture(shaderResourceView, textureTruck); //Use the truck texture

//Later on switch to the boat and bind the shader view to use that texture
graphicsDevice->device->ShaderResourceViewSetTexture(shaderResourceView, textureBoat);

But like @MJP stated it looks like the above is not possible and its a 1:1 relationship between a shader resource view and a texture

On 10/25/2017 at 2:08 PM, matt77hias said:

You need one view at least to bind and thus use a texture in the pipeline.

 

4 hours ago, noodleBowl said:

I was under the impression I could do something like



//Loads the textures and returns a ID3D11Texture2D
ID3D11Texture2D* textureTruck = loadTexture("truck.png");
ID3D11Texture2D* textureBoat = loadTexture("boat.png");

//Create the shader resource view
graphicsDevice->device->CreateShaderResourceView(textureTruck, NULL, &shaderResourceView);

//Later on when I want to use a texture (Don't think this is possible. Totally made up)
graphicsDevice->device->ShaderResourceViewSetTexture(shaderResourceView, textureTruck); //Use the truck texture

//Later on switch to the boat and bind the shader view to use that texture
graphicsD

He meant one view per texture.  As in either a rendertarget view, or a shaderresource view, or a unordered access view.  If you make only one of these per texture then you are limited to how you can bind the texture to the pipeline(what stage of the pipeline).  Either as a render target, or a "classic" texture, or a 'generic data structure'.  If you want to know why you'd want to have more than one type of view associated with a texture read up on the basics of shadow maps.

-potential energy is easily made kinetic-

Thanks @Infinisearch, exactly my point :)

🧙

This topic is closed to new replies.

Advertisement