Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resources release functions and leaks fixes to the gl_backend #428

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository = "https://github.com/not-fl3/miniquad"
description = """
Cross-platform window context and rendering library.
"""
readme="README.md"
readme = "README.md"
exclude = ["examples/"]
keywords = ["graphics", "3D", "opengl", "gamedev", "windowing"]
categories = ["rendering::graphics-api"]
Expand All @@ -24,7 +24,17 @@ log-impl = []
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["wingdi", "winuser", "libloaderapi", "windef", "shellscalingapi", "errhandlingapi", "windowsx", "winbase", "hidusage"] }
winapi = { version = "0.3", features = [
"wingdi",
"winuser",
"libloaderapi",
"windef",
"shellscalingapi",
"errhandlingapi",
"windowsx",
"winbase",
"hidusage",
] }

[target.'cfg(target_os = "android")'.dependencies]
libc = "0.2"
Expand All @@ -34,5 +44,16 @@ ndk-sys = "0.2"
objc = "0.2"

[dev-dependencies]
glam = {version = "0.24", features = ["scalar-math"] }
glam = { version = "0.24", features = ["scalar-math"] }
quad-rand = "0.1"

[profile.release]
lto = true
panic = 'abort'
opt-level = "s"
overflow-checks = false
debug-assertions = false
incremental = false
rpath = false
codegen-units = 1
strip = true
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
<script>load("instancing.wasm");</script> <!-- Your compiled wasm file -->
</body>

</html>
</html>
124 changes: 67 additions & 57 deletions js/gl.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ canvas.requestPointerLock = canvas.requestPointerLock ||
canvas.mozRequestPointerLock ||
// pointer lock in any form is not supported on iOS safari
// https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API#browser_compatibility
(function () {});
(function () { });
document.exitPointerLock = document.exitPointerLock ||
document.mozExitPointerLock ||
// pointer lock in any form is not supported on iOS safari
(function () {});
(function () { });

function assert(flag, message) {
if (flag == false) {
Expand Down Expand Up @@ -562,7 +562,7 @@ function into_sapp_keycode(key_code) {
console.log("Unsupported keyboard key: ", key_code)
}

function dpi_scale() {
function dpi_scale() {
if (high_dpi) {
return window.devicePixelRatio || 1.0;
} else {
Expand Down Expand Up @@ -614,7 +614,7 @@ var importObject = {
set_emscripten_shader_hack: function (flag) {
emscripten_shaders_hack = flag;
},
sapp_set_clipboard: function(ptr, len) {
sapp_set_clipboard: function (ptr, len) {
clipboard = UTF8ToString(ptr, len);
},
dpi_scale,
Expand Down Expand Up @@ -666,7 +666,7 @@ var importObject = {
gl.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type,
pixels ? getArray(pixels, Uint8Array, texture_size(format, width, height)) : null);
},
glReadPixels: function(x, y, width, height, format, type, pixels) {
glReadPixels: function (x, y, width, height, format, type, pixels) {
var pixelData = getArray(pixels, Uint8Array, texture_size(format, width, height));
gl.readPixels(x, y, width, height, format, type, pixelData);
},
Expand Down Expand Up @@ -845,6 +845,11 @@ var importObject = {
GL.validateGLObjectID(GL.shaders, shader, 'glAttachShader', 'shader');
gl.attachShader(GL.programs[program], GL.shaders[shader]);
},
glDettachShader: function (program, shader) {
GL.validateGLObjectID(GL.programs, program, 'glDetachShader', 'program');
GL.validateGLObjectID(GL.shaders, shader, 'glDetachShader', 'shader');
gl.detachShader(GL.programs[program], GL.shaders[shader]);
},
glLinkProgram: function (program) {
GL.validateGLObjectID(GL.programs, program, 'glLinkProgram', 'program');
gl.linkProgram(GL.programs[program]);
Expand Down Expand Up @@ -956,7 +961,7 @@ var importObject = {
array[i] = log.charCodeAt(i);
}
},
glGetString: function(id) {
glGetString: function (id) {
// getParameter returns "any": it could be GLenum, String or whatever,
// depending on the id.
var parameter = gl.getParameter(id).toString();
Expand Down Expand Up @@ -1006,7 +1011,12 @@ var importObject = {
glDrawElementsInstanced: function (mode, count, type, indices, primcount) {
gl.drawElementsInstanced(mode, count, type, indices, primcount);
},
glDeleteShader: function (shader) { gl.deleteShader(shader) },
glDeleteShader: function (shader) {
var id = GL.shaders[shader];
if (id == null) { return }
gl.deleteShader(id);
GL.shaders[shader] = null
},
glDeleteBuffers: function (n, buffers) {
for (var i = 0; i < n; i++) {
var id = getArray(buffers + i * 4, Uint32Array, 1)[0];
Expand Down Expand Up @@ -1045,45 +1055,45 @@ var importObject = {
GL.textures[id] = null;
}
},
glGenQueries: function (n, ids) {
_glGenObject(n, ids, 'createQuery', GL.timerQueries, 'glGenQueries');
},
glDeleteQueries: function (n, ids) {
glGenQueries: function (n, ids) {
_glGenObject(n, ids, 'createQuery', GL.timerQueries, 'glGenQueries');
},
glDeleteQueries: function (n, ids) {
for (var i = 0; i < n; i++) {
var id = getArray(textures + i * 4, Uint32Array, 1)[0];
var query = GL.timerQueries[id];
if (!query) {
continue;
}
continue;
}
gl.deleteQuery(query);
query.name = 0;
GL.timerQueries[id] = null;
}
},
glBeginQuery: function (target, id) {
GL.validateGLObjectID(GL.timerQueries, id, 'glBeginQuery', 'id');
gl.beginQuery(target, GL.timerQueries[id]);
},
glEndQuery: function (target) {
gl.endQuery(target);
},
glGetQueryObjectiv: function (id, pname, ptr) {
GL.validateGLObjectID(GL.timerQueries, id, 'glGetQueryObjectiv', 'id');
let result = gl.getQueryObject(GL.timerQueries[id], pname);
getArray(ptr, Uint32Array, 1)[0] = result;
},
glGetQueryObjectui64v: function (id, pname, ptr) {
GL.validateGLObjectID(GL.timerQueries, id, 'glGetQueryObjectui64v', 'id');
let result = gl.getQueryObject(GL.timerQueries[id], pname);
let heap = getArray(ptr, Uint32Array, 2);
heap[0] = result;
heap[1] = (result - heap[0])/4294967296;
},
},
glBeginQuery: function (target, id) {
GL.validateGLObjectID(GL.timerQueries, id, 'glBeginQuery', 'id');
gl.beginQuery(target, GL.timerQueries[id]);
},
glEndQuery: function (target) {
gl.endQuery(target);
},
glGetQueryObjectiv: function (id, pname, ptr) {
GL.validateGLObjectID(GL.timerQueries, id, 'glGetQueryObjectiv', 'id');
let result = gl.getQueryObject(GL.timerQueries[id], pname);
getArray(ptr, Uint32Array, 1)[0] = result;
},
glGetQueryObjectui64v: function (id, pname, ptr) {
GL.validateGLObjectID(GL.timerQueries, id, 'glGetQueryObjectui64v', 'id');
let result = gl.getQueryObject(GL.timerQueries[id], pname);
let heap = getArray(ptr, Uint32Array, 2);
heap[0] = result;
heap[1] = (result - heap[0]) / 4294967296;
},
glGenerateMipmap: function (index) {
gl.generateMipmap(index);
},

setup_canvas_size: function(high_dpi) {
setup_canvas_size: function (high_dpi) {
window.high_dpi = high_dpi;
resize(canvas);
},
Expand Down Expand Up @@ -1220,20 +1230,20 @@ var importObject = {
window.onresize = function () {
resize(canvas, wasm_exports.resize);
};
window.addEventListener("copy", function(e) {
window.addEventListener("copy", function (e) {
if (clipboard != null) {
event.clipboardData.setData('text/plain', clipboard);
event.preventDefault();
}
});
window.addEventListener("cut", function(e) {
window.addEventListener("cut", function (e) {
if (clipboard != null) {
event.clipboardData.setData('text/plain', clipboard);
event.preventDefault();
}
});

window.addEventListener("paste", function(e) {
window.addEventListener("paste", function (e) {
e.stopPropagation();
e.preventDefault();
var clipboardData = e.clipboardData || window.clipboardData;
Expand All @@ -1248,11 +1258,11 @@ var importObject = {
}
});

window.ondragover = function(e) {
window.ondragover = function (e) {
e.preventDefault();
};

window.ondrop = async function(e) {
window.ondrop = async function (e) {
e.preventDefault();

wasm_exports.on_files_dropped_start();
Expand All @@ -1276,9 +1286,9 @@ var importObject = {
};

let lastFocus = document.hasFocus();
var checkFocus = function() {
var checkFocus = function () {
let hasFocus = document.hasFocus();
if(lastFocus == hasFocus){
if (lastFocus == hasFocus) {
wasm_exports.focus(hasFocus);
lastFocus = hasFocus;
}
Expand All @@ -1296,22 +1306,22 @@ var importObject = {
FS.unique_id += 1;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.responseType = 'arraybuffer';

xhr.onreadystatechange = function() {
// looks like readyState === 4 will be fired on either successful or unsuccessful load:
// https://stackoverflow.com/a/19247992
xhr.onreadystatechange = function () {
// looks like readyState === 4 will be fired on either successful or unsuccessful load:
// https://stackoverflow.com/a/19247992
if (this.readyState === 4) {
if(this.status === 200) {
if (this.status === 200) {
var uInt8Array = new Uint8Array(this.response);

FS.loaded_files[file_id] = uInt8Array;
wasm_exports.file_loaded(file_id);
} else {
FS.loaded_files[file_id] = null;
wasm_exports.file_loaded(file_id);
}
}
}
};
xhr.send();

Expand Down Expand Up @@ -1341,22 +1351,22 @@ var importObject = {
document.exitPointerLock();
}
},
sapp_set_cursor: function(ptr, len) {
sapp_set_cursor: function (ptr, len) {
canvas.style.cursor = UTF8ToString(ptr, len);
},
sapp_is_fullscreen: function() {
sapp_is_fullscreen: function () {
let fullscreenElement = document.fullscreenElement;

return fullscreenElement != null && fullscreenElement.id == canvas.id;
},
sapp_set_fullscreen: function(fullscreen) {
sapp_set_fullscreen: function (fullscreen) {
if (!fullscreen) {
document.exitFullscreen();
} else {
canvas.requestFullscreen();
}
},
sapp_set_window_size: function(new_width, new_height) {
sapp_set_window_size: function (new_width, new_height) {
canvas.width = new_width;
canvas.height = new_height;
resize(canvas, wasm_exports.resize);
Expand Down Expand Up @@ -1406,11 +1416,11 @@ function init_plugins(plugins) {

if (plugins[i].version != crate_version) {
console.error("Plugin " + plugins[i].name + " version mismatch" +
"js version: " + plugins[i].version + ", crate version: " + crate_version)
"js version: " + plugins[i].version + ", crate version: " + crate_version)
}
}
}
}
}
}


Expand All @@ -1427,7 +1437,7 @@ function add_missing_functions_stabs(obj) {
for (const i in imports) {
if (importObject["env"][imports[i].name] == undefined) {
console.warn("No " + imports[i].name + " function in gl.js");
importObject["env"][imports[i].name] = function() {
importObject["env"][imports[i].name] = function () {
console.warn("Missed function: " + imports[i].name);
};
}
Expand All @@ -1454,7 +1464,7 @@ function load(wasm_path) {
if (version != crate_version) {
console.error(
"Version mismatch: gl.js version is: " + version +
", miniquad crate version is: " + crate_version);
", miniquad crate version is: " + crate_version);
}
init_plugins(plugins);
obj.exports.main();
Expand All @@ -1479,7 +1489,7 @@ function load(wasm_path) {
if (version != crate_version) {
console.error(
"Version mismatch: gl.js version is: " + version +
", rust sapp-wasm crate version is: " + crate_version);
", rust sapp-wasm crate version is: " + crate_version);
}
init_plugins(plugins);
obj.exports.main();
Expand Down
12 changes: 11 additions & 1 deletion src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ pub trait RenderingBackend {
params: PipelineParams,
) -> Pipeline;
fn apply_pipeline(&mut self, pipeline: &Pipeline);
fn delete_pipeline(&mut self, pipeline: Pipeline);

/// Create a buffer resource object.
/// ```ignore
Expand Down Expand Up @@ -1230,7 +1231,7 @@ pub trait RenderingBackend {
/// More high-level code on top of miniquad probably is going to call this in Drop implementation of some
/// more RAII buffer object.
///
/// There is no protection against using deleted textures later. However its not an UB in OpenGl and thats why
/// There is no protection against using deleted buffers later. However its not an UB in OpenGl and thats why
/// this function is not marked as unsafe
fn delete_buffer(&mut self, buffer: BufferId);

Expand All @@ -1243,6 +1244,15 @@ pub trait RenderingBackend {
/// this function is not marked as unsafe
fn delete_texture(&mut self, texture: TextureId);

/// Delete GPU program, leaving handle unmodified.
///
/// More high-level code on top of miniquad probably is going to call this in Drop implementation of some
/// more RAII buffer object.
///
/// There is no protection against using deleted programs later. However its not a CPU-level Porgram and thats why
/// this function is not marked as unsafe
fn delete_shader(&mut self, program: ShaderId);

/// Set a new viewport rectangle.
/// Should be applied after begin_pass.
fn apply_viewport(&mut self, x: i32, y: i32, w: i32, h: i32);
Expand Down
Loading
Loading