video: perform chunk baking on GPU side

isolation_bkp/dynres
Dominik Madarász 2021-07-19 18:03:31 +02:00
parent ee0db9c8c1
commit 7977c60922
6 changed files with 33 additions and 28 deletions

View File

@ -4,5 +4,5 @@
#include "world/blocks.h" #include "world/blocks.h"
#include "assets.h" #include "assets.h"
Image texgen_build_block(uint32_t biome, uint32_t kind); Texture2D texgen_build_block(uint32_t biome, uint32_t kind);
Texture2D texgen_build_sprite(asset_id id); Texture2D texgen_build_sprite(asset_id id);

View File

@ -51,14 +51,14 @@ void DrawRectangleEco(float posX, float posY, float width, float height, Color c
} }
static inline static inline
Image GetBlockImage(uint8_t id) { Texture2D GetBlockImage(uint8_t id) {
return *(Image*)blocks_get_img(id); return *(Texture2D*)blocks_get_img(id);
} }
static inline static inline
Texture2D GetChunkTexture(uint64_t id) { RenderTexture2D GetChunkTexture(uint64_t id) {
Texture2D *tex = (Texture2D*)blocks_get_chunk_tex(id); RenderTexture2D *tex = (RenderTexture2D*)blocks_get_chunk_tex(id);
if (!tex) return (Texture2D){0}; if (!tex) return (RenderTexture2D){0};
return *tex; return *tex;
} }

View File

@ -6,13 +6,13 @@
#include "zpl.h" #include "zpl.h"
static inline static inline
Image LoadImageEco(const char *name) { Texture2D LoadImageEco(const char *name) {
static char filename[128]; static char filename[128];
zpl_snprintf(filename, 128, "art/gen/%s.png", name); zpl_snprintf(filename, 128, "art/gen/%s.png", name);
return LoadImage(filename); return LoadTexture(filename);
} }
Image texgen_build_block(uint32_t biome, uint32_t kind) { Texture2D texgen_build_block(uint32_t biome, uint32_t kind) {
// TODO(zaklaus): // TODO(zaklaus):
switch (biome) { switch (biome) {
@ -38,7 +38,10 @@ Image texgen_build_block(uint32_t biome, uint32_t kind) {
} }
} }
return GenImageColor(WORLD_BLOCK_SIZE,WORLD_BLOCK_SIZE,ColorFromHSV(biome+kind*30, 0.13f, 0.89f)); Image img = GenImageColor(WORLD_BLOCK_SIZE,WORLD_BLOCK_SIZE,ColorFromHSV(biome+kind*30, 0.13f, 0.89f));
Texture2D tex = LoadTextureFromImage(img);
UnloadImage(img);
return tex;
} }
Texture2D texgen_build_sprite(asset_id id) { Texture2D texgen_build_sprite(asset_id id) {

View File

@ -168,8 +168,8 @@ void DEBUG_draw_ground(uint64_t key, entity_view * data) {
float x = data->x * size + offset; float x = data->x * size + offset;
float y = data->y * size + offset; float y = data->y * size + offset;
Texture2D tex = GetChunkTexture(key); RenderTexture2D tex = GetChunkTexture(key);
DrawTextureEx(tex, (Vector2){x, y}, 0.0f, 1.0f, ColorAlpha(WHITE, data->tran_time)); DrawTextureEx(tex.texture, (Vector2){x, y}, 0.0f, 1.0f, ColorAlpha(WHITE, data->tran_time));
if (zoom_overlay_tran > 0.02f) { if (zoom_overlay_tran > 0.02f) {
DrawRectangleEco(x, y, size-offset, size-offset, ColorAlpha(ColorFromHSV(key*x, 0.13f, 0.89f), data->tran_time*zoom_overlay_tran*0.75f)); DrawRectangleEco(x, y, size-offset, size-offset, ColorAlpha(ColorFromHSV(key*x, 0.13f, 0.89f), data->tran_time*zoom_overlay_tran*0.75f));

View File

@ -8,13 +8,13 @@
#define BLOCKS_COUNT (sizeof(blocks)/sizeof(block)) #define BLOCKS_COUNT (sizeof(blocks)/sizeof(block))
ZPL_TABLE(static, blocks__chunk_tbl, blocks__chunk_tbl_, Texture2D); ZPL_TABLE(static, blocks__chunk_tbl, blocks__chunk_tbl_, RenderTexture2D);
static blocks__chunk_tbl baked_chunks; static blocks__chunk_tbl baked_chunks;
static void chunks_unload_textures(uint64_t key, Texture2D *value) { static void chunks_unload_textures(uint64_t key, RenderTexture2D *value) {
(void)key; (void)key;
UnloadTexture(*value); UnloadRenderTexture(*value);
} }
typedef struct { typedef struct {
@ -25,7 +25,7 @@ typedef struct {
char symbol; char symbol;
// NOTE(zaklaus): viewer data // NOTE(zaklaus): viewer data
Image img; Texture2D img;
} block; } block;
#include "blocks_list.c" #include "blocks_list.c"
@ -42,7 +42,7 @@ int32_t blocks_setup(void) {
void blocks_destroy(void) { void blocks_destroy(void) {
for (uint32_t i=0; i<BLOCKS_COUNT; i++) { for (uint32_t i=0; i<BLOCKS_COUNT; i++) {
UnloadImage(blocks[i].img); UnloadTexture(blocks[i].img);
} }
blocks__chunk_tbl_map_mut(&baked_chunks, chunks_unload_textures); blocks__chunk_tbl_map_mut(&baked_chunks, chunks_unload_textures);
@ -85,19 +85,21 @@ void blocks_build_chunk_tex(uint64_t id, uint8_t *chunk_blocks, size_t blocks_le
(void)blocks_len; (void)blocks_len;
world_view *view = (world_view*)raw_view; world_view *view = (world_view*)raw_view;
uint16_t dims = WORLD_BLOCK_SIZE * view->chunk_size; uint16_t dims = WORLD_BLOCK_SIZE * view->chunk_size;
Image canvas = GenImageColor(dims, dims, RAYWHITE); RenderTexture2D canvas = LoadRenderTexture(dims, dims);
BeginTextureMode(canvas);
ClearBackground(WHITE);
for (int y = 0; y < view->chunk_size; y += 1) { for (int y = 0; y < view->chunk_size; y += 1) {
for (int x = 0; x < view->chunk_size; x += 1) { for (int x = 0; x < view->chunk_size; x += 1) {
Image blk = blocks[chunk_blocks[(y*view->chunk_size)+x]].img; #if 1
Texture2D blk = blocks[chunk_blocks[(y*view->chunk_size)+x]].img;
Rectangle src = {0, 0, WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE}; Rectangle src = {0, 0, WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE};
Rectangle dst = {x*WORLD_BLOCK_SIZE, y*WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE}; Rectangle dst = {x*WORLD_BLOCK_SIZE, y*WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE};
ImageDraw(&canvas, blk, src, dst, WHITE); DrawTexturePro(blk, src, dst, (Vector2){0.0f,0.0f}, 0.0f, WHITE);
#endif
} }
} }
EndTextureMode();
Texture2D tex = LoadTextureFromImage(canvas); blocks__chunk_tbl_set(&baked_chunks, id, canvas);
UnloadImage(canvas);
blocks__chunk_tbl_set(&baked_chunks, id, tex);
} }
void *blocks_get_chunk_tex(uint64_t id) { void *blocks_get_chunk_tex(uint64_t id) {
@ -105,6 +107,6 @@ void *blocks_get_chunk_tex(uint64_t id) {
} }
void blocks_remove_chunk_tex(uint64_t id) { void blocks_remove_chunk_tex(uint64_t id) {
UnloadTexture(*blocks__chunk_tbl_get(&baked_chunks, id)); UnloadRenderTexture(*blocks__chunk_tbl_get(&baked_chunks, id));
blocks__chunk_tbl_remove(&baked_chunks, id); blocks__chunk_tbl_remove(&baked_chunks, id);
} }

View File

@ -222,9 +222,9 @@ int32_t world_update() {
ecs_progress(world.ecs, 0.0f); ecs_progress(world.ecs, 0.0f);
} }
world_tracker_update(0, WORLD_TRACKER_UPDATE_FAST_MS, 1); world_tracker_update(0, WORLD_TRACKER_UPDATE_FAST_MS, 2);
world_tracker_update(1, WORLD_TRACKER_UPDATE_NORMAL_MS, 2); world_tracker_update(1, WORLD_TRACKER_UPDATE_NORMAL_MS, 4);
world_tracker_update(2, WORLD_TRACKER_UPDATE_SLOW_MS, 3); world_tracker_update(2, WORLD_TRACKER_UPDATE_SLOW_MS, 6);
return 0; return 0;
} }