Skip to content

Commit

Permalink
Transitioned to context variables and visits
Browse files Browse the repository at this point in the history
  • Loading branch information
Raikiri committed Oct 19, 2024
1 parent f62c39f commit bd473c5
Show file tree
Hide file tree
Showing 11 changed files with 342 additions and 199 deletions.
8 changes: 4 additions & 4 deletions LegitScript/include/LegitScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
#include <string>
#include <memory>
#include "LegitScriptEvents.h"
#include "LegitScriptCallbacks.h"
#include "LegitScriptInputs.h"
#include "LegitExceptions.h"

namespace ls
{
struct LegitScript
{
LegitScript(SliderFloatFunc slider_float_func, SliderIntFunc slider_int_func, TextFunc text_func);
LegitScript();
~LegitScript();
ls::ScriptContents LoadScript(std::string script_source);
ls::ScriptEvents RunScript(ivec2 swapchain_size, float time);
ls::ScriptContents LoadScript(const std::string &script_source);
ls::ScriptEvents RunScript(const std::vector<ContextInput> &context_inputs);
private:
struct Impl;
std::unique_ptr<Impl> impl;
Expand Down
9 changes: 0 additions & 9 deletions LegitScript/include/LegitScriptCallbacks.h

This file was deleted.

30 changes: 28 additions & 2 deletions LegitScript/include/LegitScriptEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <vector>
#include <string>
#include <functional>
#include <variant>
#include <cstdint>
#include "PodTypes.h"

Expand Down Expand Up @@ -134,10 +135,35 @@ namespace ls
};


struct FloatRequest
{
std::string name;
float min_val, max_val, def_val;
};
struct IntRequest
{
std::string name;
int min_val, max_val, def_val;
};
struct ColorRequest
{
std::string name;
vec4 def_val;
};
struct BoolRequest
{
std::string name;
bool def_val;
};
struct TextRequest
{
std::string text;
};
using ContextRequest = std::variant<FloatRequest, IntRequest, ColorRequest, BoolRequest, TextRequest, LoadedImageRequest, CachedImageRequest>;

struct ScriptEvents
{
std::vector<CachedImageRequest> cached_image_requests;
std::vector<LoadedImageRequest> loaded_image_requests;
std::vector<ContextRequest> context_requests;
std::vector<ShaderInvocation> script_shader_invocations;
};
}
19 changes: 19 additions & 0 deletions LegitScript/include/LegitScriptInputs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <functional>
#include <string>

namespace ls
{
struct LoadedImage
{
ivec2 size;
ls::PixelFormats pixel_format;
};
using ContextValueType = std::variant<float, vec2, vec3, vec4, int, ivec2, ivec3, ivec4, LoadedImage>;
struct ContextInput
{
std::string name;
ContextValueType value;
};
//using ContextInputs = std::vector<ContextInput>;
}
8 changes: 4 additions & 4 deletions LegitScript/include/LegitScriptJsonApi.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "LegitScriptCallbacks.h"
#include "LegitScriptEvents.h"
#include "LegitScriptInputs.h"

namespace ls
{
void InitScript(SliderFloatFunc slider_float_func, SliderIntFunc slider_int_func, TextFunc text_func);
std::string LoadScript(std::string script_source);
std::string RunScript(int swapchain_width, int swapchain_height, float time);
std::string LoadScript(const std::string &script_source);
std::string RunScript(const std::string &context_inputs);
}
64 changes: 33 additions & 31 deletions LegitScript/source/LegitScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@
#include "../include/SourceAssembler.h"
namespace ls
{
void AddShaderDescArg(ls::ShaderDesc &desc, const ls::DecoratedPodType &dec_pod_type, std::string name)
{
std::string type_name = PodTypeToString(dec_pod_type.type);

auto access_qualifier = dec_pod_type.access_qalifier.value_or(ls::DecoratedPodType::AccessQualifiers::in);
if(access_qualifier == ls::DecoratedPodType::AccessQualifiers::out)
{
desc.outs.push_back({type_name, name});
}else
{
desc.uniforms.push_back({type_name, name});
}
}
void AddShaderDescArg(ls::ShaderDesc &desc, const ls::SamplerTypes &sampler_type, std::string name)
{
desc.samplers.push_back({SamplerTypeToString(sampler_type), name});
}
void AddShaderDescArg(ls::ShaderDesc &desc, const ls::DecoratedImageType &dec_image_type, std::string name)
{
}

ls::ShaderDesc CreateShaderDesc(const ls::PassDecl &decl, std::vector<std::string> flattened_includes, const ls::Preamble &preamble, ls::BlockBody body)
{
ls::ShaderDesc desc;
Expand All @@ -24,28 +45,9 @@ namespace ls

for(const auto &arg : decl.arg_descs)
{
if(std::holds_alternative<ls::DecoratedPodType>(arg.type))
{
auto dec_pod_type = std::get<ls::DecoratedPodType>(arg.type);
std::string type_name = PodTypeToString(dec_pod_type.type);

auto access_qualifier = dec_pod_type.access_qalifier.value_or(ls::DecoratedPodType::AccessQualifiers::in);
if(access_qualifier == ls::DecoratedPodType::AccessQualifiers::out)
{
desc.outs.push_back({type_name, arg.name});
}else
{
desc.uniforms.push_back({type_name, arg.name});
}
}else
if(std::holds_alternative<ls::SamplerTypes>(arg.type))
{
auto sampler_type = std::get<ls::SamplerTypes>(arg.type);
desc.samplers.push_back({SamplerTypeToString(sampler_type), arg.name});
}else
{
assert(0);
}
std::visit([&desc, arg](const auto &a){
AddShaderDescArg(desc, a, arg.name);
}, arg.type);
}
return desc;
}
Expand Down Expand Up @@ -120,8 +122,8 @@ namespace ls

struct LegitScript::Impl
{
Impl(SliderFloatFunc slider_float_func, SliderIntFunc slider_int_func, TextFunc text_func)
: render_graph_script(slider_float_func, slider_int_func, text_func)
Impl()
: render_graph_script()
{
}
~Impl(){}
Expand Down Expand Up @@ -214,11 +216,11 @@ namespace ls

return script_contents;
}
ls::ScriptEvents RunScript(ivec2 swapchain_size, float time)
ls::ScriptEvents RunScript(const std::vector<ContextInput> &context_inputs)
{
try
{
return render_graph_script.RunScript(swapchain_size, time);
return render_graph_script.RunScript(context_inputs);
}
catch(const ls::RenderGraphRuntimeException &e)
{
Expand All @@ -237,19 +239,19 @@ namespace ls
ls::ScriptParser script_parser;
};

ls::ScriptEvents LegitScript::RunScript(ivec2 swapchain_size, float time)
ls::ScriptEvents LegitScript::RunScript(const std::vector<ContextInput> &context_inputs)
{
return impl->RunScript(swapchain_size, time);
return impl->RunScript(context_inputs);
}

ls::ScriptContents LegitScript::LoadScript(std::string script_source)
ls::ScriptContents LegitScript::LoadScript(const std::string &script_source)
{
return impl->LoadScript(script_source);
}

LegitScript::LegitScript(SliderFloatFunc slider_float_func, SliderIntFunc slider_int_func, TextFunc text_func)
LegitScript::LegitScript()
{
this->impl.reset(new LegitScript::Impl(slider_float_func, slider_int_func, text_func));
this->impl.reset(new LegitScript::Impl());
}
LegitScript::~LegitScript()
{
Expand Down
108 changes: 85 additions & 23 deletions LegitScript/source/LegitScriptJsonApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ namespace ls
std::unique_ptr<ls::LegitScript> instance;
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::ShaderDesc::Sampler> samplers)
{
Expand Down Expand Up @@ -94,8 +90,12 @@ namespace ls
return arr;
}

std::string LoadScript(std::string script_source)
std::string LoadScript(const std::string &script_source)
{
if(!instance)
{
instance.reset(new ls::LegitScript());
}
assert(instance);
json res_obj;
try
Expand Down Expand Up @@ -127,20 +127,6 @@ namespace ls
}
assert(0);
}
json SerializeCachedImgRequests(const std::vector<ls::CachedImageRequest> &cached_img_requests)
{
auto arr = json::array();
for(const auto &req : cached_img_requests)
{
arr.push_back(json::object({
{"size_x", req.size.x},
{"size_y", req.size.y},
{"pixel_format", PixelFormatToStr(req.pixel_format)},
{"id", req.id}
}));
}
return arr;
}
json SerializeVec2(vec2 val)
{
return json::object({{"x", val.x}, {"y", val.y}});
Expand Down Expand Up @@ -239,23 +225,99 @@ namespace ls
}
return arr;
}

json SerializeRequest(ls::FloatRequest req)
{
return json::object({{"name", req.name}, {"type", "FloatRequest"}, {"min_val", req.min_val}, {"max_val", req.max_val}, {"def_val", req.def_val}});
}
json SerializeRequest(ls::IntRequest req)
{
return json::object({{"name", req.name}, {"type", "IntRequest"}, {"min_val", req.min_val}, {"max_val", req.max_val}, {"def_val", req.def_val}});
}
json SerializeRequest(ls::BoolRequest req)
{
return json::object({{"name", req.name}, {"type", "BoolRequest"}, {"def_val", req.def_val}});
}
json SerializeRequest(ls::TextRequest req)
{
return json::object({{"text", req.text}, {"type", "TextRequest"}});
}
json SerializeRequest(ls::LoadedImageRequest req)
{
return json::object({{"filename", req.filename}, {"type", "LoadedImageRequest"}});
}
json SerializeRequest(ls::CachedImageRequest req)
{
return json::object({
{"type", "CachedImageRequest"},
{"size", SerializeIVec2(req.size)},
{"pixel_format", PixelFormatToStr(req.pixel_format)},
{"id", req.id}
});
}
json SerializeRequest(ls::ColorRequest req)
{
return json::object({
{"type", "ColorRequest"},
{"def_val", SerializeVec4(req.def_val)}
});
}
json SerializeContextRequests(const std::vector<ls::ContextRequest> &context_requests)
{
auto arr = json::array();
for(const auto &request : context_requests)
{
std::visit([&arr](const auto &r){
arr.push_back(SerializeRequest(r));
}, request);
}
return arr;
}
json SerializeScriptEvents(const ls::ScriptEvents &script_events, const ls::ShaderDescs &shader_descs)
{
return json::object({
{"cached_img_requests", SerializeCachedImgRequests(script_events.cached_image_requests)},
{"loaded_img_requests", json::array()},
{"context_requests", SerializeContextRequests(script_events.context_requests)},
{"shader_invocations", SerializeShaderInvocations(script_events.script_shader_invocations, shader_descs)}
});
}

ls::ContextInput ParseContextInput(json json_input)
{
ls::ContextInput context_input;
context_input.name = json_input["name"];

json value = json_input["value"];
if(json_input["type"] == "float") context_input.value = float(value);
if(json_input["type"] == "vec2") context_input.value = ls::vec2{float(value["x"]), float(value["y"])};
if(json_input["type"] == "vec3") context_input.value = ls::vec3{float(value["x"]), float(value["y"]), float(value["z"])};
if(json_input["type"] == "vec4") context_input.value = ls::vec4{float(value["x"]), float(value["y"]), float(value["z"]), float(value["w"])};
if(json_input["type"] == "int") context_input.value = int(value);
if(json_input["type"] == "ivec2") context_input.value = ls::ivec2{int(value["x"]), int(value["y"])};
if(json_input["type"] == "ivec3") context_input.value = ls::ivec3{int(value["x"]), int(value["y"]), int(value["z"])};
if(json_input["type"] == "ivec4") context_input.value = ls::ivec4{int(value["x"]), int(value["y"]), int(value["z"]), int(value["w"])};
//if(json_input["type"] == "LoadedImage") context_input.value = ls::LoadedImage{int(value["x"]), int(value["y"]), int(value["z"]), int(value["w"])};
return context_input;
}

std::vector<ls::ContextInput> ParseContextInputs(const std::string &context_inputs_str)
{
std::vector<ls::ContextInput> context_inputs;
json json_inputs = json::parse(context_inputs_str);
for(const auto &json_input : json_inputs)
{
context_inputs.push_back(ParseContextInput(json_input));
}
return context_inputs;
}

std::string RunScript(int swapchain_width, int swapchain_height, float time)
std::string RunScript(const std::string &context_inputs_json)
{
assert(instance);
json res_obj;
try
{
auto script_events = instance->RunScript({swapchain_width, swapchain_height}, time);
auto context_inputs = ParseContextInputs(context_inputs_json);
auto script_events = instance->RunScript(context_inputs);
res_obj = SerializeScriptEvents(script_events, script_contents.shader_descs);
}
catch(const ls::ScriptException &e)
Expand Down
Loading

0 comments on commit bd473c5

Please sign in to comment.