1use serde::{Deserialize, Serialize};
19
20use crate::noise::NoiseEngine;
21
22#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
30pub struct ShaderPrecisionProfile {
31 pub high_float_range_min: i32,
33 pub high_float_range_max: i32,
35 pub high_float_precision: i32,
37 pub medium_float_precision: i32,
39 pub low_float_precision: i32,
41 pub high_int_precision: i32,
43}
44
45impl Default for ShaderPrecisionProfile {
46 fn default() -> Self {
47 Self {
48 high_float_range_min: 127,
49 high_float_range_max: 127,
50 high_float_precision: 23,
51 medium_float_precision: 23,
52 low_float_precision: 23,
53 high_int_precision: 31,
54 }
55 }
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
64pub struct ContextAttributes {
65 pub alpha: bool,
67 pub antialias: bool,
69 pub depth: bool,
71 pub fail_if_major_performance_caveat: bool,
73 pub power_preference: String,
75 pub premultiplied_alpha: bool,
77 pub preserve_drawing_buffer: bool,
79 pub stencil: bool,
81 pub desynchronized: bool,
83}
84
85impl Default for ContextAttributes {
86 fn default() -> Self {
87 Self {
88 alpha: true,
89 antialias: true,
90 depth: true,
91 fail_if_major_performance_caveat: false,
92 power_preference: "default".to_string(),
93 premultiplied_alpha: true,
94 preserve_drawing_buffer: false,
95 stencil: false,
96 desynchronized: false,
97 }
98 }
99}
100
101#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
119pub struct WebGlProfile {
120 pub vendor: String,
122 pub renderer: String,
124 pub max_texture_size: u32,
126 pub max_viewport_dims: (u32, u32),
128 pub max_renderbuffer_size: u32,
130 pub max_vertex_attribs: u32,
132 pub max_varying_vectors: u32,
134 pub max_fragment_uniform_vectors: u32,
136 pub max_vertex_uniform_vectors: u32,
138 pub extensions: Vec<String>,
140 pub shader_precision: ShaderPrecisionProfile,
142 pub context_attributes: ContextAttributes,
144}
145
146impl WebGlProfile {
147 #[must_use]
158 pub fn nvidia_rtx_3060() -> Self {
159 Self {
160 vendor: "Google Inc. (NVIDIA)".to_string(),
161 renderer: "ANGLE (NVIDIA, NVIDIA GeForce RTX 3060 Direct3D11 vs_5_0 ps_5_0, D3D11)"
162 .to_string(),
163 max_texture_size: 16384,
164 max_viewport_dims: (32768, 32768),
165 max_renderbuffer_size: 16384,
166 max_vertex_attribs: 16,
167 max_varying_vectors: 15,
168 max_fragment_uniform_vectors: 1024,
169 max_vertex_uniform_vectors: 4096,
170 extensions: default_extensions(),
171 shader_precision: ShaderPrecisionProfile::default(),
172 context_attributes: ContextAttributes::default(),
173 }
174 }
175
176 #[must_use]
186 pub fn nvidia_gtx_1660() -> Self {
187 Self {
188 vendor: "Google Inc. (NVIDIA)".to_string(),
189 renderer:
190 "ANGLE (NVIDIA, NVIDIA GeForce GTX 1660 SUPER Direct3D11 vs_5_0 ps_5_0, D3D11)"
191 .to_string(),
192 max_texture_size: 16384,
193 max_viewport_dims: (32768, 32768),
194 max_renderbuffer_size: 16384,
195 max_vertex_attribs: 16,
196 max_varying_vectors: 15,
197 max_fragment_uniform_vectors: 1024,
198 max_vertex_uniform_vectors: 4096,
199 extensions: default_extensions(),
200 shader_precision: ShaderPrecisionProfile::default(),
201 context_attributes: ContextAttributes::default(),
202 }
203 }
204
205 #[must_use]
215 pub fn amd_rx_6700() -> Self {
216 Self {
217 vendor: "Google Inc. (AMD)".to_string(),
218 renderer: "ANGLE (AMD, AMD Radeon RX 6700 XT Direct3D11 vs_5_0 ps_5_0, D3D11)"
219 .to_string(),
220 max_texture_size: 16384,
221 max_viewport_dims: (32768, 32768),
222 max_renderbuffer_size: 16384,
223 max_vertex_attribs: 16,
224 max_varying_vectors: 15,
225 max_fragment_uniform_vectors: 1024,
226 max_vertex_uniform_vectors: 4096,
227 extensions: default_extensions(),
228 shader_precision: ShaderPrecisionProfile::default(),
229 context_attributes: ContextAttributes::default(),
230 }
231 }
232
233 #[must_use]
243 pub fn intel_uhd_630() -> Self {
244 Self {
245 vendor: "Google Inc. (Intel)".to_string(),
246 renderer: "ANGLE (Intel, Intel(R) UHD Graphics 630 Direct3D11 vs_5_0 ps_5_0, D3D11)"
247 .to_string(),
248 max_texture_size: 8192,
249 max_viewport_dims: (16384, 16384),
250 max_renderbuffer_size: 8192,
251 max_vertex_attribs: 16,
252 max_varying_vectors: 15,
253 max_fragment_uniform_vectors: 1024,
254 max_vertex_uniform_vectors: 4096,
255 extensions: default_extensions(),
256 shader_precision: ShaderPrecisionProfile::default(),
257 context_attributes: ContextAttributes::default(),
258 }
259 }
260
261 pub fn assert_consistent(&self) {
271 assert!(
272 self.max_texture_size <= self.max_viewport_dims.0,
273 "max_texture_size must be <= max_viewport_dims.0"
274 );
275 assert!(
276 self.max_texture_size <= self.max_viewport_dims.1,
277 "max_texture_size must be <= max_viewport_dims.1"
278 );
279 assert!(
280 self.max_renderbuffer_size <= self.max_texture_size,
281 "max_renderbuffer_size must be <= max_texture_size"
282 );
283 }
284}
285
286fn default_extensions() -> Vec<String> {
287 [
288 "ANGLE_instanced_arrays",
289 "EXT_blend_minmax",
290 "EXT_clip_control",
291 "EXT_color_buffer_half_float",
292 "EXT_depth_clamp",
293 "EXT_disjoint_timer_query",
294 "EXT_float_blend",
295 "EXT_frag_depth",
296 "EXT_shader_texture_lod",
297 "EXT_texture_compression_bptc",
298 "EXT_texture_compression_rgtc",
299 "EXT_texture_filter_anisotropic",
300 "EXT_sRGB",
301 "KHR_parallel_shader_compile",
302 "OES_element_index_uint",
303 "OES_fbo_render_mipmap",
304 "OES_standard_derivatives",
305 "OES_texture_float",
306 "OES_texture_float_linear",
307 "OES_texture_half_float",
308 "OES_texture_half_float_linear",
309 "OES_vertex_array_object",
310 "WEBGL_color_buffer_float",
311 "WEBGL_compressed_texture_s3tc",
312 "WEBGL_compressed_texture_s3tc_srgb",
313 "WEBGL_debug_shaders",
314 "WEBGL_depth_texture",
315 "WEBGL_draw_buffers",
316 "WEBGL_lose_context",
317 "WEBGL_multi_draw",
318 "WEBGL_polygon_mode",
319 ]
320 .iter()
321 .map(|s| (*s).to_string())
322 .collect()
323}
324
325#[must_use]
342#[allow(clippy::too_many_lines)]
343pub fn webgl_noise_script(profile: &WebGlProfile, engine: &NoiseEngine) -> String {
344 let noise_fn = engine.js_noise_fn();
345 let vendor = &profile.vendor;
346 let renderer = &profile.renderer;
347 let max_tex = profile.max_texture_size;
348 let vp_w = profile.max_viewport_dims.0;
349 let vp_h = profile.max_viewport_dims.1;
350 let max_rb = profile.max_renderbuffer_size;
351 let max_va = profile.max_vertex_attribs;
352 let max_varying_vectors = profile.max_varying_vectors;
353 let max_fragment_uniform_vectors = profile.max_fragment_uniform_vectors;
354 let max_vertex_uniform_vectors = profile.max_vertex_uniform_vectors;
355
356 let exts_json = {
357 let items: Vec<String> = profile
358 .extensions
359 .iter()
360 .map(|e| format!("{e:?}"))
361 .collect();
362 format!("[{}]", items.join(", "))
363 };
364
365 let sp = &profile.shader_precision;
366 let ca = &profile.context_attributes;
367 let ca_power = &ca.power_preference;
368
369 format!(
370 r"(function() {{
371 'use strict';
372
373 // ── Noise helpers ──────────────────────────────────────────────────────
374 {noise_fn}
375
376 // ── WebGL constants ────────────────────────────────────────────────────
377 const _VENDOR = 0x1F00;
378 const _RENDERER = 0x1F01;
379 const _UNMASKED_VENDOR = 0x9245;
380 const _UNMASKED_RENDERER = 0x9246;
381 const _MAX_TEXTURE_SIZE = 0x0D33;
382 const _MAX_VIEWPORT_DIMS = 0x0D3A;
383 const _MAX_RENDERBUFFER_SIZE = 0x84E8;
384 const _MAX_VERTEX_ATTRIBS = 0x8869;
385 const _MAX_VARYING_VECTORS = 0x8DFC;
386 const _MAX_FRAGMENT_UNIFORM_VECTORS = 0x8DFD;
387 const _MAX_VERTEX_UNIFORM_VECTORS = 0x8DFB;
388
389 const _PROFILE_VENDOR = {vendor:?};
390 const _PROFILE_RENDERER = {renderer:?};
391 const _EXTENSIONS = {exts_json};
392
393 // ── Spoof toString ─────────────────────────────────────────────────────
394 function _nts(name) {{ return function toString() {{ return 'function ' + name + '() {{ [native code] }}'; }}; }}
395 function _def(obj, prop, fn) {{
396 fn.toString = _nts(prop);
397 Object.defineProperty(obj, prop, {{ value: fn, writable: false, configurable: false, enumerable: false }});
398 }}
399
400 // ── Patch both WebGL1 and WebGL2 ────────────────────────────────────────
401 [WebGLRenderingContext, (typeof WebGL2RenderingContext !== 'undefined' ? WebGL2RenderingContext : null)]
402 .filter(Boolean)
403 .forEach(function(Ctx) {{
404 const proto = Ctx.prototype;
405
406 // getParameter
407 const _origGP = proto.getParameter;
408 _def(proto, 'getParameter', function getParameter(pname) {{
409 switch (pname) {{
410 case _VENDOR: return _PROFILE_VENDOR;
411 case _RENDERER: return _PROFILE_RENDERER;
412 case _UNMASKED_VENDOR: return _PROFILE_VENDOR;
413 case _UNMASKED_RENDERER: return _PROFILE_RENDERER;
414 case _MAX_TEXTURE_SIZE: return {max_tex};
415 case _MAX_VIEWPORT_DIMS: return new Int32Array([{vp_w}, {vp_h}]);
416 case _MAX_RENDERBUFFER_SIZE: return {max_rb};
417 case _MAX_VERTEX_ATTRIBS: return {max_va};
418 case _MAX_VARYING_VECTORS: return {max_varying_vectors};
419 case _MAX_FRAGMENT_UNIFORM_VECTORS: return {max_fragment_uniform_vectors};
420 case _MAX_VERTEX_UNIFORM_VECTORS: return {max_vertex_uniform_vectors};
421 default: return _origGP.call(this, pname);
422 }}
423 }});
424
425 // getSupportedExtensions
426 _def(proto, 'getSupportedExtensions', function getSupportedExtensions() {{
427 return _EXTENSIONS.slice();
428 }});
429
430 // getExtension
431 const _origGE = proto.getExtension;
432 _def(proto, 'getExtension', function getExtension(name) {{
433 if (!_EXTENSIONS.includes(name)) return null;
434 return _origGE.call(this, name);
435 }});
436
437 // getShaderPrecisionFormat
438 _def(proto, 'getShaderPrecisionFormat', function getShaderPrecisionFormat(shaderType, precisionType) {{
439 // HIGH_FLOAT = 0x8DF2, MEDIUM_FLOAT = 0x8DF1, LOW_FLOAT = 0x8DF0
440 // HIGH_INT = 0x8DF5, MEDIUM_INT = 0x8DF4, LOW_INT = 0x8DF3
441 const HIGH_FLOAT = 0x8DF2, MEDIUM_FLOAT = 0x8DF1, HIGH_INT = 0x8DF5;
442 if (precisionType === HIGH_FLOAT) {{
443 return {{ rangeMin: {sp_hfrm}, rangeMax: {sp_hfrx}, precision: {sp_hfp} }};
444 }} else if (precisionType === MEDIUM_FLOAT) {{
445 return {{ rangeMin: 127, rangeMax: 127, precision: {sp_mfp} }};
446 }} else if (precisionType === HIGH_INT) {{
447 return {{ rangeMin: 31, rangeMax: 30, precision: {sp_hip} }};
448 }}
449 return {{ rangeMin: 1, rangeMax: 1, precision: 8 }};
450 }});
451
452 // getContextAttributes
453 _def(proto, 'getContextAttributes', function getContextAttributes() {{
454 return {{
455 alpha: {ca_alpha},
456 antialias: {ca_antialias},
457 depth: {ca_depth},
458 failIfMajorPerformanceCaveat: {ca_fail},
459 powerPreference: {ca_power:?},
460 premultipliedAlpha: {ca_pma},
461 preserveDrawingBuffer: {ca_pdb},
462 stencil: {ca_stencil},
463 desynchronized: {ca_desync},
464 }};
465 }});
466
467 // readPixels — apply webgl noise to output
468 const _origRP = proto.readPixels;
469 _def(proto, 'readPixels', function readPixels(x, y, width, height, format, type, pixels) {{
470 _origRP.call(this, x, y, width, height, format, type, pixels);
471 if (pixels instanceof Uint8Array || pixels instanceof Uint8ClampedArray) {{
472 for (let i = 0; i < pixels.length; i += 4) {{
473 const px = (x + ((i / 4) % width)) >>> 0;
474 const py = (y + (((i / 4) / width) | 0)) >>> 0;
475 if (pixels[i] === 0 && pixels[i+1] === 0 && pixels[i+2] === 0 && pixels[i+3] === 0) continue;
476 const [dr, dg, db, da] = __stygian_webgl_noise('readPixels', px, py);
477 pixels[i] = Math.max(0, Math.min(255, pixels[i] + dr));
478 pixels[i+1] = Math.max(0, Math.min(255, pixels[i+1] + dg));
479 pixels[i+2] = Math.max(0, Math.min(255, pixels[i+2] + db));
480 pixels[i+3] = Math.max(0, Math.min(255, pixels[i+3] + da));
481 }}
482 }}
483 }});
484 }});
485}})();
486",
487 noise_fn = noise_fn,
488 vendor = vendor,
489 renderer = renderer,
490 exts_json = exts_json,
491 max_tex = max_tex,
492 vp_w = vp_w,
493 vp_h = vp_h,
494 max_rb = max_rb,
495 max_va = max_va,
496 max_varying_vectors = max_varying_vectors,
497 max_fragment_uniform_vectors = max_fragment_uniform_vectors,
498 max_vertex_uniform_vectors = max_vertex_uniform_vectors,
499 sp_hfrm = sp.high_float_range_min,
500 sp_hfrx = sp.high_float_range_max,
501 sp_hfp = sp.high_float_precision,
502 sp_mfp = sp.medium_float_precision,
503 sp_hip = sp.high_int_precision,
504 ca_alpha = ca.alpha,
505 ca_antialias = ca.antialias,
506 ca_depth = ca.depth,
507 ca_fail = ca.fail_if_major_performance_caveat,
508 ca_power = ca_power,
509 ca_pma = ca.premultiplied_alpha,
510 ca_pdb = ca.preserve_drawing_buffer,
511 ca_stencil = ca.stencil,
512 ca_desync = ca.desynchronized,
513 )
514}
515
516#[cfg(test)]
521mod tests {
522 use super::*;
523 use crate::noise::{NoiseEngine, NoiseSeed};
524
525 fn eng() -> NoiseEngine {
526 NoiseEngine::new(NoiseSeed::from(1_u64))
527 }
528
529 #[test]
530 fn all_profiles_consistent() {
531 WebGlProfile::nvidia_rtx_3060().assert_consistent();
532 WebGlProfile::nvidia_gtx_1660().assert_consistent();
533 WebGlProfile::amd_rx_6700().assert_consistent();
534 WebGlProfile::intel_uhd_630().assert_consistent();
535 }
536
537 #[test]
538 fn script_contains_webgl_overrides() {
539 let js = webgl_noise_script(&WebGlProfile::nvidia_rtx_3060(), &eng());
540 assert!(js.contains("getParameter"), "missing getParameter");
541 assert!(
542 js.contains("getSupportedExtensions"),
543 "missing getSupportedExtensions"
544 );
545 assert!(js.contains("getExtension"), "missing getExtension");
546 assert!(
547 js.contains("getShaderPrecisionFormat"),
548 "missing getShaderPrecisionFormat"
549 );
550 assert!(
551 js.contains("getContextAttributes"),
552 "missing getContextAttributes"
553 );
554 assert!(js.contains("readPixels"), "missing readPixels");
555 }
556
557 #[test]
558 fn script_contains_noise_reference() {
559 let js = webgl_noise_script(&WebGlProfile::nvidia_rtx_3060(), &eng());
560 assert!(
561 js.contains("__stygian_webgl_noise"),
562 "missing webgl noise fn"
563 );
564 }
565
566 #[test]
567 fn script_contains_native_tostring() {
568 let js = webgl_noise_script(&WebGlProfile::nvidia_rtx_3060(), &eng());
569 assert!(js.contains("[native code]"), "missing toString spoof");
570 }
571
572 #[test]
573 fn profile_serde_round_trip() {
574 let p = WebGlProfile::nvidia_rtx_3060();
575 let json_result = serde_json::to_string(&p);
576 assert!(json_result.is_ok(), "serialize failed: {json_result:?}");
577 let Ok(json) = json_result else {
578 return;
579 };
580 let back_result: Result<WebGlProfile, _> = serde_json::from_str(&json);
581 assert!(back_result.is_ok(), "deserialize failed: {back_result:?}");
582 let Ok(back) = back_result else {
583 return;
584 };
585 assert_eq!(back.vendor, p.vendor);
586 assert_eq!(back.renderer, p.renderer);
587 assert_eq!(back.max_texture_size, p.max_texture_size);
588 assert_eq!(back.extensions.len(), p.extensions.len());
589 }
590
591 #[test]
592 fn script_contains_renderer_string() {
593 let p = WebGlProfile::nvidia_rtx_3060();
594 let js = webgl_noise_script(&p, &eng());
595 assert!(js.contains("RTX 3060"), "renderer not in script");
596 }
597}