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