Question

I have a Texture2D readily available; I have an apparently-working shader texture sampler and shader texture variable that I can put that Texture2D in.

The only problem is, I don't know how to load a texture into a shader in DirectX11 - And either Google is being unhelpful, or it's just my inability to construct good search terms.

What I need: Code that will take a Texture2D and load it into a shader. A link on how to do so, for example.

Anyway, here's my shader code:

cbuffer CameraSet : register(b0)
{
    float4x4 ViewProj ;
} ;
cbuffer MeshSet : register(b1)
{
    float4x4 World ;
    texture2D Texture ;
    SamplerState MeshTextureSampler
    {
        Filter = MIN_MAG_MIP_LINEAR ;
        AddressU = WRAP ;
        AddressV = WRAP ;
    } ;
} ;


struct VShaderOutput
{
    float4 WorldPosition : POSITION ;
    float4 ScreenPosition : SV_POSITION ;
    float2 UV : TEXCOORD;
} ;

VShaderOutput VShader( float4 position : POSITION, float2 uv : TEXCOORD )
{
    VShaderOutput r ;

    r.WorldPosition = mul( position, World ) ;
    r.ScreenPosition = mul( r.WorldPosition, ViewProj ) ;
    r.UV.x = abs( uv.x ) ;
    r.UV.y = abs( uv.y ) ;

    return r ;
}


struct PShaderOutput
{
    float4 SV_Target : SV_TARGET ;
    float SV_Depth : SV_DEPTH ;
};

PShaderOutput PShader( VShaderOutput input )
{
    PShaderOutput r ;

    r.SV_Depth = input.ScreenPosition.z;

    r.SV_Target = Texture.Sample( MeshTextureSampler, input.UV ) ;

    return r ;
}

Thanks.

Was it helpful?

Solution

...If it's

context.PixelShader.SetShaderResource(TextureShaderResourceView, 0);

I think I just answered my own question. But why does an array value of '0' work?

OTHER TIPS

I use 2 shader Resources:

hlsl:

Texture2D<float4> Self : register(t0);
Texture2D<float4> Other : register(t1);

cs:

device.ImmediateContext.ComputeShader.SetShaderResource(resourceViewSelf, 0);
device.ImmediateContext.ComputeShader.SetShaderResource(resourceViewOther, 1);

somehow it works...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top