Skip to content

Commit

Permalink
Added actual includes
Browse files Browse the repository at this point in the history
  • Loading branch information
Raikiri committed Oct 17, 2024
1 parent 79de77e commit da25c45
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 40 deletions.
2 changes: 1 addition & 1 deletion LegitScript/include/LegitScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ls
{
LegitScript(SliderFloatFunc slider_float_func, SliderIntFunc slider_int_func, TextFunc text_func);
~LegitScript();
ls::ScriptShaderDescs LoadScript(std::string script_source);
ls::ScriptContents LoadScript(std::string script_source);
ls::ScriptEvents RunScript(ivec2 swapchain_size, float time);
private:
struct Impl;
Expand Down
19 changes: 15 additions & 4 deletions LegitScript/include/LegitScriptEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace ls
size_t start;
};

struct ScriptShaderDesc
struct ShaderDesc
{
struct Sampler
{
Expand Down Expand Up @@ -76,9 +76,20 @@ namespace ls
std::vector<std::string> includes;
BlockBody body;
};
using ScriptShaderDescs = std::vector<ScriptShaderDesc>;
using ShaderDescs = std::vector<ShaderDesc>;
struct Declaration
{
std::string name;
BlockBody body;
};
using Declarations = std::vector<Declaration>;
struct ScriptContents
{
ShaderDescs shader_descs;
Declarations declarations;
};

struct ScriptShaderInvocation
struct ShaderInvocation
{
std::string shader_name;
std::vector<ls::Image> image_sampler_bindings;
Expand Down Expand Up @@ -127,6 +138,6 @@ namespace ls
{
std::vector<CachedImageRequest> cached_image_requests;
std::vector<LoadedImageRequest> loaded_image_requests;
std::vector<ScriptShaderInvocation> script_shader_invocations;
std::vector<ShaderInvocation> script_shader_invocations;
};
}
44 changes: 28 additions & 16 deletions LegitScript/source/LegitScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include "../include/SourceAssembler.h"
namespace ls
{
ls::ScriptShaderDesc CreateShaderDesc(const ls::PassDecl &decl, std::vector<std::string> flattened_includes, const ls::Preamble &preamble, ls::BlockBody body)
ls::ShaderDesc CreateShaderDesc(const ls::PassDecl &decl, std::vector<std::string> flattened_includes, const ls::Preamble &preamble, ls::BlockBody body)
{
ls::ScriptShaderDesc desc;
ls::ShaderDesc desc;
desc.body = body;
desc.name = decl.name;
desc.includes = flattened_includes;
Expand Down Expand Up @@ -125,9 +125,9 @@ namespace ls
{
}
~Impl(){}
ls::ScriptShaderDescs LoadScript(std::string script_source)
ls::ScriptContents LoadScript(std::string script_source)
{
ls::ScriptShaderDescs shader_descs;
ls::ScriptContents script_contents;
ls::ParsedScript parsed_script;
try
{
Expand All @@ -149,18 +149,31 @@ namespace ls
for(size_t block_idx = 0; block_idx < parsed_script.blocks.size(); block_idx++)
{
const auto &block = parsed_script.blocks[block_idx];
if(!FindPreambleIsRendergraph(block.preamble) && block.decl.has_value())
if(!FindPreambleIsRendergraph(block.preamble))
{
auto pass_decl = block.decl.value();
pass_decls.push_back(pass_decl);
std::vector<std::string> includes;
for(auto included_idx : flattened_include_graph[block_idx].adjacent_nodes)
if(block.decl.has_value())
{
auto pass_decl = block.decl.value();
pass_decls.push_back(pass_decl);
std::vector<std::string> includes;
for(auto included_idx : flattened_include_graph[block_idx].adjacent_nodes)
{
auto opt_name = FindPreambleDeclName(parsed_script.blocks[included_idx].preamble);
assert(opt_name);
includes.push_back(opt_name.value());
}
script_contents.shader_descs.push_back(CreateShaderDesc(pass_decl, includes, block.preamble, block.body));
}else
{
auto opt_name = FindPreambleDeclName(parsed_script.blocks[included_idx].preamble);
assert(opt_name);
includes.push_back(opt_name.value());
auto opt_name = FindPreambleDeclName(parsed_script.blocks[block_idx].preamble);
if(opt_name)
{
ls::Declaration decl;
decl.body = block.body;
decl.name = opt_name.value();
script_contents.declarations.push_back(decl);
}
}
shader_descs.push_back(CreateShaderDesc(pass_decl, includes, block.preamble, block.body));
}
}

Expand Down Expand Up @@ -199,8 +212,7 @@ namespace ls
}
}

return shader_descs;

return script_contents;
}
ls::ScriptEvents RunScript(ivec2 swapchain_size, float time)
{
Expand Down Expand Up @@ -230,7 +242,7 @@ namespace ls
return impl->RunScript(swapchain_size, time);
}

ls::ScriptShaderDescs LegitScript::LoadScript(std::string script_source)
ls::ScriptContents LegitScript::LoadScript(std::string script_source)
{
return impl->LoadScript(script_source);
}
Expand Down
42 changes: 29 additions & 13 deletions LegitScript/source/LegitScriptJsonApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace ls
{
using json = nlohmann::json;
std::unique_ptr<ls::LegitScript> instance;
ls::ScriptShaderDescs shader_descs;
ls::ScriptContents script_contents;

void InitScript(SliderFloatFunc slider_float_func, SliderIntFunc slider_int_func, TextFunc text_func)
{
instance.reset(new ls::LegitScript(slider_float_func, slider_int_func, text_func));
}

json SerializeSamplers(const std::vector<ls::ScriptShaderDesc::Sampler> samplers)
json SerializeSamplers(const std::vector<ls::ShaderDesc::Sampler> samplers)
{
auto arr = json::array();
for(auto sampler : samplers)
Expand All @@ -22,7 +22,7 @@ namespace ls
}
return arr;
}
json SerializeUniforms(const std::vector<ls::ScriptShaderDesc::Uniform> uniforms)
json SerializeUniforms(const std::vector<ls::ShaderDesc::Uniform> uniforms)
{
auto arr = json::array();
for(auto uniform : uniforms)
Expand All @@ -31,7 +31,7 @@ namespace ls
}
return arr;
}
json SerializeInouts(const std::vector<ls::ScriptShaderDesc::Inouts> inouts)
json SerializeInouts(const std::vector<ls::ShaderDesc::Inouts> inouts)
{
auto arr = json::array();
for(auto inout : inouts)
Expand All @@ -47,7 +47,7 @@ namespace ls
{"text", body.text}
});
}
json SerializeShaderDescs(const ls::ScriptShaderDescs &descs)
json SerializeShaderDescs(const ls::ShaderDescs &descs)
{
auto arr = json::array();
for(auto desc : descs)
Expand Down Expand Up @@ -81,14 +81,30 @@ namespace ls
return json::object({{"error", e}});
}

json SerializeDeclarations(const ls::Declarations &decls)
{
auto arr = json::array();
for(auto decl : decls)
{
arr.push_back(json::object({
{"name", decl.name},
{"body", SerializeBlockBody(decl.body)},
}));
}
return arr;
}

std::string LoadScript(std::string script_source)
{
assert(instance);
json res_obj;
try
{
shader_descs = instance->LoadScript(script_source);
res_obj = json::object({{"shader_descs", SerializeShaderDescs(shader_descs)}});
script_contents = instance->LoadScript(script_source);
res_obj = json::object({
{"shader_descs", SerializeShaderDescs(script_contents.shader_descs)},
{"declarations", SerializeDeclarations(script_contents.declarations)}
});
}
catch(const ls::ScriptException &e)
{
Expand Down Expand Up @@ -161,14 +177,14 @@ namespace ls
if(type == "ivec4"){ assert(size == sizeof(ivec4)); return SerializeIVec4(*(ivec4*)ptr);}
throw std::runtime_error("Unknown type: " + type);
}
json SerializeUniform(void *ptr, size_t size, ls::ScriptShaderDesc::Uniform uniform_desc)
json SerializeUniform(void *ptr, size_t size, ls::ShaderDesc::Uniform uniform_desc)
{
return json::object({
{"type", uniform_desc.type},
{"val", SerializeUniformVal(ptr, size, uniform_desc.type)}
});
}
json SerializeUniforms(const std::vector<uint8_t> &uniform_data, const std::vector<ls::ScriptShaderInvocation::UniformValue> &uniform_vals, const std::vector<ls::ScriptShaderDesc::Uniform> &uniform_decls)
json SerializeUniforms(const std::vector<uint8_t> &uniform_data, const std::vector<ls::ShaderInvocation::UniformValue> &uniform_vals, const std::vector<ls::ShaderDesc::Uniform> &uniform_decls)
{
size_t uniform_idx;
assert(uniform_vals.size() == uniform_decls.size());
Expand All @@ -195,12 +211,12 @@ namespace ls
}
return arr;
}
json SerializeShaderInvocations(const std::vector<ls::ScriptShaderInvocation> &shader_invocations, const ls::ScriptShaderDescs &shader_descs)
json SerializeShaderInvocations(const std::vector<ls::ShaderInvocation> &shader_invocations, const ls::ShaderDescs &shader_descs)
{
auto arr = json::array();
for(const auto &inv : shader_invocations)
{
ls::ScriptShaderDesc matching_shader_desc;
ls::ShaderDesc matching_shader_desc;
bool is_found = false;
for(const auto &shader_desc : shader_descs)
{
Expand All @@ -223,7 +239,7 @@ namespace ls
}
return arr;
}
json SerializeScriptEvents(const ls::ScriptEvents &script_events, const ls::ScriptShaderDescs &shader_descs)
json SerializeScriptEvents(const ls::ScriptEvents &script_events, const ls::ShaderDescs &shader_descs)
{
return json::object({
{"cached_img_requests", SerializeCachedImgRequests(script_events.cached_image_requests)},
Expand All @@ -240,7 +256,7 @@ namespace ls
try
{
auto script_events = instance->RunScript({swapchain_width, swapchain_height}, time);
res_obj = SerializeScriptEvents(script_events, shader_descs);
res_obj = SerializeScriptEvents(script_events, script_contents.shader_descs);
}
catch(const ls::ScriptException &e)
{
Expand Down
4 changes: 2 additions & 2 deletions LegitScript/source/RenderGraphScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::string CreateAsPassFuncDeclaration(const ls::PassDecl &decl)
}
const Image::Id swapchain_img_id = 0;

void AddScriptInvocationAsArg(ScriptShaderInvocation &invocation, asIScriptGeneric *gen, size_t param_idx, ls::ArgDesc arg_desc)
void AddScriptInvocationAsArg(ShaderInvocation &invocation, asIScriptGeneric *gen, size_t param_idx, ls::ArgDesc arg_desc)
{
auto arg_type = arg_desc.type;
if(std::holds_alternative<ls::DecoratedPodType>(arg_type))
Expand Down Expand Up @@ -493,7 +493,7 @@ void RenderGraphScript::Impl::RegisterAsScriptPassFunctions(const std::vector<ls
std::string as_func_decl = CreateAsPassFuncDeclaration(pass_decl);
this->as_script_engine->RegisterGlobalFunction(as_func_decl, [this, pass_decl](asIScriptGeneric *gen)
{
ScriptShaderInvocation invocation;
ShaderInvocation invocation;
invocation.shader_name = pass_decl.name;
for(size_t param_idx = 0; param_idx < pass_decl.arg_descs.size(); param_idx++)
{
Expand Down
8 changes: 4 additions & 4 deletions tests/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <fstream>
#include <sstream>

void PrintShaderDesc(const ls::ScriptShaderDesc &shader_desc)
void PrintShaderDesc(const ls::ShaderDesc &shader_desc)
{
std::cout << "Shader desc: " << shader_desc.name << "\n";
std::cout << " Uniforms:\n";
Expand All @@ -22,7 +22,7 @@ void PrintCachedImgRequest(const ls::CachedImageRequest &req)
{
std::cout << "Image request: [" << req.size.x << ", " << req.size.y << "]\n";
}
void PrintShaderInvocation(const ls::ScriptShaderInvocation &inv)
void PrintShaderInvocation(const ls::ShaderInvocation &inv)
{
std::cout << "Shader invocation: " << inv.shader_name << "\n";
}
Expand Down Expand Up @@ -61,9 +61,9 @@ void RunTest()
std::cout << "Loading script size: " << string_stream.str().length() << "\n";
try
{
auto shader_descs = script.LoadScript(string_stream.str());
auto script_contents = script.LoadScript(string_stream.str());
std::cout << "Script loaded successfully\n";
for(const auto &shader_desc : shader_descs)
for(const auto &shader_desc : script_contents.shader_descs)
PrintShaderDesc(shader_desc);

std::cout << "Running script\n";
Expand Down

0 comments on commit da25c45

Please sign in to comment.