Browse Source

Add WebGL `ContextAttributes`

gingerBill 3 years ago
parent
commit
214b43974d
2 changed files with 53 additions and 6 deletions
  1. 17 0
      vendor/wasm/WebGL/webgl.odin
  2. 36 6
      vendor/wasm/js/runtime.mjs

+ 17 - 0
vendor/wasm/WebGL/webgl.odin

@@ -13,9 +13,26 @@ Renderbuffer :: distinct u32
 Shader       :: distinct u32
 Shader       :: distinct u32
 Texture      :: distinct u32
 Texture      :: distinct u32
 
 
+ContextAttribute :: enum u32 {
+	disableAlpha                 = 0,
+	disableAntialias             = 1,
+	disableDepth                 = 2,
+	failIfMajorPerformanceCaveat = 3,
+	disablePremultipliedAlpha    = 4,
+	preserveDrawingBuffer        = 5,
+	stencil                      = 6,
+	desynchronized               = 7,
+}
+ContextAttributes :: distinct bit_set[ContextAttribute; u32]
+
+DEFAULT_CONTEXT_ATTRIBUTES :: ContextAttributes{}
+
 @(default_calling_convention="c")
 @(default_calling_convention="c")
 foreign webgl {
 foreign webgl {
+	CreateCurrentContextById :: proc(name: string, attributes := DEFAULT_CONTEXT_ATTRIBUTES) -> bool ---
+	GetCurrentContextAttributes :: proc() -> ContextAttributes ---
 	SetCurrentContextById :: proc(name: string) -> bool ---
 	SetCurrentContextById :: proc(name: string) -> bool ---
+
 	DrawingBufferWidth  :: proc() -> i32 ---
 	DrawingBufferWidth  :: proc() -> i32 ---
 	DrawingBufferHeight :: proc() -> i32 ---
 	DrawingBufferHeight :: proc() -> i32 ---
 	
 	

+ 36 - 6
vendor/wasm/js/runtime.mjs

@@ -400,7 +400,6 @@ class WebGLInterface {
 		this.transformFeedbacks = [];
 		this.transformFeedbacks = [];
 		this.syncs              = [];
 		this.syncs              = [];
 		this.programInfos       = {};
 		this.programInfos       = {};
-		this.contextSettings    = {antialias: false};
 
 
 		this.setCurrentContext(canvasElement, contextSettings);
 		this.setCurrentContext(canvasElement, contextSettings);
 	}
 	}
@@ -417,10 +416,8 @@ class WebGLInterface {
 			return true;
 			return true;
 		}
 		}
 
 
-		if (contextSettings) {
-			this.contextSettings = contextSettings;
-		}
-		this.ctx = element.getContext("webgl2", this.contextSettings) || element.getContext("webgl", this.contextSettings);
+		contextSettings = contextSettings ?? {};
+		this.ctx = element.getContext("webgl2", contextSettings) || element.getContext("webgl", contextSettings);
 		if (!this.ctx) {
 		if (!this.ctx) {
 			return false;
 			return false;
 		}
 		}
@@ -491,7 +488,40 @@ class WebGLInterface {
 			SetCurrentContextById: (name_ptr, name_len) => {
 			SetCurrentContextById: (name_ptr, name_len) => {
 				let name = this.mem.loadString(name_ptr, name_len);
 				let name = this.mem.loadString(name_ptr, name_len);
 				let element = document.getElementById(name);
 				let element = document.getElementById(name);
-				return this.setCurrentContext(element, this.contextSettings);
+				return this.setCurrentContext(element, {alpha: true, antialias: true, depth: true, premultipliedAlpha: true});
+			},
+			CreateCurrentContextById: (name_ptr, name_len, attributes) => {
+				let name = this.mem.loadString(name_ptr, name_len);
+				let element = document.getElementById(name);
+
+				let contextSettings = {
+					alpha:                        !(attributes & (1<<0)),
+					antialias:                    !(attributes & (1<<1)),
+					depth:                        !(attributes & (1<<2)),
+					failIfMajorPerformanceCaveat: !!(attributes & (1<<3)),
+					premultipliedAlpha:           !(attributes & (1<<4)),
+					preserveDrawingBuffer:        !!(attributes & (1<<5)),
+					stencil:                      !!(attributes & (1<<6)),
+					desynchronized:               !!(attributes & (1<<7)),
+				};
+
+				return this.setCurrentContext(element, contextSettings);
+			},
+			GetCurrentContextAttributes: () => {
+				if (!this.ctx) {
+					return 0;
+				}
+				let attrs = this.ctx.getContextAttributes();
+				let res = 0;
+				if (!attrs.alpha)                        res |= 1<<0;
+				if (!attrs.antialias)                    res |= 1<<1;
+				if (!attrs.depth)                        res |= 1<<2;
+				if (attrs.failIfMajorPerformanceCaveat)  res |= 1<<3;
+				if (!attrs.premultipliedAlpha)           res |= 1<<4;
+				if (attrs.preserveDrawingBuffer)         res |= 1<<5;
+				if (attrs.stencil)                       res |= 1<<6;
+				if (attrs.desynchronized)                res |= 1<<7;
+				return res;
 			},
 			},
 
 
 			DrawingBufferWidth:  () => this.ctx.drawingBufferWidth,
 			DrawingBufferWidth:  () => this.ctx.drawingBufferWidth,