载入中...
搜索中...
未找到
StyleShaders.cpp
浏览该文件的文档.
2
4
5#include "common/Exception.h"
6#include "graphics/Graphics.h"
7#include "graphics/Shader.h"
8#include "graphics/shaders/mesh3d_toon_frag_spv.inc"
9#include "graphics/shaders/mesh3d_toon_vert_spv.inc"
11#include "stylize/shaders/anime_mesh_frag_spv.inc"
12#include "stylize/shaders/anime_mesh_wgsl.inc"
13#include "stylize/shaders/cartoon_post_frag_spv.inc"
14#include "stylize/shaders/impasto_mesh_frag_spv.inc"
15#include "stylize/shaders/impasto_mesh_vert_spv.inc"
16#include "stylize/shaders/impasto_mesh_wgsl.inc"
17#include "stylize/shaders/ink_mesh_frag_spv.inc"
18#include "stylize/shaders/ink_post_frag_spv.inc"
19#include "stylize/shaders/pixel_post_frag_spv.inc"
20#include "stylize/shaders/stylize_mesh_vert_spv.inc"
21#include "stylize/shaders/watercolor_post_frag_spv.inc"
22#include "stylize/shaders/xray_mesh_frag_spv.inc"
23#include "stylize/shaders/ysa_mesh_frag_spv.inc"
24#include "stylize/shaders/ysa_mesh_wgsl.inc"
25
26#include <algorithm>
27#include <array>
28#include <cstddef>
29#include <vector>
30
31namespace eve::stylize {
32namespace {
33
34const std::array<StyleDefinition, 8> kStyles = {{
35 {"cartoon", true, true, true, false, false, graphics::PostEffectStage::BeforeTonemap, 100},
36 {"watercolor", true, false, true, false, false, graphics::PostEffectStage::AfterTonemap, 200},
37 {"ink", true, true, true, false, false, graphics::PostEffectStage::BeforeTonemap, 110},
38 {"pixel", true, false, true, false, false, graphics::PostEffectStage::AfterTonemap, 300},
39 {"xray", false, true, false, true, false, graphics::PostEffectStage::AfterOpaque, 50},
40 {"anime", false, true, false, false, false, graphics::PostEffectStage::AfterOpaque, 40},
41 {"ysa", false, true, false, false, false, graphics::PostEffectStage::AfterOpaque, 40},
42 {"impasto", false, true, false, false, false, graphics::PostEffectStage::AfterOpaque, 40},
43}};
44
45std::vector<uint32_t> copySpv(const uint32_t *data, size_t count) {
46 return std::vector<uint32_t>(data, data + count);
47}
48
49// WebGPU (WGSL) X-ray mesh shaders. Must match the engine's mesh3d Frame UBO
50// (group 0 binding 0, std140 = Mesh3DUBO) and shared bindings (main sampler 7,
51// scene depth 9). X-ray params are packed into texBomb/parallax/clipInfo by the
52// backend flush (see bindMeshUniforms("xray") ordering below).
53const char *kXrayMeshVertWgsl = R"wgsl(
54struct VSIn {
55 @location(0) pos: vec3f,
56 @location(1) normal: vec3f,
57 @location(2) uv: vec2f,
58};
59struct Light3D {
60 posRadius: vec4f,
61 color: vec4f,
62};
63struct Frame {
64 mvp: mat4x4f,
65 model: mat4x4f,
66 lightDir: vec4f,
67 lightColor: vec4f,
68 tint: vec4f,
69 cameraPos: vec4f,
70 ambient: vec4f,
71 lights: array<Light3D, 8>,
72 texBomb: vec4f,
73 parallax: vec4f,
74 view: mat4x4f,
75 clipInfo: vec4f,
76};
77struct VSOut {
78 @builtin(position) pos: vec4f,
79 @location(0) vNormal: vec3f,
80 @location(1) vUV: vec2f,
81 @location(2) vTint: vec4f,
82 @location(3) vWorldPos: vec3f,
83 @location(4) vCameraPos: vec3f,
84};
85@group(0) @binding(0) var<uniform> ubo: Frame;
86fn inverse3x3(m: mat3x3f) -> mat3x3f {
87 let a = m[0].x; let b = m[1].x; let c = m[2].x;
88 let d = m[0].y; let e = m[1].y; let f = m[2].y;
89 let g = m[0].z; let h = m[1].z; let i = m[2].z;
90 let det = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g);
91 return mat3x3f(
92 vec3f((e * i - f * h) / det, (f * g - d * i) / det, (d * h - e * g) / det),
93 vec3f((c * h - b * i) / det, (a * i - c * g) / det, (b * g - a * h) / det),
94 vec3f((b * f - c * e) / det, (c * d - a * f) / det, (a * e - b * d) / det),
95 );
96}
97@vertex
98fn vs_main(in: VSIn) -> VSOut {
99 var out: VSOut;
100 out.pos = ubo.mvp * vec4f(in.pos, 1.0);
101 let world = ubo.model * vec4f(in.pos, 1.0);
102 out.vWorldPos = world.xyz;
103 let nrm = transpose(inverse3x3(mat3x3f(ubo.model[0].xyz, ubo.model[1].xyz, ubo.model[2].xyz))) * in.normal;
104 out.vNormal = normalize(nrm);
105 out.vUV = in.uv;
106 out.vTint = ubo.tint;
107 out.vCameraPos = ubo.cameraPos.xyz;
108 return out;
109}
110)wgsl";
111
112const char *kXrayMeshFragWgsl = R"wgsl(
113struct Light3D {
114 posRadius: vec4f,
115 color: vec4f,
116};
117struct Frame {
118 mvp: mat4x4f,
119 model: mat4x4f,
120 lightDir: vec4f,
121 lightColor: vec4f,
122 tint: vec4f,
123 cameraPos: vec4f,
124 ambient: vec4f,
125 lights: array<Light3D, 8>,
126 texBomb: vec4f,
127 parallax: vec4f,
128 view: mat4x4f,
129 clipInfo: vec4f,
130};
131struct FSIn {
132 @location(0) vNormal: vec3f,
133 @location(1) vUV: vec2f,
134 @location(2) vTint: vec4f,
135 @location(3) vWorldPos: vec3f,
136 @location(4) vCameraPos: vec3f,
137};
138@group(0) @binding(0) var<uniform> ubo: Frame;
139@group(0) @binding(7) var mainSamp: sampler;
140@group(0) @binding(9) var sceneDepth: texture_depth_2d;
141
142struct FSOut {
143 @location(0) color: vec4f,
144};
145
146@fragment
147fn fs_main(in: FSIn, @builtin(position) fragPos: vec4f) -> FSOut {
148 let color = ubo.texBomb.xyz;
149 let alpha = clamp(ubo.texBomb.w, 0.0, 1.0);
150 let bias = max(ubo.parallax.x, 0.0);
151 let screenW = max(ubo.parallax.y, 1.0);
152 let screenH = max(ubo.parallax.z, 1.0);
153 let rimStrength = clamp(ubo.parallax.w, 0.0, 1.0);
154 let rimPower = max(ubo.clipInfo.z, 0.1);
155
156 let uv = fragPos.xy / vec2f(screenW, screenH);
157 let sceneZ = textureSampleLevel(sceneDepth, mainSamp, uv, 0.0);
158 let charZ = fragPos.z;
159 let occ = smoothstep(sceneZ, sceneZ + bias, charZ);
160 if (occ < 0.5) {
161 discard;
162 }
163 let N = normalize(in.vNormal);
164 let V = normalize(in.vCameraPos - in.vWorldPos);
165 let rim = pow(clamp(1.0 - max(dot(N, V), 0.0), 0.0, 1.0), rimPower);
166 let edge = mix(1.0, rim, rimStrength);
167 var out: FSOut;
168 out.color = vec4f(color, alpha * edge * occ);
169 return out;
170}
171)wgsl";
172
173} // namespace
174
175const StyleDefinition *findStyleDefinition(const std::string &style) {
176 const auto it = std::find_if(kStyles.begin(), kStyles.end(), [&](const StyleDefinition &def) {
177 return style == def.id;
178 });
179 if (it != kStyles.end()) return &*it;
180 return findEffectDefinition(style);
181}
182
183bool isKnownStyle(const std::string &style) { return findStyleDefinition(style) != nullptr; }
184
185int styleCount() { return int(kStyles.size()) + effectStyleCount(); }
186
187std::string styleIdAt(int index) {
188 if (index < 0) return {};
189 if (index < int(kStyles.size())) return kStyles[size_t(index)].id;
190 return effectStyleIdAt(index - int(kStyles.size()));
191}
192
193bool styleSupports(const std::string &style, const std::string &feature) {
194 const StyleDefinition *def = findStyleDefinition(style);
195 if (!def) return false;
196 if (feature == "post") return def->post;
197 if (feature == "mesh") return def->mesh;
198 if (feature == "cpu") return def->cpu;
199 if (feature == "depth") return def->depth;
200 if (feature == "normal") return def->normal;
201 if (feature == "gbuffer") return def->depth || def->normal;
202 return false;
203}
204
205namespace {
206// Order is the shared GLSL/WGSL parameter ABI. Seed shader uniforms from this table.
207const StyleParameterDesc kImpastoParams[] = {
208 {"strokeScale", 0.65f, 0.1f, 32.0f},
209 {"normalStrength", 0.88f, 0.0f, 3.0f},
210 {"colorVariation", 0.32f, 0.0f, 0.8f},
211 {"parallax", 0.06f, 0.0f, 0.15f},
212 {"reflectionStrength", 0.78f, 0.0f, 2.0f},
213 {"sdfShadowStrength", 0.0f, 0.0f, 1.0f},
214 {"shadowR", 0.24f, 0.0f, 1.0f},
215 {"shadowG", 0.18f, 0.0f, 1.0f},
216 {"shadowB", 0.48f, 0.0f, 1.0f},
217 {"rimStrength", 0.7f, 0.0f, 3.0f},
218 {"rimErosion", 0.8f, 0.0f, 1.0f},
219 {"packedBrush", 0.0f, 0.0f, 1.0f},
220 {"layer", 0.0f, 0.0f, 3.0f},
221 {"shellWidth", 0.045f, 0.0f, 0.2f},
222 {"shellOffset", 0.0f, -0.2f, 0.2f},
223 {"smoothNormalMode", 0.0f, 0.0f, 2.0f},
224 {"occlusionStrength", 0.0f, 0.0f, 1.0f},
225 {"occlusionRadius", 0.8f, 0.01f, 4.0f},
226 {"sdfFloorY", 0.0f, -1000.0f, 1000.0f},
227 {"sdfOwner", 0.0f, 0.0f, 4.0f},
228 {"sdfSphereAX", 0.0f, -1000.0f, 1000.0f},
229 {"sdfSphereAY", 0.0f, -1000.0f, 1000.0f},
230 {"sdfSphereAZ", 0.0f, -1000.0f, 1000.0f},
231 {"sdfSphereASize", 0.0f, 0.0f, 1000.0f},
232 {"sdfSphereBX", 0.0f, -1000.0f, 1000.0f},
233 {"sdfSphereBY", 0.0f, -1000.0f, 1000.0f},
234 {"sdfSphereBZ", 0.0f, -1000.0f, 1000.0f},
235 {"sdfSphereBSize", 0.0f, 0.0f, 1000.0f},
236 {"sdfBoxX", 0.0f, -1000.0f, 1000.0f},
237 {"sdfBoxY", 0.0f, -1000.0f, 1000.0f},
238 {"sdfBoxZ", 0.0f, -1000.0f, 1000.0f},
239 {"sdfBoxSize", 0.0f, 0.0f, 1000.0f},
240};
241const StyleParameterDesc kYsaParams[] = {
242 {"lightMidpoint", 0.f, -1.f, 1.f},
243 {"lightSmoothness", 0.1f, 0.00001f, 1.f},
244 {"ambientR", 0.254717f, 0.f, 1.f},
245 {"ambientG", 0.254717f, 0.f, 1.f},
246 {"ambientB", 0.254717f, 0.f, 1.f},
247 {"shadowPower", 0.5f, 0.f, 1.f},
248 {"shadowSmoothness", 1.f, 0.00001f, 1.f},
249 {"specularStrength", 0.f, 0.f, 4.f},
250 {"specularMidpoint", 0.9f, -1.f, 1.f},
251 {"specularSmoothness", 0.1f, 0.00001f, 1.f},
252 {"specularR", 1.f, 0.f, 4.f},
253 {"specularG", 1.f, 0.f, 4.f},
254 {"specularB", 1.f, 0.f, 4.f},
255 {"rimStrength", 0.f, 0.f, 4.f},
256 {"rimMidpoint", 0.6f, 0.f, 1.f},
257 {"rimSmoothness", 0.1f, 0.00001f, 1.f},
258 {"rimDynamic", 0.5f, 0.f, 1.f},
259 {"rimR", 1.f, 0.f, 4.f},
260 {"rimG", 0.f, 0.f, 4.f},
261 {"rimB", 0.f, 0.f, 4.f},
262 {"alphaClip", 0.f, 0.f, 1.f},
263 {"rimHideOnShadow", 1.f, 0.f, 1.f},
264 {"linearLighting", 0.f, 0.f, 1.f},
265};
266const StyleParameterDesc kAnimeParams[] = {
267 {"shadowThreshold", -0.08f, -1.f, 1.f},
268 {"shadowSoftness", 0.035f, 0.001f, 0.5f},
269 {"shadowStrength", 1.f, 0.f, 1.f},
270 {"rimStrength", 0.19f, 0.f, 1.f},
271 {"skin", 0.f, 0.f, 1.f},
272 {"hair", 0.f, 0.f, 1.f},
273 {"skinDetail", 1.f, 0.f, 1.f},
274 {"specularStrength", 0.22f, 0.f, 1.f},
275 {"specularPower", 32.f, 1.f, 128.f},
276 {"shadowR", 0.64f, 0.f, 1.f},
277 {"shadowG", 0.66f, 0.f, 1.f},
278 {"shadowB", 0.79f, 0.f, 1.f},
279 {"fillGradient", 0.08f, 0.f, 1.f},
280 {"unlit", 0.f, 0.f, 1.f},
281 {"surfaceSpecular", 0.f, 0.f, 1.f},
282 {"castShadowStrength", 1.f, 0.f, 1.f},
283};
284const StyleParameterDesc kCartoonParams[] = {
285 {"bands", 3.f, 1.f, 16.f}, {"outlineStrength", 1.15f, 0.f, 4.f},
286 {"outlineThreshold", 0.12f, 0.f, 1.f}, {"posterize", 5.f, 1.f, 32.f},
287 {"softEdge", 0.08f, 0.f, 1.f}, {"outlineWidth", 1.5f, 0.5f, 8.f},
288 {"shadowLift", 0.12f, 0.f, 1.f}, {"rimPower", 2.8f, 0.1f, 16.f},
289 {"rimStrength", 0.55f, 0.f, 2.f},
290};
291const StyleParameterDesc kWatercolorParams[] = {
292 {"blurAmount", 2.4f, 0.f, 12.f}, {"edgeDarken", 2.f, 0.f, 5.f},
293 {"paperStrength", 0.65f, 0.f, 1.f}, {"distortion", 0.85f, 0.f, 3.f},
294 {"bleed", 0.62f, 0.f, 1.f}, {"saturation", 0.9f, 0.f, 2.f},
295 {"granulation", 0.65f, 0.f, 1.f},
296};
297const StyleParameterDesc kInkParams[] = {
298 {"inkContrast", 1.35f, 0.f, 4.f}, {"washLevels", 5.f, 1.f, 16.f},
299 {"edgeThreshold", 0.18f, 0.f, 1.f}, {"diffusion", 3.5f, 0.f, 12.f},
300 {"paperR", 0.96f, 0.f, 1.f}, {"paperG", 0.93f, 0.f, 1.f},
301 {"paperB", 0.86f, 0.f, 1.f}, {"inkDensity", 0.75f, 0.f, 2.f},
302 {"edgeStrength", 1.1f, 0.f, 4.f}, {"contrast", 1.25f, 0.f, 4.f},
303 {"rimBoost", 0.65f, 0.f, 2.f},
304};
305const StyleParameterDesc kPixelParams[] = {
306 {"pixelSize", 5.f, 1.f, 64.f}, {"paletteSteps", 6.f, 2.f, 32.f},
307 {"ditherStrength", 0.18f, 0.f, 1.f}, {"toonBands", 3.f, 1.f, 16.f},
308 {"sharpness", 1.f, 0.f, 2.f}, {"outline", 0.9f, 0.f, 4.f},
309};
310const StyleParameterDesc kXrayParams[] = {
311 {"colorR", 1.f, 0.f, 1.f}, {"colorG", 0.62f, 0.f, 1.f},
312 {"colorB", 0.12f, 0.f, 1.f}, {"bias", 0.0002f, 0.f, 0.02f},
313 {"rimPower", 2.2f, 0.1f, 16.f}, {"rimStrength", 0.7f, 0.f, 2.f},
314 {"alpha", 0.82f, 0.f, 1.f},
315};
316
317template <size_t N>
318const StyleParameterDesc *paramAt(const StyleParameterDesc (&params)[N], int index) {
319 return index < 0 || index >= int(N) ? nullptr : &params[size_t(index)];
320}
321} // namespace
322
323int styleParamCount(const std::string &style) {
324 if (style == "impasto") return int(std::size(kImpastoParams));
325 if (style == "ysa") return int(std::size(kYsaParams));
326 if (style == "anime") return int(std::size(kAnimeParams));
327 if (style == "cartoon") return int(sizeof(kCartoonParams) / sizeof(kCartoonParams[0]));
328 if (style == "watercolor") return int(sizeof(kWatercolorParams) / sizeof(kWatercolorParams[0]));
329 if (style == "ink") return int(sizeof(kInkParams) / sizeof(kInkParams[0]));
330 if (style == "pixel") return int(sizeof(kPixelParams) / sizeof(kPixelParams[0]));
331 if (style == "xray") return int(sizeof(kXrayParams) / sizeof(kXrayParams[0]));
332 return effectParamCount(style);
333}
334
335std::string styleParamName(const std::string &style, int index) {
337 return desc ? desc->id : std::string{};
338}
339
340const StyleParameterDesc *styleParameterAt(const std::string &style, int index) {
341 if (style == "impasto") return paramAt(kImpastoParams, index);
342 if (style == "ysa") return paramAt(kYsaParams, index);
343 if (style == "anime") return paramAt(kAnimeParams, index);
344 if (style == "cartoon") return paramAt(kCartoonParams, index);
345 if (style == "watercolor") return paramAt(kWatercolorParams, index);
346 if (style == "ink") return paramAt(kInkParams, index);
347 if (style == "pixel") return paramAt(kPixelParams, index);
348 if (style == "xray") return paramAt(kXrayParams, index);
349 return effectParameterAt(style, index);
350}
351
352const StyleParameterDesc *findStyleParameter(const std::string &style, const std::string &name) {
353 const int count = styleParamCount(style);
354 for (int i = 0; i < count; ++i) {
355 const StyleParameterDesc *desc = styleParameterAt(style, i);
356 if (desc && name == desc->id) return desc;
357 }
358 return nullptr;
359}
360
361void bindPostUniforms(graphics::Shader *shader, const std::string &style) {
362 if (!shader) throw eve::Exception("bindPostUniforms: null shader");
363
364 if (isEffectStyle(style)) {
366 return;
367 }
368
369 if (style == "cartoon") {
370 shader->declareFloat("bands");
371 shader->declareFloat("outlineStrength");
372 shader->declareFloat("outlineThreshold");
373 shader->declareFloat("posterize");
374 shader->declareFloat("texelW");
375 shader->declareFloat("texelH");
376 shader->declareFloat("time");
377 shader->declareFloat("softEdge");
378 shader->declareFloat("outlineWidth");
379 shader->declareFloat("shadowLift");
380 shader->sendFloat("bands", 3.f);
381 shader->sendFloat("outlineStrength", 1.15f);
382 shader->sendFloat("outlineThreshold", 0.12f);
383 shader->sendFloat("posterize", 5.f);
384 shader->sendFloat("texelW", 1.f / 256.f);
385 shader->sendFloat("texelH", 1.f / 256.f);
386 shader->sendFloat("time", 0.f);
387 shader->sendFloat("softEdge", 0.08f);
388 shader->sendFloat("outlineWidth", 1.5f);
389 shader->sendFloat("shadowLift", 0.12f);
390 return;
391 }
392 if (style == "watercolor") {
393 shader->declareFloat("blurAmount");
394 shader->declareFloat("edgeDarken");
395 shader->declareFloat("paperStrength");
396 shader->declareFloat("distortion");
397 shader->declareFloat("bleed");
398 shader->declareFloat("saturation");
399 shader->declareFloat("texelW");
400 shader->declareFloat("texelH");
401 shader->declareFloat("time");
402 shader->declareFloat("granulation");
403 shader->sendFloat("blurAmount", 2.4f);
404 shader->sendFloat("edgeDarken", 2.0f);
405 shader->sendFloat("paperStrength", 0.65f);
406 shader->sendFloat("distortion", 0.85f);
407 shader->sendFloat("bleed", 0.62f);
408 shader->sendFloat("saturation", 0.9f);
409 shader->sendFloat("texelW", 1.f / 256.f);
410 shader->sendFloat("texelH", 1.f / 256.f);
411 shader->sendFloat("time", 0.f);
412 shader->sendFloat("granulation", 0.65f);
413 return;
414 }
415 if (style == "ink") {
416 shader->declareFloat("inkContrast");
417 shader->declareFloat("washLevels");
418 shader->declareFloat("edgeThreshold");
419 shader->declareFloat("diffusion");
420 shader->declareFloat("paperR");
421 shader->declareFloat("paperG");
422 shader->declareFloat("paperB");
423 shader->declareFloat("inkDensity");
424 shader->declareFloat("texelW");
425 shader->declareFloat("texelH");
426 shader->declareFloat("time");
427 shader->declareFloat("edgeStrength");
428 shader->sendFloat("inkContrast", 1.35f);
429 shader->sendFloat("washLevels", 5.f);
430 shader->sendFloat("edgeThreshold", 0.18f);
431 shader->sendFloat("diffusion", 3.5f);
432 shader->sendFloat("paperR", 0.96f);
433 shader->sendFloat("paperG", 0.93f);
434 shader->sendFloat("paperB", 0.86f);
435 shader->sendFloat("inkDensity", 0.75f);
436 shader->sendFloat("texelW", 1.f / 256.f);
437 shader->sendFloat("texelH", 1.f / 256.f);
438 shader->sendFloat("time", 0.f);
439 shader->sendFloat("edgeStrength", 1.1f);
440 return;
441 }
442 if (style == "pixel") {
443 shader->declareFloat("pixelSize");
444 shader->declareFloat("paletteSteps");
445 shader->declareFloat("ditherStrength");
446 shader->declareFloat("toonBands");
447 shader->declareFloat("sharpness");
448 shader->declareFloat("texelW");
449 shader->declareFloat("texelH");
450 shader->declareFloat("time");
451 shader->declareFloat("screenW");
452 shader->declareFloat("screenH");
453 shader->declareFloat("outline");
454 shader->sendFloat("pixelSize", 5.f);
455 shader->sendFloat("paletteSteps", 6.f);
456 shader->sendFloat("ditherStrength", 0.18f);
457 shader->sendFloat("toonBands", 3.f);
458 shader->sendFloat("sharpness", 1.f);
459 shader->sendFloat("texelW", 1.f / 256.f);
460 shader->sendFloat("texelH", 1.f / 256.f);
461 shader->sendFloat("time", 0.f);
462 shader->sendFloat("screenW", 256.f);
463 shader->sendFloat("screenH", 256.f);
464 shader->sendFloat("outline", 0.9f);
465 return;
466 }
467 throw eve::Exception("bindPostUniforms: unknown style '%s'", style.c_str());
468}
469
470void bindMeshUniforms(graphics::Shader *shader, const std::string &style) {
471 if (!shader) throw eve::Exception("bindMeshUniforms: null shader");
472 if (style == "impasto") {
473 for (const auto &param : kImpastoParams) {
474 shader->declareFloat(param.id);
475 shader->sendFloat(param.id, param.defaultValue);
476 }
477 return;
478 }
479 if (style == "ysa") {
480 for (const auto &param : kYsaParams) {
481 shader->declareFloat(param.id);
482 shader->sendFloat(param.id, param.defaultValue);
483 }
484 return;
485 }
486 if (style == "anime") {
487 for (const auto &param : kAnimeParams) {
488 shader->declareFloat(param.id);
489 shader->sendFloat(param.id, param.defaultValue);
490 }
491 return;
492 }
493
494 if (isEffectStyle(style)) {
496 return;
497 }
498
499 if (style == "cartoon") {
500 shader->declareFloat("bands");
501 shader->declareFloat("rimPower");
502 shader->declareFloat("rimStrength");
503 shader->declareFloat("posterize");
504 shader->sendFloat("bands", 4.f);
505 shader->sendFloat("rimPower", 2.8f);
506 shader->sendFloat("rimStrength", 0.55f);
507 shader->sendFloat("posterize", 6.f);
508 return;
509 }
510 if (style == "ink") {
511 shader->declareFloat("washLevels");
512 shader->declareFloat("edgeThreshold");
513 shader->declareFloat("inkDensity");
514 shader->declareFloat("contrast");
515 shader->declareFloat("paperR");
516 shader->declareFloat("paperG");
517 shader->declareFloat("paperB");
518 shader->declareFloat("rimBoost");
519 shader->sendFloat("washLevels", 5.f);
520 shader->sendFloat("edgeThreshold", 0.35f);
521 shader->sendFloat("inkDensity", 0.9f);
522 shader->sendFloat("contrast", 1.25f);
523 shader->sendFloat("paperR", 0.93f);
524 shader->sendFloat("paperG", 0.90f);
525 shader->sendFloat("paperB", 0.82f);
526 shader->sendFloat("rimBoost", 0.65f);
527 return;
528 }
529 if (style == "xray") {
530 shader->declareFloat("colorR");
531 shader->declareFloat("colorG");
532 shader->declareFloat("colorB");
533 shader->declareFloat("bias");
534 shader->declareFloat("screenW");
535 shader->declareFloat("screenH");
536 shader->declareFloat("rimPower");
537 shader->declareFloat("rimStrength");
538 shader->declareFloat("alpha");
539 shader->sendFloat("colorR", 1.f);
540 shader->sendFloat("colorG", 0.62f);
541 shader->sendFloat("colorB", 0.12f);
542 shader->sendFloat("bias", 0.0002f);
543 shader->sendFloat("screenW", 256.f);
544 shader->sendFloat("screenH", 256.f);
545 shader->sendFloat("rimPower", 2.2f);
546 shader->sendFloat("rimStrength", 0.7f);
547 shader->sendFloat("alpha", 0.82f);
548 return;
549 }
550 throw eve::Exception("bindMeshUniforms: style '%s' has no mesh shader", style.c_str());
551}
552
553graphics::Shader *createPostShader(graphics::Graphics *gfx, const std::string &style) {
554 if (!gfx) throw eve::Exception("createPostShader: null graphics");
555 if (isEffectStyle(style)) return createEffectPostShader(gfx, style);
556 const StyleDefinition *def = findStyleDefinition(style);
557 if (!def) throw eve::Exception("createPostShader: unknown style '%s'", style.c_str());
558 if (!def->post)
559 throw eve::Exception("createPostShader: style '%s' has no post technique", style.c_str());
560
561 if (gfx->getBackendName() == "webgpu") {
562 const char *body = style == "cartoon" ? shaders::kCartoon
563 : style == "watercolor" ? shaders::kWatercolor
564 : style == "ink" ? shaders::kInk
566 graphics::Shader *sh = gfx->newShaderFromWgsl({}, std::string(shaders::kCommon) + body);
567 if (!sh || !sh->gpuHandle)
568 throw eve::Exception("createPostShader: failed to create '%s' WGSL", style.c_str());
569 bindPostUniforms(sh, style);
570 return sh;
571 }
572
573 std::vector<uint32_t> frag;
574 if (style == "cartoon")
575 frag = copySpv(cartoon_post_frag_spv, cartoon_post_frag_spv_count);
576 else if (style == "watercolor")
577 frag = copySpv(watercolor_post_frag_spv, watercolor_post_frag_spv_count);
578 else if (style == "ink")
579 frag = copySpv(ink_post_frag_spv, ink_post_frag_spv_count);
580 else
581 frag = copySpv(pixel_post_frag_spv, pixel_post_frag_spv_count);
582
583 graphics::Shader *sh = gfx->newShaderFromSpv({}, frag);
584 if (!sh || !sh->gpuHandle)
585 throw eve::Exception("createPostShader: failed to create '%s'", style.c_str());
586 bindPostUniforms(sh, style);
587 return sh;
588}
589
590graphics::Shader *createMeshShader(graphics::Graphics *gfx, const std::string &style) {
591 if (!gfx) throw eve::Exception("createMeshShader: null graphics");
592 if (isEffectStyle(style)) return createEffectMeshShader(gfx, style);
593
594 if (style == "impasto") {
596 gfx->getBackendName() == "webgpu"
597 ? gfx->newMeshShaderFromWgsl(shaders::kImpastoVert, shaders::kImpastoMesh)
598 : gfx->newMeshShaderFromSpv(copySpv(impasto_mesh_vert_spv, impasto_mesh_vert_spv_count),
599 copySpv(impasto_mesh_frag_spv, impasto_mesh_frag_spv_count));
600 if (!shader || !shader->gpuHandle)
601 throw eve::Exception("createMeshShader: failed to create impasto mesh shader");
602 bindMeshUniforms(shader, style);
603 return shader;
604 }
605
606 if (style == "ysa") {
607 // Vulkan stylize meshes share the skinning and vertex-color contract
608 // of the backend's standard WebGPU mesh vertex shader.
610 gfx->getBackendName() == "webgpu"
611 ? gfx->newMeshShaderFromWgsl({}, std::string(shaders::kMeshCommon) + shaders::kYsaMesh)
612 : gfx->newMeshShaderFromSpv(copySpv(stylize_mesh_vert_spv, stylize_mesh_vert_spv_count),
613 copySpv(ysa_mesh_frag_spv, ysa_mesh_frag_spv_count));
614 if (!shader || !shader->gpuHandle) throw eve::Exception("createMeshShader: failed to create ysa mesh shader");
615 bindMeshUniforms(shader, style);
616 return shader;
617 }
618
619 if (style == "anime") {
621 gfx->getBackendName() == "webgpu"
622 ? gfx->newMeshShaderFromWgsl({}, std::string(shaders::kMeshCommon) + shaders::kAnimeMesh)
623 : gfx->newMeshShaderFromSpv(copySpv(stylize_mesh_vert_spv, stylize_mesh_vert_spv_count),
624 copySpv(anime_mesh_frag_spv, anime_mesh_frag_spv_count));
625 if (!shader || !shader->gpuHandle) throw eve::Exception("createMeshShader: failed to create anime mesh shader");
626 bindMeshUniforms(shader, style);
627 return shader;
628 }
629
630 if (style == "cartoon") {
631 if (gfx->getBackendName() == "webgpu") {
632 graphics::Shader *sh =
634 if (!sh || !sh->gpuHandle) throw eve::Exception("createMeshShader: failed to create cartoon WGSL");
635 bindMeshUniforms(sh, style);
636 return sh;
637 }
638 auto vert = copySpv(stylize_mesh_vert_spv, stylize_mesh_vert_spv_count);
639 auto frag = copySpv(mesh3d_toon_frag_spv, mesh3d_toon_frag_spv_count);
641 if (!sh || !sh->gpuHandle) throw eve::Exception("createMeshShader: failed to create cartoon mesh shader");
642 bindMeshUniforms(sh, style);
643 return sh;
644 }
645 if (style == "ink") {
646 if (gfx->getBackendName() == "webgpu") {
648 {}, std::string(shaders::kMeshCommon) + shaders::kInkMesh);
649 if (!sh || !sh->gpuHandle)
650 throw eve::Exception("createMeshShader: failed to create ink WGSL");
651 bindMeshUniforms(sh, style);
652 return sh;
653 }
654 auto vert = copySpv(mesh3d_toon_vert_spv, mesh3d_toon_vert_spv_count);
655 auto frag = copySpv(ink_mesh_frag_spv, ink_mesh_frag_spv_count);
657 if (!sh || !sh->gpuHandle)
658 throw eve::Exception("createMeshShader: failed to create ink mesh shader");
659 bindMeshUniforms(sh, style);
660 return sh;
661 }
662 if (style == "xray") {
663 // WebGPU has no SPIR-V mesh shaders (WGSL only); Vulkan uses SPIR-V.
664 if (gfx->getBackendName() == "webgpu") {
665 graphics::Shader *sh = gfx->newMeshShaderFromWgsl(kXrayMeshVertWgsl, kXrayMeshFragWgsl);
666 if (!sh || !sh->gpuHandle)
667 throw eve::Exception("createMeshShader: failed to create xray mesh shader (wgsl)");
668 sh->setXray(true);
669 bindMeshUniforms(sh, style);
670 return sh;
671 }
672 auto vert = copySpv(mesh3d_toon_vert_spv, mesh3d_toon_vert_spv_count);
673 auto frag = copySpv(xray_mesh_frag_spv, xray_mesh_frag_spv_count);
675 if (!sh || !sh->gpuHandle)
676 throw eve::Exception("createMeshShader: failed to create xray mesh shader");
677 sh->setXray(true);
678 bindMeshUniforms(sh, style);
679 return sh;
680 }
681 throw eve::Exception(
682 "createMeshShader: style '%s' has no mesh variant (use watercolor/pixel as post)",
683 style.c_str());
684}
685
686} // namespace eve::stylize
std::string name
Definition ECS.cpp:154
scene::NodeDesc desc
vk::ShaderModule vert
vk::ShaderModule frag
std::vector< RecipeParam > params
std::string body
Shader * shader
int count
uint32_t index
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
virtual std::string getBackendName() const =0
Renderer backend id used by sibling modules (e.g. Gpgpu).
virtual Shader * newMeshShaderFromSpv(const std::vector< uint32_t > &vertSpv, const std::vector< uint32_t > &fragSpv)=0
Create a Mesh3D custom shader (MeshVertex + Frame UBO + albedo). Empty vert → default mesh3d....
virtual Shader * newShaderFromWgsl(const std::string &vertWgsl, const std::string &fragWgsl)=0
Create a 2D custom shader from WGSL source (WebGPU backend). Empty vert → default textured vertex sha...
virtual Shader * newShaderFromSpv(const std::vector< uint32_t > &vertSpv, const std::vector< uint32_t > &fragSpv)=0
Create a custom 2D shader from SPIR-V words (vert + frag). Owned by Graphics. Vertex stage may be emp...
virtual Shader * newMeshShaderFromWgsl(const std::string &vertWgsl, const std::string &fragWgsl)=0
Create a Mesh3D custom shader from WGSL source (WebGPU backend). The WGSL must declare the engine's F...
Custom GPU program.
Definition Shader.h:41
void setXray(bool x)
X-ray mesh shader. When true, drawMeshShader renders this mesh a second pass over the already-lit sce...
Definition Shader.h:74
constexpr const char * kPixel
Definition StylizeWgsl.h:19
constexpr const char * kWatercolor
Definition StylizeWgsl.h:13
constexpr const char * kCommon
Definition StylizeWgsl.h:4
constexpr const char * kInk
Definition StylizeWgsl.h:16
constexpr const char * kMeshCommon
Definition StylizeWgsl.h:22
constexpr const char * kCartoon
Definition StylizeWgsl.h:10
constexpr const char * kInkMesh
Definition StylizeWgsl.h:28
constexpr const char * kCartoonMesh
Definition StylizeWgsl.h:25
graphics::Shader * createMeshShader(graphics::Graphics *gfx, const std::string &style)
Creates mesh shader.
void bindEffectMeshUniforms(graphics::Shader *shader, const std::string &style)
Binds effect mesh uniforms.
graphics::Shader * createEffectMeshShader(graphics::Graphics *gfx, const std::string &style)
Creates effect mesh shader.
bool styleSupports(const std::string &style, const std::string &feature)
Style supports.
std::string styleIdAt(int index)
Style id at.
bool isEffectStyle(const std::string &style)
True when style names one of the ported effect shaders.
graphics::Shader * createPostShader(graphics::Graphics *gfx, const std::string &style)
Creates post shader.
graphics::Shader * createEffectPostShader(graphics::Graphics *gfx, const std::string &style)
Creates effect post shader.
void bindMeshUniforms(graphics::Shader *shader, const std::string &style)
Binds mesh uniforms.
int effectStyleCount()
Effect style count.
int styleCount()
Style count.
int styleParamCount(const std::string &style)
Style param count.
void bindEffectPostUniforms(graphics::Shader *shader, const std::string &style)
Binds effect post uniforms.
void bindPostUniforms(graphics::Shader *shader, const std::string &style)
Binds post uniforms.
bool isKnownStyle(const std::string &style)
True when known style.
std::string effectStyleIdAt(int index)
Effect style id at.
const StyleDefinition * findEffectDefinition(const std::string &style)
Effect-only StyleDefinition lookup, or nullptr when unknown.
const StyleParameterDesc * findStyleParameter(const std::string &style, const std::string &name)
Finds style parameter.
std::string styleParamName(const std::string &style, int index)
Style param name.
int effectParamCount(const std::string &style)
Effect param count.
const StyleParameterDesc * styleParameterAt(const std::string &style, int index)
Style parameter at.
const StyleParameterDesc * effectParameterAt(const std::string &style, int index)
Effect parameter at.
const StyleDefinition * findStyleDefinition(const std::string &style)
Return a built-in definition, or nullptr when the id is unknown.
Immutable capability metadata for one built-in stylization recipe.
Tooling metadata for one user-facing float parameter.