DX11/c++ loading Textures and using in shader
category: code [glöplog]
Hi,
iam a little lost, again...
Iam trying to load a texture and then access it in the pixel shader and Display/Use the Texture.
On c++ iam using the WICTexture-Loader and loading a Texture seams to be ok.
But how to i access the Texture on the Pixel Shader?
The Texture is a Font , and i want to use it to display some text.
How can i access a Pixel in the Texture?
Any Idear ? :-)
iam a little lost, again...
Iam trying to load a texture and then access it in the pixel shader and Display/Use the Texture.
On c++ iam using the WICTexture-Loader and loading a Texture seams to be ok.
Code:
// Texture Loader
ID3D11ShaderResourceView *Texture=NULL;
/*HRESULT CreateWICTextureFromFile(
_In_ ID3D11Device* d3dDevice,
_In_opt_ ID3D11DeviceContext* d3dContext,
_In_z_ const wchar_t* szFileName,
_Out_opt_ ID3D11Resource** texture,
_Out_opt_ ID3D11ShaderResourceView** textureView,
_In_ size_t maxsize = 0
);*/
hr = CreateWICTextureFromFile(g_d3dDevice, NULL, L"texture.bmp", NULL, &Texture, 4096);
if (FAILED(hr)) return false;
/*void PSSetShaderResources(
[in] UINT StartSlot,
[in] UINT NumViews,
[in, optional] ID3D11ShaderResourceView *const *ppShaderResourceViews
);*/
//g_d3dDevice->GetImmediateContext(&g_d3dDeviceContext);
g_d3dDeviceContext->PSSetShaderResources(0, 1, &Texture); // binding to t0
Texture->Release();
But how to i access the Texture on the Pixel Shader?
The Texture is a Font , and i want to use it to display some text.
How can i access a Pixel in the Texture?
Code:
Texture2D myTexture0 : register(t0);
//Texture2D myTexture1 : register(t1);
struct PS_IN {
float4 pos : SV_POSITION;
//float4 k : LOLIMASEMANTIC;
float3 color : COLOR;
};
struct PS_OUT {
float4 color : COLOR;
};
float4 SimplePixelShader(PS_IN input) : SV_Target {
PS_OUT output;
return (float4)output;
}
Any Idear ? :-)
Where are your vertices?
Do you have texture coordinates?
Do you have texture coordinates?
you'll need to call Sample() on your texture object. and you should also provide a SamplerState:
SamplerState samplerLinear : register( s0 );
in your SimplePixelshader just add:
float4 output.color = myTexture0.Sample( samplerLinear, envUV).rgb;
SamplerState samplerLinear : register( s0 );
in your SimplePixelshader just add:
float4 output.color = myTexture0.Sample( samplerLinear, envUV).rgb;
whoops, mistake: you should omit the ".rgb" in my previous post.
To create a sampler state look into ID3D11Device::CreateSamplerState, D3D11_SAMPLER_DESC and ID3D11DeviceContext::PSSetSamplers().
cheers.
To create a sampler state look into ID3D11Device::CreateSamplerState, D3D11_SAMPLER_DESC and ID3D11DeviceContext::PSSetSamplers().
cheers.
Code:
float4 SimplePixelShader(PS_IN input) : SV_Target
{
PS_OUT output;
output.color = myTexture0.Sample( samplerLinear, envUV);
return output.color;
}
You're clearly not using PS_OUT for anything good either, just get rid of it.
Okay i updatet my code to:
and
The Result is a white Box... No Texture :-(
Code:
// Texture Loader
ID3D11ShaderResourceView *Texture=NULL;
hr = CreateWICTextureFromFile(g_d3dDevice, NULL, L"texture.bmp", NULL, &Texture, 4096);
if (FAILED(hr)) return false;
g_d3dDeviceContext->PSSetShaderResources(0, 1, &Texture); // binding to t0
Texture->Release();
//Create Sampler State
D3D11_SAMPLER_DESC sd;
ZeroMemory(&sd, sizeof(D3D11_SAMPLER_DESC));
sd.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sd.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sd.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sd.ComparisonFunc = D3D11_COMPARISON_NEVER;
sd.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sd.MaxLOD = D3D11_FLOAT32_MAX;
ID3D11SamplerState *mSampler=NULL;
g_d3dDevice->CreateSamplerState(&sd, &mSampler);
g_d3dDeviceContext->PSSetSamplers(0,1,&mSampler); // binding to s0
mSampler->Release();
and
Code:
SamplerState samplerLinear : register(s0);
Texture2D myTexture0 : register(t0);
//Texture2D myTexture1 : register(t1);
struct PS_IN {
float4 pos : SV_POSITION;
//float4 k : LOLIMASEMANTIC;
float3 color : COLOR;
};
struct PS_OUT {
float4 color : COLOR;
};
float4 SimplePixelShader(PS_IN input) : SV_Target {
PS_OUT output;
output.color = myTexture0.Sample(samplerLinear, float2(0,1));
return (float4)output;
The Result is a white Box... No Texture :-(
Okay, now its working... :-)
Did the Magic ... :-))))
Code:
output.color = myTexture0.Sample(samplerLinear, float2(input.pos.r/512,input.pos.g/512));
Did the Magic ... :-))))
ehm yes, texture coords need to vary accross the triangle to see something you know ;)