This commit is contained in:
2024-11-17 11:34:35 -06:00
parent 0d12002677
commit 069c860ae1
14 changed files with 28593 additions and 363 deletions

View File

@@ -3,13 +3,12 @@ project('prosperon', ['c', 'cpp'], default_options : [ 'cpp_std=c++11'])
libtype = get_option('default_library')
link = []
src = []
if not get_option('editor')
add_project_arguments('-DNEDITOR', language:'c')
add_project_arguments('-DTRACY_ENABLE', language:'c')
endif
add_project_arguments('-Wno-incompatible-function-pointer-types', language: 'c')
add_project_arguments('-Wno-incompatible-pointer-types', language: 'c')
add_project_arguments('-Wno-narrowing', language: 'cpp')
@@ -28,7 +27,7 @@ cc = meson.get_compiler('c')
if host_machine.system() == 'linux'
deps += cc.find_library('asound', required:true)
deps += [dependency('x11'), dependency('xi'), dependency('xcursor'), dependency('egl'), dependency('gl')]
# link += '-fuse-ld=mold' # use mold, which is very fast, for debug builds
link += '-fuse-ld=mold' # use mold, which is very fast, for debug builds
endif
if host_machine.system() == 'windows'
@@ -40,14 +39,28 @@ if host_machine.system() == 'emscripten'
link += '-sUSE_WEBGPU'
endif
deps += dependency('quickjs', static:true)
tracy_opts = ['fibers=true', 'on_demand=true']
quickjs_opts = []
src += 'qjs_tracy.c'
add_project_arguments('-DTRACY_ENABLE', language:['c','cpp'])
deps += dependency('tracy', static:true, default_options:tracy_opts)
quickjs_opts += 'default_library=static'
deps += dependency('quickjs', static:true, default_options:quickjs_opts)
storefront = get_option('storefront')
if storefront == 'steam'
deps += dependency('qjs-steam',static:false)
endif
deps += dependency('qjs-layout',static:true)
deps += dependency('qjs-nota',static:true)
deps += dependency('qjs-miniz',static:true)
deps += dependency('qjs-soloud',static:true)
deps += dependency('qjs-dmon',static:true)
deps += dependency('tracy', static:true)
deps += dependency('threads')
if get_option('chipmunk')
@@ -58,13 +71,8 @@ if get_option('enet')
deps += dependency('qjs-enet',static:true)
endif
storefront = get_option('storefront')
if storefront == 'steam'
deps += dependency('qjs-steam',static:false)
endif
sources = []
src = ['anim.c', 'config.c', 'datastream.c','font.c','gameobject.c','HandmadeMath.c','jsffi.c','model.c','render.c','script.c','simplex.c','spline.c','texture.c', 'timer.c', 'transform.c','warp.c','yugine.c']
src += ['anim.c', 'config.c', 'datastream.c','font.c','gameobject.c','HandmadeMath.c','jsffi.c','model.c','render.c','render_trace.cpp','script.c','simplex.c','spline.c','texture.c', 'timer.c', 'transform.c','warp.c','yugine.c', 'glad.c']
imsrc = ['GraphEditor.cpp','ImCurveEdit.cpp','ImGradient.cpp','imgui_draw.cpp','imgui_tables.cpp','imgui_widgets.cpp','imgui.cpp','ImGuizmo.cpp','imnodes.cpp','implot_items.cpp','implot.cpp']
srceng = 'source'

View File

@@ -998,7 +998,7 @@ Object.defineProperty(Array.prototype, "mult", {
});
Object.defineProperty(Array.prototype, "apply", {
value: function (fn) {
value: function fnapply(fn) {
this.forEach(function (x) {
x[fn].apply(x);
});
@@ -1006,13 +1006,13 @@ Object.defineProperty(Array.prototype, "apply", {
});
Object.defineProperty(Array.prototype, "sorted", {
value: function () {
value: function sorted() {
return this.toSorted();
},
});
Object.defineProperty(Array.prototype, "equal", {
value: function (b) {
value: function equal(b) {
if (this.length !== b.length) return false;
if (b == null) return false;
if (this === b) return true;
@@ -1052,7 +1052,7 @@ Object.defineProperty(Array.prototype, "remove", {
});
Object.defineProperty(Array.prototype, "set", {
value: function (b) {
value: function set(b) {
if (this.length !== b.length) return;
b.forEach(function (val, i) {
@@ -1062,14 +1062,14 @@ Object.defineProperty(Array.prototype, "set", {
});
Object.defineProperty(Array.prototype, "flat", {
value: function () {
value: function flat() {
return [].concat.apply([], this);
},
});
/* Return true if array contains x */
Object.defineProperty(Array.prototype, "empty", {
get: function () {
get: function empty() {
return this.length === 0;
},
});

View File

@@ -314,7 +314,7 @@ globalThis.use = function use(file) {
var script = Resources.replstrs(file);
var fnname = file.replace(/[^a-zA-Z0-9_$]/g, "_");
script = `(function ${fnname}() { var self = this; ${script}; })`;
script = `(function ${fnname}() { var self = this; ${script}; } )`;
var fn = os.eval(file, script);
use_cache[file] = fn;
@@ -404,8 +404,6 @@ function bare_use(file) {
Object.assign(globalThis, os.eval(file, script)());
}
globalThis.debug = {};
profile.enabled = true;
console.enabled = true;
debug.enabled = true;

View File

@@ -79,6 +79,7 @@ prosperon.SIGSEGV = function()
prosperon.init = function () {
render.init();
imgui.init();
tracy.gpu_init();
globalThis.audio = use("sound.js");
world_start();
@@ -573,9 +574,9 @@ var Register = {
if (!flush) {
prosperon[name] = function (...args) {
// profile.fiber_enter(vector.fib);
// tracy.fiber_enter(vector.fib);
fns.forEach(fn => fn(...args));
// profile.fiber_leave(vector.fib);
// tracy.fiber_leave(vector.fib);
};
} else
prosperon[name] = function name(...args) {

View File

@@ -1054,8 +1054,38 @@ render.image = function image(image, rect = [0,0], rotation = 0, color = Color.w
return;
};
render.images = function(image, rects, rotations, colors)
render.images = function images(image, rects)
{
if (!image) throw Error ('Need an image to render.');
if (typeof image === "string") image = game.texture(image);
var tex = image.texture;
if (!tex) return;
var image_size = calc_image_size(image);
if (!lasttex) {
check_flush(flush_img);
lasttex = tex;
}
if (lasttex !== tex) {
flush_img();
lasttex = tex;
}
// rects = rects.flat();
var rect = rects[0];
var size = [rect.width ? rect.width : image_size.x, rect.height ? rect.height : image_size.y];
var offset = size.scale([rect.anchor_x, rect.anchor_y]);
for (var rect of rects) {
var e = img_e();
var pos = [rect.x,rect.y].sub(offset);
e.transform.trs(pos, undefined, size);
e.image = image;
e.shade = Color.white;
}
return;
}
var slice9_t = os.make_transform();
@@ -1324,6 +1354,10 @@ var imgui_fn = function imgui_fn() {
imtoggle("Meta [f7]", debug, "meta");
imtoggle("Cheats [f8]", debug, "cheat");
imtoggle("Console [f9]", debug, "console");
if (profile.tracing)
imgui.button("stop trace", profile.trace_stop);
else
imgui.button('start trace', profile.trace_start);
});
imgui.sokol_gfx();
@@ -1384,7 +1418,6 @@ try{
if (render.draw_particles) draw_emitters();
render.fillmask(0);
render.forceflush();
render.set_projection_ortho({
l:0,
r:prosperon.camera.size.x,
@@ -1410,7 +1443,6 @@ try{
}, false);
prosperon.app();
render.forceflush();
if (debug.show) imgui_fn();
} catch(e) {
throw e;
@@ -1418,6 +1450,25 @@ try{
render.end_pass();
render.commit();
endframe();
var cycles = os.gc();
if (cycles.length > 0) {
cycles.forEach(x => {
x.address = x.address.toString(16);
x.shape = x.shape.toString(16);
x.class = x.class.toString(16);
});
tracy.message(`GC cycles freed: ${json.encode(cycles)}`);
}
tracy.gpu_collect();
tracy.end_frame();
tracy.gpu_sync();
/* var rtinfo = os.rt_info();
for (var i in rtinfo) {
console.log(`${i} is ${rtinfo[i]}`);
tracy.plot(i, rtinfo[i]);
}*/
}
};
@@ -1471,7 +1522,8 @@ prosperon.process = function process() {
*/
}
prosperon.render();
tracy.gpu_zone(prosperon.render);
// prosperon.render();
};
return { render };

10132
source/glad.c Normal file

File diff suppressed because one or more lines are too long

17379
source/glad.h Normal file

File diff suppressed because one or more lines are too long

View File

@@ -32,7 +32,19 @@
#include <dirent.h>
#include "cute_aseprite.h"
#include <tracy/TracyC.h>
JSValue js_getpropertyuint32(JSContext *js, JSValue v, unsigned int i)
{
JSValue ret = JS_GetPropertyUint32(js,v,i);
JS_FreeValue(js,ret);
return ret;
}
JSValue js_getpropertystr(JSContext *js, JSValue v, const char *str)
{
JSValue ret = JS_GetPropertyStr(js, v, str);
JS_FreeValue(js,ret);
return ret;
}
JSValue number2js(JSContext *js, double g) { return JS_NewFloat64(js,g); }
double js2number(JSContext *js, JSValue v) {
@@ -68,10 +80,10 @@ typedef struct lrtb lrtb;
lrtb js2lrtb(JSContext *js, JSValue v)
{
lrtb ret = {0};
ret.l = js2number(js,JS_GetPropertyStr(js,v,"l"));
ret.b = js2number(js,JS_GetPropertyStr(js,v,"b"));
ret.t = js2number(js,JS_GetPropertyStr(js,v,"t"));
ret.r = js2number(js,JS_GetPropertyStr(js,v,"r"));
ret.l = js2number(js,js_getpropertystr(js,v,"l"));
ret.b = js2number(js,js_getpropertystr(js,v,"b"));
ret.t = js2number(js,js_getpropertystr(js,v,"t"));
ret.r = js2number(js,js_getpropertystr(js,v,"r"));
return ret;
}
@@ -208,7 +220,7 @@ QJSCLASS(skin)
static inline HMM_Mat4 js2transform_mat(JSContext *js, JSValue v)
{
transform *T = js2transform(js, v);
transform *P = js2transform(js, JS_GetPropertyStr(js,v, "parent"));
transform *P = js2transform(js, js_getpropertystr(js,v, "parent"));
if (P) {
HMM_Mat4 pm = transform2mat(P);
HMM_Mat4 tm = transform2mat(T);
@@ -222,7 +234,7 @@ static inline HMM_Mat4 js2transform_mat(JSContext *js, JSValue v)
int js_arrlen(JSContext *js,JSValue v) {
if (JS_IsUndefined(v)) return 0;
int len;
JS_ToInt32(js, &len, JS_GetPropertyStr(js, v, "length"));
JS_ToInt32(js, &len, js_getpropertystr(js, v, "length"));
return len;
}
@@ -245,7 +257,7 @@ double js2angle(JSContext *js,JSValue v) {
struct rgba js2color(JSContext *js,JSValue v) {
JSValue c[4];
for (int i = 0; i < 4; i++)
c[i] = JS_GetPropertyUint32(js, v,i);
c[i] = js_getpropertyuint32(js, v,i);
float a = JS_IsUndefined(c[3]) ? 1.0 : js2number(js,c[3]);
struct rgba color = {
.r = js2number(js,c[0])*RGBA_MAX,
@@ -254,9 +266,6 @@ struct rgba js2color(JSContext *js,JSValue v) {
.a = a*RGBA_MAX,
};
for (int i = 0; i < 4; i++)
JS_FreeValue(js,c[i]);
return color;
}
@@ -273,17 +282,17 @@ JSValue color2js(JSContext *js,struct rgba color)
HMM_Vec2 js2vec2(JSContext *js,JSValue v)
{
HMM_Vec2 v2;
v2.X = js2number(js,JS_GetPropertyUint32(js, v,0));
v2.Y = js2number(js,JS_GetPropertyUint32(js, v,1));
v2.X = js2number(js,js_getpropertyuint32(js, v,0));
v2.Y = js2number(js,js_getpropertyuint32(js, v,1));
return v2;
}
HMM_Vec3 js2vec3(JSContext *js,JSValue v)
{
HMM_Vec3 v3;
v3.x = js2number(js,JS_GetPropertyUint32(js, v,0));
v3.y = js2number(js,JS_GetPropertyUint32(js, v,1));
v3.z = js2number(js,JS_GetPropertyUint32(js, v,2));
v3.x = js2number(js,js_getpropertyuint32(js, v,0));
v3.y = js2number(js,js_getpropertyuint32(js, v,1));
v3.z = js2number(js,js_getpropertyuint32(js, v,2));
return v3;
}
@@ -325,7 +334,7 @@ HMM_Vec4 js2vec4(JSContext *js, JSValue v)
{
HMM_Vec4 v4;
for (int i = 0; i < 4; i++)
v4.e[i] = js2number(js,JS_GetPropertyUint32(js, v,i));
v4.e[i] = js2number(js,js_getpropertyuint32(js, v,i));
return v4;
}
@@ -340,7 +349,7 @@ double arr_vec_length(JSContext *js,JSValue v)
double sum = 0;
for (int i = 0; i < len; i++)
sum += pow(js2number(js,JS_GetPropertyUint32(js, v, i)), 2);
sum += pow(js2number(js,js_getpropertyuint32(js, v, i)), 2);
return sqrt(sum);
}
@@ -364,7 +373,7 @@ HMM_Vec2 *js2cpvec2arr(JSContext *js,JSValue v) {
arrsetlen(arr,n);
for (int i = 0; i < n; i++)
arr[i] = js2vec2(js,JS_GetPropertyUint32(js, v, i));
arr[i] = js2vec2(js,js_getpropertyuint32(js, v, i));
return arr;
}
@@ -375,7 +384,7 @@ HMM_Vec3 *js2cpvec3arr(JSContext *js,JSValue v)
int n = js_arrlen(js,v);
arrsetlen(arr,n);
for (int i = 0; i < n; i++)
arr[i] = js2vec3(js,JS_GetPropertyUint32(js, v,i));
arr[i] = js2vec3(js,js_getpropertyuint32(js, v,i));
return arr;
}
@@ -406,9 +415,9 @@ int js_print_exception(JSContext *js, JSValue v)
JSValue ex = JS_GetException(js);
const char *name = JS_ToCString(js, JS_GetPropertyStr(js,ex, "name"));
const char *msg = JS_ToCString(js, JS_GetPropertyStr(js,ex, "message"));
const char *stack = JS_ToCString(js, JS_GetPropertyStr(js,ex, "stack"));
const char *name = JS_ToCString(js, js_getpropertystr(js,ex, "name"));
const char *msg = JS_ToCString(js, js_getpropertystr(js,ex, "message"));
const char *stack = JS_ToCString(js, js_getpropertystr(js,ex, "stack"));
int line = 0;
printf("%s :: %s\n%s", name, msg, stack);
// mYughLog(LOG_SCRIPT, LOG_ERROR, line, "script", "%s :: %s\n%s", name, msg, stack);
@@ -425,12 +434,12 @@ int js_print_exception(JSContext *js, JSValue v)
struct rect js2rect(JSContext *js,JSValue v) {
struct rect rect;
rect.x = js2number(js,JS_GetPropertyStr(js,v, "x"));
rect.y = js2number(js,JS_GetPropertyStr(js,v, "y"));
rect.w = js2number(js,JS_GetPropertyStr(js,v, "width"));
rect.h = js2number(js,JS_GetPropertyStr(js,v, "height"));
float anchor_x = js2number(js,JS_GetPropertyStr(js,v, "anchor_x"));
float anchor_y = js2number(js,JS_GetPropertyStr(js,v, "anchor_y"));
rect.x = js2number(js,js_getpropertystr(js,v, "x"));
rect.y = js2number(js,js_getpropertystr(js,v, "y"));
rect.w = js2number(js,js_getpropertystr(js,v, "width"));
rect.h = js2number(js,js_getpropertystr(js,v, "height"));
float anchor_x = js2number(js,js_getpropertystr(js,v, "anchor_x"));
float anchor_y = js2number(js,js_getpropertystr(js,v, "anchor_y"));
rect.y -= anchor_y*rect.h;
rect.x -= anchor_x*rect.w;
@@ -550,26 +559,26 @@ static const JSCFunctionListEntry js_warp_damp_funcs [] = {
sg_bindings js2bind(JSContext *js,JSValue v)
{
sg_bindings bind = {0};
JSValue attrib = JS_GetPropertyStr(js,v, "attrib");
JSValue attrib = js_getpropertystr(js,v, "attrib");
for (int i = 0; i < js_arrlen(js,attrib); i++)
bind.vertex_buffers[i] = *js2sg_buffer(js, JS_GetPropertyUint32(js, attrib,i));
bind.vertex_buffers[i] = *js2sg_buffer(js, js_getpropertyuint32(js, attrib,i));
JSValue index = JS_GetPropertyStr(js,v, "index");
JSValue index = js_getpropertystr(js,v, "index");
if (!JS_IsUndefined(index))
bind.index_buffer = *js2sg_buffer(js, index);
JSValue imgs = JS_GetPropertyStr(js,v, "images");
JSValue samplers = JS_GetPropertyStr(js,v, "samplers");
JSValue imgs = js_getpropertystr(js,v, "images");
JSValue samplers = js_getpropertystr(js,v, "samplers");
for (int i = 0; i < js_arrlen(js,imgs); i++) {
bind.fs.images[i] = js2texture(js, JS_GetPropertyUint32(js, imgs, i))->id;
int use_std = JS_ToBool(js,JS_GetPropertyUint32(js, samplers, i));
bind.fs.images[i] = js2texture(js, js_getpropertyuint32(js, imgs, i))->id;
int use_std = JS_ToBool(js,js_getpropertyuint32(js, samplers, i));
bind.fs.samplers[i] = use_std ? std_sampler : tex_sampler;
}
JSValue ssbo = JS_GetPropertyStr(js,v, "ssbo");
JSValue ssbo = js_getpropertystr(js,v, "ssbo");
for (int i = 0; i < js_arrlen(js,ssbo); i++)
bind.vs.storage_buffers[i] = *js2sg_buffer(js, JS_GetPropertyUint32(js, ssbo,i));
bind.vs.storage_buffers[i] = *js2sg_buffer(js, js_getpropertyuint32(js, ssbo,i));
return bind;
}
@@ -612,7 +621,9 @@ JSC_CCALL(render_viewport,
JSC_CCALL(render_commit,
sg_commit();
TracyCFrameMark
#ifdef TRACY_ENABLE
render_dump_trace();
#endif
)
JSC_CCALL(render_end_pass, sg_end_pass())
@@ -625,7 +636,7 @@ HMM_Mat4 transform2view(transform *t)
}
JSC_CCALL(render_camera_screen2world,
HMM_Mat4 view = transform2view(js2transform(js, JS_GetPropertyStr(js,argv[0], "transform")));
HMM_Mat4 view = transform2view(js2transform(js, js_getpropertystr(js,argv[0], "transform")));
view = HMM_InvGeneralM4(view);
HMM_Vec4 p = js2vec4(js, argv[1]);
return vec42js(js, HMM_MulM4V4(view, p));
@@ -663,15 +674,15 @@ JSC_CCALL(render_set_view,
sg_shader_uniform_block_desc js2uniform_block(JSContext *js,JSValue v)
{
sg_shader_uniform_block_desc desc = {0};
int slot = js2number(js,JS_GetPropertyStr(js,v, "slot"));
desc.size = js2number(js,JS_GetPropertyStr(js,v, "size"));
int slot = js2number(js,js_getpropertystr(js,v, "slot"));
desc.size = js2number(js,js_getpropertystr(js,v, "size"));
desc.layout = SG_UNIFORMLAYOUT_STD140;
JSValue uniforms = JS_GetPropertyStr(js,v, "uniforms");
JSValue uniforms = js_getpropertystr(js,v, "uniforms");
for (int j = 0; j < js_arrlen(js,uniforms); j++) {
JSValue uniform = JS_GetPropertyUint32(js, uniforms, j);
desc.uniforms[j].name = js2strdup(js, JS_GetPropertyStr(js,v, "struct_name"));
desc.uniforms[j].array_count = js2number(js,JS_GetPropertyStr(js,uniform, "array_count"));
JSValue uniform = js_getpropertyuint32(js, uniforms, j);
desc.uniforms[j].name = js2strdup(js, js_getpropertystr(js,v, "struct_name"));
desc.uniforms[j].array_count = js2number(js,js_getpropertystr(js,uniform, "array_count"));
desc.uniforms[j].type = SG_UNIFORMTYPE_FLOAT4;
}
@@ -682,63 +693,63 @@ sg_shader js2shader(JSContext *js,JSValue v)
{
sg_shader_desc desc = {0};
JSValue prog = v;
desc.label = js2strdup(js, JS_GetPropertyStr(js,v, "name"));
JSValue vs = JS_GetPropertyStr(js,prog, "vs");
JSValue fs = JS_GetPropertyStr(js,prog, "fs");
const char *vsf = JS_ToCString(js, JS_GetPropertyStr(js,vs, "code"));
const char *fsf = JS_ToCString(js, JS_GetPropertyStr(js,fs, "code"));
desc.label = js2strdup(js, js_getpropertystr(js,v, "name"));
JSValue vs = js_getpropertystr(js,prog, "vs");
JSValue fs = js_getpropertystr(js,prog, "fs");
const char *vsf = JS_ToCString(js, js_getpropertystr(js,vs, "code"));
const char *fsf = JS_ToCString(js, js_getpropertystr(js,fs, "code"));
desc.vs.source = vsf;
desc.fs.source = fsf;
const char *vsmain = JS_ToCString(js, JS_GetPropertyStr(js,vs, "entry_point"));
const char *fsmain = JS_ToCString(js, JS_GetPropertyStr(js,fs, "entry_point"));
const char *vsmain = JS_ToCString(js, js_getpropertystr(js,vs, "entry_point"));
const char *fsmain = JS_ToCString(js, js_getpropertystr(js,fs, "entry_point"));
desc.vs.entry = vsmain;
desc.fs.entry = fsmain;
desc.vs.d3d11_target = "vs_4_0";
desc.fs.d3d11_target = "ps_4_0";
JSValue attrs = JS_GetPropertyStr(js,vs, "inputs");
JSValue attrs = js_getpropertystr(js,vs, "inputs");
int atin = js_arrlen(js,attrs);
for (int i = 0; i < atin; i++) {
JSValue u = JS_GetPropertyUint32(js, attrs, i);
int slot = js2number(js,JS_GetPropertyStr(js,u, "slot"));
desc.attrs[slot].sem_name = js2strdup(js,JS_GetPropertyStr(js,u,"sem_name"));
desc.attrs[slot].sem_index = js2number(js,JS_GetPropertyStr(js,u, "sem_index"));
JSValue u = js_getpropertyuint32(js, attrs, i);
int slot = js2number(js,js_getpropertystr(js,u, "slot"));
desc.attrs[slot].sem_name = js2strdup(js,js_getpropertystr(js,u,"sem_name"));
desc.attrs[slot].sem_index = js2number(js,js_getpropertystr(js,u, "sem_index"));
}
JSValue vsu = JS_GetPropertyStr(js,vs, "uniform_blocks");
JSValue vsu = js_getpropertystr(js,vs, "uniform_blocks");
for (int i = 0; i < js_arrlen(js,vsu); i++)
desc.vs.uniform_blocks[i] = js2uniform_block(js,JS_GetPropertyUint32(js, vsu, i));
desc.vs.uniform_blocks[i] = js2uniform_block(js,js_getpropertyuint32(js, vsu, i));
JSValue fsu = JS_GetPropertyStr(js,fs, "uniform_blocks");
JSValue fsu = js_getpropertystr(js,fs, "uniform_blocks");
for (int i = 0; i < js_arrlen(js,fsu); i++)
desc.fs.uniform_blocks[i] = js2uniform_block(js,JS_GetPropertyUint32(js, fsu, i));
desc.fs.uniform_blocks[i] = js2uniform_block(js,js_getpropertyuint32(js, fsu, i));
JSValue imgs = JS_GetPropertyStr(js,fs, "images");
JSValue imgs = js_getpropertystr(js,fs, "images");
for (int i = 0; i < js_arrlen(js,imgs); i++) {
JSValue u = JS_GetPropertyUint32(js, imgs, i);
int slot = js2number(js,JS_GetPropertyStr(js,u, "slot"));
JSValue u = js_getpropertyuint32(js, imgs, i);
int slot = js2number(js,js_getpropertystr(js,u, "slot"));
desc.fs.images[i].used = true;
desc.fs.images[i].multisampled = JS_ToBool(js,JS_GetPropertyStr(js,u, "multisampled"));
desc.fs.images[i].multisampled = JS_ToBool(js,js_getpropertystr(js,u, "multisampled"));
desc.fs.images[i].image_type = SG_IMAGETYPE_2D;
desc.fs.images[i].sample_type = SG_IMAGESAMPLETYPE_FLOAT;
}
JSValue samps = JS_GetPropertyStr(js,fs, "samplers");
JSValue samps = js_getpropertystr(js,fs, "samplers");
for (int i = 0; i < js_arrlen(js,samps); i++) {
JSValue sampler = JS_GetPropertyUint32(js, samps, i);
JSValue sampler = js_getpropertyuint32(js, samps, i);
desc.fs.samplers[0].used = true;
desc.fs.samplers[0].sampler_type = SG_SAMPLERTYPE_FILTERING;
}
JSValue pairs = JS_GetPropertyStr(js,fs, "image_sampler_pairs");
JSValue pairs = js_getpropertystr(js,fs, "image_sampler_pairs");
for (int i = 0; i < js_arrlen(js,pairs); i++) {
JSValue pair = JS_GetPropertyUint32(js, pairs, i);
JSValue pair = js_getpropertyuint32(js, pairs, i);
desc.fs.image_sampler_pairs[0].used = true;
desc.fs.image_sampler_pairs[0].image_slot = js2number(js,JS_GetPropertyStr(js,pair, "slot"));
desc.fs.image_sampler_pairs[0].image_slot = js2number(js,js_getpropertystr(js,pair, "slot"));
desc.fs.image_sampler_pairs[0].sampler_slot = 0;
desc.fs.image_sampler_pairs[0].glsl_name = js2strdup(js,JS_GetPropertyStr(js,pair, "name"));
desc.fs.image_sampler_pairs[0].glsl_name = js2strdup(js,js_getpropertystr(js,pair, "name"));
}
JSValue ssbos = JS_GetPropertyStr(js,vs, "storage_buffers");
JSValue ssbos = js_getpropertystr(js,vs, "storage_buffers");
for (int i = 0; i < js_arrlen(js,ssbos); i++) {
desc.vs.storage_buffers[i].used = true;
desc.vs.storage_buffers[i].readonly = true;
@@ -784,11 +795,11 @@ static int mat2type(int mat)
sg_vertex_layout_state js2vertex_layout(JSContext *js,JSValue v)
{
sg_vertex_layout_state st = {0};
JSValue inputs = JS_GetPropertyStr(js,JS_GetPropertyStr(js,v, "vs"), "inputs");
JSValue inputs = js_getpropertystr(js,js_getpropertystr(js,v, "vs"), "inputs");
for (int i = 0; i < js_arrlen(js,inputs); i++) {
JSValue attr = JS_GetPropertyUint32(js, inputs, i);
int slot = js2number(js,JS_GetPropertyStr(js,attr, "slot"));
int mat = js2number(js,JS_GetPropertyStr(js,attr, "mat"));
JSValue attr = js_getpropertyuint32(js, inputs, i);
int slot = js2number(js,js_getpropertystr(js,attr, "slot"));
int mat = js2number(js,js_getpropertystr(js,attr, "mat"));
st.attrs[slot].format = mat2type(mat);
st.attrs[slot].buffer_index = slot;
}
@@ -799,41 +810,41 @@ sg_vertex_layout_state js2vertex_layout(JSContext *js,JSValue v)
sg_depth_state js2depth(JSContext *js,JSValue v)
{
sg_depth_state depth = {0};
depth.compare = js2number(js,JS_GetPropertyStr(js,v, "compare"));
depth.write_enabled = JS_ToBool(js,JS_GetPropertyStr(js,v, "write"));
depth.bias = js2number(js,JS_GetPropertyStr(js,v, "bias"));
depth.bias_slope_scale = js2number(js,JS_GetPropertyStr(js,v, "bias_slope_scale"));
depth.bias_clamp = js2number(js,JS_GetPropertyStr(js,v, "bias_clamp"));
depth.compare = js2number(js,js_getpropertystr(js,v, "compare"));
depth.write_enabled = JS_ToBool(js,js_getpropertystr(js,v, "write"));
depth.bias = js2number(js,js_getpropertystr(js,v, "bias"));
depth.bias_slope_scale = js2number(js,js_getpropertystr(js,v, "bias_slope_scale"));
depth.bias_clamp = js2number(js,js_getpropertystr(js,v, "bias_clamp"));
return depth;
}
sg_stencil_face_state js2face_state(JSContext *js,JSValue v)
{
sg_stencil_face_state face = {0};
face.compare = js2number(js,JS_GetPropertyStr(js,v, "compare"));
face.fail_op = js2number(js,JS_GetPropertyStr(js,v, "fail"));
face.depth_fail_op = js2number(js,JS_GetPropertyStr(js,v, "depth_fail"));
face.pass_op = js2number(js,JS_GetPropertyStr(js,v, "pass_op"));
face.compare = js2number(js,js_getpropertystr(js,v, "compare"));
face.fail_op = js2number(js,js_getpropertystr(js,v, "fail"));
face.depth_fail_op = js2number(js,js_getpropertystr(js,v, "depth_fail"));
face.pass_op = js2number(js,js_getpropertystr(js,v, "pass_op"));
return face;
}
sg_stencil_state js2stencil(JSContext *js,JSValue v)
{
sg_stencil_state stencil = {0};
stencil.enabled = JS_ToBool(js,JS_GetPropertyStr(js,v, "enabled"));
stencil.read_mask = JS_ToBool(js,JS_GetPropertyStr(js,v, "read")) ? 0xFF : 0x00;
stencil.write_mask = JS_ToBool(js,JS_GetPropertyStr(js,v, "write")) ? 0xFF : 0x00;
stencil.front = js2face_state(js,JS_GetPropertyStr(js,v, "front"));
stencil.back = js2face_state(js,JS_GetPropertyStr(js,v, "back"));
stencil.ref = js2number(js,JS_GetPropertyStr(js,v, "ref"));
stencil.enabled = JS_ToBool(js,js_getpropertystr(js,v, "enabled"));
stencil.read_mask = JS_ToBool(js,js_getpropertystr(js,v, "read")) ? 0xFF : 0x00;
stencil.write_mask = JS_ToBool(js,js_getpropertystr(js,v, "write")) ? 0xFF : 0x00;
stencil.front = js2face_state(js,js_getpropertystr(js,v, "front"));
stencil.back = js2face_state(js,js_getpropertystr(js,v, "back"));
stencil.ref = js2number(js,js_getpropertystr(js,v, "ref"));
return stencil;
}
#define GETNUMVALUE(STRUCT, NAME) STRUCT.NAME = js2number(js,JS_GetPropertyStr(js,v, #NAME));
#define GETNUMVALUE(STRUCT, NAME) STRUCT.NAME = js2number(js,js_getpropertystr(js,v, #NAME));
sg_blend_state js2blend(JSContext *js,JSValue v)
{
sg_blend_state blend = {0};
blend.enabled = JS_ToBool(js,JS_GetPropertyStr(js,v, "enabled"));
blend.enabled = JS_ToBool(js,js_getpropertystr(js,v, "enabled"));
GETNUMVALUE(blend, src_factor_rgb);
GETNUMVALUE(blend, dst_factor_rgb);
GETNUMVALUE(blend, op_rgb);
@@ -847,19 +858,19 @@ JSC_CCALL(render_make_pipeline,
sg_pipeline_desc p = {0};
p.shader = js2shader(js,argv[0]);
p.layout = js2vertex_layout(js,argv[0]);
p.primitive_type = js2number(js,JS_GetPropertyStr(js,argv[0], "primitive"));
if (JS_ToBool(js,JS_GetPropertyStr(js,argv[0], "indexed")))
p.primitive_type = js2number(js,js_getpropertystr(js,argv[0], "primitive"));
if (JS_ToBool(js,js_getpropertystr(js,argv[0], "indexed")))
p.index_type = SG_INDEXTYPE_UINT16;
JSValue pipe = argv[1];
p.primitive_type = js2number(js,JS_GetPropertyStr(js,pipe, "primitive"));
p.cull_mode = js2number(js,JS_GetPropertyStr(js,pipe, "cull"));
p.face_winding = js2number(js,JS_GetPropertyStr(js,pipe, "face"));
p.colors[0].blend = js2blend(js,JS_GetPropertyStr(js,pipe, "blend"));
p.colors[0].write_mask = js2number(js,JS_GetPropertyStr(js,pipe, "write_mask"));
p.alpha_to_coverage_enabled = JS_ToBool(js,JS_GetPropertyStr(js,pipe, "alpha_to_coverage"));
p.depth = js2depth(js,JS_GetPropertyStr(js,pipe, "depth"));
p.stencil = js2stencil(js,JS_GetPropertyStr(js,pipe, "stencil"));
p.primitive_type = js2number(js,js_getpropertystr(js,pipe, "primitive"));
p.cull_mode = js2number(js,js_getpropertystr(js,pipe, "cull"));
p.face_winding = js2number(js,js_getpropertystr(js,pipe, "face"));
p.colors[0].blend = js2blend(js,js_getpropertystr(js,pipe, "blend"));
p.colors[0].write_mask = js2number(js,js_getpropertystr(js,pipe, "write_mask"));
p.alpha_to_coverage_enabled = JS_ToBool(js,js_getpropertystr(js,pipe, "alpha_to_coverage"));
p.depth = js2depth(js,js_getpropertystr(js,pipe, "depth"));
p.stencil = js2stencil(js,js_getpropertystr(js,pipe, "stencil"));
sg_pipeline *g = malloc(sizeof(*g));
*g = sg_make_pipeline(&p);
@@ -888,7 +899,7 @@ JSC_CCALL(render_setuniv4,
HMM_Vec4 v = {0};
if (JS_IsArray(js, argv[2])) {
for (int i = 0; i < js_arrlen(js,argv[2]); i++)
v.e[i] = js2number(js,JS_GetPropertyUint32(js, argv[2], i));
v.e[i] = js2number(js,js_getpropertyuint32(js, argv[2], i));
} else
v.x = js2number(js,argv[2]);
@@ -918,10 +929,10 @@ JSC_CCALL(render_setunim4,
JSValue arr = argv[2];
int n = js_arrlen(js,arr);
if (n == 1)
m = transform2mat(js2transform(js,JS_GetPropertyUint32(js, arr,0)));
m = transform2mat(js2transform(js,js_getpropertyuint32(js, arr,0)));
else {
for (int i = 0; i < n; i++) {
HMM_Mat4 p = transform2mat(js2transform(js,JS_GetPropertyUint32(js, arr, i)));
HMM_Mat4 p = transform2mat(js2transform(js,js_getpropertyuint32(js, arr, i)));
m = HMM_MulM4(p,m);
}
}
@@ -961,9 +972,9 @@ JSC_CCALL(render_make_particle_ssbo,
}
for (int i = 0; i < js_arrlen(js,array); i++) {
JSValue sub = JS_GetPropertyUint32(js, array,i);
ms[i].model = transform2mat(js2transform(js,JS_GetPropertyStr(js,sub, "transform")));
ms[i].color = js2vec4(js,JS_GetPropertyStr(js,sub,"color"));
JSValue sub = js_getpropertyuint32(js, array,i);
ms[i].model = transform2mat(js2transform(js,js_getpropertystr(js,sub, "transform")));
ms[i].color = js2vec4(js,js_getpropertystr(js,sub,"color"));
}
int offset = sg_append_buffer(*b, (&(sg_range){
@@ -1000,14 +1011,14 @@ JSC_CCALL(render_make_sprite_ssbo,
}
for (int i = 0; i < js_arrlen(js,array); i++) {
JSValue sub = JS_GetPropertyUint32(js, array,i);
JSValue sub = js_getpropertyuint32(js, array,i);
// transform *tr = js2transform(js,JS_GetPropertyStr(js,sub, "transform"));
JSValue image = JS_GetPropertyStr(js,sub, "image");
// transform *tr = js2transform(js,js_getpropertystr(js,sub, "transform"));
JSValue image = js_getpropertystr(js,sub, "image");
ms[i].model = js2transform_mat(js, JS_GetPropertyStr(js,sub,"transform"));// transform2mat(tr);
ms[i].rect = js2rect(js,JS_GetPropertyStr(js,image,"rect"));
ms[i].shade = js2vec4(js,JS_GetPropertyStr(js,sub,"shade"));
ms[i].model = js2transform_mat(js, js_getpropertystr(js,sub,"transform"));// transform2mat(tr);
ms[i].rect = js2rect(js,js_getpropertystr(js,image,"rect"));
ms[i].shade = js2vec4(js,js_getpropertystr(js,sub,"shade"));
}
int offset = sg_append_buffer(*b, (&(sg_range){
@@ -1037,7 +1048,7 @@ JSC_CCALL(render_make_t_ssbo,
}
for (int i = 0; i < js_arrlen(js,array); i++)
ms[i] = transform2mat(js2transform(js,JS_GetPropertyUint32(js, array, i)));
ms[i] = transform2mat(js2transform(js,js_getpropertyuint32(js, array, i)));
sg_append_buffer(*b, (&(sg_range){
.ptr = ms,
@@ -1246,7 +1257,7 @@ JSC_CCALL(vector_norm,
JSValue newarr = JS_NewArray(js);
for (int i = 0; i < len; i++)
JS_SetPropertyUint32(js, newarr, i, number2js(js,js2number(js,JS_GetPropertyUint32(js, argv[0],i))/length));
JS_SetPropertyUint32(js, newarr, i, number2js(js,js2number(js,js_getpropertyuint32(js, argv[0],i))/length));
return newarr;
)
@@ -1293,7 +1304,7 @@ JSC_CCALL(vector_clamp,
)
JSC_SSCALL(vector_trimchr,
int len = js2number(js,JS_GetPropertyStr(js,argv[0], "length"));
int len = js2number(js,js_getpropertystr(js,argv[0], "length"));
const char *start = str;
while (*start == *str2)
@@ -1349,7 +1360,7 @@ JSC_CCALL(vector_mean,
double len = js_arrlen(js,argv[0]);
double sum;
for (int i = 0; i < len; i++)
sum += js2number(js,JS_GetPropertyUint32(js, argv[0], i));
sum += js2number(js,js_getpropertyuint32(js, argv[0], i));
return number2js(js,sum/len);
)
@@ -1358,7 +1369,7 @@ JSC_CCALL(vector_sum,
double sum = 0.0;
int len = js_arrlen(js,argv[0]);
for (int i = 0; i < len; i++)
sum += js2number(js,JS_GetPropertyUint32(js, argv[0], i));
sum += js2number(js,js_getpropertyuint32(js, argv[0], i));
return number2js(js,sum);
)
@@ -1367,14 +1378,14 @@ JSC_CCALL(vector_sigma,
int len = js_arrlen(js,argv[0]);
double sum;
for (int i = 0; i < len; i++)
sum += js2number(js,JS_GetPropertyUint32(js, argv[0], i));
sum += js2number(js,js_getpropertyuint32(js, argv[0], i));
double mean = sum/(double)len;
double sq_diff = 0;
for (int i = 0; i < len; i++) {
double x = js2number(js,JS_GetPropertyUint32(js, argv[0],i));
double x = js2number(js,js_getpropertyuint32(js, argv[0],i));
sq_diff += pow(x-mean, 2);
}
@@ -1389,7 +1400,7 @@ JSC_CCALL(vector_median,
double temp;
for (int i = 0; i < len; i++)
arr[i] = js2number(js,JS_GetPropertyUint32(js, argv[0], i));
arr[i] = js2number(js,js_getpropertyuint32(js, argv[0], i));
for (int i = 0; i < len-1; i++) {
for (int j = i+1; j < len; j++) {
@@ -1416,6 +1427,29 @@ JSC_CCALL(vector_fib,
printf("ANSWER IS %d\n", fib);
)
JSC_CCALL(vector_from_to,
HMM_Vec2 from = js2vec2(js,argv[0]);
HMM_Vec2 to = js2vec2(js,argv[1]);
float space = js2number(js,argv[2]);
float from_offset = js2number(js,argv[3]);
float to_offset = js2number(js,argv[4]);
HMM_Vec2 dir = HMM_NormV2(HMM_SubV2(to, from));
from = HMM_AddV2(from, HMM_MulV2F(dir, from_offset));
to = HMM_SubV2(to,HMM_MulV2F(dir, to_offset));
float length = HMM_DistV2(from, to);
int steps = floor(length/space);
int stepsize = length/(steps+1);
ret = JS_NewArray(js);
JS_SetPropertyUint32(js,ret,0,vec22js(js,from));
for (int i = 1; i < steps+1; i++) {
HMM_Vec2 val = HMM_AddV2(from, HMM_MulV2F(dir, i*stepsize));
JS_SetPropertyUint32(js, ret, i, vec22js(js,val));
}
JS_SetPropertyUint32(js, ret, steps+1, vec22js(js,to));
)
static const JSCFunctionListEntry js_vector_funcs[] = {
MIST_FUNC_DEF(vector, dot,2),
MIST_FUNC_DEF(vector, project,2),
@@ -1441,6 +1475,7 @@ static const JSCFunctionListEntry js_vector_funcs[] = {
MIST_FUNC_DEF(vector, median, 1),
MIST_FUNC_DEF(vector, length, 1),
MIST_FUNC_DEF(vector, fib, 1),
MIST_FUNC_DEF(vector, from_to, 5),
};
#define JS_HMM_FN(OP, HMM, SIGN) \
@@ -1450,7 +1485,7 @@ JSC_CCALL(array_##OP, \
double n = js2number(js,argv[0]); \
JSValue arr = JS_NewArray(js); \
for (int i = 0; i < len; i++) \
JS_SetPropertyUint32(js, arr, i, number2js(js,js2number(js,JS_GetPropertyUint32(js, self,i)) SIGN n)); \
JS_SetPropertyUint32(js, arr, i, number2js(js,js2number(js,js_getpropertyuint32(js, self,i)) SIGN n)); \
return arr; \
} \
switch(len) { \
@@ -1464,8 +1499,8 @@ JSC_CCALL(array_##OP, \
\
JSValue arr = JS_NewArray(js); \
for (int i = 0; i < len; i++) { \
double a = js2number(js,JS_GetPropertyUint32(js, self,i)); \
double b = js2number(js,JS_GetPropertyUint32(js, argv[0],i)); \
double a = js2number(js,js_getpropertyuint32(js, self,i)); \
double b = js2number(js,js_getpropertyuint32(js, argv[0],i)); \
JS_SetPropertyUint32(js, arr, i, number2js(js,a SIGN b)); \
} \
return arr; \
@@ -1482,8 +1517,8 @@ JSC_CCALL(array_lerp,
JSValue arr = JS_NewArray(js);
for (int i = 0; i < len; i++) {
double from = js2number(js,JS_GetPropertyUint32(js, self, i));
double to = js2number(js,JS_GetPropertyUint32(js, argv[0], i));
double from = js2number(js,js_getpropertyuint32(js, self, i));
double to = js2number(js,js_getpropertyuint32(js, argv[0], i));
JS_SetPropertyUint32(js, arr, i, number2js(js,(to - from) * t + from));
}
return arr;
@@ -1541,25 +1576,25 @@ JSC_CCALL(game_engine_start,
start_desc.frame_userdata_cb = c_frame;
start_desc.event_userdata_cb = c_event;
start_desc.cleanup_userdata_cb = c_cleanup;
js_sg_init = JS_DupValue(js, JS_GetPropertyStr(js,argv[0], "init"));
js_sg_frame = JS_DupValue(js, JS_GetPropertyStr(js,argv[0], "frame"));
js_sg_cleanup = JS_DupValue(js, JS_GetPropertyStr(js,argv[0], "cleanup"));
js_sg_event = JS_DupValue(js, JS_GetPropertyStr(js,argv[0], "event"));
start_desc.width = js2number(js, JS_GetPropertyStr(js, argv[0], "width"));
start_desc.height = js2number(js, JS_GetPropertyStr(js, argv[0], "height"));
start_desc.sample_count = js2number(js, JS_GetPropertyStr(js, argv[0], "sample_count"));
start_desc.high_dpi = js2number(js, JS_GetPropertyStr(js, argv[0], "high_dpi"));
start_desc.alpha = js2number(js, JS_GetPropertyStr(js, argv[0], "alpha"));
start_desc.fullscreen = js2number(js, JS_GetPropertyStr(js, argv[0], "fullscreen"));
start_desc.swap_interval = js2number(js, JS_GetPropertyStr(js, argv[0], "swap_interval"));
start_desc.window_title = JS_ToCString(js, JS_GetPropertyStr(js, argv[0], "title"));
start_desc.enable_clipboard = JS_ToBool(js, JS_GetPropertyStr(js, argv[0], "enable_clipboard"));
start_desc.enable_dragndrop = JS_ToBool(js, JS_GetPropertyStr(js, argv[0], "enable_dragndrop"));
start_desc.max_dropped_files = js2number(js, JS_GetPropertyStr(js, argv[0], "max_dropped_files"));
js_sg_init = JS_DupValue(js, js_getpropertystr(js,argv[0], "init"));
js_sg_frame = JS_DupValue(js, js_getpropertystr(js,argv[0], "frame"));
js_sg_cleanup = JS_DupValue(js, js_getpropertystr(js,argv[0], "cleanup"));
js_sg_event = JS_DupValue(js, js_getpropertystr(js,argv[0], "event"));
start_desc.width = js2number(js, js_getpropertystr(js, argv[0], "width"));
start_desc.height = js2number(js, js_getpropertystr(js, argv[0], "height"));
start_desc.sample_count = js2number(js, js_getpropertystr(js, argv[0], "sample_count"));
start_desc.high_dpi = js2number(js, js_getpropertystr(js, argv[0], "high_dpi"));
start_desc.alpha = js2number(js, js_getpropertystr(js, argv[0], "alpha"));
start_desc.fullscreen = js2number(js, js_getpropertystr(js, argv[0], "fullscreen"));
start_desc.swap_interval = js2number(js, js_getpropertystr(js, argv[0], "swap_interval"));
start_desc.window_title = JS_ToCString(js, js_getpropertystr(js, argv[0], "title"));
start_desc.enable_clipboard = JS_ToBool(js, js_getpropertystr(js, argv[0], "enable_clipboard"));
start_desc.enable_dragndrop = JS_ToBool(js, js_getpropertystr(js, argv[0], "enable_dragndrop"));
start_desc.max_dropped_files = js2number(js, js_getpropertystr(js, argv[0], "max_dropped_files"));
start_desc.max_dropped_file_path_length = 2048;
start_desc.max_dropped_files = 4;
start_desc.logger.func = slog_func;
texture *tex = js2texture(js, JS_GetPropertyStr(js, argv[0], "icon"));
texture *tex = js2texture(js, js_getpropertystr(js, argv[0], "icon"));
if (tex) start_desc.icon = texture2icon(tex);
sapp_run(&start_desc);
)
@@ -1663,18 +1698,6 @@ JSC_CCALL(profile_gather_stop,
JS_SetInterruptHandler(JS_GetRuntime(js),NULL,NULL);
)
JSC_CCALL(profile_trace_start,
#ifdef TRACY_MANUAL_LIFETIME
___tracy_startup_profiler();
#endif
)
JSC_CCALL(profile_trace_stop,
#ifdef TRACY_MANUAL_LIFETIME
___tracy_shutdown_profiler();
#endif
)
JSC_CCALL(profile_best_t,
char* result[50];
double seconds = stm_sec(js2number(js,argv[0]));
@@ -1690,50 +1713,32 @@ JSC_CCALL(profile_best_t,
ret = JS_NewString(js,result);
)
JSC_CCALL(profile_message,
size_t len;
const char *str = JS_ToCStringLen(js, &len, argv[0]);
TracyCMessage(str,len);
JS_FreeCString(js,str);
)
JSC_CCALL(profile_secs, return number2js(js,stm_sec(js2number(js,argv[0]))); )
JSC_SCALL(profile_plot,
TracyCPlot(str, js2number(js,argv[1]));
)
JSC_SCALL(profile_plot_config,
TracyCPlotConfig(str, js2number(js,argv[1]), JS_ToBool(js,argv[2]), JS_ToBool(js,argv[3]), js2number(js,argv[4]))
)
JSC_CCALL(profile_fiber_enter,
JSAtom atom = JS_ValueToAtom(js,argv[0]);
const char *str = JS_AtomToCString(js, atom);
TracyCFiberEnter(str);
JS_FreeAtom(js,atom);
)
JSC_CCALL(profile_fiber_leave,
JSAtom atom = JS_ValueToAtom(js,argv[0]);
const char *str = JS_AtomToCString(js, atom);
TracyCFiberLeave(str);
JS_FreeAtom(js,atom);
)
static const JSCFunctionListEntry js_profile_funcs[] = {
MIST_FUNC_DEF(profile,now,0),
MIST_FUNC_DEF(profile,best_t, 1),
MIST_FUNC_DEF(profile,gather,2),
MIST_FUNC_DEF(profile,gather_rate,1),
MIST_FUNC_DEF(profile,gather_stop,0),
MIST_FUNC_DEF(profile,trace_start,0),
MIST_FUNC_DEF(profile,secs,1),
MIST_FUNC_DEF(profile, message, 1),
MIST_FUNC_DEF(profile, plot, 2),
MIST_FUNC_DEF(profile, plot_config, 5),
MIST_FUNC_DEF(profile, fiber_enter, 1),
MIST_FUNC_DEF(profile, fiber_leave, 1),
};
JSC_CCALL(debug_stack_depth, return number2js(js,js_debugger_stack_depth(js)))
JSC_CCALL(debug_build_backtrace, return js_debugger_build_backtrace(js,NULL))
JSC_CCALL(debug_closure_vars, return js_debugger_closure_variables(js,argv[0]))
JSC_CCALL(debug_local_vars, return js_debugger_local_variables(js, js2number(js,argv[0])))
JSC_CCALL(debug_fn_info, return js_debugger_fn_info(js, argv[0]));
JSC_CCALL(debug_backtrace_fns, return js_debugger_backtrace_fns(js,NULL));
JSC_CCALL(debug_dump_obj, return js_dump_value(js, argv[0]));
static const JSCFunctionListEntry js_debug_funcs[] = {
MIST_FUNC_DEF(debug, stack_depth, 0),
MIST_FUNC_DEF(debug, build_backtrace, 0),
MIST_FUNC_DEF(debug, closure_vars, 1),
MIST_FUNC_DEF(debug, local_vars, 1),
MIST_FUNC_DEF(debug,fn_info, 1),
MIST_FUNC_DEF(debug,backtrace_fns,0),
MIST_FUNC_DEF(debug, dump_obj, 1),
};
static void list_files(const char *path, JSContext *js, JSValue v, int *n)
@@ -1801,7 +1806,7 @@ JSValue js_io_slurpwrite(JSContext *js, JSValue self, int argc, JSValue *argv)
{
const char *f = JS_ToCString(js, argv[0]);
FILE *file = fopen(f,"wb");
if (!f) return JS_ThrowReferenceError(js, "could not open file for writing at %s", f);
if (!file) return JS_ThrowReferenceError(js, "could not open file for writing at %s", f);
size_t len;
JSValue ret;
if (JS_IsString(argv[1])) {
@@ -2159,7 +2164,9 @@ JSC_CCALL(os_backend,
)
JSC_CCALL(os_exit, exit(js2number(js,argv[0]));)
JSC_CCALL(os_gc, JS_RunGC(JS_GetRuntime(js)))
JSC_CCALL(os_gc,
return JS_RunGC(JS_GetRuntime(js), js)
)
JSC_CCALL(os_mem_limit, JS_SetMemoryLimit(JS_GetRuntime(js), js2number(js,argv[0])))
JSC_CCALL(os_gc_threshold, JS_SetGCThreshold(JS_GetRuntime(js), js2number(js,argv[0])))
JSC_CCALL(os_max_stacksize, JS_SetMaxStackSize(JS_GetRuntime(js), js2number(js,argv[0])))
@@ -2176,14 +2183,6 @@ static JSValue tmp2js(JSContext *js,FILE *tmp)
return ret;
}
JSC_CCALL(os_dump_value,
FILE *tmp = tmpfile();
quickjs_set_dumpout(tmp);
JS_DumpMyValue(JS_GetRuntime(js), argv[0]);
ret = tmp2js(js,tmp);
)
JSC_CCALL(os_dump_atoms,
FILE *tmp = tmpfile();
quickjs_set_dumpout(tmp);
@@ -2507,7 +2506,7 @@ JSC_CCALL(os_make_buffer,
int type = js2number(js,argv[1]);
float *b = malloc(sizeof(float)*js_arrlen(js,argv[0]));
for (int i = 0; i < js_arrlen(js,argv[0]); i++)
b[i] = js2number(js,JS_GetPropertyUint32(js, argv[0],i));
b[i] = js2number(js,js_getpropertyuint32(js, argv[0],i));
sg_buffer *p = malloc(sizeof(sg_buffer));
@@ -2678,7 +2677,7 @@ JSC_CCALL(os_rectpack,
stbrp_rect rects[num];
for (int i = 0; i < num; i++) {
HMM_Vec2 wh = js2vec2(js,JS_GetPropertyUint32(js, argv[2], i));
HMM_Vec2 wh = js2vec2(js,js_getpropertyuint32(js, argv[2], i));
rects[i].w = wh.x;
rects[i].h = wh.y;
rects[i].id = i;
@@ -2777,7 +2776,6 @@ static const JSCFunctionListEntry js_os_funcs[] = {
MIST_FUNC_DEF(os, gc_threshold, 1),
MIST_FUNC_DEF(os, max_stacksize, 1),
MIST_FUNC_DEF(os, rt_info, 0),
MIST_FUNC_DEF(os, dump_value, 1),
MIST_FUNC_DEF(os, dump_mem, 0),
MIST_FUNC_DEF(os, dump_shapes, 0),
MIST_FUNC_DEF(os, dump_atoms,0),
@@ -2803,6 +2801,10 @@ JSValue js_soloud_use(JSContext *js);
JSValue js_chipmunk2d_use(JSContext *js);
JSValue js_dmon_use(JSContext *js);
#ifdef TRACY_ENABLE
JSValue js_tracy_use(JSContext *js);
#endif
#ifndef NEDITOR
JSValue js_imgui(JSContext *js);
#endif
@@ -2863,6 +2865,7 @@ void ffi_load(JSContext *js) {
QJSGLOBALCLASS(time);
QJSGLOBALCLASS(console);
QJSGLOBALCLASS(profile);
QJSGLOBALCLASS(debug);
QJSGLOBALCLASS(game);
QJSGLOBALCLASS(gui);
QJSGLOBALCLASS(render);
@@ -2876,8 +2879,8 @@ void ffi_load(JSContext *js) {
JS_SetPropertyStr(js, prosperon, "date", JS_NewString(js,"date"));
JS_SetPropertyStr(js, globalThis, "texture", JS_DupValue(js,texture_proto));
JSValue array_proto = JS_GetPropertyStr(js,globalThis, "Array");
array_proto = JS_GetPropertyStr(js,array_proto, "prototype");
JSValue array_proto = js_getpropertystr(js,globalThis, "Array");
array_proto = js_getpropertystr(js,array_proto, "prototype");
JS_SetPropertyFunctionList(js, array_proto, js_array_funcs, countof(js_array_funcs));
srand(stm_now());
@@ -2888,6 +2891,10 @@ void ffi_load(JSContext *js) {
JS_SetPropertyStr(js, globalThis, "chipmunk2d", js_chipmunk2d_use(js));
JS_SetPropertyStr(js, globalThis, "dmon", js_dmon_use(js));
#ifdef TRACY_ENABLE
JS_SetPropertyStr(js, globalThis, "tracy", js_tracy_use(js));
#endif
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGSEGV, signal_handler);
@@ -2898,8 +2905,6 @@ void ffi_load(JSContext *js) {
JS_SetPropertyStr(js, globalThis, "imgui", js_imgui(js));
#endif
TracyCSetThreadName("MAIN_QUICKJS")
JS_FreeValue(js,globalThis);
}

311
source/khrplatform.h Normal file
View File

@@ -0,0 +1,311 @@
#ifndef __khrplatform_h_
#define __khrplatform_h_
/*
** Copyright (c) 2008-2018 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
/* Khronos platform-specific types and definitions.
*
* The master copy of khrplatform.h is maintained in the Khronos EGL
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
* The last semantic modification to khrplatform.h was at commit ID:
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
*
* Adopters may modify this file to suit their platform. Adopters are
* encouraged to submit platform specific modifications to the Khronos
* group so that they can be included in future versions of this file.
* Please submit changes by filing pull requests or issues on
* the EGL Registry repository linked above.
*
*
* See the Implementer's Guidelines for information about where this file
* should be located on your system and for more details of its use:
* http://www.khronos.org/registry/implementers_guide.pdf
*
* This file should be included as
* #include <KHR/khrplatform.h>
* by Khronos client API header files that use its types and defines.
*
* The types in khrplatform.h should only be used to define API-specific types.
*
* Types defined in khrplatform.h:
* khronos_int8_t signed 8 bit
* khronos_uint8_t unsigned 8 bit
* khronos_int16_t signed 16 bit
* khronos_uint16_t unsigned 16 bit
* khronos_int32_t signed 32 bit
* khronos_uint32_t unsigned 32 bit
* khronos_int64_t signed 64 bit
* khronos_uint64_t unsigned 64 bit
* khronos_intptr_t signed same number of bits as a pointer
* khronos_uintptr_t unsigned same number of bits as a pointer
* khronos_ssize_t signed size
* khronos_usize_t unsigned size
* khronos_float_t signed 32 bit floating point
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
* nanoseconds
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
* khronos_boolean_enum_t enumerated boolean type. This should
* only be used as a base type when a client API's boolean type is
* an enum. Client APIs which use an integer or other type for
* booleans cannot use this as the base type for their boolean.
*
* Tokens defined in khrplatform.h:
*
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
*
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
*
* Calling convention macros defined in this file:
* KHRONOS_APICALL
* KHRONOS_APIENTRY
* KHRONOS_APIATTRIBUTES
*
* These may be used in function prototypes as:
*
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
* int arg1,
* int arg2) KHRONOS_APIATTRIBUTES;
*/
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
# define KHRONOS_STATIC 1
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APICALL
*-------------------------------------------------------------------------
* This precedes the return type of the function in the function prototype.
*/
#if defined(KHRONOS_STATIC)
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
* header compatible with static linking. */
# define KHRONOS_APICALL
#elif defined(_WIN32)
# define KHRONOS_APICALL __declspec(dllimport)
#elif defined (__SYMBIAN32__)
# define KHRONOS_APICALL IMPORT_C
#elif defined(__ANDROID__)
# define KHRONOS_APICALL __attribute__((visibility("default")))
#else
# define KHRONOS_APICALL
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APIENTRY
*-------------------------------------------------------------------------
* This follows the return type of the function and precedes the function
* name in the function prototype.
*/
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
/* Win32 but not WinCE */
# define KHRONOS_APIENTRY __stdcall
#else
# define KHRONOS_APIENTRY
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APIATTRIBUTES
*-------------------------------------------------------------------------
* This follows the closing parenthesis of the function prototype arguments.
*/
#if defined (__ARMCC_2__)
#define KHRONOS_APIATTRIBUTES __softfp
#else
#define KHRONOS_APIATTRIBUTES
#endif
/*-------------------------------------------------------------------------
* basic type definitions
*-----------------------------------------------------------------------*/
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
/*
* Using <stdint.h>
*/
#include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
/*
* To support platform where unsigned long cannot be used interchangeably with
* inptr_t (e.g. CHERI-extended ISAs), we can use the stdint.h intptr_t.
* Ideally, we could just use (u)intptr_t everywhere, but this could result in
* ABI breakage if khronos_uintptr_t is changed from unsigned long to
* unsigned long long or similar (this results in different C++ name mangling).
* To avoid changes for existing platforms, we restrict usage of intptr_t to
* platforms where the size of a pointer is larger than the size of long.
*/
#if defined(__SIZEOF_LONG__) && defined(__SIZEOF_POINTER__)
#if __SIZEOF_POINTER__ > __SIZEOF_LONG__
#define KHRONOS_USE_INTPTR_T
#endif
#endif
#elif defined(__VMS ) || defined(__sgi)
/*
* Using <inttypes.h>
*/
#include <inttypes.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
/*
* Win32
*/
typedef __int32 khronos_int32_t;
typedef unsigned __int32 khronos_uint32_t;
typedef __int64 khronos_int64_t;
typedef unsigned __int64 khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif defined(__sun__) || defined(__digital__)
/*
* Sun or Digital
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
#if defined(__arch64__) || defined(_LP64)
typedef long int khronos_int64_t;
typedef unsigned long int khronos_uint64_t;
#else
typedef long long int khronos_int64_t;
typedef unsigned long long int khronos_uint64_t;
#endif /* __arch64__ */
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif 0
/*
* Hypothetical platform with no float or int64 support
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
#define KHRONOS_SUPPORT_INT64 0
#define KHRONOS_SUPPORT_FLOAT 0
#else
/*
* Generic fallback
*/
#include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#endif
/*
* Types that are (so far) the same on all platforms
*/
typedef signed char khronos_int8_t;
typedef unsigned char khronos_uint8_t;
typedef signed short int khronos_int16_t;
typedef unsigned short int khronos_uint16_t;
/*
* Types that differ between LLP64 and LP64 architectures - in LLP64,
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
* to be the only LLP64 architecture in current use.
*/
#ifdef KHRONOS_USE_INTPTR_T
typedef intptr_t khronos_intptr_t;
typedef uintptr_t khronos_uintptr_t;
#elif defined(_WIN64)
typedef signed long long int khronos_intptr_t;
typedef unsigned long long int khronos_uintptr_t;
#else
typedef signed long int khronos_intptr_t;
typedef unsigned long int khronos_uintptr_t;
#endif
#if defined(_WIN64)
typedef signed long long int khronos_ssize_t;
typedef unsigned long long int khronos_usize_t;
#else
typedef signed long int khronos_ssize_t;
typedef unsigned long int khronos_usize_t;
#endif
#if KHRONOS_SUPPORT_FLOAT
/*
* Float type
*/
typedef float khronos_float_t;
#endif
#if KHRONOS_SUPPORT_INT64
/* Time types
*
* These types can be used to represent a time interval in nanoseconds or
* an absolute Unadjusted System Time. Unadjusted System Time is the number
* of nanoseconds since some arbitrary system event (e.g. since the last
* time the system booted). The Unadjusted System Time is an unsigned
* 64 bit value that wraps back to 0 every 584 years. Time intervals
* may be either signed or unsigned.
*/
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
typedef khronos_int64_t khronos_stime_nanoseconds_t;
#endif
/*
* Dummy value used to pad enum types to 32 bits.
*/
#ifndef KHRONOS_MAX_ENUM
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
#endif
/*
* Enumerated boolean type
*
* Values other than zero should be considered to be true. Therefore
* comparisons should not be made against KHRONOS_TRUE.
*/
typedef enum {
KHRONOS_FALSE = 0,
KHRONOS_TRUE = 1,
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
} khronos_boolean_enum_t;
#endif /* __khrplatform_h_ */

285
source/qjs_tracy.c Normal file
View File

@@ -0,0 +1,285 @@
#include <quickjs.h>
#include "glad.h"
#include <tracy/TracyC.h>
#include <string.h>
#include <stdlib.h>
static JSValue js_tracy_fiber_enter(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected) {
JS_Call(js,argv[0], JS_UNDEFINED,0,NULL);
return JS_UNDEFINED;
}
#endif
JSValue jsname = JS_GetPropertyStr(js, argv[0], "name");
const char *fnname = JS_ToCString(js,jsname);
TracyCFiberEnter(fnname);
JS_Call(js, argv[0], JS_UNDEFINED, 0, NULL);
TracyCFiberLeave(fnname);
JS_FreeCString(js, fnname);
JS_FreeValue(js,jsname);
}
static JSValue js_tracy_fiber_leave(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected)
return JS_UNDEFINED;
#endif
JSAtom atom = JS_ValueToAtom(js,argv[0]);
const char *str = JS_AtomToCString(js, atom);
// printf("EXIT STR AT %p\n", str);
TracyCFiberLeave(str);
JS_FreeAtom(js,atom);
}
static JSValue js_tracy_plot(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected)
return JS_UNDEFINED;
#endif
const char *str = JS_ToCString(js, argv[0]);
double n;
JS_ToFloat64(js, &n, argv[1]);
TracyCPlot(str, n);
JS_FreeCString(js,str);
}
static JSValue js_tracy_plot_config(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected)
return JS_UNDEFINED;
#endif
// TracyCPlotConfig(str, js2number(js,argv[1]), JS_ToBool(js,argv[2]), JS_ToBool(js,argv[3]), js2number(js,argv[4]))
return JS_UNDEFINED;
}
static JSValue js_tracy_frame_mark(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected)
return JS_UNDEFINED;
#endif
TracyCFrameMark
}
static JSValue js_tracy_message(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected)
return JS_UNDEFINED;
#endif
size_t len;
const char *str = JS_ToCStringLen(js, &len, argv[0]);
TracyCMessage(str,len);
JS_FreeCString(js,str);
}
static JSValue js_tracy_thread_name(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected)
return JS_UNDEFINED;
#endif
const char *str = JS_ToCString(js, argv[0]);
TracyCSetThreadName(str);
JS_FreeCString(js,str);
}
static JSValue js_tracy_zone_begin(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected) {
JS_Call(js,argv[0], JS_UNDEFINED,0,NULL);
return JS_UNDEFINED;
}
#endif
#ifndef DEEP_TRACE
const char *fn_src = JS_AtomToCString(js, js_fn_filename(js, argv[0]));
const char *js_fn_name = get_func_name(js, argv[0]);
const char *fn_name;
if (!js_fn_name || js_fn_name[0] == 0)
fn_name = "<anonymous>";
else
fn_name = js_fn_name;
TracyCZoneCtx TCTX = ___tracy_emit_zone_begin_alloc(___tracy_alloc_srcloc(js_fn_linenum(js,argv[0]), fn_src, strlen(fn_src), fn_name, strlen(fn_name), (int)fn_src),1);
JS_FreeCString(js,js_fn_name);
JS_Call(js, argv[0], JS_UNDEFINED, 0, NULL);
TracyCZoneEnd(TCTX);
#endif
}
GLuint *ids;
static GLsizei query_count = 64*1024;
static int qhead = 0;
static int qtail = 0;
static JSValue js_tracy_gpu_zone_begin(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected) {
JS_Call(js,argv[0], JS_UNDEFINED, 0, NULL);
return JS_UNDEFINED;
}
#endif
// glQueryCounter
glQueryCounter(ids[qhead], GL_TIMESTAMP);
struct ___tracy_gpu_zone_begin_data data;
const char *fn_src = JS_AtomToCString(js, js_fn_filename(js, argv[0]));
const char *js_fn_name = get_func_name(js, argv[0]);
const char *fn_name;
if (!js_fn_name || js_fn_name[0] == 0)
fn_name = "<anonymous>";
else
fn_name = js_fn_name;
data.srcloc = ___tracy_alloc_srcloc(js_fn_linenum(js,argv[0]), fn_src, strlen(fn_src), fn_name, strlen(fn_name), (int)fn_src);
data.queryId = ids[qhead];
data.context = 0;
___tracy_emit_gpu_zone_begin_alloc(data);
JS_FreeCString(js,js_fn_name);
qhead = (qhead+1)%query_count;
JS_Call(js, argv[0], JS_UNDEFINED, 0, NULL);
glQueryCounter(ids[qhead], GL_TIMESTAMP);
struct ___tracy_gpu_zone_end_data enddata = {
.queryId = ids[qhead],
.context = 0
};
___tracy_emit_gpu_zone_end(enddata);
qhead = (qhead+1)%query_count;
return JS_UNDEFINED;
}
static JSValue js_tracy_gpu_zone_end(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected)
return JS_UNDEFINED;
#endif
glQueryCounter(ids[qhead], GL_TIMESTAMP);
struct ___tracy_gpu_zone_end_data data = {
.queryId = ids[qhead],
.context = 0
};
___tracy_emit_gpu_zone_end(data);
qhead = (qhead+1)%query_count;
return JS_UNDEFINED;
}
static JSValue js_tracy_gpu_init(JSContext *js, JSValue self, int argc, JSValue *argv)
{
ids = malloc(sizeof(GLuint)*query_count);
glGenQueries(query_count, ids); // generate new query ids
int64_t tgpu;
glGetInteger64v(GL_TIMESTAMP, &tgpu);
float period = 1.f;
struct ___tracy_gpu_new_context_data gpuctx = {
.gpuTime = tgpu,
.period = period,
.context = 0,
.flags = 0,
.type = 1
};
___tracy_emit_gpu_new_context(gpuctx);
return JS_UNDEFINED;
}
static JSValue js_tracy_gpu_sync(JSContext *js, JSValue self, int argc, JSValue *argv)
{
#ifdef TRACY_ON_DEMAND
if (!TracyCIsConnected)
return JS_UNDEFINED;
#endif
int64_t tgpu;
glGetInteger64v(GL_TIMESTAMP, &tgpu);
struct ___tracy_gpu_time_sync_data data = {
.context = 0,
.gpuTime = tgpu
};
___tracy_emit_gpu_time_sync(data);
return JS_UNDEFINED;
}
static JSValue js_tracy_gpu_collect(JSContext *js, JSValue self, int argc, JSValue *argv)
{
GLint available;
while (qtail != qhead) {
glGetQueryObjectiv(ids[qtail], GL_QUERY_RESULT_AVAILABLE, &available);
if (!available) return JS_UNDEFINED;
uint64_t time;
glGetQueryObjectui64v(ids[qtail], GL_QUERY_RESULT, &time);
struct ___tracy_gpu_time_data gtime = {
.gpuTime = time,
.queryId = ids[qtail],
.context = 0
};
qtail = (qtail+1)%query_count;
___tracy_emit_gpu_time(gtime);
}
return JS_UNDEFINED;
}
static const JSCFunctionListEntry js_tracy_funcs[] = {
JS_CFUNC_DEF("fiber", 1, js_tracy_fiber_enter),
JS_CFUNC_DEF("fiber_leave", 1, js_tracy_fiber_leave),
JS_CFUNC_DEF("gpu_zone", 1, js_tracy_gpu_zone_begin),
JS_CFUNC_DEF("gpu_collect", 0, js_tracy_gpu_collect),
JS_CFUNC_DEF("gpu_init", 0, js_tracy_gpu_init),
JS_CFUNC_DEF("end_frame", 0, js_tracy_frame_mark),
JS_CFUNC_DEF("zone", 1, js_tracy_zone_begin),
JS_CFUNC_DEF("message", 1, js_tracy_message),
JS_CFUNC_DEF("plot", 2, js_tracy_plot),
};
JSValue js_tracy_use(JSContext *js)
{
JSValue expy = JS_NewObject(js);
JS_SetPropertyFunctionList(js, expy, js_tracy_funcs, sizeof(js_tracy_funcs)/sizeof(JSCFunctionListEntry));
return expy;
}
static int js_tracy_init(JSContext *js, JSModuleDef *m) {
js_tracy_use(js);
JS_SetModuleExportList(js, m, js_tracy_funcs, sizeof(js_tracy_funcs)/sizeof(JSCFunctionListEntry));
return 0;
}
#ifdef JS_SHARED_LIBRARY
#define JS_INIT_MODULE js_init_module
#else
#define JS_INIT_MODULE js_init_module_tracy
#endif
JSModuleDef *JS_INIT_MODULE(JSContext *ctx, const char *module_name) {
JSModuleDef *m = JS_NewCModule(ctx, module_name, js_tracy_init);
if (!m) return NULL;
JS_AddModuleExportList(ctx, m, js_tracy_funcs, sizeof(js_tracy_funcs)/sizeof(JSCFunctionListEntry));
return m;
}

View File

@@ -2,144 +2,44 @@
#include "sokol/sokol_gfx.h"
#include "sokol/sokol_glue.h"
#include "sokol/util/sokol_gl.h"
#include "HandmadeMath.h"
#include <stdio.h>
#include <tracy/TracyC.h>
sg_sampler std_sampler;
sg_sampler tex_sampler;
viewstate globalview = {0};
struct rfd {
int num_passes;
int num_apply_viewport;
int num_apply_scissor_rect;
int num_apply_pipeline;
int num_apply_uniforms;
int size_uniforms;
};
#ifdef TRACY_ENABLE
#include <tracy/TracyC.h>
static struct rfd rfd = {0};
#define DUMPSTAT(NAME) TracyCPlotI(#NAME, stats.NAME);
void trace_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range *data, void *user_data)
void render_dump_trace()
{
rfd.num_apply_uniforms++;
rfd.size_uniforms += data->size;
}
sg_frame_stats stats = sg_query_frame_stats();
DUMPSTAT(num_passes)
DUMPSTAT(num_draw)
DUMPSTAT(num_tris)
DUMPSTAT(num_verts)
void trace_apply_pipeline(sg_pipeline pip, void *data)
{
// YughSpam("Applying pipeline %u %s.", pip, sg_query_pipeline_desc(pip).label);
}
void trace_begin_pass(sg_pass pass, const sg_pass_action *action, void *data)
{
// YughSpam("Begin pass %s", pass.label);
}
void trace_apply_viewport(int x, int y, int width, int height, bool origin_top_left, void *user_data)
{
}
void trace_draw(int base_element, int num_elements, int num_instances, void *user_data)
{
TracyCPlotI("draw", num_elements);
}
void trace_alloc_buffer(sg_buffer result, void *data) {
TracyCMessageL("BUFFER ALLOC");
}
void trace_init_buffer(sg_buffer id, const sg_buffer_desc *desc, void *data){
TracyCMessageL("BUFFER INIT");
}
void trace_make_buffer(const sg_buffer_desc *desc, sg_buffer id, void *data){
TracyCAllocN((void*)id.id, desc->data.size, "buffer");
}
void trace_append_buffer(sg_buffer id, const sg_range *data, int result, void *user_data) {
sg_buffer_desc desc = sg_query_buffer_desc(id);
}
void trace_update_buffer(sg_buffer buf, const sg_range *data, void *user_data){
TracyCMessageL("BUFFER UPDATED");
}
void trace_uninit_buffer(sg_buffer id, void *data){
TracyCMessageL("BUFFER UNINIT");
}
void trace_dealloc_buffer(sg_buffer id, void *data){
TracyCMessageL("BUFFER DEALLOC");
}
void trace_destroy_buffer(sg_buffer id, void *data){
TracyCFreeN((void*)id.id, "buffer");
}
void trace_fail_buffer(sg_buffer id, void *data){}
void trace_alloc_image(sg_image result, void *data) {
TracyCMessageL("IMAGE ALLOC");
}
void trace_init_image(sg_image id, const sg_image_desc *desc, void *data){
TracyCMessageL("IMAGE INIT");
}
int image_data_size(sg_image_data *data)
{
int total_size = 0;
for (int i = 0; i < SG_CUBEFACE_NUM; i++)
for (int j = 0; j < SG_MAX_MIPMAPS; j++)
total_size += data->subimage[i][j].size;
return total_size;
}
void trace_make_image(const sg_image_desc *desc, sg_image id, void *data){
int total_size = 0;
for (int i = 0; i < desc->num_mipmaps; i++)
total_size += desc->data.subimage[i]->size;
TracyCAllocN((void*)id.id, image_data_size(&desc->data), "tex_gpu");
}
void trace_update_image(sg_image id, const sg_image_data *data, void *user_data){
TracyCFreeN((void*)id.id, "tex_gpu");
TracyCAllocN((void*)id.id, image_data_size(data), "tex_gpu");
}
void trace_uninit_image(sg_image id, void *data){
TracyCMessageL("IMAGE UNINIT");
}
void trace_dealloc_image(sg_image id, void *data){
TracyCMessageL("IMAGE DEALLOC");
}
void trace_destroy_image(sg_image id, void *data){
TracyCFreeN((void*)id.id, "tex_gpu");
}
void trace_fail_image(sg_image id, void *data){}
DUMPSTAT(num_apply_pipeline)
DUMPSTAT(num_apply_bindings)
DUMPSTAT(num_apply_uniforms)
DUMPSTAT(size_apply_uniforms)
#define SG_HOOK_SET(NAME) \
.alloc_##NAME = trace_alloc_##NAME, \
.dealloc_##NAME = trace_dealloc_##NAME, \
.init_##NAME = trace_init_##NAME, \
.uninit_##NAME = trace_uninit_##NAME, \
.fail_##NAME = trace_fail_##NAME, \
.destroy_##NAME = trace_destroy_##NAME, \
.make_##NAME = trace_make_##NAME \
DUMPSTAT(size_update_buffer)
DUMPSTAT(size_append_buffer)
DUMPSTAT(size_update_image)
static sg_trace_hooks hooks = {
.apply_pipeline = trace_apply_pipeline,
.begin_pass = trace_begin_pass,
SG_HOOK_SET(buffer),
.append_buffer = trace_append_buffer,
SG_HOOK_SET(image),
// SG_HOOK_SET(shader),
// SG_HOOK_SET(sampler),
// SG_HOOK_SET(pipeline),
// SG_HOOK_SET(attachments),
DUMPSTAT(num_apply_viewport)
DUMPSTAT(num_apply_scissor_rect)
}
.draw = trace_draw,
};
#endif
void render_trace_init();
void render_init() {
sg_setup(&(sg_desc){
@@ -149,8 +49,13 @@ void render_init() {
.image_pool_size = 1024,
});
#ifndef NDEBUG
sg_trace_hooks hh = sg_install_trace_hooks(&hooks);
#ifdef TRACY_ENABLE
sg_enable_frame_stats();
TracyCPlotConfig("size_apply_uniforms", TracyPlotFormatMemory, 1, 1, 5474985);
TracyCPlotConfig("size_update_buffer", TracyPlotFormatMemory, 1, 1, 547498235);
TracyCPlotConfig("size_append_buffer", TracyPlotFormatMemory, 1, 1, 12345615);
TracyCPlotConfig("size_update_image", TracyPlotFormatMemory, 1, 1, 49831049);
render_trace_init();
#endif
std_sampler = sg_make_sampler(&(sg_sampler_desc){});

View File

@@ -56,6 +56,8 @@ struct rgba {
unsigned char a;
};
void render_dump_trace();
typedef struct rgba rgba;
static inline rgba vec2rgba(HMM_Vec4 v) {

152
source/render_trace.cpp Normal file
View File

@@ -0,0 +1,152 @@
#include "glad.h"
#include "sokol/sokol_gfx.h"
#include <tracy/Tracy.hpp>
#include <tracy/TracyOpenGL.hpp>
void trace_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range *data, void *user_data)
{
}
void trace_apply_pipeline(sg_pipeline pip, void *data)
{
}
void trace_begin_pass(const sg_pass *action, void *data)
{
}
void trace_apply_viewport(int x, int y, int width, int height, bool origin_top_left, void *user_data)
{
}
void trace_apply_bindings(const sg_bindings *bindings, void *data)
{
}
void trace_draw(int base_element, int num_elements, int num_instances, void *user_data)
{
}
void trace_alloc_buffer(sg_buffer result, void *data) {
TracyMessageL("BUFFER ALLOC");
}
void trace_init_buffer(sg_buffer id, const sg_buffer_desc *desc, void *data){
TracyMessageL("BUFFER INIT");
}
void trace_make_buffer(const sg_buffer_desc *desc, sg_buffer id, void *data){
TracyAllocN((void*)id.id, desc->data.size, "buffer");
}
void trace_append_buffer(sg_buffer id, const sg_range *data, int result, void *user_data) {
}
void trace_update_buffer(sg_buffer buf, const sg_range *data, void *user_data){
TracyMessageL("BUFFER UPDATED");
}
void trace_uninit_buffer(sg_buffer id, void *data){
TracyMessageL("BUFFER UNINIT");
}
void trace_dealloc_buffer(sg_buffer id, void *data){
TracyMessageL("BUFFER DEALLOC");
}
void trace_destroy_buffer(sg_buffer id, void *data){
TracyFreeN((void*)id.id, "buffer");
}
void trace_fail_buffer(sg_buffer id, void *data){}
void trace_alloc_image(sg_image result, void *data) {
TracyMessageL("IMAGE ALLOC");
}
void trace_init_image(sg_image id, const sg_image_desc *desc, void *data){
TracyMessageL("IMAGE INIT");
}
int image_data_size(sg_image_data *data)
{
int total_size = 0;
for (int i = 0; i < SG_CUBEFACE_NUM; i++)
for (int j = 0; j < SG_MAX_MIPMAPS; j++)
total_size += data->subimage[i][j].size;
return total_size;
}
void trace_make_image(const sg_image_desc *desc, sg_image id, void *data){
int total_size = 0;
for (int i = 0; i < desc->num_mipmaps; i++)
total_size += desc->data.subimage[i]->size;
// TracyAllocN((void*)id.id, image_data_size(&desc->data), "tex_gpu");
}
void trace_update_image(sg_image id, const sg_image_data *data, void *user_data){
// TracyFreeN((void*)id.id, "tex_gpu");
// TracyAllocN((void*)id.id, image_data_size(data), "tex_gpu");
}
void trace_uninit_image(sg_image id, void *data){
TracyMessageL("IMAGE UNINIT");
}
void trace_dealloc_image(sg_image id, void *data){
TracyMessageL("IMAGE DEALLOC");
}
void trace_destroy_image(sg_image id, void *data){
TracyFreeN((void*)id.id, "tex_gpu");
}
void trace_fail_image(sg_image id, void *data){}
#define SG_HOOK_SET(NAME) \
hooks.alloc_##NAME = trace_alloc_##NAME; \
hooks.dealloc_##NAME = trace_dealloc_##NAME; \
hooks.init_##NAME = trace_init_##NAME; \
hooks.uninit_##NAME = trace_uninit_##NAME; \
hooks.fail_##NAME = trace_fail_##NAME; \
hooks.destroy_##NAME = trace_destroy_##NAME; \
hooks.make_##NAME = trace_make_##NAME; \
static sg_trace_hooks hooks;
extern "C"{
void render_trace_init()
{
hooks.apply_pipeline = trace_apply_pipeline;
hooks.begin_pass = trace_begin_pass;
SG_HOOK_SET(buffer);
hooks.append_buffer = trace_append_buffer;
SG_HOOK_SET(image);
// SG_HOOK_SET(shader),
// SG_HOOK_SET(sampler),
// SG_HOOK_SET(pipeline),
// SG_HOOK_SET(attachments),
hooks.apply_uniforms = trace_apply_uniforms;
hooks.draw = trace_draw;
sg_trace_hooks hh = sg_install_trace_hooks(&hooks);
printf("GLAD LOAD %d\n", gladLoadGL());
TracyGpuContext;
}
}
/*static const JSCFunctionListEntry js_tracy_funcs[] = {
JS_CFUNC_DEF("fiber", 1, js_tracy_fiber_enter),
JS_CFUNC_DEF("fiber_leave", 1, js_tracy_fiber_leave),
JS_CFUNC_DEF("gpu_zone_begin", 1, js_tracy_gpu_zone_begin),
JS_CFUNC_DEF("gpu_zone_end", 1, js_tracy_gpu_zone_end),
JS_CFUNC_DEF("gpu_collect", 0, js_tracy_gpu_collect),
JS_CFUNC_DEF("end_frame", 0, js_tracy_frame_mark),
JS_CFUNC_DEF("zone", 1, js_tracy_zone_begin),
JS_CFUNC_DEF("message", 1, js_tracy_message),
JS_CFUNC_DEF("plot", 2, js_tracy_plot),
};
JSValue js_tracy_use(JSContext *js)
{
JSValue expy = JS_NewObject(js);
JS_SetPropertyFunctionList(js, expy, js_tracy_funcs, sizeof(js_tracy_funcs)/sizeof(JSCFunctionListEntry));
return expy;
}
*/