@@ -122,7 +122,6 @@ void Mesh::InitConstantBuffer() {
122122void Mesh::UpdateConstantBuffer () {
123123 UINT nWVPSize = (sizeof (this ->m_wvp ) + 255 ) & ~255 ;
124124 this ->m_wvp .World = XMMatrixTranspose (XMMatrixIdentity ());
125- this ->m_wvp .World *= XMMatrixTranspose (XMMatrixScaling (0 .1f , 0 .1f , 0 .1f ));
126125 this ->m_wvp .World *= XMMatrixTranspose (XMMatrixRotationX (XMConvertToRadians (this ->m_transform .rotation .x )));
127126 this ->m_wvp .World *= XMMatrixTranspose (XMMatrixRotationY (XMConvertToRadians (this ->m_transform .rotation .y )));
128127 this ->m_wvp .World *= XMMatrixTranspose (XMMatrixRotationZ (XMConvertToRadians (this ->m_transform .rotation .z )));
@@ -164,7 +163,7 @@ void Mesh::Render() {
164163 Descriptor wvpDesc = this ->m_cbv_srvHeap ->GetDescriptor (this ->m_nWvpIndex );
165164
166165 this ->m_list ->SetGraphicsRootDescriptorTable (0 , this ->m_samplerDescriptor .gpuHandle );
167- this ->m_list ->SetGraphicsRootDescriptorTable (2 , wvpDesc.gpuHandle );
166+ this ->m_list ->SetGraphicsRootDescriptorTable (3 , wvpDesc.gpuHandle );
168167
169168 int i = 0 ;
170169 for (D3D12_VERTEX_BUFFER_VIEW vbv : this ->m_VBVs ) {
@@ -173,8 +172,11 @@ void Mesh::Render() {
173172 this ->m_list ->IASetIndexBuffer (&ibv);
174173
175174 UINT nTextureIndex = this ->m_textureIndices [i];
175+ UINT nMetalIndex = this ->m_nORMIndices [i];
176176 Descriptor texDesc = this ->m_cbv_srvHeap ->GetDescriptor (nTextureIndex);
177+ Descriptor metalDesc = this ->m_cbv_srvHeap ->GetDescriptor (nMetalIndex);
177178 this ->m_list ->SetGraphicsRootDescriptorTable (1 , texDesc.gpuHandle );
179+ this ->m_list ->SetGraphicsRootDescriptorTable (2 , metalDesc.gpuHandle );
178180
179181 this ->m_list ->DrawIndexedInstanced (this ->m_indices [i].size (), 1 , 0 , 0 , 0 );
180182 i++;
@@ -189,22 +191,27 @@ void Mesh::InitPipeline() {
189191 UINT nPixelSize = this ->m_shader ->GetBuffer (SHADER_BUFFER::PIXEL, lpPixel);
190192
191193 CD3DX12_DESCRIPTOR_RANGE albedoRange;
194+ CD3DX12_DESCRIPTOR_RANGE ORMRange;
192195 CD3DX12_DESCRIPTOR_RANGE samplerRange;
193196 CD3DX12_DESCRIPTOR_RANGE wvpRange;
194197 albedoRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1 , 0 );
198+ ORMRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1 , 1 );
195199 samplerRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 1 , 0 );
196200 wvpRange.Init (D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1 , 0 );
197201
198202 CD3DX12_ROOT_PARAMETER albedoParam;
203+ CD3DX12_ROOT_PARAMETER ORMParam;
199204 CD3DX12_ROOT_PARAMETER samplerParam;
200205 CD3DX12_ROOT_PARAMETER wvpParam;
201206 albedoParam.InitAsDescriptorTable (1 , &albedoRange, D3D12_SHADER_VISIBILITY_PIXEL);
207+ ORMParam.InitAsDescriptorTable (1 , &ORMRange, D3D12_SHADER_VISIBILITY_PIXEL);
202208 samplerParam.InitAsDescriptorTable (1 , &samplerRange, D3D12_SHADER_VISIBILITY_PIXEL);
203209 wvpParam.InitAsDescriptorTable (1 , &wvpRange, D3D12_SHADER_VISIBILITY_VERTEX);
204210
205211 D3D12_ROOT_PARAMETER rootParams[] = {
206212 samplerParam,
207213 albedoParam,
214+ ORMParam,
208215 wvpParam
209216 };
210217
@@ -254,7 +261,8 @@ void Mesh::InitPipeline() {
254261 plDesc.RTVFormats [0 ] = DXGI_FORMAT_B8G8R8A8_UNORM;
255262 plDesc.RTVFormats [1 ] = DXGI_FORMAT_B8G8R8A8_UNORM;
256263 plDesc.RTVFormats [2 ] = DXGI_FORMAT_B8G8R8A8_UNORM;
257- plDesc.NumRenderTargets = 3 ;
264+ plDesc.RTVFormats [3 ] = DXGI_FORMAT_B8G8R8A8_UNORM;
265+ plDesc.NumRenderTargets = 4 ;
258266 plDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
259267 plDesc.SampleDesc .Count = 8 ;
260268 plDesc.SampleMask = UINT32_MAX;
@@ -281,9 +289,10 @@ void Mesh::D3D12Init(D3D12* renderer) {
281289void Mesh::InitSampler (D3D12* renderer) {
282290 this ->m_cbv_srvHeap = renderer->m_cbvSrvHeap ;
283291 UINT nNumTextures = this ->m_textures .size ();
284- this ->m_cbv_srvHeap ->Allocate (this ->m_textures .size ());
292+ UINT nNumORMTextures = this ->m_ORMTextures .size ();
293+ this ->m_cbv_srvHeap ->Allocate (nNumTextures + nNumORMTextures);
285294 UINT nLastIndex = m_cbv_srvHeap->GetLastDescriptorIndex ();
286- UINT nFirstIndex = nLastIndex - nNumTextures + 1 ;
295+ UINT nFirstIndex = nLastIndex - nNumTextures;
287296
288297 UINT nActualIndex = nFirstIndex;
289298 for (std::pair<UINT, ComPtr<ID3D12Resource>> resource : this ->m_textures ) {
@@ -295,9 +304,23 @@ void Mesh::InitSampler(D3D12* renderer) {
295304
296305 Descriptor resDesc = m_cbv_srvHeap->GetDescriptor (nActualIndex);
297306 this ->m_textureIndices [resource.first ] = nActualIndex;
307+
308+ this ->m_dev ->CreateShaderResourceView (resource.second .Get (), &srvDesc, resDesc.cpuHandle );
298309 nActualIndex++;
310+ }
311+
312+ for (std::pair<UINT, ComPtr<ID3D12Resource>> resource : this ->m_ORMTextures ) {
313+ D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = { };
314+ srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
315+ srvDesc.Texture2D .MipLevels = 1 ;
316+ srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
317+ srvDesc.Format = resource.second ->GetDesc ().Format ;
318+
319+ Descriptor resDesc = m_cbv_srvHeap->GetDescriptor (nActualIndex);
320+ this ->m_nORMIndices [resource.first ] = nActualIndex;
299321
300322 this ->m_dev ->CreateShaderResourceView (resource.second .Get (), &srvDesc, resDesc.cpuHandle );
323+ nActualIndex++;
301324 }
302325
303326 renderer->m_samplerHeap ->Allocate (1 );
@@ -374,14 +397,28 @@ void Mesh::LoadModel(std::string filename) {
374397 this ->m_vertices [i] = vertices;
375398
376399 aiString texPath;
377- if (material->GetTextureCount (aiTextureType_DIFFUSE) > 0 && material->GetTexture (aiTextureType_DIFFUSE, 0 , &texPath) == AI_SUCCESS) {
400+ aiString metalPath;
401+ if (material->GetTextureCount (aiTextureType_BASE_COLOR) > 0 && material->GetTexture (aiTextureType_DIFFUSE, 0 , &texPath) == AI_SUCCESS) {
378402 const aiTexture* texture = scene->GetEmbeddedTexture (texPath.C_Str ());
379403 if (texture != nullptr ) {
380404 ResourceManager* resMgr = ResourceManager::GetInstance ();
381405 ComPtr<ID3D12Resource> resource;
382406 resMgr->LoadTexture ((BYTE*)texture->pcData , texture->mWidth , texture->mFilename .C_Str (), resource);
407+ resource->SetName (L" Mesh Base color" );
383408 this ->m_textures [i] = resource;
384409 }
385410 }
411+
412+ if (material->GetTextureCount (aiTextureType_METALNESS) > 0 && material->GetTexture (aiTextureType_METALNESS, 0 , &metalPath) == AI_SUCCESS) {
413+ const aiTexture* texture = scene->GetEmbeddedTexture (metalPath.C_Str ());
414+
415+ if (texture != nullptr ) {
416+ ResourceManager* resMgr = ResourceManager::GetInstance ();
417+ ComPtr<ID3D12Resource> resource;
418+ resMgr->LoadTexture ((BYTE*)texture->pcData , texture->mWidth , texture->mFilename .C_Str (), resource);
419+ resource->SetName (L" Mesh Metallic Roughness" );
420+ this ->m_ORMTextures [i] = resource;
421+ }
422+ }
386423 }
387424}
0 commit comments