Add new shader viewing tool

This commit is contained in:
Henrik Rydgard committed 2015-10-14 18:10:21 +02:00
1 parent 09cc80f413
commit 7f4e473e8c
25 files changed
+555 -201

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
// Copyright (c) 2015- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#pragma once
enum DebugShaderType {
SHADER_TYPE_VERTEX = 0,
SHADER_TYPE_FRAGMENT = 1,
};
enum DebugShaderStringType {
SHADER_STRING_SHORT_DESC = 0,
SHADER_STRING_SOURCE_CODE = 1,
SHADER_STRING_STATS = 2,
};
+33
View File
@@ -24,6 +24,7 @@
#endif
#include <cstdio>
#include <sstream>
#include "base/logging.h"
#include "gfx_es2/gpu_features.h"
@@ -330,6 +331,38 @@ enum {
BIT_FLATSHADE = 46,
};
std::string FragmentShaderDesc(const ShaderID &id) {
std::stringstream desc;
if (id.Bit(BIT_CLEARMODE)) desc << "Clear ";
if (id.Bit(BIT_DO_TEXTURE)) desc << "Tex ";
if (id.Bit(BIT_DO_TEXTURE_PROJ)) desc << "TexProj ";
if (id.Bit(BIT_FLIP_TEXTURE)) desc << "Flip ";
if (id.Bit(BIT_TEXALPHA)) desc << "TexAlpha ";
if (id.Bit(BIT_TEXTURE_AT_OFFSET)) desc << "TexOffset ";
if (id.Bit(BIT_LMODE)) desc << "LM ";
if (id.Bit(BIT_ENABLE_FOG)) desc << "Fog ";
if (id.Bit(BIT_COLOR_DOUBLE)) desc << "Double ";
if (id.Bit(BIT_FLATSHADE)) desc << "Flat ";
if (id.Bit(BIT_SHADER_TEX_CLAMP)) desc << "Texclamp";
if (id.Bit(BIT_CLAMP_S)) desc << "Clamp S ";
if (id.Bit(BIT_CLAMP_T)) desc << "Clamp T ";
switch (id.Bits(BIT_STENCIL_TO_ALPHA, 2)) {
case REPLACE_ALPHA_NO: break;
case REPLACE_ALPHA_YES: desc << "StencilToAlpha "; break;
case REPLACE_ALPHA_DUALSOURCE: desc << "StencilToAlphaDualSrc "; break;
}
if (id.Bit(BIT_ALPHA_TEST)) desc << "AlphaTest ";
if (id.Bit(BIT_ALPHA_AGAINST_ZERO)) desc << "AlphaTest0 ";
if (id.Bit(BIT_COLOR_TEST)) desc << "ColorTest ";
if (id.Bit(BIT_COLOR_AGAINST_ZERO)) desc << "ColorTest0 ";
// TODO: A few more...
return desc.str();
}
// Here we must take all the bits of the gstate that determine what the fragment shader will
// look like, and concatenate them together into an ID.
void ComputeFragmentShaderID(ShaderID *id_out, uint32_t vertType) {
+1
View File
@@ -23,6 +23,7 @@ struct ShaderID;
void ComputeFragmentShaderID(ShaderID *id, uint32_t vertType);
bool GenerateFragmentShader(const ShaderID &id, char *buffer);
std::string FragmentShaderDesc(const ShaderID &id);
enum StencilValueType {
STENCIL_VALUE_UNIFORM,
+7
View File
@@ -2411,3 +2411,10 @@ bool GLES_GPU::DescribeCodePtr(const u8 *ptr, std::string &name) {
}
return false;
}
std::vector<std::string> GLES_GPU::DebugGetShaderIDs(DebugShaderType type) {
return shaderManager_->DebugGetShaderIDs(type);
}
std::string GLES_GPU::DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType) {
return shaderManager_->DebugGetShaderString(id, type, stringType);
}
+4
View File
@@ -150,6 +150,10 @@ public:
void Execute_BoneMtxData(u32 op, u32 diff);
void Execute_BlockTransferStart(u32 op, u32 diff);
// Using string because it's generic - makes no assumptions on the size of the shader IDs of this backend.
std::vector<std::string> DebugGetShaderIDs(DebugShaderType shader) override;
std::string DebugGetShaderString(std::string id, DebugShaderType shader, DebugShaderStringType stringType) override;
protected:
void FastRunLoop(DisplayList &list) override;
void ProcessEvent(GPUEvent ev) override;
+73 -7
View File
@@ -41,9 +41,10 @@
#include "Framebuffer.h"
#include "i18n/i18n.h"
Shader::Shader(const char *code, uint32_t shaderType, bool useHWTransform, const ShaderID &shaderID)
Shader::Shader(const char *code, uint32_t glShaderType, bool useHWTransform, const ShaderID &shaderID)
: id_(shaderID), failed_(false), useHWTransform_(useHWTransform) {
PROFILE_THIS_SCOPE("shadercomp");
isFragment_ = glShaderType == GL_FRAGMENT_SHADER;
source_ = code;
#ifdef SHADERLOG
#ifdef _WIN32
@@ -52,7 +53,7 @@ Shader::Shader(const char *code, uint32_t shaderType, bool useHWTransform, const
printf("%s\n", code);
#endif
#endif
shader = glCreateShader(shaderType);
shader = glCreateShader(glShaderType);
glShaderSource(shader, 1, &code, 0);
glCompileShader(shader);
GLint success = 0;
@@ -130,13 +131,17 @@ LinkedShader::LinkedShader(Shader *vs, Shader *fs, u32 vertType, bool useHWTrans
ELOG("Could not link program:\n %s", buf);
#endif
ERROR_LOG(G3D, "Could not link program:\n %s", buf);
ERROR_LOG(G3D, "VS:\n%s", vs->source().c_str());
ERROR_LOG(G3D, "FS:\n%s", fs->source().c_str());
Reporting::ReportMessage("Error in shader program link: info: %s / fs: %s / vs: %s", buf, fs->source().c_str(), vs->source().c_str());
ERROR_LOG(G3D, "VS desc:\n%s\n", vs->GetShaderString(SHADER_STRING_SHORT_DESC).c_str());
ERROR_LOG(G3D, "FS desc:\n%s\n", fs->GetShaderString(SHADER_STRING_SHORT_DESC).c_str());
std::string vs_source = vs->GetShaderString(SHADER_STRING_SOURCE_CODE);
std::string fs_source = fs->GetShaderString(SHADER_STRING_SOURCE_CODE);
ERROR_LOG(G3D, "VS:\n%s\n", vs_source.c_str());
ERROR_LOG(G3D, "FS:\n%s\n", fs_source.c_str());
Reporting::ReportMessage("Error in shader program link: info: %s / fs: %s / vs: %s", buf, fs_source.c_str(), vs_source.c_str());
#ifdef SHADERLOG
OutputDebugStringUTF8(buf);
OutputDebugStringUTF8(vs->source().c_str());
OutputDebugStringUTF8(fs->source().c_str());
OutputDebugStringUTF8(vs_source.c_str());
OutputDebugStringUTF8(fs_source.c_str());
#endif
delete [] buf; // we're dead!
}
@@ -879,3 +884,64 @@ LinkedShader *ShaderManager::ApplyFragmentShader(Shader *vs, int prim, u32 vertT
lastShader_ = ls;
return ls;
}
std::string Shader::GetShaderString(DebugShaderStringType type) const {
switch (type) {
case SHADER_STRING_SOURCE_CODE:
return source_;
case SHADER_STRING_SHORT_DESC:
return isFragment_ ? FragmentShaderDesc(id_) : VertexShaderDesc(id_);
default:
return "N/A";
}
}
std::vector<std::string> ShaderManager::DebugGetShaderIDs(DebugShaderType type) {
std::string id;
std::vector<std::string> ids;
switch (type) {
case SHADER_TYPE_VERTEX:
{
for (auto iter : vsCache_) {
iter.first.ToString(&id);
ids.push_back(id);
}
}
break;
case SHADER_TYPE_FRAGMENT:
{
for (auto iter : fsCache_) {
iter.first.ToString(&id);
ids.push_back(id);
}
}
break;
}
return ids;
}
std::string ShaderManager::DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType) {
ShaderID shaderId;
shaderId.FromString(id);
switch (type) {
case SHADER_TYPE_VERTEX:
{
auto iter = vsCache_.find(shaderId);
if (iter == vsCache_.end()) {
return "";
}
return iter->second->GetShaderString(stringType);
}
case SHADER_TYPE_FRAGMENT:
{
auto iter = fsCache_.find(shaderId);
if (iter == fsCache_.end()) {
return "";
}
return iter->second->GetShaderString(stringType);
}
default:
return "N/A";
}
}
+21 -6
View File
@@ -20,8 +20,10 @@
#include "base/basictypes.h"
#include "Globals.h"
#include <map>
#include "VertexShaderGenerator.h"
#include "FragmentShaderGenerator.h"
#include "GPU/Common/ShaderCommon.h"
#include "GPU/GLES/VertexShaderGenerator.h"
#include "GPU/GLES/FragmentShaderGenerator.h"
class Shader;
@@ -30,12 +32,12 @@ struct ShaderID {
clear();
}
void clear() {
for (int i = 0; i < ARRAY_SIZE(d); i++) {
for (size_t i = 0; i < ARRAY_SIZE(d); i++) {
d[i] = 0;
}
}
void set_invalid() {
for (int i = 0; i < ARRAY_SIZE(d); i++) {
for (size_t i = 0; i < ARRAY_SIZE(d); i++) {
d[i] = 0xFFFFFFFF;
}
}
@@ -77,6 +79,14 @@ struct ShaderID {
d[bit >> 5] |= (value & mask) << (bit & 31);
}
}
void ToString(std::string *dest) const {
dest->resize(sizeof(d));
memcpy(&(*dest)[0], d, sizeof(d));
}
void FromString(std::string src) {
memcpy(d, &(src)[0], sizeof(d));
}
};
// Pre-fetched attrs and uniforms
@@ -213,20 +223,22 @@ enum {
class Shader {
public:
Shader(const char *code, uint32_t shaderType, bool useHWTransform, const ShaderID &shaderID);
Shader(const char *code, uint32_t glShaderType, bool useHWTransform, const ShaderID &shaderID);
~Shader();
uint32_t shader;
const std::string &source() const { return source_; }
bool Failed() const { return failed_; }
bool UseHWTransform() const { return useHWTransform_; }
const ShaderID &ID() const { return id_; }
std::string GetShaderString(DebugShaderStringType type) const;
private:
std::string source_;
ShaderID id_;
bool failed_;
bool useHWTransform_;
bool isFragment_;
};
class ShaderManager {
@@ -251,6 +263,9 @@ public:
int NumFragmentShaders() const { return (int)fsCache_.size(); }
int NumPrograms() const { return (int)linkedShaderCache_.size(); }
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type);
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType);
private:
void Clear();
static bool DebugAreShadersCompatibleForLinking(Shader *vs, Shader *fs);
+58 -24
View File
@@ -43,9 +43,8 @@
#define WRITE p+=sprintf
// These bits are internal to this file, although the resulting IDs will be externally visible.
// TODO: We will cut away many of these, turning them into uniforms. This should reduce the number
// of generated shaders drastically.
// TODO: There will be additional bits, indicating that groups of these will be
// sent to the shader and processed there. This will cut down the number of shaders ("ubershader approach")
enum {
BIT_LMODE = 0,
BIT_IS_THROUGH = 1,
@@ -57,32 +56,71 @@ enum {
BIT_USE_HW_TRANSFORM = 8,
BIT_HAS_NORMAL = 9, // conditioned on hw transform
BIT_NORM_REVERSE = 10,
BIT_HAS_TEXCOORD = 11,
BIT_HAS_TEXCOORD = 11, // 5 free after
BIT_UVGEN_MODE = 16,
BIT_UVPROJ_MODE = 18, // 2, can overlap with LS0
BIT_LS0 = 18, // 2
BIT_LS1 = 20, // 2
BIT_BONES = 22, // 3 should be enough, not 8
BIT_ENABLE_BONES = 30,
BIT_LIGHT0_COMP = 32,
BIT_LIGHT0_TYPE = 34,
BIT_LIGHT1_COMP = 36,
BIT_LIGHT1_TYPE = 38,
BIT_LIGHT2_COMP = 40,
BIT_LIGHT2_TYPE = 42,
BIT_LIGHT3_COMP = 44,
BIT_LIGHT3_TYPE = 46,
BIT_MATERIAL_UPDATE = 48,
BIT_LIGHT0_COMP = 32, // 2 bits
BIT_LIGHT0_TYPE = 34, // 2 bits
BIT_LIGHT1_COMP = 36, // 2 bits
BIT_LIGHT1_TYPE = 38, // 2 bits
BIT_LIGHT2_COMP = 40, // 2 bits
BIT_LIGHT2_TYPE = 42, // 2 bits
BIT_LIGHT3_COMP = 44, // 2 bits
BIT_LIGHT3_TYPE = 46, // 2 bits
BIT_MATERIAL_UPDATE = 48, // 3 bits, 1 free after
BIT_LIGHT0_ENABLE = 52,
BIT_LIGHT1_ENABLE = 53,
BIT_LIGHT2_ENABLE = 54,
BIT_LIGHT3_ENABLE = 55,
BIT_LIGHTING_ENABLE = 56,
BIT_WEIGHT_FMTSCALE = 57,
BIT_WEIGHT_FMTSCALE = 57, // only two bits, 1 free after
BIT_TEXCOORD_FMTSCALE = 60,
BIT_FLATSHADE = 62,
BIT_FLATSHADE = 62, // 1 free after
};
std::string VertexShaderDesc(const ShaderID &id) {
std::stringstream desc;
if (id.Bit(BIT_IS_THROUGH)) desc << "THR ";
if (id.Bit(BIT_USE_HW_TRANSFORM)) desc << "HWX ";
if (id.Bit(BIT_HAS_COLOR)) desc << "C ";
if (id.Bit(BIT_HAS_TEXCOORD)) desc << "T ";
if (id.Bit(BIT_HAS_NORMAL)) desc << "N ";
if (id.Bit(BIT_LMODE)) desc << "LM ";
if (id.Bit(BIT_ENABLE_FOG)) desc << "Fog ";
if (id.Bit(BIT_NORM_REVERSE)) desc << "RevN ";
if (id.Bit(BIT_DO_TEXTURE)) desc << "Tex ";
if (id.Bit(BIT_DO_TEXTURE_PROJ)) desc << "TexProj ";
if (id.Bit(BIT_FLIP_TEXTURE)) desc << "Flip ";
int uvgMode = id.Bits(BIT_UVGEN_MODE, 2);
const char *uvgModes[4] = { "UV ", "UVMtx ", "UVEnv ", "UVUnk " };
if (uvgMode) desc << uvgModes[uvgMode];
if (id.Bit(BIT_ENABLE_BONES)) desc << "Bones:" << (id.Bits(BIT_BONES, 3) + 1) << " ";
// Lights
if (id.Bit(BIT_LIGHTING_ENABLE)) {
desc << "Light: ";
for (int i = 0; i < 4; i++) {
if (id.Bit(BIT_LIGHT0_ENABLE + i)) {
desc << i << ": ";
desc << "c:" << id.Bits(BIT_LIGHT0_COMP + 4 * i, 2) << " t:" << id.Bits(BIT_LIGHT0_TYPE + 4 * i, 2) << " ";
}
}
}
desc << "MatUp:" << id.Bits(BIT_MATERIAL_UPDATE, 3) << " ";
desc << "WScale " << id.Bits(BIT_WEIGHT_FMTSCALE, 2) << " ";
desc << "TCScale " << id.Bits(BIT_TEXCOORD_FMTSCALE, 2) << " ";
if (id.Bit(BIT_FLATSHADE)) desc << "Flat ";
// TODO: More...
return desc.str();
}
bool CanUseHardwareTransform(int prim) {
if (!g_Config.bHardwareTransform)
return false;
@@ -118,7 +156,7 @@ void ComputeVertexShaderID(ShaderID *id_out, u32 vertType, bool useHWTransform)
id.SetBit(BIT_USE_HW_TRANSFORM);
id.SetBit(BIT_HAS_NORMAL, hasNormal);
// UV generation mode.
// UV generation mode. doShadeMapping is implicitly stored here.
id.SetBits(BIT_UVGEN_MODE, 2, gstate.getUVGenMode());
// The next bits are used differently depending on UVgen mode
@@ -138,20 +176,17 @@ void ComputeVertexShaderID(ShaderID *id_out, u32 vertType, bool useHWTransform)
// Okay, d[1] coming up. ==============
if (gstate.isLightingEnabled() || doShadeMapping) {
// doShadeMapping is stored as UVGenMode, so this is enough for isLightingEnabled.
id.SetBit(BIT_LIGHTING_ENABLE);
// Light bits
for (int i = 0; i < 4; i++) {
id.SetBit(BIT_LIGHT0_ENABLE + i, gstate.isLightChanEnabled(i) != 0);
if (gstate.isLightChanEnabled(i) || (doShadeMapping && (gstate.getUVLS0() == i || gstate.getUVLS1() == i))) {
id.SetBits(BIT_LIGHT0_COMP + 4 * i, 2, gstate.getLightComputation(i));
id.SetBits(BIT_LIGHT0_TYPE + 4 * i, 2, gstate.getLightType(i));
}
}
id.SetBits(BIT_MATERIAL_UPDATE, 3, gstate.getMaterialUpdate() & 7);
// TODO: Optimize by shifting in all the light bits together.
for (int i = 0; i < 4; i++) {
id.SetBit(BIT_LIGHT0_ENABLE + i, gstate.isLightChanEnabled(i) != 0);
}
// doShadeMapping is stored as UVGenMode, so this is enough for isLightingEnabled.
id.SetBit(BIT_LIGHTING_ENABLE);
}
// 2 bits. We should probably send in the weight scalefactor as a uniform instead,
@@ -159,7 +194,7 @@ void ComputeVertexShaderID(ShaderID *id_out, u32 vertType, bool useHWTransform)
id.SetBits(BIT_WEIGHT_FMTSCALE, 2, vertTypeGetWeightMask(vertType));
id.SetBit(BIT_NORM_REVERSE, gstate.areNormalsReversed());
if (doTextureProjection && gstate.getUVProjMode() == GE_PROJMAP_UV) {
id.SetBit(BIT_TEXCOORD_FMTSCALE, (vertType & GE_VTYPE_TC_MASK) >> GE_VTYPE_TC_SHIFT); // two bits
id.SetBits(BIT_TEXCOORD_FMTSCALE, 2, (vertType & GE_VTYPE_TC_MASK) >> GE_VTYPE_TC_SHIFT); // two bits
} else {
id.SetBit(BIT_HAS_TEXCOORD, hasTexcoord);
}
@@ -406,7 +441,6 @@ void GenerateVertexShader(const ShaderID &id, char *buffer) {
WRITE(p, "uniform lowp vec4 u_ambient;\n");
if ((matUpdate & 2) == 0 || !hasColor)
WRITE(p, "uniform lowp vec3 u_matdiffuse;\n");
// if ((gstate.materialupdate & 4) == 0)
WRITE(p, "uniform lowp vec4 u_matspecular;\n"); // Specular coef is contained in alpha
WRITE(p, "uniform lowp vec3 u_matemissive;\n");
}
+5 -1
View File
@@ -17,7 +17,7 @@
#pragma once
#include "Globals.h"
#include "Common/CommonTypes.h"
// #define USE_BONE_ARRAY
@@ -27,3 +27,7 @@ bool CanUseHardwareTransform(int prim);
void ComputeVertexShaderID(ShaderID *id, u32 vertexType, bool useHWTransform);
void GenerateVertexShader(const ShaderID &id, char *buffer);
// Generates a compact string that describes the shader. Useful in a list to get an overview
// of the current flora of shaders.
std::string VertexShaderDesc(const ShaderID &id);
+1
View File
@@ -186,6 +186,7 @@
<ClInclude Include="Common\GPUStateUtils.h" />
<ClInclude Include="Common\IndexGenerator.h" />
<ClInclude Include="Common\PostShader.h" />
<ClInclude Include="Common\ShaderCommon.h" />
<ClInclude Include="Common\SoftwareTransformCommon.h" />
<ClInclude Include="Common\SplineCommon.h" />
<ClInclude Include="Common\TextureDecoderNEON.h">
+4 -1
View File
@@ -195,6 +195,9 @@
<ClInclude Include="Common\GPUStateUtils.h">
<Filter>Common</Filter>
</ClInclude>
<ClInclude Include="Common\ShaderCommon.h">
<Filter>Common</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Math3D.cpp">
@@ -375,4 +378,4 @@
<Filter>GLES</Filter>
</ClCompile>
</ItemGroup>
</Project>
</Project>
+5
View File
@@ -120,6 +120,11 @@ public:
virtual void ClearShaderCache() {}
virtual void CleanupBeforeUI() {}
std::vector<std::string> DebugGetShaderIDs(DebugShaderType shader) override { return std::vector<std::string>(); };
std::string DebugGetShaderString(std::string id, DebugShaderType shader, DebugShaderStringType stringType) override {
return "N/A";
}
protected:
// To avoid virtual calls to PreExecuteOp().
virtual void FastRunLoop(DisplayList &list) = 0;
+5
View File
@@ -24,6 +24,7 @@
#include "GPU/GPU.h"
#include "Core/MemMap.h"
#include "GPU/ge_constants.h"
#include "GPU/Common/ShaderCommon.h"
struct PspGeListArgs;
struct GPUgstate;
@@ -285,4 +286,8 @@ public:
virtual const std::list<int>& GetDisplayLists() = 0;
virtual bool DecodeTexture(u8* dest, const GPUgstate &state) = 0;
virtual std::vector<FramebufferInfo> GetFramebufferList() = 0;
// For debugging. The IDs returned are opaque, do not poke in them or display them in any way.
virtual std::vector<std::string> DebugGetShaderIDs(DebugShaderType type) = 0;
virtual std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType) = 0;
};