Ivan Belyavtsev
04.03.2022
model
classD3D12_STATIC_SAMPLER_DESC sampler_desc{};
sampler_desc.Filter = D3D12_FILTER_ANISOTROPIC;
sampler_desc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
sampler_desc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
sampler_desc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
sampler_desc.MipLODBias = 0;
sampler_desc.MaxAnisotropy = 16;
sampler_desc.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
sampler_desc.BorderColor =
D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
sampler_desc.MinLOD = 0;
sampler_desc.MaxLOD = D3D12_FLOAT32_MAX;
sampler_desc.ShaderRegister = 0;
sampler_desc.RegisterSpace = 0;
sampler_desc.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
Texture2D g_texture : register(t0);
SamplerState g_sampler : register(s0);
struct PSInput
{
float4 position : SV_POSITION;
float4 color: COLOR;
float2 uv : TEXCOORD;
};
PSInput VSMain(float4 position : POSITION, float4 normal: NORMAL, float4 ambient : COLOR0, float4 diffuse : COLOR1, float4 emissive : COLOR2, float4 texcoords : TEXCOORD)
{
PSInput result;
result.position = mul(mwpMatrix, position);
result.color = ambient;
result.uv = texcoords.xy;
return result;
}
float4 PSMain_texture(PSInput input) : SV_TARGET
{
return g_texture.Sample(g_sampler, input.uv);
}
std::string full_name = std::filesystem::absolute(
model->get_per_shape_texture_files()[s]).string();
int tex_width, tex_height, tex_channels;
unsigned char* image =
stbi_load(full_name.c_str(), &tex_width,
&tex_height, &tex_channels, STBI_rgb_alpha);
if (image == nullptr)
{
throw std::runtime_error("Can't load texture");
}
tex_width
, tex_height
DXGI_FORMAT_R8G8B8A8_UNORM
GetRequiredIntermediateSize(textures[s].Get(), 0, 1)
tex_width * STBI_rgb_alpha
texture_data.RowPitch * tex_height
PIXEL_SHADER_RESOURCE
D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc{};
srv_desc.Shader4ComponentMapping =
D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srv_desc.Format = texture_desc.Format;
srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srv_desc.Texture2D.MipLevels = 1;
device->CreateShaderResourceView(textures[s].Get(), &srv_desc,
cbv_srv_handle);
if (!model->get_per_shape_texture_files()[s].empty()) {
cbv_srv_handle.InitOffsetted(
cbv_srv_heap->GetGPUDescriptorHandleForHeapStart(),
static_cast<INT>(s + 1), cbv_srv_descriptor_size);
command_list->SetGraphicsRootDescriptorTable(1,
cbv_srv_handle);
command_list->SetPipelineState(
pipeline_state_texture.Get());
} else {
command_list->SetPipelineState(pipeline_state.Get());
}