Loading...
Searching...
No Matches
isf.hpp
1#pragma once
2#include <ossia/detail/variant.hpp>
3
4#include <score_plugin_gfx_export.h>
5
6#include <array>
7#include <optional>
8#include <stdexcept>
9#include <string>
10#include <vector>
11
12namespace isf
13{
14class invalid_file : public std::runtime_error
15{
16public:
17 using std::runtime_error::runtime_error;
18};
19
20struct event_input
21{
22};
23
24struct bool_input
25{
26 using value_type = bool;
27 using has_default = std::true_type;
28 bool def{};
29};
30
31struct long_input
32{
33 using value_type = int64_t;
34 using has_minmax = std::true_type;
35 std::vector<ossia::variant<int64_t, double, std::string>> values;
36 std::vector<std::string> labels;
37 std::size_t def{}; // index of default value
38};
39struct float_input
40{
41 using value_type = double;
42 using has_minmax = std::true_type;
43 double min{0.};
44 double max{1.};
45 double def{0.5};
46};
47
48struct point2d_input
49{
50 using value_type = std::array<double, 2>;
51 using has_minmax = std::true_type;
52 std::optional<value_type> def{};
53 std::optional<value_type> min{};
54 std::optional<value_type> max{};
55};
56
57struct point3d_input
58{
59 using value_type = std::array<double, 3>;
60 using has_minmax = std::true_type;
61 std::optional<value_type> def{};
62 std::optional<value_type> min{};
63 std::optional<value_type> max{};
64};
65
66struct color_input
67{
68 using value_type = std::array<double, 4>;
69 using has_minmax = std::true_type;
70 std::optional<value_type> def{};
71 std::optional<value_type> min{};
72 std::optional<value_type> max{};
73};
74
75struct image_input
76{
77};
78
79struct audio_input
80{
81 int max{};
82};
83
84struct audioFFT_input
85{
86 int max{};
87};
88
89struct audioHist_input
90{
91 int max{};
92};
93
94// CSF-specific input types
95struct storage_input
96{
97 std::string access; // "read_only", "write_only", "read_write"
98
99 struct layout_field
100 {
101 std::string name;
102 std::string type;
103 };
104
105 std::vector<layout_field> layout;
106};
107
108struct texture_input
109{
110 // For sampled textures in CSF
111};
112
113struct csf_image_input
114{
115 std::string access; // "read_only", "write_only", "read_write"
116 std::string format; // "RGBA8", "R32F", etc.
117
118 std::string width_expression;
119 std::string height_expression;
120};
121
122struct input
123{
124 using input_impl = ossia::variant<
125 float_input, long_input, event_input, bool_input, color_input, point2d_input,
126 point3d_input, image_input, audio_input, audioFFT_input, audioHist_input,
127 storage_input, texture_input, csf_image_input>;
128
129 std::string name;
130 std::string label;
131
132 input_impl data;
133};
134
135// Matches QShaderDescription::VariableType
136enum class attribute_type
137{
138 Unknown = 0,
139
140 Float,
141 Vec2,
142 Vec3,
143 Vec4,
144 Mat2,
145 Mat2x3,
146 Mat2x4,
147 Mat3,
148 Mat3x2,
149 Mat3x4,
150 Mat4,
151 Mat4x2,
152 Mat4x3,
153
154 Int,
155 Int2,
156 Int3,
157 Int4,
158
159 Uint,
160 Uint2,
161 Uint3,
162 Uint4,
163
164 Bool,
165 Bool2,
166 Bool3,
167 Bool4,
168
169 Double,
170 Double2,
171 Double3,
172 Double4,
173 DMat2,
174 DMat2x3,
175 DMat2x4,
176 DMat3,
177 DMat3x2,
178 DMat3x4,
179 DMat4,
180 DMat4x2,
181 DMat4x3,
182
183 Sampler1D,
184 Sampler2D,
185 Sampler2DMS,
186 Sampler3D,
187 SamplerCube,
188 Sampler1DArray,
189 Sampler2DArray,
190 Sampler2DMSArray,
191 Sampler3DArray,
192 SamplerCubeArray,
193 SamplerRect,
194 SamplerBuffer,
195 SamplerExternalOES,
196 Sampler,
197
198 Image1D,
199 Image2D,
200 Image2DMS,
201 Image3D,
202 ImageCube,
203 Image1DArray,
204 Image2DArray,
205 Image2DMSArray,
206 Image3DArray,
207 ImageCubeArray,
208 ImageRect,
209 ImageBuffer,
210
211 Struct,
212
213 Half,
214 Half2,
215 Half3,
216 Half4
217};
218
219struct vertex_attribute
220{
221 int location{};
222 attribute_type type{};
223 std::string name;
224};
225
226struct vertex_input : vertex_attribute
227{
228};
229struct vertex_output : vertex_attribute
230{
231};
232struct fragment_input : vertex_attribute
233{
234};
235struct fragment_output : vertex_attribute
236{
237};
238
239struct pass
240{
241 std::string target;
242 bool persistent{};
243 bool float_storage{};
244 bool nearest_filter{};
245 std::string width_expression{};
246 std::string height_expression{};
247};
248
249struct descriptor
250{
251 enum Mode
252 {
253 ISF,
254 VSA,
255 CSF,
256 RawRaster
257 } mode{ISF};
258 std::string description;
259 std::string credits;
260 std::vector<std::string> categories;
261 std::vector<input> inputs;
262 std::vector<pass> passes;
263 std::vector<std::string> pass_targets;
264 bool default_vertex_shader{};
265
266 // For VSA
267 int point_count{};
268 std::string primitive_mode;
269 std::string line_size;
270 std::array<double, 4> background_color;
271
272 // For CSF
273 struct type_definition
274 {
275 std::string name;
276 std::vector<storage_input::layout_field> layout;
277 };
278 std::vector<type_definition> types;
279
280 struct dispatch_info
281 {
282 std::array<int, 3> local_size{16, 16, 1};
283 std::string execution_type{"2D_IMAGE"}; // "2D_IMAGE", "1D_BUFFER", "MANUAL", etc.
284 std::string target_resource;
285 std::array<int, 3> workgroups{1, 1, 1}; // For MANUAL mode
286 };
287 std::vector<dispatch_info> csf_passes;
288
289 // For raw shaders
290
291 std::vector<vertex_input> vertex_inputs;
292 std::vector<vertex_output> vertex_outputs;
293 std::vector<fragment_input> fragment_inputs;
294 std::vector<fragment_output> fragment_outputs;
295};
296
297class SCORE_PLUGIN_GFX_EXPORT parser
298{
299 std::string m_sourceVertex;
300 std::string m_sourceFragment;
301 std::string m_source_geometry_filter;
302 int m_version{450};
303
304 std::string m_vertex;
305 std::string m_fragment;
306 std::string m_geometry_filter;
307
308 descriptor m_desc;
309
310public:
311 enum class ShaderType
312 {
313 Autodetect,
314 ISF,
315 ShaderToy,
316 GLSLSandBox,
317 GeometryFilter,
318 VertexShaderArt,
319 CSF,
320 RawRasterPipeline,
321 RawRaytracePipeline,
322 RawMeshPipeline
323 };
324 parser(std::string vert, std::string frag, int glslVersion, ShaderType);
325 explicit parser(std::string isf_geom_filter, ShaderType t);
326
327 descriptor data() const;
328 descriptor::Mode mode() const;
329 std::string vertex() const;
330 std::string fragment() const;
331 std::string geometry_filter() const;
332 std::string compute_shader() const;
333 static std::pair<int, descriptor> parse_isf_header(std::string_view source);
334 void parse_shadertoy_json(const std::string& json);
335
336 std::string write_isf() const;
337
338private:
339 void parse_isf();
340 void parse_raw_raster_pipeline();
341 void parse_shadertoy();
342 void parse_glsl_sandbox();
343 void parse_geometry_filter();
344 void parse_vsa();
345 void parse_csf();
346};
347}