Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
-Remove some unneeded and unused code
-Optimize water TexAniMap
  • Loading branch information
SaiyansKing committed Oct 9, 2021
1 parent 11766cb commit d46dcfc
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 252 deletions.
11 changes: 1 addition & 10 deletions D3D11Engine/D3D11GShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@

using namespace DirectX;

D3D11GShader::D3D11GShader() {

// Insert into state-map
ID = D3D11ObjectIdManager::AddGShader( this );
}
D3D11GShader::D3D11GShader() {}

D3D11GShader::~D3D11GShader() {
// Remove from state map
D3D11ObjectIdManager::EraseGShader( this );

for ( unsigned int i = 0; i < ConstantBuffers.size(); i++ ) {
delete ConstantBuffers[i];
}
Expand Down Expand Up @@ -74,9 +67,7 @@ XRESULT D3D11GShader::LoadShader( const char* geometryShader, const std::vector<
D3D11GraphicsEngineBase* engine = (D3D11GraphicsEngineBase*)Engine::GraphicsEngine;

Microsoft::WRL::ComPtr<ID3DBlob> gsBlob;

LogInfo() << "Compiling geometry shader: " << geometryShader;
File = geometryShader;

if ( !createStreamOutFromVS ) {
// Compile shaders
Expand Down
10 changes: 0 additions & 10 deletions D3D11Engine/D3D11GShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class D3D11GShader {
D3D11GShader();
~D3D11GShader();


/** Loads shader */
XRESULT LoadShader( const char* geometryShader, const std::vector<D3D_SHADER_MACRO>& makros = std::vector<D3D_SHADER_MACRO>(), bool createStreamOutFromVS = false, int soLayout = 0 );

Expand All @@ -22,20 +21,11 @@ class D3D11GShader {
/** Returns the shader */
Microsoft::WRL::ComPtr<ID3D11GeometryShader> GetShader() { return GeometryShader.Get(); }

/** Returns this textures ID */
UINT16 GetID() { return ID; };

private:

/** Compiles the shader from file and outputs error messages if needed */
HRESULT CompileShaderFromFile( const CHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut, const std::vector<D3D_SHADER_MACRO>& makros );

Microsoft::WRL::ComPtr<ID3D11GeometryShader> GeometryShader;
std::vector<D3D11ConstantBuffer*> ConstantBuffers;

std::string File;

/** ID of this shader */
UINT16 ID;
};

31 changes: 10 additions & 21 deletions D3D11Engine/D3D11GraphicsEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ XRESULT D3D11GraphicsEngine::DrawWorldMesh( bool noTextures ) {

// Check surface type
if ( worldMesh.first.Info->MaterialType == MaterialInfo::MT_Water ) {
FrameWaterSurfaces[worldMesh.first.Material].push_back( worldMesh.second );
FrameWaterSurfaces[aniTex].push_back( worldMesh.second );
continue;
}

Expand Down Expand Up @@ -2429,7 +2429,7 @@ XRESULT D3D11GraphicsEngine::DrawWorldMeshW( bool noTextures ) {

// Check surface type
if ( info->MaterialType == MaterialInfo::MT_Water ) {
//FrameWaterSurfaces[textureInfo.first] = textureInfo.second.second;
FrameWaterSurfaces[textureInfo.first] = textureInfo.second.second;
textureInfo.second.second.resize( 0 );
continue;
}
Expand Down Expand Up @@ -2580,7 +2580,8 @@ void D3D11GraphicsEngine::DrawWaterSurfaces() {
SetupVS_ExMeshDrawCall();
SetupVS_ExConstantBuffer();

ActiveVS->GetConstantBuffer()[1]->UpdateBuffer( &XMMatrixIdentity() );
float totalTime = Engine::GAPI->GetTotalTime();
ActiveVS->GetConstantBuffer()[1]->UpdateBuffer( &totalTime, 4 );
ActiveVS->GetConstantBuffer()[1]->BindToVertexShader( 1 );

// Do Z-prepass on the water to make sure only the visible pixels will get drawn instead of multiple layers of water
Expand Down Expand Up @@ -2629,26 +2630,14 @@ void D3D11GraphicsEngine::DrawWaterSurfaces() {
// Bind reflection cube
GetContext()->PSSetShaderResources( 3, 1, ReflectionCube.GetAddressOf() );
for ( auto const& it : FrameWaterSurfaces ) {
if ( zCTexture* texture = it.first->GetTexture() ) {
// Bind diffuse
texture->CacheIn( -1 ); // Force immediate cache in, because water
// is important!
texture->Bind( 0 );
}
// Bind diffuse
zCTexture* texture = it.first;
texture->CacheIn( -1 ); // Force immediate cache in, because water
// is important!
texture->Bind( 0 );

// Draw surfaces
for ( auto const& mesh : it.second ) {
if ( it.first->HasTexAniMap() ) {
float time = Engine::GAPI->GetTotalTime();
DirectX::XMFLOAT2 texAniMap = it.first->GetTexAniMapDelta();
float textureAniMap[2] = { texAniMap.x * time, texAniMap.y * time };
ActiveVS->GetConstantBuffer()[1]->UpdateBuffer( &textureAniMap, 8 );
ActiveVS->GetConstantBuffer()[1]->BindToVertexShader( 1 );
} else {
float textureAniMap[2] = {};
ActiveVS->GetConstantBuffer()[1]->UpdateBuffer( &textureAniMap, 8 );
ActiveVS->GetConstantBuffer()[1]->BindToVertexShader( 1 );
}

DrawVertexBufferIndexedUINT( nullptr, nullptr,
mesh->Indices.size(), mesh->BaseIndexLocation );
}
Expand Down
5 changes: 1 addition & 4 deletions D3D11Engine/D3D11GraphicsEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,8 @@ class D3D11GraphicsEngine : public D3D11GraphicsEngineBase {
/** The editorcontrols */
std::unique_ptr<D2DView> UIView;

/** Map of texture/index */
stdext::unordered_map<zCTexture*, int> TexArrayIndexByTexture;

/** List of water surfaces for this frame */
std::unordered_map<zCMaterial*, std::vector<WorldMeshInfo*>> FrameWaterSurfaces;
std::unordered_map<zCTexture*, std::vector<WorldMeshInfo*>> FrameWaterSurfaces;

/** List of worldmeshes we have to render using alphablending */
std::vector<std::pair<MeshKey, MeshInfo*>> FrameTransparencyMeshes;
Expand Down
106 changes: 0 additions & 106 deletions D3D11Engine/D3D11GraphicsEngineBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,112 +12,6 @@ class D3D11HDShader;
class D3D11Texture;
class D3D11GShader;

namespace D3D11ObjectIDs {

/** Map to get a texture by ID */
__declspec(selectany) std::map<UINT16, D3D11Texture*> TextureByID;


__declspec(selectany) std::map<UINT8, D3D11PShader*> PShadersByID;

__declspec(selectany) std::map<UINT8, D3D11VShader*> VShadersByID;

__declspec(selectany) std::map<UINT8, D3D11HDShader*> HDShadersByID;

__declspec(selectany) std::map<UINT8, D3D11GShader*> GShadersByID;

__declspec(selectany) std::map<UINT8, D3D11DepthBufferState*> DepthStateByID;

__declspec(selectany) std::map<UINT8, D3D11BlendStateInfo*> BlendStateByID;

__declspec(selectany) std::map<UINT8, D3D11RasterizerStateInfo*> RasterizerStateByID;

__declspec(selectany) struct CounterStruct {
CounterStruct() {
memset( this, 0, sizeof( CounterStruct ) );
}

int PShadersCounter;
int TextureCounter;
int VShadersCounter;
int HDShadersCounter;
int GShadersCounter;
int DepthStateCounter;
int BlendStateCounter;
int RasterizerCounter;
} Counters;
}

class D3D11ObjectIdManager {
public:
static UINT8 AddVShader( D3D11VShader* s ) {
std::unique_lock<std::mutex> lock( VShadersByIDMutex );
UINT8 id = 0;
if ( !D3D11ObjectIDs::VShadersByID.empty() ) {
id = D3D11ObjectIDs::Counters.VShadersCounter++;
}
D3D11ObjectIDs::VShadersByID[id] = s;
return id;
}
static UINT8 AddPShader( D3D11PShader* s ) {
std::unique_lock<std::mutex> lock( PShadersByIDMutex );
UINT8 id = 0;
if ( !D3D11ObjectIDs::PShadersByID.empty() ) {
id = D3D11ObjectIDs::Counters.PShadersCounter++;
}
D3D11ObjectIDs::PShadersByID[id] = s;
return id;
}
static UINT8 AddHDShader( D3D11HDShader* s ) {
std::unique_lock<std::mutex> lock( HDShadersByIDMutex );
UINT8 id = 0;
if ( !D3D11ObjectIDs::HDShadersByID.empty() ) {
id = D3D11ObjectIDs::Counters.HDShadersCounter++;
}
D3D11ObjectIDs::HDShadersByID[id] = s;
return id;
}
static UINT8 AddGShader( D3D11GShader* s ) {
std::unique_lock<std::mutex> lock( GShadersByIDMutex );
UINT8 id = 0;
if ( !D3D11ObjectIDs::GShadersByID.empty() ) {
id = D3D11ObjectIDs::Counters.GShadersCounter++;
}
D3D11ObjectIDs::GShadersByID[id] = s;
return id;
}

static void EraseVShader( D3D11VShader* s ) {
std::unique_lock<std::mutex> lock( VShadersByIDMutex );
for ( auto it = D3D11ObjectIDs::VShadersByID.begin(); it != D3D11ObjectIDs::VShadersByID.end();) {
if ( it->second == s ) { it = D3D11ObjectIDs::VShadersByID.erase( it ); } else { ++it; }
}
}
static void ErasePShader( D3D11PShader* s ) {
std::unique_lock<std::mutex> lock( PShadersByIDMutex );
for ( auto it = D3D11ObjectIDs::PShadersByID.begin(); it != D3D11ObjectIDs::PShadersByID.end();) {
if ( it->second == s ) { it = D3D11ObjectIDs::PShadersByID.erase( it ); } else { ++it; }
}
}
static void EraseHDShader( D3D11HDShader* s ) {
std::unique_lock<std::mutex> lock( HDShadersByIDMutex );
for ( auto it = D3D11ObjectIDs::HDShadersByID.begin(); it != D3D11ObjectIDs::HDShadersByID.end();) {
if ( it->second == s ) { it = D3D11ObjectIDs::HDShadersByID.erase( it ); } else { ++it; }
}
}
static void EraseGShader( D3D11GShader* s ) {
std::unique_lock<std::mutex> lock( GShadersByIDMutex );
for ( auto it = D3D11ObjectIDs::GShadersByID.begin(); it != D3D11ObjectIDs::GShadersByID.end();) {
if ( it->second == s ) { it = D3D11ObjectIDs::GShadersByID.erase( it ); } else { ++it; }
}
}
private:
inline static std::mutex VShadersByIDMutex;
inline static std::mutex PShadersByIDMutex;
inline static std::mutex HDShadersByIDMutex;
inline static std::mutex GShadersByIDMutex;
};

struct RenderToTextureBuffer;
struct RenderToDepthStencilBuffer;
class D3D11ShaderManager;
Expand Down
10 changes: 1 addition & 9 deletions D3D11Engine/D3D11HDShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,15 @@ using namespace DirectX;

D3D11HDShader::D3D11HDShader() {
ConstantBuffers = std::vector<D3D11ConstantBuffer*>();

// Insert into state-map
ID = D3D11ObjectIdManager::AddHDShader( this );
}


D3D11HDShader::~D3D11HDShader() {
// Remove from state map
D3D11ObjectIdManager::EraseHDShader( this );

for ( unsigned int i = 0; i < ConstantBuffers.size(); i++ ) {
delete ConstantBuffers[i];
}
}


//--------------------------------------------------------------------------------------
// Find and compile the specified shader
//--------------------------------------------------------------------------------------
Expand Down Expand Up @@ -79,7 +72,6 @@ XRESULT D3D11HDShader::LoadShader( const char* hullShader, const char* domainSha

if ( Engine::GAPI->GetRendererState().RendererSettings.EnableDebugLog )
LogInfo() << "Compilling hull shader: " << hullShader;
File = hullShader;

// Compile shaders
if ( FAILED( CompileShaderFromFile( hullShader, "HSMain", "hs_5_0", hsBlob.GetAddressOf() ) ) ) {
Expand Down Expand Up @@ -121,4 +113,4 @@ void D3D11HDShader::Unbind() {
/** Returns a reference to the constantBuffer vector*/
std::vector<D3D11ConstantBuffer*>& D3D11HDShader::GetConstantBuffer() {
return ConstantBuffers;
}
}
10 changes: 0 additions & 10 deletions D3D11Engine/D3D11HDShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ class D3D11ConstantBuffer;
class D3D11VertexBuffer;
class D3D11HDShader {
public:
static std::map<UINT8, D3D11HDShader*> ShadersByID;

D3D11HDShader();
~D3D11HDShader();

Expand All @@ -28,20 +26,12 @@ class D3D11HDShader {
/** Returns the shader */
Microsoft::WRL::ComPtr<ID3D11DomainShader> GetDShader() { return DomainShader.Get(); }

/** Returns this textures ID */
UINT16 GetID() { return ID; };
private:

/** Compiles the shader from file and outputs error messages if needed */
HRESULT CompileShaderFromFile( const CHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut );

Microsoft::WRL::ComPtr<ID3D11HullShader> HullShader;
Microsoft::WRL::ComPtr<ID3D11DomainShader> DomainShader;
std::vector<D3D11ConstantBuffer*> ConstantBuffers;

std::string File;

/** ID of this shader */
UINT16 ID;
};

10 changes: 1 addition & 9 deletions D3D11Engine/D3D11PShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@

using namespace DirectX;

D3D11PShader::D3D11PShader() {

// Insert into state-map
ID = D3D11ObjectIdManager::AddPShader( this );
}
D3D11PShader::D3D11PShader() {}

D3D11PShader::~D3D11PShader() {
// Remove from state map
D3D11ObjectIdManager::ErasePShader( this );

for ( unsigned int i = 0; i < ConstantBuffers.size(); i++ ) {
delete ConstantBuffers[i];
}
Expand Down Expand Up @@ -79,7 +72,6 @@ XRESULT D3D11PShader::LoadShader( const char* pixelShader, const std::vector<D3D

if ( Engine::GAPI->GetRendererState().RendererSettings.EnableDebugLog )
LogInfo() << "Compilling pixel shader: " << pixelShader;
File = pixelShader;

// Compile shaders
if ( FAILED( CompileShaderFromFile( pixelShader, "PSMain", "ps_4_0", psBlob.GetAddressOf(), makros ) ) ) {
Expand Down
9 changes: 0 additions & 9 deletions D3D11Engine/D3D11PShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,11 @@ class D3D11PShader {
/** Returns the shader */
Microsoft::WRL::ComPtr<ID3D11PixelShader> GetShader() { return PixelShader.Get(); }

/** Returns this textures ID */
UINT16 GetID() { return ID; };

private:

/** Compiles the shader from file and outputs error messages if needed */
HRESULT CompileShaderFromFile( const CHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut, const std::vector<D3D_SHADER_MACRO>& makros );

Microsoft::WRL::ComPtr<ID3D11PixelShader> PixelShader;
std::vector<D3D11ConstantBuffer*> ConstantBuffers;

std::string File;

/** ID of this shader */
UINT16 ID;
};

Loading

0 comments on commit d46dcfc

Please sign in to comment.