Compare commits

...

2 commits

Author SHA1 Message Date
John Alanbrook f8db84538f Correct metal rendering 2024-05-14 15:22:24 -05:00
John Alanbrook 3b3e58cf39 Split up end pass and commit 2024-05-13 14:07:00 -05:00
9 changed files with 124 additions and 52 deletions

View file

@ -44,15 +44,6 @@ cgltf_attribute *get_attr_type(cgltf_primitive *p, cgltf_attribute_type t)
unsigned short pack_short_tex(float c) { return c * USHRT_MAX; }
uint32_t pack_int10_n2(float *norm)
{
uint32_t ret = 0;
for (int i = 0; i < 3; i++) {
int n = (norm[i]+1.0)*511;
ret |= (n & 0x3ff) << (10*i);
}
return ret;
}
void mesh_add_material(primitive *prim, cgltf_material *mat)
{
@ -124,8 +115,19 @@ sg_buffer index_buffer(float *f, int verts)
});
}
uint32_t pack_int10_n2(float *norm)
{
uint32_t ret = 0;
for (int i = 0; i < 3; i++) {
int n = (norm[i]+1.0)*511;
ret |= (n & 0x3ff) << (10*i);
}
return ret;
}
sg_buffer normal_floats(float *f, int n)
{
return float_buffer(f, n);
uint32_t packed_norms[n/3];
for (int v = 0, i = 0; v < n/3; v++, i+= 3)
packed_norms[v] = pack_int10_n2(f+i);
@ -179,13 +181,14 @@ HMM_Vec3 index_to_vert(uint32_t idx, float *f)
void primitive_gen_indices(primitive *prim)
{
if (prim->idx_count == 0) return;
if (prim->idx_count != 0) return;
uint16_t *idxs = malloc(sizeof(*idxs)*prim->idx_count);
for (int z = 0; z < prim->idx_count; z++)
idxs[z] = z;
prim->idx = sg_make_buffer(&(sg_buffer_desc){
prim->index = sg_make_buffer(&(sg_buffer_desc){
.data.ptr = idxs,
.data.size = sizeof(uint16_t) * prim->idx_count,
.type = SG_BUFFERTYPE_INDEXBUFFER});
@ -206,7 +209,7 @@ struct primitive mesh_add_primitive(cgltf_primitive *prim)
for (int i = 0; i < n; i++)
idxs[i] = fidx[i];
retp.idx = sg_make_buffer(&(sg_buffer_desc){
retp.index = sg_make_buffer(&(sg_buffer_desc){
.data.ptr = idxs,
.data.size = sizeof(*idxs) * n,
.type = SG_BUFFERTYPE_INDEXBUFFER,
@ -497,7 +500,7 @@ sg_bindings primitive_bind(primitive *p)
b.vertex_buffers[MAT_BONE] = p->bone;
b.vertex_buffers[MAT_WEIGHT] = p->weight;
b.vertex_buffers[MAT_COLOR] = p->color;
b.index_buffer = p->idx;
b.index_buffer = p->index;
b.fs.images[0] = p->mat->diffuse->id;
b.fs.samplers[0] = tex_sampler;
return b;
@ -540,6 +543,7 @@ int mat2type(int mat)
{
switch(mat) {
case MAT_POS:
case MAT_NORM:
return SG_VERTEXFORMAT_FLOAT3;
case MAT_PPOS:
case MAT_WH:
@ -548,7 +552,6 @@ int mat2type(int mat)
case MAT_UV:
case MAT_TAN:
return SG_VERTEXFORMAT_USHORT2N;
case MAT_NORM:
return SG_VERTEXFORMAT_UINT10_N2;
case MAT_BONE:
return SG_VERTEXFORMAT_UBYTE4;
@ -605,7 +608,7 @@ sg_bindings primitive_bindings(primitive *p, JSValue v)
b.vertex_buffers[slot] = buf;
}
b.index_buffer = p->idx;
b.index_buffer = p->index;
return b;
}

View file

@ -43,7 +43,7 @@ typedef struct primitive {
sg_buffer bone;
sg_buffer weight;
sg_buffer color;
sg_buffer idx;
sg_buffer index;
material *mat;
uint32_t idx_count;
} primitive;

View file

@ -1229,6 +1229,18 @@ HMM_Mat4 HMM_InvOrthographic(HMM_Mat4 OrthoMatrix) {
return Result;
}
HMM_Mat4 HMM_Perspective_Metal(float FOV, float AspectRatio, float Near, float Far)
{
HMM_Mat4 adjust = {0};
adjust.e[0][0] = 1;
adjust.e[1][1] = 1;
adjust.e[2][2] = 0.5;
adjust.e[3][2] = 0.5;
adjust.e[3][3] = 1;
HMM_Mat4 opengl = HMM_Perspective_LH_NO(FOV, AspectRatio, Near, Far);
return HMM_MulM4(opengl,adjust);
}
HMM_Mat4 HMM_Perspective_RH_NO(float FOV, float AspectRatio, float Near, float Far) {
HMM_Mat4 Result = {0};
@ -1747,7 +1759,8 @@ HMM_Mat4 HMM_M4TRS(HMM_Vec3 t, HMM_Quat q, HMM_Vec3 s)
HMM_Mat4 T = HMM_Translate(t);
HMM_Mat4 R = HMM_QToM4(q);
HMM_Mat4 S = HMM_Scale(s);
return HMM_MulM4(T,S);
return HMM_MulM4(T, HMM_MulM4(R, S));
//return HMM_MulM4(T,S);
HMM_Mat4 l;
float *lm = (float*)&l;

View file

@ -615,6 +615,8 @@ HMM_Mat4 HMM_Perspective_RH_NO(float FOV, float AspectRatio, float Near, float F
HMM_Mat4 HMM_Perspective_RH_ZO(float FOV, float AspectRatio, float Near, float Far);
HMM_Mat4 HMM_Perspective_LH_NO(float FOV, float AspectRatio, float Near, float Far);
HMM_Mat4 HMM_Perspective_LH_ZO(float FOV, float AspectRatio, float Near, float Far);
HMM_Mat4 HMM_Perspective_Metal(float FOV, float AspectRation, float Near, float Far);
HMM_Mat4 HMM_InvPerspective_RH(HMM_Mat4 PerspectiveMatrix);
HMM_Mat4 HMM_InvPerspective_LH(HMM_Mat4 PerspectiveMatrix);
HMM_Mat4 HMM_Translate(HMM_Vec3 Translation);

View file

@ -44,6 +44,8 @@ static JSValue globalThis;
static JSClassID js_ptr_id;
static JSClassDef js_ptr_class = { "POINTER" };
static JSValue JSUNDEF;
JSValue str2js(const char *c, ...) {
if (!c) return JS_UNDEFINED;
char *result = NULL;
@ -81,7 +83,6 @@ QJSCLASS(material)
QJSCLASS(model)
QJSCLASS(window)
QJSCLASS(constraint)
QJSCLASS(primitive)
QJSCLASS(sg_buffer)
QJSCLASS(datastream)
@ -671,22 +672,22 @@ JSC_CCALL(render_end_pass,
switch(mainwin.mode) {
case MODE_STRETCH:
sg_apply_viewportf(0,0,mainwin.size.x,mainwin.size.y,1);
sg_apply_viewportf(0,0,mainwin.size.x,mainwin.size.y,0);
break;
case MODE_WIDTH:
sg_apply_viewportf(0, mainwin.top, mainwin.size.x, mainwin.psize.y,1); // keep width
sg_apply_viewportf(0, mainwin.top, mainwin.size.x, mainwin.psize.y,0); // keep width
break;
case MODE_HEIGHT:
sg_apply_viewportf(mainwin.left,0,mainwin.psize.x, mainwin.size.y,1); // keep height
sg_apply_viewportf(mainwin.left,0,mainwin.psize.x, mainwin.size.y,0); // keep height
break;
case MODE_KEEP:
sg_apply_viewportf(0,0,mainwin.rendersize.x, mainwin.rendersize.y, 1); // no scaling
sg_apply_viewportf(0,0,mainwin.rendersize.x, mainwin.rendersize.y, 0); // no scaling
break;
case MODE_EXPAND:
if (mainwin.aspect < mainwin.raspect)
sg_apply_viewportf(0, mainwin.top, mainwin.size.x, mainwin.psize.y,1); // keep width
sg_apply_viewportf(0, mainwin.top, mainwin.size.x, mainwin.psize.y,0); // keep width
else
sg_apply_viewportf(mainwin.left,0,mainwin.psize.x, mainwin.size.y,1); // keep height
sg_apply_viewportf(mainwin.left,0,mainwin.psize.x, mainwin.size.y,0); // keep height
break;
}
p.id = js2number(argv[0]);
@ -702,23 +703,23 @@ JSC_CCALL(render_end_pass,
sg_commit();
)
JSC_CCALL(render_commit, sg_commit())
JSC_SCALL(render_text_size, ret = bb2js(text_bb(str, js2number(argv[1]), js2number(argv[2]), 1)))
JSC_CCALL(render_set_camera,
JSValue cam = argv[0];
int ortho = js2boolean(js_getpropstr(cam, "ortho"));
int near = js2number(js_getpropstr(cam, "near"));
int far = js2number(js_getpropstr(cam, "far"));
float fov = js2number(js_getpropstr(cam, "fov"));
HMM_Vec4 viewport = js2vec4(js_getpropstr(cam, "viewport"));
float near = js2number(js_getpropstr(cam, "near"));
float far = js2number(js_getpropstr(cam, "far"));
float fov = js2number(js_getpropstr(cam, "fov"))*HMM_DegToRad;
transform *t = js2transform(js_getpropstr(cam, "transform"));
globalview.v = transform2mat(*t);
HMM_Vec2 size = mainwin.mode == MODE_FULL ? mainwin.size : mainwin.rendersize;
sg_apply_viewportf(viewport.x*size.x, viewport.y*size.y, viewport.z*size.x, viewport.w*size.y,1);
if (ortho)
globalview.p = HMM_Orthographic_RH_NO(
globalview.p = HMM_Orthographic_LH_ZO(
-size.x/2,
size.x/2,
-size.y/2,
@ -727,10 +728,9 @@ JSC_CCALL(render_set_camera,
far
);
else
globalview.p = HMM_Perspective_RH_NO(fov, size.y/size.x, near, far);
globalview.p = HMM_Perspective_Metal(fov, size.x/size.y, near, far);
globalview.vp = HMM_MulM4(globalview.p, globalview.v);
projection = globalview.vp;
)
sg_shader js2shader(JSValue v)
@ -830,12 +830,17 @@ JSC_CCALL(render_pipeline,
p.layout = js2layout(argv[0]);
p.cull_mode = js2number(js_getpropstr(argv[0], "cull"));
p.primitive_type = js2number(js_getpropstr(argv[0], "primitive"));
//p.face_winding = js2number(js_getpropstr(argv[0], "face"));
p.face_winding = 1;
p.index_type = SG_INDEXTYPE_UINT16;
if (js2boolean(js_getpropstr(argv[0], "blend")))
p.colors[0].blend = blend_trans;
//p.depth.write_enabled = true;
//p.depth.compare = SG_COMPAREFUNC_LESS_EQUAL;
int depth = js2boolean(js_getpropstr(argv[0], "depth"));
if (depth) {
p.depth.write_enabled = true;
p.depth.compare = SG_COMPAREFUNC_LESS;
}
sg_pipeline pipe = sg_make_pipeline(&p);
@ -872,7 +877,15 @@ JSC_CCALL(render_setuniv4,
)
JSC_CCALL(render_setuniproj,
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(projection));
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(globalview.p));
)
JSC_CCALL(render_setuniview,
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(globalview.v));
)
JSC_CCALL(render_setunivp,
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(globalview.vp));
)
JSC_CCALL(render_setunim4,
@ -914,7 +927,7 @@ JSC_CCALL(render_text_ssbo,
static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render, flushtext, 0),
MIST_FUNC_DEF(render, end_pass, 0),
MIST_FUNC_DEF(render, end_pass, 2),
MIST_FUNC_DEF(render, text_size, 3),
MIST_FUNC_DEF(render, text_ssbo, 0),
MIST_FUNC_DEF(render, set_camera, 1),
@ -923,10 +936,13 @@ static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render, setuniv, 2),
MIST_FUNC_DEF(render, spdraw, 1),
MIST_FUNC_DEF(render, setuniproj, 2),
MIST_FUNC_DEF(render, setuniview, 2),
MIST_FUNC_DEF(render, setunivp, 2),
MIST_FUNC_DEF(render, setunim4, 3),
MIST_FUNC_DEF(render, setuniv2, 2),
MIST_FUNC_DEF(render, setuniv4, 2),
MIST_FUNC_DEF(render, setpipeline, 1)
MIST_FUNC_DEF(render, setpipeline, 1),
MIST_FUNC_DEF(render, commit, 0),
};
JSC_CCALL(gui_flush, text_flush());
@ -1386,15 +1402,15 @@ JSC_GETSET(transform, rotation, quat)
JSC_CCALL(transform_lookat,
HMM_Vec3 point = js2vec3(argv[0]);
transform *go = js2transform(this);
HMM_Mat4 m = HMM_LookAt_RH(go->pos, point, vUP);
go->rotation = HMM_M4ToQ_RH(m);
HMM_Mat4 m = HMM_LookAt_LH(go->pos, point, vUP);
go->rotation = HMM_M4ToQ_LH(m);
)
JSC_CCALL(transform_rotate,
HMM_Vec3 axis = js2vec3(argv[0]);
transform *t = js2transform(this);
HMM_Quat rot = HMM_QFromAxisAngle_RH(axis, js2number(argv[1]));
t->rotation = HMM_MulQ(t->rotation, rot);
HMM_Quat rot = HMM_QFromAxisAngle_LH(axis, js2number(argv[1]));
t->rotation = HMM_MulQ(rot, t->rotation);
)
static const JSCFunctionListEntry js_transform_funcs[] = {
@ -1849,7 +1865,34 @@ JSC_CCALL(os_make_font,
JSC_CCALL(os_make_transform, return transform2js(make_transform()))
JSC_SCALL(os_system, return number2js(system(str)); )
JSC_SCALL(os_make_model, ret = model2js(model_make(str)))
#define PRIMCHECK(VAR, JS, VAL) \
if (VAR->VAL.id) js_setpropstr(JS, #VAL, sg_buffer2js(&VAR->VAL)); \
JSValue primitive2js(primitive *p)
{
JSValue v = JS_NewObject(js);
PRIMCHECK(p, v, pos)
PRIMCHECK(p,v,norm)
PRIMCHECK(p,v,uv)
PRIMCHECK(p,v,bone)
PRIMCHECK(p,v,weight)
PRIMCHECK(p,v,color)
PRIMCHECK(p,v,index)
js_setpropstr(v, "count", number2js(p->idx_count));
printf("new primitive with count %d\n", p->idx_count);
return v;
}
JSC_SCALL(os_make_model,
model *m = model_make(str);
if (arrlen(m->meshes) != 1) return JSUNDEF;
mesh *me = m->meshes+0;
return primitive2js(me->primitives+0);
)
JSC_CCALL(os_make_emitter,
emitter *e = make_emitter();
ret = emitter2js(e);
@ -2052,6 +2095,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
#include "steam.h"
void ffi_load() {
JSUNDEF = JS_UNDEFINED;
globalThis = JS_GetGlobalObject(js);
QJSCLASSPREP(ptr);

View file

@ -95,7 +95,6 @@ void emitter_draw(emitter *e)
}
void emitter_step(emitter *e, double dt, transform *t) {
HMM_Vec4 g_accel = HMM_MulV4F((HMM_Vec4){cpSpaceGetGravity(space).x, cpSpaceGetGravity(space).y, 0, 0}, dt);
for (int i = 0; i < arrlen(e->particles); i++) {

View file

@ -148,6 +148,9 @@ void render_init() {
font_init();
sg_features feat = sg_query_features();
printf("top left? %d\n", feat.origin_top_left);
sg_color c = (sg_color){0,0,0,1};
pass_action = (sg_pass_action){
.colors[0] = {.load_action = SG_LOADACTION_CLEAR, .clear_value = c},
@ -155,7 +158,12 @@ void render_init() {
sg_color oc = (sg_color){35.0/255,60.0/255,92.0/255,1};
off_action = (sg_pass_action){
.colors[0] = {.load_action = SG_LOADACTION_CLEAR, .clear_value = oc}
.colors[0] = {.load_action = SG_LOADACTION_CLEAR, .clear_value = oc},
.depth = {
.load_action = SG_LOADACTION_CLEAR,
.clear_value = 1
//.store_action = SG_STOREACTION_STORE
}
};
screencolor = sg_make_image(&(sg_image_desc){
@ -176,11 +184,10 @@ void render_init() {
offscreen = (sg_pass){
.attachments = sg_make_attachments(&(sg_attachments_desc){
.colors[0].image = screencolor,
.depth_stencil.image = screendepth
.depth_stencil.image = screendepth,
}),
.action = off_action,
};
}
HMM_Mat4 projection = {0.f};
@ -219,6 +226,7 @@ void openglRender(struct window *window) {
};
}
lastuse = usesize;
sg_begin_pass(&offscreen);
}

View file

@ -6,6 +6,7 @@ transform *make_transform()
{
transform *t = calloc(sizeof(transform),1);
t->scale = (HMM_Vec3){1,1,1};
t->rotation = (HMM_Quat){0,0,0,1};
return t;
}
@ -46,7 +47,7 @@ HMM_Mat4 transform2mat(transform t) {
HMM_Quat angle2rotation(float angle)
{
return HMM_QFromAxisAngle_RH(vUP, angle);
return HMM_QFromAxisAngle_LH(vUP, angle);
}
float transform2angle(HMM_Vec3 axis)
@ -62,6 +63,6 @@ transform mat2transform(HMM_Mat4 m)
t.scale.Elements[i] = HMM_LenV3(m.Columns[i].xyz);
// for (int i = 0; i < 2; i++)
// m.Columns[i].xyz = HMM_MulV3(m.Columns[i].xyz, t.scale.Elements[i]);
t.rotation = HMM_M4ToQ_RH(m);
t.rotation = HMM_M4ToQ_LH(m);
return t;
}

View file

@ -7,6 +7,8 @@ typedef struct transform {
HMM_Vec3 pos;
HMM_Vec3 scale;
HMM_Quat rotation;
HMM_Mat4 cache;
int dirty;
} transform;
transform *make_transform();