Files
space_game/shaders/vs_cubes.sc
2026-01-29 17:06:34 +01:00

98 lines
3.5 KiB
Python

$input i_data0, i_data1, i_data2, i_data3
$output tex_coord, world_pos
#include "verts.sh"
#include "bgfx_shader.sh"
#include "shaderlib.sh"
// ATTENTION must match config.h
// VertexID Layout for texturing (not used)
// Bits: 19 | 3 | 10
// Data: texture index | texture corner | vertex index
const uint vertex_index_bits = 10;
const uint vertex_index_mask = (1 << vertex_index_bits) - 1;
const uint texture_corner_bits = 3;
const uint texture_corner_mask = (1 << texture_corner_bits) - 1;
const uint texture_index_bits = 19;
const uint texture_index_mask = (1 << texture_index_bits) - 1;
const uint TA_WIDTH = 2; // patches in atlas
const uint TA_HEIGHT = 2; // patches in atlas
const uint TA_MAIN_PATCH_SIZE = 8; // textures per patch
const uint TA_TEXTURE_SIZE = 4; // texels per texture
const uint TA_TEXTURES_PER_ROW = TA_WIDTH * TA_MAIN_PATCH_SIZE;
const uint TA_TEXTURES_PER_COL = TA_HEIGHT * TA_MAIN_PATCH_SIZE;
const float texel_stride_x = 1.0f / (TA_WIDTH * TA_MAIN_PATCH_SIZE * TA_TEXTURE_SIZE);
const float texel_stride_y = 1.0f / (TA_HEIGHT * TA_MAIN_PATCH_SIZE * TA_TEXTURE_SIZE);
const float texture_stride_x = 1.0f / TA_TEXTURES_PER_ROW;
const float texture_stride_y = 1.0f / TA_TEXTURES_PER_ROW;
const vec2 corner_offsets[8] =
{
{ 0.000000, 0.000000}, // TopLeft
{ 0.000000, 2*texel_stride_y}, // CenterLeft
{ 0.000000, 4*texel_stride_y}, // BotLeft
{ 2*texel_stride_x, 4*texel_stride_y}, // CenterBot
{ 4*texel_stride_x, 4*texel_stride_y}, // BotRight
{ 4*texel_stride_x, 2*texel_stride_y}, // CenterRight
{ 4*texel_stride_x, 0.000000}, // TopRight
{ 2*texel_stride_x, 0.000000}, // CenterTop
};
// Texturing rework V2
const uint IA_WIDTH = 2*8; // id's per row
const uint IA_HEIGHT = 2*8; // id's per column
const float ID_STRIDE_X = 1.0f / IA_WIDTH;
const float ID_STRIDE_Y = 1.0f / IA_HEIGHT;
const vec2 ID_CORNER_OFFSETS[8] =
{
{ 0.0, 0.0}, // TopLeft
{ 0.0, 0.5*ID_STRIDE_Y}, // CenterLeft
{ 0.0, ID_STRIDE_Y}, // BotLeft
{ 0.5*ID_STRIDE_X, ID_STRIDE_Y}, // CenterBot
{ ID_STRIDE_X, ID_STRIDE_Y}, // BotRight
{ ID_STRIDE_X, 0.5*ID_STRIDE_Y}, // CenterRight
{ ID_STRIDE_X, 0.0}, // TopRight
{ 0.5*ID_STRIDE_X, 0.0}, // CenterTop
};
void main()
{
uint index = uint(gl_VertexID); // gl_VertexIndex ??
// Calculate vertex coordinates
uint vert_index = index & vertex_index_mask;
mat4 model_mtx = mtxFromCols(i_data0, i_data1, i_data2, i_data3);
vec4 worldPos = mul(model_mtx, vec4(verts[vert_index], 1.0));
gl_Position = mul(u_viewProj, worldPos);
world_pos = (worldPos.xyz); // pass world pos to fs
// // Calculate texture coordinates
// uint texture_corner = (index >> vertex_index_bits) & texture_corner_mask;
// uint texture_index = (index >> (texture_corner_bits + vertex_index_bits)) & texture_index_mask;
// uint texture_x = texture_index % TA_TEXTURES_PER_ROW;
// uint texture_y = texture_index / TA_TEXTURES_PER_ROW;
// tex_coord = vec2(texture_x * texture_stride_x, texture_y * texture_stride_y);
// tex_coord = tex_coord + corner_offsets[texture_corner];
// Texturing rework V2
// split index bits
uint texture_corner = (index >> vertex_index_bits) & texture_corner_mask;
uint id_index = (index >> (texture_corner_bits + vertex_index_bits)) & texture_index_mask;
// Calc id_coords (in id_atlas, for sampling [tex_id,light_id])
// in [0;ID_ATLAS_SIZE]
uint id_x = id_index % IA_WIDTH;
uint id_y = id_index / IA_HEIGHT;
// TODO rename tex_coord->id_coord
tex_coord = vec2(id_x * ID_STRIDE_X, id_y * ID_STRIDE_Y);
tex_coord = tex_coord + ID_CORNER_OFFSETS[texture_corner];
}