Map shader base, add memory util.

This commit is contained in:
2025-03-02 17:29:00 -06:00
parent 639da129f6
commit 122997c58c
35 changed files with 511 additions and 75 deletions

View File

@ -14,4 +14,5 @@ target_sources(${DUSK_TARGET_NAME}
# Subdirs
add_subdirectory(data)
add_subdirectory(entityshader)
add_subdirectory(mapshader)
add_subdirectory(fragments)

View File

@ -8,4 +8,5 @@ target_sources(${DUSK_TARGET_NAME}
PRIVATE
transforms.c
entities.c
map.c
)

View File

@ -8,6 +8,7 @@
#include "entities.h"
#include "overworld/overworld.h"
#include "assert/assert.h"
#include "util/memory.h"
shaderbuffer_t ENTITIES_BUFFER;
entitiesdata_t ENTITIES_DATA;
@ -27,13 +28,12 @@ void entitiesUpdate() {
entitiesdataent_t *dst = &ENTITIES_DATA.entities[i];
// Copy position data.
assertMemoryRangeMatches(
memoryCopyRangeSafe(
&dst->position,
&src->x,
&src->direction,
sizeof(uint_t),
"Entity data is not packed correctly any more."
&src->subY + sizeof(uint8_t),
sizeof(uint_t)
);
memcpy(&dst->position, &src->x, sizeof(uint_t));
}
}

View File

@ -0,0 +1,43 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "map.h"
#include "util/memory.h"
shaderbuffer_t MAP_BUFFER;
mapdata_t MAP_DATA;
void mapInit() {
memset(&MAP_DATA, 0, sizeof(mapdata_t));
shaderBufferInit(&MAP_BUFFER, sizeof(mapdata_t));
}
void mapUpdate() {
// Copy map size.
memoryCopyRangeSafe(
&MAP_DATA.mapSize,
&OVERWORLD.mapWidth,
&OVERWORLD.mapHeight + sizeof(uint8_t),
sizeof(uint_t)
);
// Copy tile ids.
memoryCopyRangeSafe(
&MAP_DATA.tileIds,
&OVERWORLD.tileIds[0],
&OVERWORLD.tileIds[OVERWORLD.mapWidth * OVERWORLD.mapHeight],
OVERWORLD_TILE_COUNT_MAX * sizeof(tileid_t)
);
shaderBufferBind(&MAP_BUFFER);
shaderBufferSetData(&MAP_BUFFER, &MAP_DATA);
}
void mapDispose() {
shaderBufferDispose(&MAP_BUFFER);
}

View File

@ -0,0 +1,42 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "../../../../dusk/overworld/overworlddefs.h"
#include "../fragments/packed.glsl"
#include "../fragments/quad.glsl"
#define MAP_PACKED_SIZE 4
struct TileData {
uint nothing;
uvec3 _padding0;
};
layout(std140) uniform b_Map {
uint mapSize;
uint mapTileIds[OVERWORLD_TILE_COUNT_MAX / MAP_PACKED_SIZE];
TileData mapTileData[OVERWORLD_TILE_COUNT_MAX];
uvec3 _padding0;
};
vec2 mapTileGetSize() {
return vec2(float(OVERWORLD_TILE_WIDTH), float(OVERWORLD_TILE_HEIGHT));
}
vec2 mapGetVertice(uint instanceIndex, uint indiceIndex) {
vec2 quad = quadGetVertice(indiceIndex);
uint mapWidth = packedGetU8(0u, mapSize);
uint mapHeight = packedGetU8(1u, mapSize);
quad += vec2(
float(instanceIndex % mapWidth),
float(instanceIndex / mapWidth)
);
quad *= mapTileGetSize();
return quad;
}

View File

@ -0,0 +1,44 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma ocne
#include "display/shader/shaderbuffer.h"
#include "overworld/overworld.h"
#define MAP_BLOCK_NAME "b_Map"
#define MAP_PACKED_SIZE 4
typedef struct {
uint32_t nothing;
uvec3_t _padding0;
} maptiledata_t;
typedef struct {
uint_t mapSize;
uvec3_t _padding0;
uint32_t tileIds[OVERWORLD_TILE_COUNT_MAX / MAP_PACKED_SIZE];
maptiledata_t tileData[OVERWORLD_TILE_COUNT_MAX];
} mapdata_t;
extern shaderbuffer_t MAP_BUFFER;
extern mapdata_t MAP_DATA;
/**
* Initializes the map buffer and data.
*/
void mapInit();
/**
* Updates the map buffer with the current data.
*/
void mapUpdate();
/**
* Destroys the map buffer.
*/
void mapDispose();

View File

@ -12,7 +12,7 @@ out vec2 v_TextureCoord;
void main() {
uint instanceIndex = uint(gl_InstanceID);
uint indiceIndex = uint(gl_VertexID % 6);
uint indiceIndex = quadGetIndiceIndex(gl_VertexID);
vec2 vert = entityGetVertice(instanceIndex, indiceIndex);
vec2 uv = quadGetTextureCoordinate(indiceIndex);

View File

@ -7,6 +7,7 @@
#include "assert/assert.h"
#include "assert/assertgl.h"
#include "util/memory.h"
#include "entityshader.h"
#include "entity_vert.glsl.h"
#include "entity_frag.glsl.h"
@ -16,7 +17,7 @@
entityshader_t ENTITY_SHADER;
void entityShaderInit() {
memset(&ENTITY_SHADER, 0, sizeof(entityshader_t));
memoryZero(&ENTITY_SHADER, sizeof(entityshader_t));
shaderInit(
&ENTITY_SHADER.shader,

View File

@ -3,6 +3,14 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
uint quadGetIndiceIndex(uint vertexId) {
return vertexId % 6u;
}
uint quadGetIndiceIndex(int vertexId) {
return quadGetIndiceIndex(uint(vertexId));
}
vec2 quadGetVertice(uint indiceIndex) {
vec2 vert = vec2(0, 0);

View File

@ -0,0 +1,14 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Shaders
glsltool(map_vert.glsl)
glsltool(map_frag.glsl)
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
mapshader.c
)

View File

@ -0,0 +1,16 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "../fragments/header.glsl"
// Inputs from vertex shader
in vec2 v_TextureCoord;
// Frag pixel color
out vec4 FragColor;
void main() {
FragColor = vec4(1, 0, 0, 1);
}

View File

@ -0,0 +1,22 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "../fragments/header.glsl"
#include "../data/transforms.glsl"
#include "../data/map.glsl"
// Outputs to fragment shader
out vec2 v_TextureCoord;
void main() {
uint instanceIndex = uint(gl_InstanceID);
uint indiceIndex = quadGetIndiceIndex(gl_VertexID);
vec2 vert = mapGetVertice(instanceIndex, indiceIndex);
vec2 uv = quadGetTextureCoordinate(indiceIndex);
gl_Position = transforms.projection * transforms.view * vec4(vert, 0.0, 1.0);
v_TextureCoord = uv;
}

View File

@ -0,0 +1,50 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "mapshader.h"
#include "util/memory.h"
#include "display/shader/data/map.h"
#include "display/shader/data/transforms.h"
#include "map_vert.glsl.h"
#include "map_frag.glsl.h"
mapshader_t MAP_SHADER;
void mapShaderInit() {
memoryZero(&MAP_SHADER, sizeof(mapshader_t));
shaderInit(
&MAP_SHADER.shader,
map_vertShaderSource,
map_fragShaderSource
);
// Uniform blocks
MAP_SHADER.mapBlock = shaderGetBlock(
&MAP_SHADER.shader,
MAP_BLOCK_NAME
);
MAP_SHADER.transformsBlock = shaderGetBlock(
&MAP_SHADER.shader,
TRANSFORMS_BLOCK_NAME
);
// Bind
shaderBufferBindToBlock(&MAP_BUFFER, MAP_SHADER.mapBlock);
shaderBufferBindToBlock(&TRANSFORMS_BUFFER, MAP_SHADER.transformsBlock);
}
void mapShaderUse() {
shaderUse(&MAP_SHADER.shader);
shaderBufferBindToBlock(&MAP_BUFFER, MAP_SHADER.mapBlock);
shaderBufferBindToBlock(&TRANSFORMS_BUFFER, MAP_SHADER.transformsBlock);
}
void mapShaderDispose() {
shaderDispose(&MAP_SHADER.shader);
}

View File

@ -0,0 +1,32 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/shader/shader.h"
typedef struct {
shader_t shader;
GLuint mapBlock;
GLuint transformsBlock;
} mapshader_t;
extern mapshader_t MAP_SHADER;
/**
* Initializes the map shader.
*/
void mapShaderInit();
/**
* Uses the map shader.
*/
void mapShaderUse();
/**
* Destroys the map shader.
*/
void mapShaderDispose();

View File

@ -9,16 +9,20 @@
#include "assert/assert.h"
#include "display/shader/data/transforms.h"
#include "display/shader/data/entities.h"
#include "display/shader/data/map.h"
#include "display/shader/entityshader/entityshader.h"
#include "display/shader/mapshader/mapshader.h"
shadermanagerdatacallback_t SHADER_MANAGER_DATA_CALLBACKS[] = {
{ transformsInit, transformsUpdate, transformsDispose },
{ entitiesInit, entitiesUpdate, entitiesDispose }
{ entitiesInit, entitiesUpdate, entitiesDispose },
{ mapInit, mapUpdate, mapDispose }
};
shadermanagershadercallback_t SHADER_MANAGER_SHADER_CALLBACKS[] = {
{ entityShaderInit, entityShaderDispose }
{ entityShaderInit, entityShaderDispose },
{ mapShaderInit, mapShaderDispose }
};
void shaderManagerInit() {