SRV (Shader Resource View) - Pixel Shader에게 어떠한 텍스처를 보내줄 것인가?
- 파이프라인 과정에서 픽셀 쉐이더에 어떠한 텍스처를 넘겨줄 것인가?
기본적으로 GPU는 Raw texture 데이터를 바로 읽지 못하기에 SRV를 생성하여 접근 가능하게 한다!
다음 코드는 DDS파일을 읽어와서 그를 바탕으로 SRV를 생성하는 과정을 담은 코드이다. (Texture 읽는 라이브러리 사용) 후에 m_texureMap이란 map에 저장된 SRV를 파일의 path를 이용해 불러와서 shader와 mapping해주는 과정을 거친다.
void CTextureManager::LoadSetTexture() {
ScratchImage image;
for (auto path : TexturePaths)
{
HRESULT hr = LoadFromDDSFile(path.c_str(), DDS_FLAGS_NONE, nullptr, image);
if (FAILED(hr)) {
// 실패 시 로그 출력 후 continue 또는 return (필요에 따라)
continue;
}
const TexMetadata& metadata = image.GetMetadata();
ComPtr<ID3D11ShaderResourceView> textureView;
hr = CreateShaderResourceView(Device, image.GetImages(), image.GetImageCount(), metadata, textureView.GetAddressOf());
if (FAILED(hr)) {
continue;
}
m_textureMap[path] = textureView;
}
}
Sampler - DX가 텍스처를 어떻게 읽을 것인가?
셈플러는 DX가 텍스처를 어떠한 방식으로 읽을 것인지에 대한 정보를 담고 있다.
텍스처는 texel이라고 불리는 작은 점들로 되어있지만 쉐이더가 텍스처를 셈플링할때는 부동 소수점 uv좌표계로 계산하기에 이를 읽어줄 방식이 필요하다.
Textures are made of discrete pixels (texels), but shaders sample textures with floating-point UV coordinates.
Sampler를 생성하는 함수
void CTextureManager::CreateSamplerState() {
D3D11_SAMPLER_DESC sampDesc = {};
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; // 선형 필터링
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; // 랩 모드
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
HRESULT hr = Device->CreateSamplerState(&sampDesc, SamplerState.GetAddressOf());
if (FAILED(hr))
{
OutputDebugString(L"Failed to create sampler state\n");
}
}
아래는 hlsl로 작성된 Shader코드의 일부이다.
tex는 우리가 쉐이더에 넘겨준 텍스처이고 samplerState는 넘겨준 샘플러이다.
결론적으로 보면 텍스처에서 samplerState와 uv좌표를 이용해 색을 추출해내고 있다.
float4 PS(VS_OUTPUT input) : SV_TARGET
{
// 텍스처에서 샘플링하여 색상을 반환
float4 textureColor = tex.Sample(samplerState, input.TexCoord); // 텍스처 샘플링
// 검은색 (R, G, B가 모두 0인 경우) 날리기
if (textureColor.r <= 0.1f && textureColor.g <= 0.1f && textureColor.b <= 0.1f)
{
discard; // 검은색은 날림
}
// 텍스처 샘플링된 색상과 전달된 색상 혼합
return textureColor * input.Color; // 텍스처 색상에 기존 색상을 곱함
}
'DirectX11' 카테고리의 다른 글
[DX11] Matarial Sorting을 통한 최적화 (0) | 2025.03.29 |
---|---|
[DX11] Constant Buffer에 값 넘길 때 주의할 점(Shader로 Matarial값 넘길 때 주의할점) (0) | 2025.03.26 |
[DX11] Shader에 Vertex넘길 때 주의 해야 할 점!! (Shader 디버깅 Tip) (0) | 2025.03.25 |
[DX11] UV좌표를 활용한 Text 렌더링 (0) | 2025.03.19 |
[DX11] Texture Atlas, UV좌표계 (0) | 2025.03.19 |