Loading...
Searching...
No Matches
isf.hpp
1#pragma once
2#include <ossia/detail/variant.hpp>
3
4#include <array>
5#include <optional>
6#include <stdexcept>
7#include <string>
8#include <vector>
9
10namespace isf
11{
12class invalid_file : public std::runtime_error
13{
14public:
15 using std::runtime_error::runtime_error;
16};
17
18struct event_input
19{
20};
21
22struct bool_input
23{
24 using value_type = bool;
25 using has_default = std::true_type;
26 bool def{};
27};
28
29struct long_input
30{
31 using value_type = int64_t;
32 using has_minmax = std::true_type;
33 std::vector<ossia::variant<int64_t, double, std::string>> values;
34 std::vector<std::string> labels;
35 std::size_t def{}; // index of default value
36};
37struct float_input
38{
39 using value_type = double;
40 using has_minmax = std::true_type;
41 double min{0.};
42 double max{1.};
43 double def{0.5};
44};
45
46struct point2d_input
47{
48 using value_type = std::array<double, 2>;
49 using has_minmax = std::true_type;
50 std::optional<value_type> def{};
51 std::optional<value_type> min{};
52 std::optional<value_type> max{};
53};
54
55struct point3d_input
56{
57 using value_type = std::array<double, 3>;
58 using has_minmax = std::true_type;
59 std::optional<value_type> def{};
60 std::optional<value_type> min{};
61 std::optional<value_type> max{};
62};
63
64struct color_input
65{
66 using value_type = std::array<double, 4>;
67 using has_minmax = std::true_type;
68 std::optional<value_type> def{};
69 std::optional<value_type> min{};
70 std::optional<value_type> max{};
71};
72
73struct image_input
74{
75};
76
77struct audio_input
78{
79 int max{};
80};
81
82struct audioFFT_input
83{
84 int max{};
85};
86
87struct audioHist_input
88{
89 int max{};
90};
91
92// CSF-specific input types
93struct storage_input
94{
95 std::string access; // "read_only", "write_only", "read_write"
96
97 struct layout_field
98 {
99 std::string name;
100 std::string type;
101 };
102
103 std::vector<layout_field> layout;
104};
105
106struct texture_input
107{
108 // For sampled textures in CSF
109};
110
111struct csf_image_input
112{
113 std::string access; // "read_only", "write_only", "read_write"
114 std::string format; // "RGBA8", "R32F", etc.
115
116 std::string width_expression;
117 std::string height_expression;
118};
119
120struct input
121{
122 using input_impl = ossia::variant<
123 float_input, long_input, event_input, bool_input, color_input, point2d_input,
124 point3d_input, image_input, audio_input, audioFFT_input, audioHist_input,
125 storage_input, texture_input, csf_image_input>;
126
127 std::string name;
128 std::string label;
129
130 input_impl data;
131};
132
133struct pass
134{
135 std::string target;
136 bool persistent{};
137 bool float_storage{};
138 bool nearest_filter{};
139 std::string width_expression{};
140 std::string height_expression{};
141};
142
143struct descriptor
144{
145 enum Mode
146 {
147 ISF,
148 VSA,
149 CSF
150 } mode{ISF};
151 std::string description;
152 std::string credits;
153 std::vector<std::string> categories;
154 std::vector<input> inputs;
155 std::vector<pass> passes;
156 std::vector<std::string> pass_targets;
157 bool default_vertex_shader{};
158
159 // For VSA
160 int point_count{};
161 std::string primitive_mode;
162 std::string line_size;
163 std::array<double, 4> background_color;
164
165 // For CSF
166 struct type_definition
167 {
168 std::string name;
169 std::vector<storage_input::layout_field> layout;
170 };
171 std::vector<type_definition> types;
172
173 struct dispatch_info
174 {
175 std::array<int, 3> local_size{16, 16, 1};
176 std::string execution_type{"2D_IMAGE"}; // "2D_IMAGE", "1D_BUFFER", "MANUAL", etc.
177 std::string target_resource;
178 std::array<int, 3> workgroups{1, 1, 1}; // For MANUAL mode
179 };
180 std::vector<dispatch_info> csf_passes;
181};
182
183class parser
184{
185 std::string m_sourceVertex;
186 std::string m_sourceFragment;
187 std::string m_source_geometry_filter;
188 int m_version{450};
189
190 std::string m_vertex;
191 std::string m_fragment;
192 std::string m_geometry_filter;
193
194 descriptor m_desc;
195
196public:
197 enum class ShaderType
198 {
199 Autodetect,
200 ISF,
201 ShaderToy,
202 GLSLSandBox,
203 GeometryFilter,
204 VertexShaderArt,
205 CSF
206 };
207 parser(std::string vert, std::string frag, int glslVersion, ShaderType);
208 explicit parser(std::string isf_geom_filter, ShaderType t);
209
210 descriptor data() const;
211 descriptor::Mode mode() const;
212 std::string vertex() const;
213 std::string fragment() const;
214 std::string geometry_filter() const;
215 std::string compute_shader() const;
216 static std::pair<int, descriptor> parse_isf_header(std::string_view source);
217 void parse_shadertoy_json(const std::string& json);
218
219 std::string write_isf() const;
220
221private:
222 void parse_isf();
223 void parse_shadertoy();
224 void parse_glsl_sandbox();
225 void parse_geometry_filter();
226 void parse_vsa();
227 void parse_csf();
228};
229}