improve tex unit allocation

main
Dominik Madarász 2024-03-21 07:43:51 +01:00
parent c8a6c79bf1
commit fad3653201
5 changed files with 111 additions and 100 deletions

View File

@ -39,6 +39,7 @@ int main() {
model_t m2 = model("suzanne.obj", MODEL_NO_ANIMATIONS|MODEL_MATCAPS);
// model_t m3 = model("damagedhelmet.gltf", MODEL_NO_ANIMATIONS|MODEL_PBR);
model_t m3 = model("Scutum_low.fbx", MODEL_NO_ANIMATIONS|MODEL_PBR);
// model_t m4 = model("avp/scene.gltf", MODEL_NO_ANIMATIONS|MODEL_PBR);
// model_t m3 = model("Cerberus_LP.FBX", MODEL_NO_ANIMATIONS|MODEL_PBR);
// spawn object1 (diffuse)
@ -72,6 +73,13 @@ int main() {
object_move(obj4, vec3(-10+6*3,0,-10));
object_pivot(obj4, vec3(0,0,90));
// spawn object5 (pbr)
// object_t* obj5 = scene_spawn();
// object_model(obj5, m4);
// object_scale(obj5, vec3(3,3,3));
// object_move(obj5, vec3(-10+6*3,0,-10));
// object_pivot(obj5, vec3(0,0,90));
// create point light
scene_spawn_light(); // sun
light_t* l = scene_spawn_light();

View File

@ -1,79 +1,79 @@
#include "v4k.h"
model_t litm;
void bakedrawmodel(lightmap_t *lm, model_t *m, float *view, float *proj, void *userdata) {
shader_bind(lm->shader);
model_render(*m, proj, view, m->pivot, lm->shader);
shader_float("u_litboost", 4.0);
model_render(litm, proj, view, litm.pivot, lm->shader);
}
void progressupdate(float progress) {
static double lastUpdateTime = 0.0;
double time = time_ss();
if (time - lastUpdateTime > 1.0) {
lastUpdateTime = time;
PRINTF("progress: %.02f%%", progress*100);
}
// window_swap();
}
int main()
{
window_create(0.5, 0);
window_title(__FILE__);
camera_t cam = camera();
skybox_t sky = skybox(0, 0); skybox_mie_calc_sh(&sky, 2.0f);
model_t mdl = model(option("--model","gazebo.obj"), 0);
litm = model("cube.obj", MODEL_MATCAPS);
{
mat44 lp; scaling44(lp, 0.3, 0.3, 0.3); translate44(lp, 8,4,0);
copy44(litm.pivot, lp);
}
rotate44(mdl.pivot, -90, 1, 0, 0);
scale44(mdl.pivot, 4,4,4);
shader_bind(mdl.program);
shader_vec3v("u_coefficients_sh", 9, sky.cubemap.sh);
// shader_bool("u_texmod", 0);
unsigned char emissive[] = { 255, 180, 0, 255 };
texture_t emission = texture_create(1,1,4,emissive,TEXTURE_LINEAR);
model_set_texture(litm, emission);
lightmap_t baker = lightmap(64, 0.01, 100, vec3(0,0,0), 2, 0.01, 0.0);
lightmap_setup(&baker, 512, 512);
array_push(baker.models, &mdl);
bool do_bake=0;
int b=1;
while (window_swap() && !input(KEY_ESC)) {
bool active = ui_active() || ui_hover() || gizmo_active() ? false : input(MOUSE_L) || input(MOUSE_M) || input(MOUSE_R);
if( active ) cam.speed = clampf(cam.speed + input_diff(MOUSE_W) / 10, 0.05f, 5.0f);
vec2 mouse = scale2(vec2(input_diff(MOUSE_X), -input_diff(MOUSE_Y)), 0.2f * active);
vec3 wasdecq = scale3(vec3(input(KEY_D)-input(KEY_A),input(KEY_E)-(input(KEY_C)||input(KEY_Q)),input(KEY_W)-input(KEY_S)), cam.speed);
camera_moveby(&cam, wasdecq);
camera_fps(&cam, mouse.x,mouse.y);
window_cursor( !active );
skybox_render(&sky, cam.proj, cam.view);
model_render(mdl, cam.proj, cam.view, mdl.pivot, 0);
model_render(litm, cam.proj, cam.view, litm.pivot, 0);
if( ui_panel("Lightmapper", PANEL_OPEN) ) {
ui_label2("Freecam", "Mouse + W/A/S/D/E/Q keys");
ui_label("Warning " ICON_MD_WARNING "@This will take a few seconds and bake a lightmap illuminated by: The mesh itself (initially black) + A white sky (1.0f, 1.0f, 1.0f)");
ui_int("Bounces", &b);
if( ui_button(va("Bake %d light bounce(s)", b)) ) {
do_bake=1;
}
ui_panel_end();
}
if (do_bake) {
do_bake=0;
lightmap_bake(&baker, b, bakedrawmodel, progressupdate, 0);
}
}
}
#include "v4k.h"
model_t litm;
void bakedrawmodel(lightmap_t *lm, model_t *m, float *view, float *proj, void *userdata) {
shader_bind(lm->shader);
model_render(*m, proj, view, m->pivot, lm->shader);
shader_float("u_litboost", 4.0);
model_render(litm, proj, view, litm.pivot, lm->shader);
}
void progressupdate(float progress) {
static double lastUpdateTime = 0.0;
double time = time_ss();
if (time - lastUpdateTime > 1.0) {
lastUpdateTime = time;
PRINTF("progress: %.02f%%", progress*100);
}
// window_swap();
}
int main()
{
window_create(0.5, 0);
window_title(__FILE__);
camera_t cam = camera();
skybox_t sky = skybox(0, 0); skybox_mie_calc_sh(&sky, 2.0f);
model_t mdl = model(option("--model","gazebo.obj"), 0);
litm = model("cube.obj", 0);
{
mat44 lp; scaling44(lp, 0.3, 0.3, 0.3); translate44(lp, 8,4,0);
copy44(litm.pivot, lp);
}
rotate44(mdl.pivot, -90, 1, 0, 0);
scale44(mdl.pivot, 4,4,4);
shader_bind(mdl.program);
shader_vec3v("u_coefficients_sh", 9, sky.cubemap.sh);
// shader_bool("u_texmod", 0);
unsigned char emissive[] = { 255, 180, 0, 255 };
texture_t emission = texture_create(1,1,4,emissive,TEXTURE_LINEAR);
model_set_texture(litm, emission);
lightmap_t baker = lightmap(64, 0.01, 100, vec3(0,0,0), 2, 0.01, 0.0);
lightmap_setup(&baker, 512, 512);
array_push(baker.models, &mdl);
bool do_bake=0;
int b=1;
while (window_swap() && !input(KEY_ESC)) {
bool active = ui_active() || ui_hover() || gizmo_active() ? false : input(MOUSE_L) || input(MOUSE_M) || input(MOUSE_R);
if( active ) cam.speed = clampf(cam.speed + input_diff(MOUSE_W) / 10, 0.05f, 5.0f);
vec2 mouse = scale2(vec2(input_diff(MOUSE_X), -input_diff(MOUSE_Y)), 0.2f * active);
vec3 wasdecq = scale3(vec3(input(KEY_D)-input(KEY_A),input(KEY_E)-(input(KEY_C)||input(KEY_Q)),input(KEY_W)-input(KEY_S)), cam.speed);
camera_moveby(&cam, wasdecq);
camera_fps(&cam, mouse.x,mouse.y);
window_cursor( !active );
skybox_render(&sky, cam.proj, cam.view);
model_render(mdl, cam.proj, cam.view, mdl.pivot, 0);
model_render(litm, cam.proj, cam.view, litm.pivot, 0);
if( ui_panel("Lightmapper", PANEL_OPEN) ) {
ui_label2("Freecam", "Mouse + W/A/S/D/E/Q keys");
ui_label("Warning " ICON_MD_WARNING "@This will take a few seconds and bake a lightmap illuminated by: The mesh itself (initially black) + A white sky (1.0f, 1.0f, 1.0f)");
ui_int("Bounces", &b);
if( ui_button(va("Bake %d light bounce(s)", b)) ) {
do_bake=1;
}
ui_panel_end();
}
if (do_bake) {
do_bake=0;
lightmap_bake(&baker, b, bakedrawmodel, progressupdate, 0);
}
}
}

View File

@ -370072,7 +370072,7 @@ void glDebugEnable() {
static
void glCopyBackbufferToTexture( texture_t *tex ) { // unused
glActiveTexture( GL_TEXTURE0 + tex->unit );
glActiveTexture( GL_TEXTURE0 + allocate_texture_unit() );
glBindTexture( GL_TEXTURE_2D, tex->id );
glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, window_width(), window_height(), 0 );
}
@ -373755,6 +373755,9 @@ void model_draw_call(model_t m, int shader) {
if(!m.iqm) return;
iqm_t *q = m.iqm;
handle old_shader = last_shader;
shader_bind(shader);
glBindVertexArray( q->vao );
struct iqmtriangle *tris = NULL;
@ -373762,9 +373765,8 @@ void model_draw_call(model_t m, int shader) {
struct iqmmesh *im = &q->meshes[i];
if (m.shading != SHADING_PBR) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, q->textures[i] );
glUniform1i(glGetUniformLocation(shader, "u_texture2d"), allocate_texture_unit() );
shader_texture_unit("u_texture2d", q->textures[i], allocate_texture_unit());
shader_texture("u_lightmap", m.lightmap);
int loc;
if ((loc = glGetUniformLocation(shader, "u_textured")) >= 0) {
@ -373775,9 +373777,6 @@ void model_draw_call(model_t m, int shader) {
}
}
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m.lightmap.id);
glUniform1i(glGetUniformLocation(shader, "u_lightmap"), allocate_texture_unit() );
} else {
const pbr_material_t *material = &m.pbr_materials[i];
shader_colormap( "map_diffuse", material->diffuse );
@ -373798,6 +373797,8 @@ void model_draw_call(model_t m, int shader) {
}
glBindVertexArray( 0 );
shader_bind(old_shader);
}
void model_render_instanced(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count) {

View File

@ -45,7 +45,7 @@ void glDebugEnable() {
static
void glCopyBackbufferToTexture( texture_t *tex ) { // unused
glActiveTexture( GL_TEXTURE0 + tex->unit );
glActiveTexture( GL_TEXTURE0 + allocate_texture_unit() );
glBindTexture( GL_TEXTURE_2D, tex->id );
glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, window_width(), window_height(), 0 );
}
@ -3728,6 +3728,9 @@ void model_draw_call(model_t m, int shader) {
if(!m.iqm) return;
iqm_t *q = m.iqm;
handle old_shader = last_shader;
shader_bind(shader);
glBindVertexArray( q->vao );
struct iqmtriangle *tris = NULL;
@ -3735,9 +3738,8 @@ void model_draw_call(model_t m, int shader) {
struct iqmmesh *im = &q->meshes[i];
if (m.shading != SHADING_PBR) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, q->textures[i] );
glUniform1i(glGetUniformLocation(shader, "u_texture2d"), allocate_texture_unit() );
shader_texture_unit("u_texture2d", q->textures[i], allocate_texture_unit());
shader_texture("u_lightmap", m.lightmap);
int loc;
if ((loc = glGetUniformLocation(shader, "u_textured")) >= 0) {
@ -3748,9 +3750,6 @@ void model_draw_call(model_t m, int shader) {
}
}
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m.lightmap.id);
glUniform1i(glGetUniformLocation(shader, "u_lightmap"), allocate_texture_unit() );
} else {
const pbr_material_t *material = &m.pbr_materials[i];
shader_colormap( "map_diffuse", material->diffuse );
@ -3771,6 +3770,8 @@ void model_draw_call(model_t m, int shader) {
}
glBindVertexArray( 0 );
shader_bind(old_shader);
}
void model_render_instanced(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count) {

View File

@ -17219,7 +17219,7 @@ void glDebugEnable() {
static
void glCopyBackbufferToTexture( texture_t *tex ) { // unused
glActiveTexture( GL_TEXTURE0 + tex->unit );
glActiveTexture( GL_TEXTURE0 + allocate_texture_unit() );
glBindTexture( GL_TEXTURE_2D, tex->id );
glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, window_width(), window_height(), 0 );
}
@ -20902,6 +20902,9 @@ void model_draw_call(model_t m, int shader) {
if(!m.iqm) return;
iqm_t *q = m.iqm;
handle old_shader = last_shader;
shader_bind(shader);
glBindVertexArray( q->vao );
struct iqmtriangle *tris = NULL;
@ -20909,9 +20912,8 @@ void model_draw_call(model_t m, int shader) {
struct iqmmesh *im = &q->meshes[i];
if (m.shading != SHADING_PBR) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, q->textures[i] );
glUniform1i(glGetUniformLocation(shader, "u_texture2d"), allocate_texture_unit() );
shader_texture_unit("u_texture2d", q->textures[i], allocate_texture_unit());
shader_texture("u_lightmap", m.lightmap);
int loc;
if ((loc = glGetUniformLocation(shader, "u_textured")) >= 0) {
@ -20922,9 +20924,6 @@ void model_draw_call(model_t m, int shader) {
}
}
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m.lightmap.id);
glUniform1i(glGetUniformLocation(shader, "u_lightmap"), allocate_texture_unit() );
} else {
const pbr_material_t *material = &m.pbr_materials[i];
shader_colormap( "map_diffuse", material->diffuse );
@ -20945,6 +20944,8 @@ void model_draw_call(model_t m, int shader) {
}
glBindVertexArray( 0 );
shader_bind(old_shader);
}
void model_render_instanced(model_t m, mat44 proj, mat44 view, mat44* models, int shader, unsigned count) {