[win32, canvas] added back texture atlasing and fixed incorrect culling of solid tiles when using a source texture

This commit is contained in:
martinfouilleul 2023-07-04 17:24:41 +02:00
parent 5c4c8ce505
commit bdb9ddf888
5 changed files with 38 additions and 22 deletions

View File

@ -15,12 +15,8 @@
#include"milepost.h" #include"milepost.h"
#define LOG_SUBSYSTEM "Main"
int main() int main()
{ {
LogLevel(LOG_LEVEL_WARNING);
mp_init(); mp_init();
mp_clock_init(); //TODO put that in mp_init()? mp_clock_init(); //TODO put that in mp_init()?
@ -30,14 +26,19 @@ int main()
mp_rect contentRect = mp_window_get_content_rect(window); mp_rect contentRect = mp_window_get_content_rect(window);
//NOTE: create surface //NOTE: create surface
mg_surface surface = mg_surface_create_for_window(window, MG_BACKEND_DEFAULT); mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS);
if(mg_surface_is_nil(surface))
{
log_error("couldn't create surface\n");
return(-1);
}
mg_surface_swap_interval(surface, 0); mg_surface_swap_interval(surface, 0);
//NOTE: create canvas //NOTE: create canvas
mg_canvas canvas = mg_canvas_create(surface); mg_canvas canvas = mg_canvas_create();
if(mg_canvas_is_nil(canvas)) if(mg_canvas_is_nil(canvas))
{ {
printf("Error: couldn't create canvas\n"); log_error("Error: couldn't create canvas\n");
return(-1); return(-1);
} }
@ -46,7 +47,7 @@ int main()
mem_arena_init(&permanentArena); mem_arena_init(&permanentArena);
mg_rect_atlas* atlas = mg_rect_atlas_create(&permanentArena, 16000, 16000); mg_rect_atlas* atlas = mg_rect_atlas_create(&permanentArena, 16000, 16000);
mg_image atlasImage = mg_image_create(16000, 16000); mg_image atlasImage = mg_image_create(surface, 16000, 16000);
str8 path1 = path_executable_relative(mem_scratch(), STR8("../resources/triceratops.png")); str8 path1 = path_executable_relative(mem_scratch(), STR8("../resources/triceratops.png"));
str8 path2 = path_executable_relative(mem_scratch(), STR8("../resources/Top512.png")); str8 path2 = path_executable_relative(mem_scratch(), STR8("../resources/Top512.png"));
@ -61,10 +62,10 @@ int main()
while(!mp_should_quit()) while(!mp_should_quit())
{ {
mp_pump_events(0); mp_pump_events(0);
mp_event event = {0}; mp_event* event = 0;
while(mp_next_event(&event)) while((event = mp_next_event(mem_scratch())) != 0)
{ {
switch(event.type) switch(event->type)
{ {
case MP_EVENT_WINDOW_CLOSE: case MP_EVENT_WINDOW_CLOSE:
{ {
@ -86,7 +87,7 @@ int main()
mg_image_draw_region(image1.image, image1.rect, (mp_rect){100, 100, 300, 300}); mg_image_draw_region(image1.image, image1.rect, (mp_rect){100, 100, 300, 300});
mg_image_draw_region(image2.image, image2.rect, (mp_rect){300, 200, 300, 300}); mg_image_draw_region(image2.image, image2.rect, (mp_rect){300, 200, 300, 300});
mg_flush(); mg_render(surface, canvas);
mg_surface_present(surface); mg_surface_present(surface);
mem_arena_clear(mem_scratch()); mem_arena_clear(mem_scratch());

View File

@ -81,7 +81,7 @@ int main()
mg_set_color_rgba(1, 1, 1, 1); mg_set_color_rgba(1, 1, 1, 1);
/*
mg_matrix_push((mg_mat2x3){0.707, -0.707, 200, mg_matrix_push((mg_mat2x3){0.707, -0.707, 200,
0.707, 0.707, 100}); 0.707, 0.707, 100});
mg_set_image(image); mg_set_image(image);
@ -99,6 +99,9 @@ int main()
mg_matrix_pop(); mg_matrix_pop();
mg_image_draw(image2, (mp_rect){300, 200, 300, 300}); mg_image_draw(image2, (mp_rect){300, 200, 300, 300});
*/
mg_image_draw(image, (mp_rect){100, 100, 300, 300});
mg_image_draw(image2, (mp_rect){300, 200, 300, 300});
mg_render(surface, canvas); mg_render(surface, canvas);
mg_surface_present(surface); mg_surface_present(surface);

View File

@ -981,6 +981,16 @@ void mg_gl_render_batch(mg_gl_canvas_backend* backend,
glUniform1f(1, scale); glUniform1f(1, scale);
glUniform1i(2, pathCount); glUniform1i(2, pathCount);
// if there's an image, don't cull solid tiles
if(image)
{
glUniform1i(3, 0);
}
else
{
glUniform1i(3, 1);
}
glDispatchCompute(nTilesX, nTilesY, 1); glDispatchCompute(nTilesX, nTilesY, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
@ -1238,7 +1248,7 @@ mg_image_data* mg_gl_canvas_image_create(mg_canvas_backend* interface, vec2 size
{ {
glGenTextures(1, &image->texture); glGenTextures(1, &image->texture);
glBindTexture(GL_TEXTURE_2D, image->texture); glBindTexture(GL_TEXTURE_2D, image->texture);
// glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, size.x, size.y); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, size.x, size.y);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@ -1263,7 +1273,7 @@ void mg_gl_canvas_image_upload_region(mg_canvas_backend* interface,
//TODO: check that this image belongs to this context //TODO: check that this image belongs to this context
mg_gl_image* image = (mg_gl_image*)imageInterface; mg_gl_image* image = (mg_gl_image*)imageInterface;
glBindTexture(GL_TEXTURE_2D, image->texture); glBindTexture(GL_TEXTURE_2D, image->texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, region.w, region.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); glTexSubImage2D(GL_TEXTURE_2D, 0, region.x, region.y, region.w, region.h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
} }
//-------------------------------------------------------------------- //--------------------------------------------------------------------

View File

@ -37,6 +37,7 @@ layout(binding = 5) restrict writeonly buffer screenTilesBufferSSBO
layout(location = 0) uniform int tileSize; layout(location = 0) uniform int tileSize;
layout(location = 1) uniform float scale; layout(location = 1) uniform float scale;
layout(location = 2) uniform int pathCount; layout(location = 2) uniform int pathCount;
layout(location = 3) uniform int cullSolidTiles;
void main() void main()
{ {
@ -89,6 +90,7 @@ void main()
if( lastOpIndex < 0 if( lastOpIndex < 0
||(pathBuffer.elements[pathIndex].color.a == 1 ||(pathBuffer.elements[pathIndex].color.a == 1
&& cullSolidTiles != 0
&& tileBox.x >= clip.x && tileBox.x >= clip.x
&& tileBox.z < clip.z && tileBox.z < clip.z
&& tileBox.y >= clip.y && tileBox.y >= clip.y

View File

@ -237,9 +237,9 @@ MP_API vec2 mg_image_size(mg_image image);
//NOTE: rectangle allocator //NOTE: rectangle allocator
typedef struct mg_rect_atlas mg_rect_atlas; typedef struct mg_rect_atlas mg_rect_atlas;
mg_rect_atlas* mg_rect_atlas_create(mem_arena* arena, i32 width, i32 height); MP_API mg_rect_atlas* mg_rect_atlas_create(mem_arena* arena, i32 width, i32 height);
mp_rect mg_rect_atlas_alloc(mg_rect_atlas* atlas, i32 width, i32 height); MP_API mp_rect mg_rect_atlas_alloc(mg_rect_atlas* atlas, i32 width, i32 height);
void mg_rect_atlas_recycle(mg_rect_atlas* atlas, mp_rect rect); MP_API void mg_rect_atlas_recycle(mg_rect_atlas* atlas, mp_rect rect);
//NOTE: image atlas helpers //NOTE: image atlas helpers
typedef struct mg_image_region typedef struct mg_image_region
@ -248,10 +248,10 @@ typedef struct mg_image_region
mp_rect rect; mp_rect rect;
} mg_image_region; } mg_image_region;
mg_image_region mg_image_atlas_alloc_from_rgba8(mg_rect_atlas* atlas, mg_image backingImage, u32 width, u32 height, u8* pixels); MP_API mg_image_region mg_image_atlas_alloc_from_rgba8(mg_rect_atlas* atlas, mg_image backingImage, u32 width, u32 height, u8* pixels);
mg_image_region mg_image_atlas_alloc_from_data(mg_rect_atlas* atlas, mg_image backingImage, str8 data, bool flip); MP_API mg_image_region mg_image_atlas_alloc_from_data(mg_rect_atlas* atlas, mg_image backingImage, str8 data, bool flip);
mg_image_region mg_image_atlas_alloc_from_file(mg_rect_atlas* atlas, mg_image backingImage, str8 path, bool flip); MP_API mg_image_region mg_image_atlas_alloc_from_file(mg_rect_atlas* atlas, mg_image backingImage, str8 path, bool flip);
void mg_image_atlas_recycle(mg_rect_atlas* atlas, mg_image_region imageRgn); MP_API void mg_image_atlas_recycle(mg_rect_atlas* atlas, mg_image_region imageRgn);
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
//NOTE(martin): transform, viewport and clipping //NOTE(martin): transform, viewport and clipping