Permalink
Browse files

GPU: Use more typesafe shader IDs.

Since they really are fairly distinct, let's make sure we can't mix them
accidentally.
  • Loading branch information...
unknownbrackets committed Dec 2, 2017
1 parent 5c5dd5c commit 4731a2918c0b7174322be472444b0e7a8d90da85
View
@@ -156,6 +156,24 @@ struct ShaderID {
}
};
struct VShaderID : ShaderID {
VShaderID() : ShaderID() {
}
explicit VShaderID(ShaderID &src) {
memcpy(d, src.d, sizeof(d));
}
};
struct FShaderID : ShaderID {
FShaderID() : ShaderID() {
}
explicit FShaderID(ShaderID &src) {
memcpy(d, src.d, sizeof(d));
}
};
bool CanUseHardwareTransform(int prim);
void ComputeVertexShaderID(ShaderID *id, uint32_t vertexType, bool useHWTransform);
@@ -19,6 +19,6 @@
#include "GPU/D3D11/FragmentShaderGeneratorD3D11.h"
#include "GPU/Directx9/PixelShaderGeneratorDX9.h"
void GenerateFragmentShaderD3D11(const ShaderID &id, char *buffer, ShaderLanguage lang) {
void GenerateFragmentShaderD3D11(const FShaderID &id, char *buffer, ShaderLanguage lang) {
DX9::GenerateFragmentShaderHLSL(id, buffer, lang);
}
@@ -19,4 +19,4 @@
#include "GPU/Common/ShaderId.h"
void GenerateFragmentShaderD3D11(const ShaderID &id, char *buffer, ShaderLanguage lang);
void GenerateFragmentShaderD3D11(const FShaderID &id, char *buffer, ShaderLanguage lang);
@@ -38,7 +38,7 @@
#include "GPU/D3D11/VertexShaderGeneratorD3D11.h"
#include "GPU/D3D11/D3D11Util.h"
D3D11FragmentShader::D3D11FragmentShader(ID3D11Device *device, D3D_FEATURE_LEVEL featureLevel, ShaderID id, const char *code, bool useHWTransform)
D3D11FragmentShader::D3D11FragmentShader(ID3D11Device *device, D3D_FEATURE_LEVEL featureLevel, FShaderID id, const char *code, bool useHWTransform)
: device_(device), id_(id), failed_(false), useHWTransform_(useHWTransform), module_(0) {
source_ = code;
@@ -63,7 +63,7 @@ std::string D3D11FragmentShader::GetShaderString(DebugShaderStringType type) con
}
}
D3D11VertexShader::D3D11VertexShader(ID3D11Device *device, D3D_FEATURE_LEVEL featureLevel, ShaderID id, const char *code, int vertType, bool useHWTransform, bool usesLighting)
D3D11VertexShader::D3D11VertexShader(ID3D11Device *device, D3D_FEATURE_LEVEL featureLevel, VShaderID id, const char *code, int vertType, bool useHWTransform, bool usesLighting)
: device_(device), id_(id), failed_(false), useHWTransform_(useHWTransform), module_(nullptr), usesLighting_(usesLighting) {
source_ = code;
@@ -178,8 +178,8 @@ void ShaderManagerD3D11::BindUniforms() {
}
void ShaderManagerD3D11::GetShaders(int prim, u32 vertType, D3D11VertexShader **vshader, D3D11FragmentShader **fshader, bool useHWTransform) {
ShaderID VSID;
ShaderID FSID;
VShaderID VSID;
FShaderID FSID;
if (gstate_c.IsDirty(DIRTY_VERTEXSHADER_STATE)) {
gstate_c.Clean(DIRTY_VERTEXSHADER_STATE);
@@ -268,7 +268,7 @@ std::string ShaderManagerD3D11::DebugGetShaderString(std::string id, DebugShader
switch (type) {
case SHADER_TYPE_VERTEX:
{
auto iter = vsCache_.find(shaderId);
auto iter = vsCache_.find(VShaderID(shaderId));
if (iter == vsCache_.end()) {
return "";
}
@@ -277,7 +277,7 @@ std::string ShaderManagerD3D11::DebugGetShaderString(std::string id, DebugShader
case SHADER_TYPE_FRAGMENT:
{
auto iter = fsCache_.find(shaderId);
auto iter = fsCache_.find(FShaderID(shaderId));
if (iter == fsCache_.end()) {
return "";
}
@@ -31,7 +31,7 @@ class D3D11PushBuffer;
class D3D11FragmentShader {
public:
D3D11FragmentShader(ID3D11Device *device, D3D_FEATURE_LEVEL featureLevel, ShaderID id, const char *code, bool useHWTransform);
D3D11FragmentShader(ID3D11Device *device, D3D_FEATURE_LEVEL featureLevel, FShaderID id, const char *code, bool useHWTransform);
~D3D11FragmentShader();
const std::string &source() const { return source_; }
@@ -49,12 +49,12 @@ class D3D11FragmentShader {
std::string source_;
bool failed_;
bool useHWTransform_;
ShaderID id_;
FShaderID id_;
};
class D3D11VertexShader {
public:
D3D11VertexShader(ID3D11Device *device, D3D_FEATURE_LEVEL featureLevel, ShaderID id, const char *code, int vertType, bool useHWTransform, bool usesLighting);
D3D11VertexShader(ID3D11Device *device, D3D_FEATURE_LEVEL featureLevel, VShaderID id, const char *code, int vertType, bool useHWTransform, bool usesLighting);
~D3D11VertexShader();
const std::string &source() const { return source_; }
@@ -81,7 +81,7 @@ class D3D11VertexShader {
bool failed_;
bool useHWTransform_;
bool usesLighting_;
ShaderID id_;
VShaderID id_;
};
class D3D11PushBuffer;
@@ -123,10 +123,10 @@ class ShaderManagerD3D11 : public ShaderManagerCommon {
ID3D11DeviceContext *context_;
D3D_FEATURE_LEVEL featureLevel_;
typedef std::map<ShaderID, D3D11FragmentShader *> FSCache;
typedef std::map<FShaderID, D3D11FragmentShader *> FSCache;
FSCache fsCache_;
typedef std::map<ShaderID, D3D11VertexShader *> VSCache;
typedef std::map<VShaderID, D3D11VertexShader *> VSCache;
VSCache vsCache_;
char *codeBuffer_;
@@ -144,6 +144,6 @@ class ShaderManagerD3D11 : public ShaderManagerCommon {
D3D11FragmentShader *lastFShader_;
D3D11VertexShader *lastVShader_;
ShaderID lastFSID_;
ShaderID lastVSID_;
};
FShaderID lastFSID_;
VShaderID lastVSID_;
};
@@ -19,7 +19,7 @@
#include "GPU/D3D11/VertexShaderGeneratorD3D11.h"
#include "GPU/Directx9/VertexShaderGeneratorDX9.h"
void GenerateVertexShaderD3D11(const ShaderID &id, char *buffer, bool *usesLighting, ShaderLanguage lang) {
void GenerateVertexShaderD3D11(const VShaderID &id, char *buffer, bool *usesLighting, ShaderLanguage lang) {
*usesLighting = true;
DX9::GenerateVertexShaderHLSL(id, buffer, lang);
}
@@ -19,4 +19,4 @@
#include "GPU/Common/ShaderId.h"
void GenerateVertexShaderD3D11(const ShaderID &id, char *buffer, bool *usesLighting, ShaderLanguage lang);
void GenerateVertexShaderD3D11(const VShaderID &id, char *buffer, bool *usesLighting, ShaderLanguage lang);
@@ -33,7 +33,7 @@ namespace DX9 {
// Missing: Z depth range
// Also, logic ops etc, of course, as they are not supported in DX9.
bool GenerateFragmentShaderHLSL(const ShaderID &id, char *buffer, ShaderLanguage lang) {
bool GenerateFragmentShaderHLSL(const FShaderID &id, char *buffer, ShaderLanguage lang) {
char *p = buffer;
bool lmode = id.Bit(FS_BIT_LMODE);
@@ -22,7 +22,7 @@
namespace DX9 {
bool GenerateFragmentShaderHLSL(const ShaderID &id, char *buffer, ShaderLanguage lang = HLSL_DX9);
bool GenerateFragmentShaderHLSL(const FShaderID &id, char *buffer, ShaderLanguage lang = HLSL_DX9);
#define CONST_PS_TEXENV 0
#define CONST_PS_ALPHACOLORREF 1
@@ -41,7 +41,7 @@
namespace DX9 {
PSShader::PSShader(LPDIRECT3DDEVICE9 device, ShaderID id, const char *code) : id_(id), shader(nullptr), failed_(false) {
PSShader::PSShader(LPDIRECT3DDEVICE9 device, FShaderID id, const char *code) : id_(id), shader(nullptr), failed_(false) {
source_ = code;
#ifdef SHADERLOG
OutputDebugString(ConvertUTF8ToWString(code).c_str());
@@ -91,7 +91,7 @@ std::string PSShader::GetShaderString(DebugShaderStringType type) const {
}
}
VSShader::VSShader(LPDIRECT3DDEVICE9 device, ShaderID id, const char *code, bool useHWTransform) : id_(id), shader(nullptr), failed_(false), useHWTransform_(useHWTransform) {
VSShader::VSShader(LPDIRECT3DDEVICE9 device, VShaderID id, const char *code, bool useHWTransform) : id_(id), shader(nullptr), failed_(false), useHWTransform_(useHWTransform) {
source_ = code;
#ifdef SHADERLOG
OutputDebugString(ConvertUTF8ToWString(code).c_str());
@@ -539,15 +539,15 @@ void ShaderManagerDX9::DirtyLastShader() { // disables vertex arrays
VSShader *ShaderManagerDX9::ApplyShader(int prim, u32 vertType) {
bool useHWTransform = CanUseHardwareTransform(prim);
ShaderID VSID;
VShaderID VSID;
if (gstate_c.IsDirty(DIRTY_VERTEXSHADER_STATE)) {
gstate_c.Clean(DIRTY_VERTEXSHADER_STATE);
ComputeVertexShaderID(&VSID, vertType, useHWTransform);
} else {
VSID = lastVSID_;
}
ShaderID FSID;
FShaderID FSID;
if (gstate_c.IsDirty(DIRTY_FRAGMENTSHADER_STATE)) {
gstate_c.Clean(DIRTY_FRAGMENTSHADER_STATE);
ComputeFragmentShaderID(&FSID);
@@ -660,7 +660,7 @@ std::string ShaderManagerDX9::DebugGetShaderString(std::string id, DebugShaderTy
switch (type) {
case SHADER_TYPE_VERTEX:
{
auto iter = vsCache_.find(shaderId);
auto iter = vsCache_.find(VShaderID(shaderId));
if (iter == vsCache_.end()) {
return "";
}
@@ -669,7 +669,7 @@ std::string ShaderManagerDX9::DebugGetShaderString(std::string id, DebugShaderTy
case SHADER_TYPE_FRAGMENT:
{
auto iter = fsCache_.find(shaderId);
auto iter = fsCache_.find(FShaderID(shaderId));
if (iter == fsCache_.end()) {
return "";
}
@@ -37,7 +37,7 @@ class VSShader;
class PSShader {
public:
PSShader(LPDIRECT3DDEVICE9 device, ShaderID id, const char *code);
PSShader(LPDIRECT3DDEVICE9 device, FShaderID id, const char *code);
~PSShader();
const std::string &source() const { return source_; }
@@ -51,12 +51,12 @@ class PSShader {
protected:
std::string source_;
bool failed_;
ShaderID id_;
FShaderID id_;
};
class VSShader {
public:
VSShader(LPDIRECT3DDEVICE9 device, ShaderID id, const char *code, bool useHWTransform);
VSShader(LPDIRECT3DDEVICE9 device, VShaderID id, const char *code, bool useHWTransform);
~VSShader();
const std::string &source() const { return source_; }
@@ -72,7 +72,7 @@ class VSShader {
std::string source_;
bool failed_;
bool useHWTransform_;
ShaderID id_;
VShaderID id_;
};
class ShaderManagerDX9 : public ShaderManagerCommon {
@@ -114,18 +114,18 @@ class ShaderManagerDX9 : public ShaderManagerCommon {
LPDIRECT3DDEVICE9 device_;
ShaderID lastFSID_;
ShaderID lastVSID_;
FShaderID lastFSID_;
VShaderID lastVSID_;
char *codeBuffer_;
VSShader *lastVShader_;
PSShader *lastPShader_;
typedef std::map<ShaderID, PSShader *> FSCache;
typedef std::map<FShaderID, PSShader *> FSCache;
FSCache fsCache_;
typedef std::map<ShaderID, VSShader *> VSCache;
typedef std::map<VShaderID, VSShader *> VSCache;
VSCache vsCache_;
};
@@ -55,7 +55,7 @@ enum DoLightComputation {
LIGHT_FULL,
};
void GenerateVertexShaderHLSL(const ShaderID &id, char *buffer, ShaderLanguage lang) {
void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage lang) {
char *p = buffer;
const u32 vertType = gstate.vertType;
@@ -23,7 +23,7 @@ namespace DX9 {
// #define USE_BONE_ARRAY
void GenerateVertexShaderHLSL(const ShaderID &id, char *buffer, ShaderLanguage lang = HLSL_DX9);
void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage lang = HLSL_DX9);
#define CONST_VS_PROJ 0
#define CONST_VS_PROJ_THROUGH 4
@@ -449,7 +449,7 @@ void DrawEngineGLES::DoFlush() {
ApplyDrawState(prim);
CHECK_GL_ERROR_IF_DEBUG();
ShaderID vsid;
VShaderID vsid;
Shader *vshader = shaderManager_->ApplyVertexShader(prim, lastVType_, &vsid);
if (vshader->UseHWTransform()) {
@@ -36,7 +36,7 @@
// #define DEBUG_SHADER
// Missing: Z depth range
bool GenerateFragmentShader(const ShaderID &id, char *buffer, uint64_t *uniformMask) {
bool GenerateFragmentShader(const FShaderID &id, char *buffer, uint64_t *uniformMask) {
char *p = buffer;
*uniformMask = 0;
@@ -17,6 +17,6 @@
#pragma once
struct ShaderID;
struct FShaderID;
bool GenerateFragmentShader(const ShaderID &id, char *buffer, uint64_t *uniformMask);
bool GenerateFragmentShader(const FShaderID &id, char *buffer, uint64_t *uniformMask);
Oops, something went wrong.

0 comments on commit 4731a29

Please sign in to comment.