mirror of
https://github.com/odin-lang/Odin.git
synced 2026-06-04 09:44:40 +00:00
Add WebGL ContextAttributes
This commit is contained in:
17
vendor/wasm/WebGL/webgl.odin
vendored
17
vendor/wasm/WebGL/webgl.odin
vendored
@@ -13,9 +13,26 @@ Renderbuffer :: distinct u32
|
||||
Shader :: 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")
|
||||
foreign webgl {
|
||||
CreateCurrentContextById :: proc(name: string, attributes := DEFAULT_CONTEXT_ATTRIBUTES) -> bool ---
|
||||
GetCurrentContextAttributes :: proc() -> ContextAttributes ---
|
||||
SetCurrentContextById :: proc(name: string) -> bool ---
|
||||
|
||||
DrawingBufferWidth :: proc() -> i32 ---
|
||||
DrawingBufferHeight :: proc() -> i32 ---
|
||||
|
||||
|
||||
42
vendor/wasm/js/runtime.mjs
vendored
42
vendor/wasm/js/runtime.mjs
vendored
@@ -400,7 +400,6 @@ class WebGLInterface {
|
||||
this.transformFeedbacks = [];
|
||||
this.syncs = [];
|
||||
this.programInfos = {};
|
||||
this.contextSettings = {antialias: false};
|
||||
|
||||
this.setCurrentContext(canvasElement, contextSettings);
|
||||
}
|
||||
@@ -417,10 +416,8 @@ class WebGLInterface {
|
||||
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) {
|
||||
return false;
|
||||
}
|
||||
@@ -491,7 +488,40 @@ class WebGLInterface {
|
||||
SetCurrentContextById: (name_ptr, name_len) => {
|
||||
let name = this.mem.loadString(name_ptr, name_len);
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user