Skip to content

Commit

Permalink
Rename Translator -> Compiler, add HLSL vectors & matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
dfranx committed Jan 24, 2020
1 parent 5a2e4b2 commit a8855fc
Show file tree
Hide file tree
Showing 16 changed files with 1,226 additions and 243 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ set(SOURCES
main.cpp
src/Utils.cpp
src/GLSLLibrary.cpp
src/GLSLTranslator.cpp
src/HLSLTranslator.cpp
src/GLSLCompiler.cpp
src/HLSLLibrary.cpp
src/HLSLCompiler.cpp
src/ShaderDebugger.cpp
src/Texture.cpp

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Run the program:
std::string src = ... your shader code ...;

sd::ShaderDebugger dbg;
dbg.SetSource<sd::GLSLTranslator>(sd::ShaderType::Pixel, src, "main", NULL, sd::Library::GLSL());
dbg.SetSource<sd::GLSLCompiler>(sd::ShaderType::Pixel, src, "main", NULL, sd::Library::GLSL());

dbg.SetValue("iFactor", 0.7f);
dbg.SetValue("iColor", "vec3", glm::vec3(0.5f, 0.6f, 0.7f));
Expand Down Expand Up @@ -65,6 +65,7 @@ List of other cool functions: `StepOver()`, `StepOut()`, `Continue()`, `Jump()`,
## Limitations
List of things that currently don't work (I plan to fix/add them):
### GLSL
- doesn't support interface blocks, local & structure member arrays & global variable initalization (unless it has const keyword)
- parsed, but not stored & not implemented: auxilary, precision, memory, invariant
Expand All @@ -73,7 +74,6 @@ List of things that currently don't work (I plan to fix/add them):
### HLSL
- classes / interfaces
- switch statement
- else if () ??
### Both
- cubemaps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern "C" {

namespace sd
{
class Translator
class Compiler
{
public:
inline ag::Generator& GetBytecodeGenerator() { return m_gen; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include <vector>
#include <stack>

#include <ShaderDebugger/Translator.h>
#include <ShaderDebugger/Compiler.h>

namespace sd
{
class GLSLTranslator : public Translator
class GLSLCompiler : public Compiler
{
public:
virtual bool Parse(ShaderType type, const std::string& source, std::string entry = "main");
Expand Down
6 changes: 3 additions & 3 deletions inc/ShaderDebugger/GLSLLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ extern "C" {

namespace sd
{
namespace Library
namespace GLSL
{
bv_library* GLSL();
bv_variable GLSLswizzle(bv_program* prog, bv_object* obj, const bv_string field);
bv_library* Library();
bv_variable Swizzle(bv_program* prog, bv_object* obj, const bv_string field);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <hlslparser/HLSLTree.h>
#include <ShaderDebugger/Translator.h>
#include <ShaderDebugger/Compiler.h>

#include <unordered_map>
#include <string>
Expand All @@ -10,11 +10,11 @@
namespace M4 { class Allocator; class Logger; class HLSLNode; }
namespace sd
{
class HLSLTranslator : public Translator
class HLSLCompiler : public Compiler
{
public:
HLSLTranslator();
~HLSLTranslator();
HLSLCompiler();
~HLSLCompiler();

virtual bool Parse(ShaderType type, const std::string& source, std::string entry = "main");

Expand Down Expand Up @@ -75,7 +75,7 @@ namespace sd
ExpressionType m_convertType(const M4::HLSLBaseType& type, const char* userDefined = NULL);
ExpressionType m_convertType(const M4::HLSLType& type);
ExpressionType m_convertType(const std::string& type);
void m_generateConvert(ExpressionType etype);
void m_generateConvert(ExpressionType etype, int args = 1);
bool m_isTypeActuallyStruct(ExpressionType type);

Function m_matchFunction(const char* name, int argCount, M4::HLSLExpression* args);
Expand All @@ -84,6 +84,9 @@ namespace sd

inline void m_exportLine(const M4::HLSLNode* node, bool force = false)
{
if (!node)
return;

if ((m_lastLineSaved != node->line || force) && m_curFuncData != nullptr) {
m_gen.Function.DebugLineNumber(node->line);
m_lastLineSaved = node->line;
Expand Down
14 changes: 14 additions & 0 deletions inc/ShaderDebugger/HLSLLibrary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

extern "C" {
#include <BlueVM.h>
}

namespace sd
{
namespace HLSL
{
bv_library* Library();
bv_variable Swizzle(bv_program* prog, bv_object* obj, const bv_string field);
}
}
28 changes: 15 additions & 13 deletions inc/ShaderDebugger/ShaderDebugger.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include <ShaderDebugger/Translator.h>
#include <ShaderDebugger/Compiler.h>
#include <ShaderDebugger/Texture.h>
#include <ShaderDebugger/Breakpoint.h>
#include <glm/glm.hpp>
Expand All @@ -20,17 +20,19 @@ namespace sd
ShaderDebugger();
~ShaderDebugger();

template<typename CodeTranslator>
template<typename CodeCompiler>
bool SetSource(sd::ShaderType stage, const std::string& src, const std::string& entry, bv_stack* args = NULL, bv_library* library = NULL)
{
if (m_transl != nullptr)
delete m_transl;
if (m_compiler != nullptr)
delete m_compiler;
if (m_immCompiler != nullptr)
delete m_immCompiler;

m_transl = new CodeTranslator();
m_transl->SetImmediate(false);
m_compiler = new CodeCompiler();
m_compiler->SetImmediate(false);

m_immTransl = new CodeTranslator();
m_immTransl->SetImmediate(true);
m_immCompiler = new CodeCompiler();
m_immCompiler->SetImmediate(true);

m_entry = entry;
m_library = library;
Expand All @@ -40,8 +42,8 @@ namespace sd

m_type = stage;

bool done = m_transl->Parse(stage, src, entry);
m_bytecode = m_transl->GetBytecode();
bool done = m_compiler->Parse(stage, src, entry);
m_bytecode = m_compiler->GetBytecode();

if (done && m_bytecode.size() > 0) {
m_prog = bv_program_create(m_bytecode.data());
Expand All @@ -51,7 +53,7 @@ namespace sd
m_prog->user_data = (void*)this;
bv_program_add_function(m_prog, "$$discard", DiscardFunction);

m_prog->property_getter = m_transl->PropertyGetter;
m_prog->property_getter = m_compiler->PropertyGetter;

bv_function* entryPtr = bv_program_get_function(m_prog, entry.c_str());
if (entryPtr == nullptr)
Expand All @@ -66,7 +68,7 @@ namespace sd
return true;
}

inline Translator* GetTranslator() { return m_transl; }
inline Compiler* GetCompiler() { return m_compiler; }

inline bv_variable Execute() { return Execute(m_entry, m_args); }
bv_variable Execute(const std::string& func, bv_stack* args = NULL); // TODO: arguments
Expand Down Expand Up @@ -110,7 +112,7 @@ namespace sd
std::vector<Breakpoint> m_breakpoints;

sd::ShaderType m_type;
Translator* m_transl, *m_immTransl;
Compiler* m_compiler, *m_immCompiler;
std::string m_entry;
bv_library* m_library;
bv_program* m_prog;
Expand Down
1 change: 0 additions & 1 deletion inc/ShaderDebugger/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ namespace sd
unsigned int AsUnsignedInteger(const bv_variable& var);
unsigned int AsBool(const bv_variable& var);

u8 GetVectorSizeFromName(const char* name);
bv_type GetVectorTypeFromName(const char* name);

void GetMatrixSizeFromName(const char* name, int* cols, int* rows);
Expand Down
53 changes: 36 additions & 17 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#include <ShaderDebugger/Utils.h>
#include <ShaderDebugger/Texture.h>
#include <ShaderDebugger/GLSLLibrary.h>
#include <ShaderDebugger/GLSLTranslator.h>
#include <ShaderDebugger/HLSLTranslator.h>
#include <ShaderDebugger/GLSLCompiler.h>
#include <ShaderDebugger/HLSLLibrary.h>
#include <ShaderDebugger/HLSLCompiler.h>
#include <ShaderDebugger/ShaderDebugger.h>


Expand All @@ -26,6 +27,33 @@ std::vector<std::string> SplitString(const std::string& str, const std::string&
return ret;
}

std::pair<int, uint8_t> GetVectorData(const std::string& vectype)
{
static const std::vector<std::string> vecNames =
{
"float4", "int4", "bool4", "half4", "uint4",
"float3", "int3", "bool3", "half3", "uint3",
"float2", "int2", "bool2", "half2", "uint2",
"vec4", "ivec4", "bvec4", "dvec4", "uvec4",
"vec3", "ivec3", "bvec3", "dvec3", "uvec3"
"vec2", "ivec2", "bvec2", "dvec2", "uvec2"
};
static const std::vector<bv_type> vecTypes =
{
bv_type_float, bv_type_int, bv_type_uchar, bv_type_float, bv_type_uint
};
static const std::vector<int> vecSizes =
{
4,3,2
};

for (int i = 0; i < vecNames.size(); i++)
if (vecNames[i] == vectype)
return std::make_pair(vecSizes[(i/5)%3], vecTypes[i % 5]);

return std::make_pair<int, uint8_t>(0, bv_type_void);
}

void OutputVariableValue(bv_variable* val)
{
if (val == nullptr)
Expand All @@ -44,25 +72,16 @@ void OutputVariableValue(bv_variable* val)
std::string objtypeName(obj->type->name);

// vectors
// do additional checks... the user might have defined structure zvec5... you never know :P
bool isVector = false;
size_t vecPos = objtypeName.find("vec");
if (vecPos != std::string::npos && (objtypeName.size() == 4 || objtypeName.size() == 5)) // vecX or gvecX
std::pair<int, bv_type> vecData = GetVectorData(objtypeName);
if (vecData.second != bv_type_void) // vecX or gvecX
{
const char* fieldNames = "xyzw";

u8 vecLen = sd::GetVectorSizeFromName(objtypeName.c_str());
if (vecPos == 0 && !isdigit(objtypeName[3]))
vecLen = -1;
else if (vecPos == 1 && !isdigit(objtypeName[4]))
vecLen = -1;
u8 vecLen = obj->type->props.name_count;

if (vecLen >= 2 && vecLen <= 4) {
bv_type vecType = sd::GetVectorTypeFromName(objtypeName.c_str());

char chType = objtypeName[0];
if (vecPos == 1 && chType != 'b' && chType != 'i' && chType != 'u' && chType != 'd')
vecType = bv_type_void;
bv_type vecType = vecData.second;

// actual vector
if (vecType != bv_type_void) {
Expand Down Expand Up @@ -159,7 +178,7 @@ int main() {
t.close();

sd::ShaderDebugger dbg;
bool res = dbg.SetSource<sd::HLSLTranslator>(sd::ShaderType::Pixel, src, "main", NULL, sd::Library::GLSL());
bool res = dbg.SetSource<sd::HLSLCompiler>(sd::ShaderType::Pixel, src, "main", NULL, sd::HLSL::Library());

if (!res) {
printf("[ERROR] Failed to compile the shader.\n");
Expand All @@ -171,7 +190,7 @@ int main() {
white.Fill(glm::vec4(0.5f, 0.1f, 0.6f, 0.2f));

// initialize globals
const auto& globals = dbg.GetTranslator()->GetGlobals();
const auto& globals = dbg.GetCompiler()->GetGlobals();
for (const auto& global : globals)
{
if (global.Storage == sd::Variable::StorageType::Uniform ||
Expand Down
Loading

0 comments on commit a8855fc

Please sign in to comment.