Skip to content

Commit

Permalink
Fix else statement + more HLSL functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dfranx committed Jan 26, 2020
1 parent 2874823 commit 92f240b
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 184 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Dario Franjic
Copyright (c) 2020 Dario Franjic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ List of things that currently don't work (I plan to fix/add them):
### HLSL
- classes / interfaces
- namespaces
- switch statement
### Both
- **preprocessor**
- cubemaps
- preprocessor
- initialize structure members
7 changes: 5 additions & 2 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
- rename *Translator to *Compiler

GLSL BlueVM implementation:
- move DiscardFunction to libCommon
- recode vec constructor so that initalizations such as vec4(float, vec2, float) are possible

HLSL BlueVM
- libHLSL
- float2x2 * float2
- modulo operator
- initializer lists
- matrix<T, X, Y>, vector<T, X>u
- testing

Both:
Expand All @@ -14,7 +17,7 @@ Both:
- test variables with no initial value

- [FUTURE] #define & #version
- [FUTURE] dFdx, dFdy, dFdxCoarse, dFdyCoarse, dFdxFine, dFdyFine, fwidth, fwidthCoarse, fwidthFine
- [FUTURE] func implementations
- [FUTURE] reimplement noise functions
- [FUTURE] mat4 m; m[0].x = 1.0f;
- [FUTURE] test: mat4 m[4]; m[0][0][0];
Expand Down
13 changes: 12 additions & 1 deletion inc/ShaderDebugger/CommonLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace sd
{
namespace Common
{
bv_variable Discard(bv_program* prog, u8 count, bv_variable* args);

bv_variable create_float3(bv_program* prog, glm::vec3 val = glm::vec3(0.0f));
bv_variable create_int3(bv_program* prog, glm::ivec3 val = glm::ivec3(0));
bv_variable create_uint3(bv_program* prog, glm::uvec3 val = glm::uvec3(0u));
Expand All @@ -29,7 +31,7 @@ namespace sd
bv_variable create_vec(bv_program* prog, bv_type type, u16 components);
bv_variable create_mat(bv_program* prog, const char* name, sd::Matrix* mat);

bv_type lib_merge_type(bv_type type1, bv_type type2);
bv_type merge_type(bv_type type1, bv_type type2);

/* vectors and operators */
bv_variable lib_common_vec_constructor(bv_program* prog, bv_object* me, u8 count, bv_variable* args);
Expand Down Expand Up @@ -120,6 +122,15 @@ namespace sd
bv_variable lib_common_determinant(bv_program* prog, u8 count, bv_variable* args);
bv_variable lib_common_transpose(bv_program* prog, u8 count, bv_variable* args);

// integer
bv_variable lib_common_bitCount(bv_program* prog, u8 count, bv_variable* args);
bv_variable lib_common_findLSB(bv_program* prog, u8 count, bv_variable* args);
bv_variable lib_common_findMSB(bv_program* prog, u8 count, bv_variable* args);

// floating point
bv_variable lib_common_frexp(bv_program* prog, u8 count, bv_variable* args);
bv_variable lib_common_ldexp(bv_program* prog, u8 count, bv_variable* args);

/* helper functions to create vector & matrix definitions */
bv_object_info* lib_add_vec(bv_library* lib, const char* name, u8 comp, u8 logNot = 0);
bv_object_info* lib_add_mat(bv_library* lib, const char* name);
Expand Down
14 changes: 10 additions & 4 deletions inc/ShaderDebugger/ShaderDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ShaderDebugger/Compiler.h>
#include <ShaderDebugger/Texture.h>
#include <ShaderDebugger/Breakpoint.h>
#include <ShaderDebugger/CommonLibrary.h>
#include <glm/glm.hpp>

#include <fstream>
Expand All @@ -12,8 +13,6 @@ extern "C" {

namespace sd
{
bv_variable DiscardFunction(bv_program* prog, u8 count, bv_variable* args);

class ShaderDebugger
{
public:
Expand Down Expand Up @@ -52,7 +51,7 @@ namespace sd
return false; // invalid bytecode

m_prog->user_data = (void*)this;
bv_program_add_function(m_prog, "$$discard", DiscardFunction);
bv_program_add_function(m_prog, "$$discard", Common::Discard);

m_prog->property_getter = m_compiler->PropertyGetter;

Expand Down Expand Up @@ -101,7 +100,14 @@ namespace sd

bv_variable* GetValue(const std::string& gvarname);

inline void SetDiscarded(bool d) { m_discarded = d; }
inline void SetDiscarded(bool d) {
m_discarded = d;

if (d) {
bv_function_stepper_abort(m_stepper);
bv_program_abort(m_prog);
}
}
inline bool IsDiscarded() { return m_discarded; }

private:
Expand Down
2 changes: 1 addition & 1 deletion libs/BlueVM
2 changes: 1 addition & 1 deletion libs/hlslparser
Submodule hlslparser updated 1 files
+124 −3 HLSLParser.cpp
173 changes: 173 additions & 0 deletions src/CommonLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ namespace sd
{
namespace Common
{
bv_variable Discard(bv_program* prog, u8 count, bv_variable* args)
{
ShaderDebugger* dbgr = (ShaderDebugger*)prog->user_data;
dbgr->SetDiscarded(true);
return bv_variable_create_void();
}

bool isGLSL(bv_program* prog)
{
auto dbg = (sd::ShaderDebugger*)prog->user_data;
Expand Down Expand Up @@ -2357,6 +2364,172 @@ namespace sd
return ret;
}

/* integer */
bv_variable lib_common_bitCount(bv_program* prog, u8 count, bv_variable* args)
{
/* bitCount(genIType) */
if (count == 1) {
if (args[0].type == bv_type_object) {
bv_object* vec = bv_variable_get_object(args[0]);
glm::ivec4 vecData = glm::bitCount(sd::AsVector<4, int>(args[0]));

bv_type outType = vec->prop[0].type;

bv_variable ret = Common::create_vec(prog, outType, vec->type->props.name_count);
bv_object* retObj = bv_variable_get_object(ret);

for (u16 i = 0; i < retObj->type->props.name_count; i++)
retObj->prop[i] = bv_variable_cast(outType, bv_variable_create_int(vecData[i]));

return ret;
}
// bitCount(scalar)
else if (args[0].type == bv_type_int)
return bv_variable_create_int(glm::bitCount(bv_variable_get_int(args[0])));
// bitCount(scalar)
else
return bv_variable_create_uint(glm::bitCount(bv_variable_get_uint(bv_variable_cast(bv_type_uint, args[0]))));
}

return bv_variable_create_int(0);
}
bv_variable lib_common_findLSB(bv_program* prog, u8 count, bv_variable* args)
{
/* findLSB(genI/UType) */
if (count == 1) {
if (args[0].type == bv_type_object) {
bv_object* vec = bv_variable_get_object(args[0]);
glm::ivec4 vecData = glm::findLSB(sd::AsVector<4, int>(args[0]));

bv_type outType = vec->prop[0].type;
bv_variable ret = Common::create_vec(prog, outType, vec->type->props.name_count);
bv_object* retObj = bv_variable_get_object(ret);

for (u16 i = 0; i < retObj->type->props.name_count; i++)
retObj->prop[i] = bv_variable_cast(outType, bv_variable_create_int(vecData[i]));

return ret;
}
// bitfieldReverse(scalar)
else if (args[0].type == bv_type_int)
return bv_variable_create_int(glm::findLSB(bv_variable_get_int(args[0])));
// bitfieldReverse(scalar)
else
return bv_variable_create_uint(glm::findLSB(bv_variable_get_uint(bv_variable_cast(bv_type_uint, args[0]))));
}

return bv_variable_create_int(0);
}
bv_variable lib_common_findMSB(bv_program* prog, u8 count, bv_variable* args)
{
/* bitfieldReverse(genI/UType) */
if (count == 1) {
if (args[0].type == bv_type_object) {
bv_object* vec = bv_variable_get_object(args[0]);
glm::ivec4 vecData = glm::findMSB(sd::AsVector<4, int>(args[0]));

bv_type outType = vec->prop[0].type;
bv_variable ret = Common::create_vec(prog, outType, vec->type->props.name_count);
bv_object* retObj = bv_variable_get_object(ret);

for (u16 i = 0; i < retObj->type->props.name_count; i++)
retObj->prop[i] = bv_variable_cast(outType, bv_variable_create_int(vecData[i]));

return ret;
}
// bitfieldReverse(scalar)
else if (args[0].type == bv_type_int)
return bv_variable_create_int(glm::findMSB(bv_variable_get_int(args[0])));
// bitfieldReverse(scalar)
else
return bv_variable_create_uint(glm::findMSB(bv_variable_get_uint(bv_variable_cast(bv_type_uint, args[0]))));
}

return bv_variable_create_int(0);
}

/* floating point */
bv_variable lib_common_frexp(bv_program* prog, u8 count, bv_variable* args)
{
/* frexp(genType, out genIType), frexp(float, out int), also for genDType */
if (count == 2) {
if (args[0].type == bv_type_object) {
bv_object* vec = bv_variable_get_object(args[0]);

// using: genType, float, genDType, double
glm::vec4 x = sd::AsVector<4, float>(args[0]);
glm::vec4 signData(0.0f);
glm::ivec4 expValData(0.0f);

signData = glm::frexp(x, expValData);

// param out value
bv_variable* outPtr = bv_variable_get_pointer(args[1]);
bv_object* outObj = bv_variable_get_object(*outPtr);
for (u16 i = 0; i < outObj->type->props.name_count; i++)
outObj->prop[i] = bv_variable_create_int(expValData[i]);

// return value
bv_variable ret = Common::create_vec(prog, bv_type_float, vec->type->props.name_count);
bv_object* retObj = bv_variable_get_object(ret);
for (u16 i = 0; i < retObj->type->props.name_count; i++)
retObj->prop[i] = bv_variable_create_float(signData[i]);

return ret;
}

// frexp(float, out int)
else {
float x = bv_variable_get_float(bv_variable_cast(bv_type_float, args[0]));

int expVal = 0;
float significand = glm::frexp(x, expVal);

bv_variable* outPtr = bv_variable_get_pointer(args[1]);
bv_variable_set_int(outPtr, expVal);

return bv_variable_create_float(significand);
}
}

return bv_variable_create_float(0.0f);
}
bv_variable lib_common_ldexp(bv_program* prog, u8 count, bv_variable* args)
{
/* ldexp(genType, genIType), ldexp(float, int), also for genDType */
if (count == 2) {
if (args[0].type == bv_type_object) {
bv_object* vec = bv_variable_get_object(args[0]);

// using: genType, float, genDType, double
glm::vec4 x = sd::AsVector<4, float>(args[0]);
glm::ivec4 exp = sd::AsVector<4, int>(args[1]);

glm::vec4 resData = glm::ldexp(x, exp);

// return value
bv_variable ret = Common::create_vec(prog, bv_type_float, vec->type->props.name_count);
bv_object* retObj = bv_variable_get_object(ret);
for (u16 i = 0; i < retObj->type->props.name_count; i++)
retObj->prop[i] = bv_variable_create_float(resData[i]);

return ret;
}

// ldexp(float, int)
else {
float x = bv_variable_get_float(bv_variable_cast(bv_type_float, args[0]));
int exp = bv_variable_get_int(bv_variable_cast(bv_type_int, args[1]));

float res = glm::ldexp(x, exp);

return bv_variable_create_float(res);
}
}

return bv_variable_create_float(0.0f);
}

/* helper functions to create vector & matrix definitions */
bv_object_info* lib_add_vec(bv_library* lib, const char* name, u8 comp, u8 logNot)
{
Expand Down
3 changes: 3 additions & 0 deletions src/GLSLCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1302,10 +1302,13 @@ namespace sd
translateExpression(statement->condition);
size_t pos = m_gen.Function.If();
translateStatement(statement->thenStatement);
size_t goto_ref = m_gen.Function.Goto();
m_gen.Function.SetAddress(pos, m_gen.Function.GetCurrentAddress());

if (statement->elseStatement)
translateStatement(statement->elseStatement);

m_gen.Function.SetAddress(goto_ref, m_gen.Function.GetCurrentAddress());
}

void GLSLCompiler::translateSwitchStatement(glsl::astSwitchStatement *statement) {
Expand Down
Loading

0 comments on commit 92f240b

Please sign in to comment.