Skip to content

Commit 1c30028

Browse files
committed
App:
* added entire sponza scene assets * uses a texture lookup table to determine which textures go with which material LibRenderer: * General - typo and minor fixes * Hardware states - render and sampler states are now correctly reset upon device reset * Hardware states - anisotropic filtering fixes * Resource manager - utility functions for searching for loaded models and textures in the resource manager by using the path to the compiled files which were loaded * Shaders - support for setting per texture sampler states when binding * Textures - some functions renamed * Textures - support for configuring per texture sampler states (used when enabling a shader which binds this texture to a shader input to set the proper sampler states) Model compiler: * Assimp postprocess flags update Texture compiler: * now applies vertical flip for source images with a lower left origin
1 parent 6678c31 commit 1c30028

File tree

109 files changed

+634
-408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+634
-408
lines changed

GITechDemo/Code/AppMain/GITechDemo/GITechDemo.cpp

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
2222
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window c
2323
HWND hWnd;
2424

25-
VertexBuffer* VBuf = nullptr;
26-
Texture* Tex = nullptr;
25+
// Usage: TextureLUT[TextureType][MaterialIndex] = TextureIndex
26+
std::vector<unsigned int> TextureLUT[Model::TextureDesc::TT_UNKNOWN];
27+
28+
ResourceManager* resMan = nullptr;
2729
ShaderTemplate* VShd = nullptr;
2830
ShaderTemplate* PShd = nullptr;
2931
ShaderInput* VSInput = nullptr;
@@ -286,17 +288,50 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
286288
// NB: when creating a resource, the resource manager
287289
// will return an index, with which you can retrieve
288290
// a pointer to the actual resource
289-
ResourceManager* resMan = renderer->GetResourceManager();
291+
resMan = renderer->GetResourceManager();
290292

291293
// First of all, load a model file
292294
const unsigned int modelIdx = resMan->CreateModel("sponza\\sponza.lrm");
293295
model = resMan->GetModel(modelIdx);
294296

297+
// Now load all the textures referenced by the model and build a lookup table
298+
for (unsigned int tt = Model::TextureDesc::TT_NONE; tt < Model::TextureDesc::TT_UNKNOWN; tt++)
299+
TextureLUT[tt].resize(model->arrMaterial.size(), -1);
300+
301+
for (unsigned int i = 0; i < model->arrMaterial.size(); i++)
302+
{
303+
for (unsigned int j = 0; j < model->arrMaterial[i]->arrTexture.size(); j++)
304+
{
305+
std::string filePath = "sponza\\" + model->arrMaterial[i]->arrTexture[j]->szFilePath;
306+
307+
const unsigned int offset = (unsigned int)filePath.rfind('.');
308+
if (offset != std::string::npos)
309+
filePath.replace(offset, UINT_MAX, ".lrt");
310+
311+
unsigned int texIdx = -1;
312+
texIdx = resMan->FindTexture(filePath.c_str());
313+
if (texIdx == -1)
314+
{
315+
texIdx = resMan->CreateTexture(filePath.c_str());
316+
317+
// Set the sampling filter to linearly interpolate between texels and mips
318+
// and set the maximum anisotropy level for maximum quality
319+
Texture* tex = resMan->GetTexture(texIdx);
320+
tex->SetAnisotropy(MAX_ANISOTROPY);
321+
tex->SetFilter(SF_MIN_MAG_LINEAR_MIP_LINEAR);
322+
}
323+
324+
assert(texIdx != -1);
325+
assert(TextureLUT[model->arrMaterial[i]->arrTexture[j]->eTexType][i] == -1 ||
326+
TextureLUT[model->arrMaterial[i]->arrTexture[j]->eTexType][i] == texIdx);
327+
328+
if(TextureLUT[model->arrMaterial[i]->arrTexture[j]->eTexType][i] == -1)
329+
TextureLUT[model->arrMaterial[i]->arrTexture[j]->eTexType][i] = texIdx;
330+
}
331+
}
332+
295333
// Read the contents of the file
296334
std::ifstream t("simple.hlsl");
297-
//std::ifstream t("normal.hlsl");
298-
//std::ifstream t("normal_scale.hlsl");
299-
//std::ifstream t("pos_only.hlsl");
300335
int length;
301336
t.seekg(0, std::ios::end); // go to the end
302337
length = (int)t.tellg(); // report location (this is the length)
@@ -328,9 +363,6 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
328363
const unsigned int psiIdx = resMan->CreateShaderInput(PShd);
329364
PSInput = resMan->GetShaderInput(psiIdx);
330365

331-
const unsigned int texIdx = resMan->CreateTexture("sponza\\textures\\sponza_curtain_diff.lrt");
332-
Tex = resMan->GetTexture(texIdx);
333-
334366
return TRUE;
335367
}
336368

@@ -452,14 +484,6 @@ void RenderScene()
452484
VSInput->SetMatrix4x4(handle, matWVP[2] * matWVP[1] * matWVP[0]);
453485
}
454486

455-
if (PSInput->GetInputHandleByName("Tex", handle))
456-
{
457-
// Set the texture as an input to the pixel shader
458-
PSInput->SetTexture(handle, Tex);
459-
// Also set the sampling filter to linearly interpolate between texels and mips
460-
renderer->GetSamplerStateManager()->SetFilter(PSInput->GetInputDesc(handle).nRegisterIndex, SF_MIN_MAG_LINEAR_MIP_LINEAR);
461-
}
462-
463487
// Clear our backbuffer so that we have a nice black background
464488
// and a depth value that's as far away as possible (at our Z far, which is 2000.f)
465489
renderer->Clear(Vec4f(0.f, 0.f, 0.f, 0.f), 1.f, 0);
@@ -474,17 +498,34 @@ void RenderScene()
474498
// return value of BeginFrame() before submiting draw calls!
475499
if (renderer->BeginFrame())
476500
{
477-
// Set the vertex and pixel shaders used to render the scene
478-
VShd->Enable(VSInput);
479-
PShd->Enable(PSInput);
480-
501+
renderer->GetRenderStateManager()->SetAlphaTestEnable(true);
502+
renderer->GetRenderStateManager()->SetAlphaFunc(CMP_GREATER);
503+
renderer->GetRenderStateManager()->SetAlphaRef(0.5f);
504+
481505
// Draw the contents of the vertex buffers
482506
for (unsigned int mesh = 0; mesh < model->arrMesh.size(); mesh++)
507+
{
508+
const unsigned int diffTexIdx = TextureLUT[Model::TextureDesc::TT_DIFFUSE][model->arrMesh[mesh]->nMaterialIdx];
509+
if (diffTexIdx != -1)
510+
{
511+
Texture* Tex = resMan->GetTexture(diffTexIdx);
512+
if (PSInput->GetInputHandleByName("Tex", handle) && Tex != nullptr)
513+
{
514+
// Set the texture as an input to the pixel shader
515+
PSInput->SetTexture(handle, Tex);
516+
}
517+
}
518+
519+
// Set the vertex and pixel shaders used to render the scene
520+
VShd->Enable(VSInput);
521+
PShd->Enable(PSInput);
522+
483523
renderer->DrawVertexBuffer(model->arrMesh[mesh]->pVertexBuffer);
484524

485-
// Disable shaders
486-
VShd->Disable();
487-
PShd->Disable();
525+
// Disable shaders
526+
VShd->Disable();
527+
PShd->Disable();
528+
}
488529

489530
// End the frame
490531
renderer->EndFrame();

GITechDemo/Code/Libraries/LibRenderer/Common/RenderState.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ namespace LibRendererDll
9999

100100
LIBRENDERER_DLL const Fill GetFillMode() const { return m_eFillMode; }
101101

102+
virtual LIBRENDERER_DLL void Reset() = 0;
102103

103104
protected:
104105
RenderState();

GITechDemo/Code/Libraries/LibRenderer/Common/ResourceData.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ namespace LibRendererDll
140140
CMP_LESSEQUAL,
141141
CMP_GREATER,
142142
CMP_NOTEQUAL,
143-
CMP_GREATEQUAL,
143+
CMP_GREATEREQUAL,
144144
CMP_ALWAYS,
145145

146146
CMP_MAX, // DO NOT USE! INTERNAL USAGE ONLY!
@@ -208,6 +208,11 @@ namespace LibRendererDll
208208

209209
// SAMPLER STATES /////////////////////////////////////////
210210

211+
enum
212+
{
213+
MAX_ANISOTROPY = 16 // Maximum anisotropy level
214+
};
215+
211216
enum
212217
{
213218
MAX_NUM_VSAMPLERS = 4, // Maximum number of texture samplers: vs3.0 has 4, vs2.0 has 0
@@ -622,6 +627,8 @@ namespace LibRendererDll
622627
std::vector<Mesh*> arrMesh; // Meshes associated with the model
623628
std::vector<Material*> arrMaterial; // Materials associated with the mesh
624629

630+
std::string szSourceFile; // File from which model was loaded
631+
625632
LIBRENDERER_DLL friend std::ostream& operator<<(std::ostream& output_out, const Model& model_in);
626633
friend std::istream& operator>>(std::istream& s_in, Model& model_out);
627634

GITechDemo/Code/Libraries/LibRenderer/Common/ResourceManager.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ const unsigned int ResourceManager::CreateTexture(const char* pathToFile)
152152
{
153153
texFile >> *GetTexture(texIdx);
154154
texFile.close();
155+
GetTexture(texIdx)->m_szSourceFile = pathToFile;
155156
return texIdx;
156157
}
157158
else
@@ -171,6 +172,7 @@ const unsigned int ResourceManager::CreateModel(const char* pathToFile)
171172
{
172173
modelFile >> *m_arrModel.back();
173174
modelFile.close();
175+
m_arrModel.back()->szSourceFile = pathToFile;
174176
return (unsigned int)m_arrModel.size() - 1;
175177
}
176178
else
@@ -179,4 +181,32 @@ const unsigned int ResourceManager::CreateModel(const char* pathToFile)
179181
m_arrModel.pop_back();
180182
return -1;
181183
}
182-
}
184+
}
185+
186+
const unsigned int LibRendererDll::ResourceManager::FindTexture(const char * pathToFile, const bool strict)
187+
{
188+
for (unsigned int i = 0; i < m_arrTexture.size(); i++)
189+
if (m_arrTexture[i]->m_szSourceFile == pathToFile)
190+
return i;
191+
192+
if (!strict)
193+
for (unsigned int i = 0; i < m_arrTexture.size(); i++)
194+
if (m_arrTexture[i]->m_szSourceFile.find(pathToFile) != std::string::npos)
195+
return i;
196+
197+
return -1;
198+
}
199+
200+
const unsigned int LibRendererDll::ResourceManager::FindModel(const char * pathToFile, const bool strict)
201+
{
202+
for (unsigned int i = 0; i < m_arrModel.size(); i++)
203+
if (m_arrModel[i]->szSourceFile == pathToFile)
204+
return i;
205+
206+
if(!strict)
207+
for (unsigned int i = 0; i < m_arrModel.size(); i++)
208+
if (m_arrModel[i]->szSourceFile.find(pathToFile) != std::string::npos)
209+
return i;
210+
211+
return -1;
212+
}

GITechDemo/Code/Libraries/LibRenderer/Common/ResourceManager.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace LibRendererDll
7373
LIBRENDERER_DLL const unsigned int CreateShaderTemplate(ShaderProgram* const shaderProgram);
7474

7575
// Create a texture
76-
virtual LIBRENDERER_DLL const unsigned int CreateTexture(const PixelFormat texFormat, const TexType texType, const unsigned int sizeX, const unsigned int sizeY = 1, const unsigned int sizeZ = 1, const unsigned int mipmapLevelCount = 0, const BufferUsage usage = BU_TEXTURE) = 0;
76+
virtual LIBRENDERER_DLL const unsigned int CreateTexture(const PixelFormat texFormat, const TexType texType, const unsigned int sizeX, const unsigned int sizeY = 1, const unsigned int sizeZ = 1, const unsigned int mipCount = 0, const BufferUsage usage = BU_TEXTURE) = 0;
7777
// Create a texture and load data from an image file
7878
LIBRENDERER_DLL const unsigned int CreateTexture(const char* pathToFile);
7979

@@ -94,6 +94,13 @@ namespace LibRendererDll
9494
LIBRENDERER_DLL RenderTarget* const GetRenderTarget(const unsigned int idx) const { assert(idx < m_arrRenderTarget.size()); return m_arrRenderTarget[idx]; }
9595
LIBRENDERER_DLL Model* const GetModel(const unsigned int idx) const { assert(idx < m_arrModel.size()); return m_arrModel[idx]; }
9696

97+
// Utility functions for finding a resource by its' original file name from which it was loaded
98+
// NB: If the strict parameter is set to false, it will first try to find an exact match, then try
99+
// to find a substring in the resource's original file path; default behaviour (i.e. strict = true)
100+
// is to only search for an exact match
101+
LIBRENDERER_DLL const unsigned int FindTexture(const char* pathToFile, const bool strict = true);
102+
LIBRENDERER_DLL const unsigned int FindModel(const char* pathToFile, const bool strict = true);
103+
97104
// Destroy various resource types, freeing memory
98105
// NB: Destroying a high level resource also destroys the associated low level resources
99106
LIBRENDERER_DLL void ReleaseVertexFormat(const unsigned int idx) { assert(idx < m_arrVertexFormat.size()); delete m_arrVertexFormat[idx]; m_arrVertexFormat[idx] = nullptr; }

GITechDemo/Code/Libraries/LibRenderer/Common/ResourceSerialization.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -538,15 +538,15 @@ std::ostream& LibRendererDll::operator<<(std::ostream& output_out, Texture& tex_
538538

539539
output_out.write((char*)&tex_in.m_eTexFormat, sizeof(PixelFormat));
540540
output_out.write((char*)&tex_in.m_eTexType, sizeof(TexType));
541-
output_out.write((char*)&tex_in.m_nMipmapLevelCount, sizeof(unsigned int));
541+
output_out.write((char*)&tex_in.m_nMipCount, sizeof(unsigned int));
542542

543543
output_out.write((char*)&tex_in.m_nDimensionCount, sizeof(unsigned int));
544-
for (unsigned int i = 0; i < tex_in.m_nMipmapLevelCount; i++)
544+
for (unsigned int i = 0; i < tex_in.m_nMipCount; i++)
545545
output_out.write((char*)&tex_in.m_nDimension[i], sizeof(Vec<unsigned int, 3U>));
546-
for (unsigned int i = 0; i < tex_in.m_nMipmapLevelCount; i++)
547-
output_out.write((char*)&tex_in.m_nMipmapLevelByteCount[i], sizeof(unsigned int));
548-
for (unsigned int i = 0; i < tex_in.m_nMipmapLevelCount; i++)
549-
output_out.write((char*)&tex_in.m_nMipmapLevelOffset[i], sizeof(unsigned int));
546+
for (unsigned int i = 0; i < tex_in.m_nMipCount; i++)
547+
output_out.write((char*)&tex_in.m_nMipSizeBytes[i], sizeof(unsigned int));
548+
for (unsigned int i = 0; i < tex_in.m_nMipCount; i++)
549+
output_out.write((char*)&tex_in.m_nMipOffset[i], sizeof(unsigned int));
550550

551551
return output_out;
552552
}
@@ -557,15 +557,15 @@ std::istream& LibRendererDll::operator>>(std::istream& s_in, Texture& tex_out)
557557

558558
s_in.read((char*)&tex_out.m_eTexFormat, sizeof(PixelFormat));
559559
s_in.read((char*)&tex_out.m_eTexType, sizeof(TexType));
560-
s_in.read((char*)&tex_out.m_nMipmapLevelCount, sizeof(unsigned int));
560+
s_in.read((char*)&tex_out.m_nMipCount, sizeof(unsigned int));
561561

562562
s_in.read((char*)&tex_out.m_nDimensionCount, sizeof(unsigned int));
563-
for (unsigned int i = 0; i < tex_out.m_nMipmapLevelCount; i++)
563+
for (unsigned int i = 0; i < tex_out.m_nMipCount; i++)
564564
s_in.read((char*)&tex_out.m_nDimension[i], sizeof(Vec<unsigned int, 3U>));
565-
for (unsigned int i = 0; i < tex_out.m_nMipmapLevelCount; i++)
566-
s_in.read((char*)&tex_out.m_nMipmapLevelByteCount[i], sizeof(unsigned int));
567-
for (unsigned int i = 0; i < tex_out.m_nMipmapLevelCount; i++)
568-
s_in.read((char*)&tex_out.m_nMipmapLevelOffset[i], sizeof(unsigned int));
565+
for (unsigned int i = 0; i < tex_out.m_nMipCount; i++)
566+
s_in.read((char*)&tex_out.m_nMipSizeBytes[i], sizeof(unsigned int));
567+
for (unsigned int i = 0; i < tex_out.m_nMipCount; i++)
568+
s_in.read((char*)&tex_out.m_nMipOffset[i], sizeof(unsigned int));
569569

570570
tex_out.Bind();
571571

GITechDemo/Code/Libraries/LibRenderer/Common/SamplerState.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ namespace LibRendererDll
5858
LIBRENDERER_DLL const SamplerAddressingMode GetAddressingModeW(const unsigned int slot) const { assert(slot < MAX_NUM_PSAMPLERS); return m_tCurrentState[slot].eAddressingMode[2]; }
5959
LIBRENDERER_DLL const SamplerAddressingMode GetAddressingMode(const unsigned int slot) const { return (GetAddressingModeU(slot) == GetAddressingModeV(slot) && GetAddressingModeV(slot) == GetAddressingModeW(slot)) ? GetAddressingModeU(slot) : SAM_NONE; }
6060

61+
virtual LIBRENDERER_DLL void Reset() = 0;
62+
6163
protected:
6264
SamplerState();
6365
virtual ~SamplerState();

GITechDemo/Code/Libraries/LibRenderer/Common/ShaderInput.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,10 @@ const Matrix44f ShaderInput::GetMatrix4x4(const unsigned int handle, const unsig
378378
return GetMatrix<float, 4, 4>(handle, idx);
379379
}
380380

381-
382381
void ShaderInput::SetTexture(const unsigned int handle, const Texture* const tex)
383382
{
384383
assert(handle < m_pShaderTemplate->m_arrInputDesc.size());
385-
ShaderInputDesc desc = m_pShaderTemplate->m_arrInputDesc[handle];
384+
const ShaderInputDesc& desc = m_pShaderTemplate->m_arrInputDesc[handle];
386385
assert(tex);
387386
assert(
388387
(desc.eInputType == IT_SAMPLER) ||

GITechDemo/Code/Libraries/LibRenderer/Common/ShaderProgram.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace LibRendererDll
5555
virtual const unsigned int GetConstantColumnCount(const unsigned int handle) const = 0;
5656
virtual const unsigned int GetConstantArrayElementCount(const unsigned int handle) const = 0;
5757
virtual const unsigned int GetConstantStructMemberCount(const unsigned int handle) const = 0;
58-
virtual const unsigned int GetConstantByteCount(const unsigned int handle) const = 0;
58+
virtual const unsigned int GetConstantSizeBytes(const unsigned int handle) const = 0;
5959

6060
void SetValue(const RegisterType registerType, const unsigned int registerIndex, const void* const data, const unsigned int registerCount);
6161
virtual void SetFloat(const unsigned int registerIndex, const float* const data, const unsigned int registerCount) = 0;

GITechDemo/Code/Libraries/LibRenderer/Common/ShaderTemplate.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void ShaderTemplate::DescribeShaderInputs()
5252
inputDesc.nRows = m_pProgram->GetConstantRowCount(i);
5353
inputDesc.nColumns = m_pProgram->GetConstantColumnCount(i);
5454
inputDesc.nArrayElements = m_pProgram->GetConstantArrayElementCount(i);
55-
inputDesc.nBytes = m_pProgram->GetConstantByteCount(i);
55+
inputDesc.nBytes = m_pProgram->GetConstantSizeBytes(i);
5656
inputDesc.nOffsetInBytes = offset;
5757
offset += inputDesc.nRegisterCount * sizeof(float) * 4u;
5858

@@ -107,7 +107,17 @@ void ShaderTemplate::Enable(ShaderInput* shaderInput)
107107
//m_pProgram->SetTexture(m_arrInputDesc[i].nRegisterIndex, (Texture*)*(unsigned long long*)(shaderInput->GetData() + m_arrInputDesc[i].nOffsetInBytes));
108108
Texture* tex = (Texture*)*(unsigned long long*)(shaderInput->GetData() + m_arrInputDesc[i].nOffsetInBytes);
109109
if (tex)
110+
{
110111
tex->Enable(m_arrInputDesc[i].nRegisterIndex);
112+
SamplerState* ssm = Renderer::GetInstance()->GetSamplerStateManager();
113+
ssm->SetAnisotropy(m_arrInputDesc[i].nRegisterIndex, tex->GetAnisotropy());
114+
ssm->SetLodBias(m_arrInputDesc[i].nRegisterIndex, tex->GetLodBias());
115+
ssm->SetFilter(m_arrInputDesc[i].nRegisterIndex, tex->GetFilter());
116+
ssm->SetBorderColor(m_arrInputDesc[i].nRegisterIndex, tex->GetBorderColor());
117+
ssm->SetAddressingModeU(m_arrInputDesc[i].nRegisterIndex, tex->GetAddressingModeU());
118+
ssm->SetAddressingModeV(m_arrInputDesc[i].nRegisterIndex, tex->GetAddressingModeV());
119+
ssm->SetAddressingModeW(m_arrInputDesc[i].nRegisterIndex, tex->GetAddressingModeW());
120+
}
111121
}
112122
else
113123
assert(false); // shouldn't happen

0 commit comments

Comments
 (0)