From 2d55ef6f66263458ca32b73f14f436eb1a14e6d3 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 29 Sep 2024 02:12:15 +0200 Subject: [PATCH] testing: started rendering debug information / ui --- asset/AssetManagementSystem.h | 6 +++ camera/Camera.h | 25 +++++++++++++ gpuapi/opengl/OpenglUtils.h | 38 +++++++++++++++++-- gpuapi/opengl/OpenglWin32.h | 4 ++ image/Png.h | 28 ++++++++------ image/default_colors.h | 39 ++++++++++--------- input/ControllerInput.h | 6 ++- input/Input.h | 1 - math/matrix/MatrixFloat32.h | 70 +++++++++++++++++------------------ memory/BufferMemory.h | 11 +++++- memory/ChunkMemory.h | 2 +- memory/DebugMemory.h | 10 ++--- memory/RingMemory.h | 19 +++++++--- object/Texture.h | 1 - object/Vertex.h | 36 ++++++++++++++---- utils/MathUtils.h | 1 + utils/SystemInfo.h | 52 +++++++++++++------------- 17 files changed, 231 insertions(+), 118 deletions(-) diff --git a/asset/AssetManagementSystem.h b/asset/AssetManagementSystem.h index dbf2f96..f7baac0 100644 --- a/asset/AssetManagementSystem.h +++ b/asset/AssetManagementSystem.h @@ -62,6 +62,12 @@ void ams_create(AssetManagementSystem* ams, BufferMemory* buf, int chunk_size, i ams->last = NULL; } +inline +int32 ams_calculate_chunks(AssetManagementSystem* ams, int32 byte_size) +{ + return (int32) CEIL_DIV(byte_size, ams->asset_data_memory.chunk_size); +} + inline int64 ams_get_buffer_size(int count, int chunk_size) { diff --git a/camera/Camera.h b/camera/Camera.h index 3b02339..85d24a7 100644 --- a/camera/Camera.h +++ b/camera/Camera.h @@ -202,6 +202,31 @@ void camera_movement(Camera* camera, CameraMovement* movement, float dt, bool re } } +// @question should we remove the fov for this or not, fov shouldn't matter for UI!? +inline +void camera_orth_matrix_lh(const Camera* __restrict camera, float* __restrict orth) +{ + mat4_identity_sparse(orth); + + float ymax, xmax; + ymax = camera->znear * tanf(camera->fov * 0.5f); + xmax = ymax * camera->aspect; + + mat4_ortho_sparse_lh(orth, -xmax, xmax, -ymax, ymax, camera->znear, camera->zfar); +} + +inline +void camera_orth_matrix_rh(const Camera* __restrict camera, float* __restrict orth) +{ + mat4_identity_sparse(orth); + + float ymax, xmax; + ymax = camera->znear * tanf(camera->fov * 0.5f); + xmax = ymax * camera->aspect; + + mat4_ortho_sparse_rh(orth, -xmax, xmax, -ymax, ymax, camera->znear, camera->zfar); +} + inline void camera_projection_matrix_lh(const Camera* __restrict camera, float* __restrict projection) { diff --git a/gpuapi/opengl/OpenglUtils.h b/gpuapi/opengl/OpenglUtils.h index 7bebe3f..5742214 100644 --- a/gpuapi/opengl/OpenglUtils.h +++ b/gpuapi/opengl/OpenglUtils.h @@ -137,10 +137,17 @@ void load_texture_to_gpu(const Texture* texture, int mipmap_level = 0) inline void texture_use(const Texture* texture, uint32 texture_unit) { - glActiveTexture(GL_TEXTURE0 + texture_unit); + glActiveTexture(GL_TEXTURE0 + texture->sample_id); glBindTexture(GL_TEXTURE_2D, (GLuint) texture->id); } +inline +void texture_use_1D(const Texture* texture, uint32 texture_unit) +{ + glActiveTexture(GL_TEXTURE0 + texture->sample_id); + glBindTexture(GL_TEXTURE_1D, (GLuint) texture->id); +} + GLuint shader_make(GLenum type, const char *source, RingMemory* ring) { GLuint shader = glCreateShader(type); @@ -425,7 +432,7 @@ int calculate_face_size(int components, int faces) // generates faces // data is no longer needed after this inline -uint32 gpuapi_buffer_generate(int size, void* data) +uint32 gpuapi_buffer_generate(int size, const void* data) { uint32 vbo; @@ -437,7 +444,7 @@ uint32 gpuapi_buffer_generate(int size, void* data) } inline -uint32 gpuapi_shaderbuffer_generate(int size, void* data) +uint32 gpuapi_shaderbuffer_generate(int size, const void* data) { uint32 sbo; @@ -448,8 +455,25 @@ uint32 gpuapi_shaderbuffer_generate(int size, void* data) return sbo; } +uint32 gpuapi_upload_color_palette(const byte* palette, int count, int sampler_id) +{ + uint32 texture_id; + + glGenTextures(1, &texture_id); + glActiveTexture(GL_TEXTURE0 + sampler_id); + glBindTexture(GL_TEXTURE_1D, texture_id); + + glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, count, 0, GL_RGBA, GL_UNSIGNED_BYTE, palette); + + glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + return texture_id; +} + inline -uint32 gpuapi_uniformbuffer_generate(int size, void* data) +uint32 gpuapi_uniformbuffer_generate(int size, const void* data) { uint32 ubo; @@ -495,6 +519,12 @@ void gpuapi_buffer_delete(GLuint buffer) glDeleteBuffers(1, &buffer); } +inline +void gpuapi_vertex_array_delete(GLuint buffer) +{ + glDeleteVertexArrays(1, &buffer); +} + int get_gpu_free_memory() { GLint available = 0; diff --git a/gpuapi/opengl/OpenglWin32.h b/gpuapi/opengl/OpenglWin32.h index d8d953e..c593506 100644 --- a/gpuapi/opengl/OpenglWin32.h +++ b/gpuapi/opengl/OpenglWin32.h @@ -1172,6 +1172,9 @@ static type_glActiveTexture* glActiveTexture; typedef void WINAPI type_glDeleteProgram(GLuint program); static type_glDeleteProgram* glDeleteProgram; +typedef void WINAPI type_glDeleteVertexArrays(GLsizei n, const GLuint* arrays); +static type_glDeleteVertexArrays* glDeleteVertexArrays; + typedef void WINAPI type_glDeleteShader(GLuint shader); static type_glDeleteShader* glDeleteShader; @@ -1482,6 +1485,7 @@ void opengl_init_gl() glBufferData = (type_glBufferData *) wglGetProcAddress("glBufferData"); glActiveTexture = (type_glActiveTexture *) wglGetProcAddress("glActiveTexture"); glDeleteProgram = (type_glDeleteProgram *) wglGetProcAddress("glDeleteProgram"); + glDeleteVertexArrays = (type_glDeleteVertexArrays *) wglGetProcAddress("glDeleteVertexArrays"); glDeleteShader = (type_glDeleteShader *) wglGetProcAddress("glDeleteShader"); glDeleteFramebuffers = (type_glDeleteFramebuffers *) wglGetProcAddress("glDeleteFramebuffers"); glDrawBuffers = (type_glDrawBuffers *) wglGetProcAddress("glDrawBuffers"); diff --git a/image/Png.h b/image/Png.h index d39955f..ba96390 100644 --- a/image/Png.h +++ b/image/Png.h @@ -553,13 +553,13 @@ bool image_png_generate(const FileBody* src_data, Image* image, RingMemory* ring chunk.length = SWAP_ENDIAN_BIG(*((uint32 *) stream.pos)); stream.pos += sizeof(chunk.length); - chunk.type = *((uint32 *) stream.pos); + chunk.type = SWAP_ENDIAN_BIG(*((uint32 *) stream.pos)); stream.pos += sizeof(chunk.type); - if (chunk.type == SWAP_ENDIAN_BIG('IEND')) { + if (chunk.type == 'IEND') { // we arrived at the end of the file break; - } else if (chunk.type == SWAP_ENDIAN_BIG('PLTE')) { + } else if (chunk.type == 'PLTE') { // @todo change so that the tRANS directly sets the alpha for the respective color // This means we increase the palette by 1 byte per index (chunk.length/3*4) palette = ring_get_memory(ring, chunk.length); @@ -568,7 +568,7 @@ bool image_png_generate(const FileBody* src_data, Image* image, RingMemory* ring stream.pos += chunk.length + sizeof(chunk.crc); continue; - } else if (chunk.type == SWAP_ENDIAN_BIG('tRNS')) { + } else if (chunk.type == 'tRNS') { // @todo remove, we can store this information directly in the palette transparency.values = ring_get_memory(ring, chunk.length); memcpy(transparency.values, stream.pos, chunk.length); @@ -584,7 +584,7 @@ bool image_png_generate(const FileBody* src_data, Image* image, RingMemory* ring stream.pos += chunk.length + sizeof(chunk.crc); continue; - } else if (chunk.type != SWAP_ENDIAN_BIG('IDAT')) { + } else if (chunk.type != 'IDAT') { // some other data // Jump to next chunk @@ -602,6 +602,7 @@ bool image_png_generate(const FileBody* src_data, Image* image, RingMemory* ring // // This means we cannot jump here (or better we need to check if the bit position is != 0) // BUT WE MIGHT NOT CARE ABOUT MULTIPLE IDAT CHUNKS? + idat_header.zlib_method_flag = *stream.pos; ++stream.pos; @@ -617,6 +618,11 @@ bool image_png_generate(const FileBody* src_data, Image* image, RingMemory* ring return false; } + // @todo Potential solution + // We could check how many bytes remain from the old chunk move them forward + // essentially overwriting the **current** chunk header data, which doesn't matter since we already parsed it + // then we reset the pos pointer backwards to where we want to start... gg + // https://www.ietf.org/rfc/rfc1951.txt - defalte // This data might be stored in the prvious IDAT chunk?! BFINAL = (uint8) BITS_GET_8_R2L(*stream.pos, stream.bit_pos, 1); @@ -733,7 +739,7 @@ bool image_png_generate(const FileBody* src_data, Image* image, RingMemory* ring } if (literal_length <= 255) { - *dest++ = (literal_length & 0xFF); + *dest++ = (uint8) (literal_length & 0xFF); } else { uint32 length_tab_index = literal_length - 257; PngHuffmanEntry length_tab = PNG_LENGTH_EXTRA[length_tab_index]; @@ -749,13 +755,13 @@ bool image_png_generate(const FileBody* src_data, Image* image, RingMemory* ring uint32 dist_tab_index = huffman_png_decode(distance_huffman, &stream); - PngHuffmanEntry dist_tab = PNG_DIST_EXTRA[dist_tab_index]; - uint32 dist = dist_tab.symbol; + const PngHuffmanEntry* dist_tab = &PNG_DIST_EXTRA[dist_tab_index]; + uint32 dist = dist_tab->symbol; - if (dist_tab.bits_used) { + if (dist_tab->bits_used) { // @performance If we knew that bits_used is always <= 15 we could use more efficient MERGE/GET - uint32 extra_bits = BITS_GET_32_R2L(BYTES_MERGE_4_R2L(stream.pos), stream.bit_pos, dist_tab.bits_used); - bits_walk(&stream, dist_tab.bits_used); + uint32 extra_bits = BITS_GET_32_R2L(BYTES_MERGE_4_R2L(stream.pos), stream.bit_pos, dist_tab->bits_used); + bits_walk(&stream, dist_tab->bits_used); dist += extra_bits; } diff --git a/image/default_colors.h b/image/default_colors.h index 9c336a6..c6c02d4 100644 --- a/image/default_colors.h +++ b/image/default_colors.h @@ -11,26 +11,29 @@ #include "../stdlib/Types.h" -// @todo some of the cullors are bad +// @todo some of the colors are bad // @todo basically all the dark colors could be made darker -> space out some of the very bright colors a little bit more (see 3rd - 5th column) -const int default_colors_256[256] = { - 0x123456, 0xFFFFFF, 0xF1EEED, 0xDCDEDE, 0xBFC1BF, 0xAEB0AD, 0x9E9D9E, 0x8E8F94, 0x717373, 0x5E5F61, 0x4D4D4C, 0x3F403A, 0x332C2A, 0x241E22, 0x0C1210, 0x000000, - 0xF6D3CF, 0xFABABC, 0xFBA699, 0xF19580, 0xFB837A, 0xFC615A, 0xFF4E4B, 0xFF4040, 0xF72128, 0xF60303, 0xE40000, 0xC70002, 0x900009, 0x630604, 0x4f0107, 0x2d0205, - 0xFDDDCE, 0xFCC5AC, 0xFFB9A4, 0xF6A378, 0xF49A80, 0xFF7D4F, 0xF8742C, 0xFE5A22, 0xF44A0B, 0xDA3A06, 0xC03500, 0x853004, 0x912600, 0x672300, 0x471608, 0x2b0d05, - 0xFCEBCF, 0xFCD8AA, 0xFFCD98, 0xFCC27F, 0xF3B267, 0xFDA660, 0xFD942D, 0xFF8001, 0xDF6E00, 0xCC6B00, 0xA85D00, 0xA55300, 0x734700, 0x612D08, 0x562600, 0x351700, - 0xFFF6C9, 0xFFECA8, 0xFDE884, 0xFADC6D, 0xF8DE6F, 0xFFCF43, 0xFFBD00, 0xEBB800, 0xC1A800, 0xBC8F09, 0x9B7A00, 0x8E6C08, 0x795F01, 0x5C4A00, 0x523B00, 0x392900, - 0xFFFFCD, 0xFDFEAF, 0xFCFBA1, 0xFDFF69, 0xF9FF2A, 0xFFFE04, 0xEFEE00, 0xE0DE00, 0xBDBF13, 0xB4AF00, 0x9F9900, 0x909002, 0x717300, 0x505400, 0x4A4F00, 0x343700, - 0xE4FFBD, 0xDFFEB9, 0xD1FF8F, 0xCAFC84, 0xC0F96B, 0xBAF353, 0x98FB00, 0x9AEE0F, 0x78CE00, 0x74C100, 0x61A401, 0x578A03, 0x4F7E02, 0x3C6200, 0x2E4F00, 0x203700, - 0xE1F8D8, 0xBBFEAD, 0xA2F592, 0xA1F79A, 0x81FF7A, 0x5DFF59, 0x48FF58, 0x00F600, 0x12D31F, 0x0ACB04, 0x10A40A, 0x089811, 0x06780E, 0x05640F, 0x005200, 0x003100, - 0xD5FFF8, 0xBCFFEA, 0xA7FED3, 0x99F2C4, 0x6CFFB5, 0x53F29E, 0x4CFEA1, 0x0AF779, 0x0CD56A, 0x0BC868, 0x01AA50, 0x07A557, 0x008642, 0x075A30, 0x00562C, 0x00331a, - 0xD9FDFE, 0xB3FCFF, 0xACFFFF, 0x90FFFF, 0x76FEFF, 0x5FFAFD, 0x08FEFE, 0x22F3F2, 0x06C9C2, 0x08B2C4, 0x049FA4, 0x078C97, 0x008286, 0x025D5D, 0x005056, 0x003135, - 0xE2F5FF, 0xB4EBFF, 0xA3DAFF, 0x89CCF8, 0x79D1FF, 0x61C3F7, 0x59C5F3, 0x3FB9F6, 0x05A4FA, 0x0592F0, 0x0677B8, 0x00649A, 0x065A77, 0x004974, 0x003154, 0x00243e, - 0xD1DDFF, 0xB5D0EF, 0xA7BFF6, 0x90B6F4, 0x85AAF7, 0x67AAFF, 0x5386FF, 0x437AFF, 0x3A6AFF, 0x044EFC, 0x034EF7, 0x0A37BF, 0x09268B, 0x062871, 0x001D4C, 0x001435, - 0xC7C3FF, 0xC7C6FE, 0x9D9CFF, 0x9194F6, 0x7E81FE, 0x776FF7, 0x595AFF, 0x4852FF, 0x2D2EFF, 0x2020F8, 0x0400E1, 0x0000DD, 0x010097, 0x000086, 0x03005D, 0x020035, - 0xE1D4FF, 0xD8ACFF, 0xCD9BFF, 0xC88DFA, 0xBD8AF9, 0xB160FF, 0xAA52FE, 0x9841FD, 0x8726FF, 0x8700F5, 0x7200F4, 0x5C00B7, 0x460489, 0x350077, 0x28004F, 0x1c0037, - 0xFFC7FF, 0xFFB2FF, 0xFF9AFF, 0xF181F1, 0xFB6FFD, 0xF850FB, 0xFB46FF, 0xF91FFF, 0xF900FF, 0xDD00E6, 0xBF00C7, 0x9B0199, 0xB70090, 0x670362, 0x4F0153, 0x330035, - 0xFDD2E6, 0xF9B5DA, 0xF7A4D4, 0xF198CB, 0xF682BD, 0xFF5FAE, 0xFF4CA9, 0xFF3CA4, 0xFF1A94, 0xF90979, 0xE80071, 0xC40061, 0x96004A, 0x670132, 0x4F0024, 0x310016 +const uint32 default_colors_256[256] = { + 0xFFFFFF00, 0xFFFFFFFF, 0xF1EEEDFF, 0xDCDEDEFF, 0xBFC1BFFF, 0xAEB0ADFF, 0x9E9D9EFF, 0x8E8F94FF, 0x717373FF, 0x5E5F61FF, 0x4D4D4CFF, 0x3F403AFF, 0x332C2AFF, 0x241E22FF, 0x0C1210FF, 0x000000FF, + 0xF6D3CFFF, 0xFABABCFF, 0xFBA699FF, 0xF19580FF, 0xFB837AFF, 0xFC615AFF, 0xFF4E4BFF, 0xFF4040FF, 0xF72128FF, 0xF60303FF, 0xE40000FF, 0xC70002FF, 0x900009FF, 0x630604FF, 0x4f0107FF, 0x2d0205FF, + 0xFDDDCEFF, 0xFCC5ACFF, 0xFFB9A4FF, 0xF6A378FF, 0xF49A80FF, 0xFF7D4FFF, 0xF8742CFF, 0xFE5A22FF, 0xF44A0BFF, 0xDA3A06FF, 0xC03500FF, 0x853004FF, 0x912600FF, 0x672300FF, 0x471608FF, 0x2b0d05FF, + 0xFCEBCFFF, 0xFCD8AAFF, 0xFFCD98FF, 0xFCC27FFF, 0xF3B267FF, 0xFDA660FF, 0xFD942DFF, 0xFF8001FF, 0xDF6E00FF, 0xCC6B00FF, 0xA85D00FF, 0xA55300FF, 0x734700FF, 0x612D08FF, 0x562600FF, 0x351700FF, + 0xFFF6C9FF, 0xFFECA8FF, 0xFDE884FF, 0xFADC6DFF, 0xF8DE6FFF, 0xFFCF43FF, 0xFFBD00FF, 0xEBB800FF, 0xC1A800FF, 0xBC8F09FF, 0x9B7A00FF, 0x8E6C08FF, 0x795F01FF, 0x5C4A00FF, 0x523B00FF, 0x392900FF, + 0xFFFFCDFF, 0xFDFEAFFF, 0xFCFBA1FF, 0xFDFF69FF, 0xF9FF2AFF, 0xFFFE04FF, 0xEFEE00FF, 0xE0DE00FF, 0xBDBF13FF, 0xB4AF00FF, 0x9F9900FF, 0x909002FF, 0x717300FF, 0x505400FF, 0x4A4F00FF, 0x343700FF, + 0xE4FFBDFF, 0xDFFEB9FF, 0xD1FF8FFF, 0xCAFC84FF, 0xC0F96BFF, 0xBAF353FF, 0x98FB00FF, 0x9AEE0FFF, 0x78CE00FF, 0x74C100FF, 0x61A401FF, 0x578A03FF, 0x4F7E02FF, 0x3C6200FF, 0x2E4F00FF, 0x203700FF, + 0xE1F8D8FF, 0xBBFEADFF, 0xA2F592FF, 0xA1F79AFF, 0x81FF7AFF, 0x5DFF59FF, 0x48FF58FF, 0x00F600FF, 0x12D31FFF, 0x0ACB04FF, 0x10A40AFF, 0x089811FF, 0x06780EFF, 0x05640FFF, 0x005200FF, 0x003100FF, + 0xD5FFF8FF, 0xBCFFEAFF, 0xA7FED3FF, 0x99F2C4FF, 0x6CFFB5FF, 0x53F29EFF, 0x4CFEA1FF, 0x0AF779FF, 0x0CD56AFF, 0x0BC868FF, 0x01AA50FF, 0x07A557FF, 0x008642FF, 0x075A30FF, 0x00562CFF, 0x00331aFF, + 0xD9FDFEFF, 0xB3FCFFFF, 0xACFFFFFF, 0x90FFFFFF, 0x76FEFFFF, 0x5FFAFDFF, 0x08FEFEFF, 0x22F3F2FF, 0x06C9C2FF, 0x08B2C4FF, 0x049FA4FF, 0x078C97FF, 0x008286FF, 0x025D5DFF, 0x005056FF, 0x003135FF, + 0xE2F5FFFF, 0xB4EBFFFF, 0xA3DAFFFF, 0x89CCF8FF, 0x79D1FFFF, 0x61C3F7FF, 0x59C5F3FF, 0x3FB9F6FF, 0x05A4FAFF, 0x0592F0FF, 0x0677B8FF, 0x00649AFF, 0x065A77FF, 0x004974FF, 0x003154FF, 0x00243eFF, + 0xD1DDFFFF, 0xB5D0EFFF, 0xA7BFF6FF, 0x90B6F4FF, 0x85AAF7FF, 0x67AAFFFF, 0x5386FFFF, 0x437AFFFF, 0x3A6AFFFF, 0x044EFCFF, 0x034EF7FF, 0x0A37BFFF, 0x09268BFF, 0x062871FF, 0x001D4CFF, 0x001435FF, + 0xC7C3FFFF, 0xC7C6FEFF, 0x9D9CFFFF, 0x9194F6FF, 0x7E81FEFF, 0x776FF7FF, 0x595AFFFF, 0x4852FFFF, 0x2D2EFFFF, 0x2020F8FF, 0x0400E1FF, 0x0000DDFF, 0x010097FF, 0x000086FF, 0x03005DFF, 0x020035FF, + 0xE1D4FFFF, 0xD8ACFFFF, 0xCD9BFFFF, 0xC88DFAFF, 0xBD8AF9FF, 0xB160FFFF, 0xAA52FEFF, 0x9841FDFF, 0x8726FFFF, 0x8700F5FF, 0x7200F4FF, 0x5C00B7FF, 0x460489FF, 0x350077FF, 0x28004FFF, 0x1c0037FF, + 0xFFC7FFFF, 0xFFB2FFFF, 0xFF9AFFFF, 0xF181F1FF, 0xFB6FFDFF, 0xF850FBFF, 0xFB46FFFF, 0xF91FFFFF, 0xF900FFFF, 0xDD00E6FF, 0xBF00C7FF, 0x9B0199FF, 0xB70090FF, 0x670362FF, 0x4F0153FF, 0x330035FF, + 0xFDD2E6FF, 0xF9B5DAFF, 0xF7A4D4FF, 0xF198CBFF, 0xF682BDFF, 0xFF5FAEFF, 0xFF4CA9FF, 0xFF3CA4FF, 0xFF1A94FF, 0xF90979FF, 0xE80071FF, 0xC40061FF, 0x96004AFF, 0x670132FF, 0x4F0024FF, 0x310016FF }; +// @todo Implement (extends the default_colors_256 -> both together form 1024 colors) +const uint32 default_colors_768[768] = {}; + #endif \ No newline at end of file diff --git a/input/ControllerInput.h b/input/ControllerInput.h index d68f258..ecc4fb4 100644 --- a/input/ControllerInput.h +++ b/input/ControllerInput.h @@ -11,6 +11,8 @@ #include "../stdlib/Types.h" +#define MAX_CONTROLLER_KEYS 32 + enum ControllerButton { CONTROLLER_BUTTON_STICK_LEFT_BUTTON, CONTROLLER_BUTTON_STICK_LEFT_HORIZONTAL, @@ -49,8 +51,8 @@ enum ControllerButton { }; struct ControllerInput { - int8 button[32]; - bool is_analog[32]; // = uses deadzone + int8 button[MAX_CONTROLLER_KEYS]; + bool is_analog[MAX_CONTROLLER_KEYS]; // = uses deadzone int16 gyro_x; int16 gyro_y; diff --git a/input/Input.h b/input/Input.h index 74539e3..b49c2d1 100644 --- a/input/Input.h +++ b/input/Input.h @@ -19,7 +19,6 @@ // How many keys/buttons do we support for the devices #define MAX_KEYBOARD_KEYS 255 #define MAX_MOUSE_KEYS 10 -#define MAX_CONTROLLER_KEYS 24 #define MIN_INPUT_DEVICES 2 diff --git a/math/matrix/MatrixFloat32.h b/math/matrix/MatrixFloat32.h index e141d94..e0bd2f0 100644 --- a/math/matrix/MatrixFloat32.h +++ b/math/matrix/MatrixFloat32.h @@ -683,30 +683,29 @@ void mat4_frustum_sparse_rh( float left, float right, float bottom, float top, float znear, float zfar ) { - float temp, temp2, temp3, temp4; - temp = 2.0f * znear; - temp2 = right - left; - temp3 = top - bottom; - temp4 = zfar - znear; + float temp = 2.0f * znear; + float rl_delta = right - left; + float tb_delta = top - bottom; + float fn_delta = zfar - znear; - matrix[0] = temp / temp2; + matrix[0] = temp / rl_delta; //matrix[1] = 0.0f; //matrix[2] = 0.0f; //matrix[3] = 0.0f; //matrix[4] = 0.0f; - matrix[5] = temp / temp3; + matrix[5] = temp / tb_delta; //matrix[6] = 0.0f; //matrix[7] = 0.0f; - matrix[8] = (right + left) / temp2; - matrix[9] = (top + bottom) / temp3; - matrix[10] = -(zfar + znear) / temp4; + matrix[8] = (right + left) / rl_delta; + matrix[9] = (top + bottom) / tb_delta; + matrix[10] = -(zfar + znear) / fn_delta; matrix[11] = -1.0f; //matrix[12] = 0.0f; //matrix[13] = 0.0f; - matrix[14] = (-temp * zfar) / temp4; + matrix[14] = (-temp * zfar) / fn_delta; //matrix[15] = 0.0f; } @@ -715,34 +714,34 @@ void mat4_frustum_sparse_lh( float left, float right, float bottom, float top, float znear, float zfar ) { - float temp, temp2, temp3, temp4; - temp = 2.0f * znear; - temp2 = right - left; - temp3 = top - bottom; - temp4 = zfar - znear; + float temp = 2.0f * znear; + float rl_delta = right - left; + float tb_delta = top - bottom; + float fn_delta = zfar - znear; - matrix[0] = temp / temp2; + matrix[0] = temp / rl_delta; //matrix[1] = 0.0f; //matrix[2] = 0.0f; //matrix[3] = 0.0f; //matrix[4] = 0.0f; - matrix[5] = temp / temp3; + matrix[5] = temp / tb_delta; //matrix[6] = 0.0f; //matrix[7] = 0.0f; - matrix[8] = (right + left) / temp2; - matrix[9] = (top + bottom) / temp3; - matrix[10] = (zfar + znear) / temp4; + matrix[8] = (right + left) / rl_delta; + matrix[9] = (top + bottom) / tb_delta; + matrix[10] = (zfar + znear) / fn_delta; matrix[11] = 1.0f; //matrix[12] = 0.0f; //matrix[13] = 0.0f; - matrix[14] = (temp * zfar) / temp4; + matrix[14] = (temp * zfar) / fn_delta; //matrix[15] = 0.0f; } // fov needs to be in rad +inline void mat4_perspective_sparse_lh( float *matrix, float fov, float aspect, float znear, float zfar) @@ -756,6 +755,7 @@ void mat4_perspective_sparse_lh( mat4_frustum_sparse_lh(matrix, -xmax, xmax, -ymax, ymax, znear, zfar); } +inline void mat4_perspective_sparse_rh( float *matrix, float fov, float aspect, float znear, float zfar) @@ -769,14 +769,14 @@ void mat4_perspective_sparse_rh( mat4_frustum_sparse_rh(matrix, -xmax, xmax, -ymax, ymax, znear, zfar); } -void mat4_ortho_sparse_rh( +void mat4_ortho_sparse_lh( float *matrix, float left, float right, float bottom, float top, - float near_dist, float far_dist + float znear, float zfar ) { float rl_delta = right - left; float tb_delta = top - bottom; - float fn_delta = far_dist - near_dist; + float fn_delta = zfar - znear; matrix[0] = 2.0f / rl_delta; //matrix[1] = 0.0f; @@ -795,37 +795,37 @@ void mat4_ortho_sparse_rh( matrix[12] = -(right + left) / rl_delta; matrix[13] = -(top + bottom) / tb_delta; - matrix[14] = -(far_dist + near_dist) / fn_delta; + matrix[14] = -(zfar + znear) / fn_delta; matrix[15] = 1.0f; } -void mat4_ortho_sparse_lh( +void mat4_ortho_sparse_rh( float *matrix, float left, float right, float bottom, float top, - float near_dist, float far_dist + float znear, float zfar ) { float rl_delta = right - left; float tb_delta = top - bottom; - float fn_delta = far_dist - near_dist; + float fn_delta = zfar - znear; matrix[0] = 2.0f / rl_delta; //matrix[1] = 0.0f; //matrix[2] = 0.0f; - //matrix[3] = 0.0f; + matrix[3] = -(right + left) / rl_delta; //matrix[4] = 0.0f; matrix[5] = 2.0f / tb_delta; //matrix[6] = 0.0f; - //matrix[7] = 0.0f; + matrix[7] = -(top + bottom) / tb_delta; //matrix[8] = 0.0f; //matrix[9] = 0.0f; matrix[10] = 2.0f / fn_delta; - //matrix[11] = 0.0f; + matrix[11] = -(zfar + znear) / fn_delta; - matrix[12] = -(right + left) / rl_delta; - matrix[13] = -(top + bottom) / tb_delta; - matrix[14] = -(far_dist + near_dist) / fn_delta; + //matrix[12] = 0.0f; + //matrix[13] = = 0.0f; + //matrix[14] = 0.0f; matrix[15] = 1.0f; } diff --git a/memory/BufferMemory.h b/memory/BufferMemory.h index 7eabb01..c01686c 100644 --- a/memory/BufferMemory.h +++ b/memory/BufferMemory.h @@ -16,6 +16,8 @@ #include "Allocation.h" #include "DebugMemory.h" +// @question Consider to use element_alignment to automatically align/pad elmeents + struct BufferMemory { byte* memory; @@ -23,10 +25,11 @@ struct BufferMemory { uint64 size; uint64 pos; int alignment; + int element_alignment; }; inline -void buffer_alloc(BufferMemory* buf, uint64 size, int alignment = 1) +void buffer_alloc(BufferMemory* buf, uint64 size, int alignment = 64) { buf->memory = alignment < 2 ? (byte *) playform_alloc(size) @@ -55,10 +58,14 @@ void buffer_reset(BufferMemory* buf) } inline -byte* buffer_get_memory(BufferMemory* buf, uint64 size, int aligned = 1, bool zeroed = false) +byte* buffer_get_memory(BufferMemory* buf, uint64 size, int aligned = 0, bool zeroed = false) { ASSERT_SIMPLE(size <= buf->size); + if (aligned == 0) { + aligned = (byte) OMS_MAX(buf->element_alignment, 1); + } + if (aligned > 1) { uintptr_t address = (uintptr_t) buf->memory; buf->pos += (aligned - ((address + buf->pos) & (aligned - 1))) % aligned; diff --git a/memory/ChunkMemory.h b/memory/ChunkMemory.h index 77bd653..5df6457 100644 --- a/memory/ChunkMemory.h +++ b/memory/ChunkMemory.h @@ -30,7 +30,7 @@ struct ChunkMemory { }; inline -void chunk_alloc(ChunkMemory* buf, uint64 count, uint64 chunk_size, int alignment = 1) +void chunk_alloc(ChunkMemory* buf, uint64 count, uint64 chunk_size, int alignment = 64) { buf->memory = alignment < 2 ? (byte *) playform_alloc(count * chunk_size + sizeof(buf->free) * CEIL_DIV(count, 64)) diff --git a/memory/DebugMemory.h b/memory/DebugMemory.h index babcf1c..5f211d5 100644 --- a/memory/DebugMemory.h +++ b/memory/DebugMemory.h @@ -23,7 +23,7 @@ struct DebugMemoryRange { uint64 start; - uint64 end; + uint64 size; uint64 time; }; @@ -38,7 +38,7 @@ struct DebugMemory { #if DEBUG #define DEBUG_MEMORY(mem, start, end) debug_memory_add_range((mem), (start), (end)) #define DEBUG_MEMORY_RESET(mem) debug_memory_reset((mem)) - #define DEBUG_MEMORY_FREE(mem, start, end) debug_memory_add_range((mem), (start), (end)) + #define DEBUG_MEMORY_FREE(mem) debug_memory_free((mem)) #else #define DEBUG_MEMORY(mem, start, end) ((void) 0) #define DEBUG_MEMORY_RESET(mem) ((void) 0) @@ -58,20 +58,20 @@ void debug_memory_resize(DebugMemory* mem) } } -void debug_memory_add_range(DebugMemory* mem, uint64 start, uint64 end) +void debug_memory_add_range(DebugMemory* mem, uint64 start, uint64 size) { if (mem->debug_range_idx >= mem->debug_range_size) { debug_memory_resize(mem); } mem->debug_ranges[mem->debug_range_idx].start = start; - mem->debug_ranges[mem->debug_range_idx].end = end; + mem->debug_ranges[mem->debug_range_idx].size = size; // @question consider to use other time_ms() since __rdtsc is variable (boost, power saving) mem->debug_ranges[mem->debug_range_idx].time = __rdtsc(); ++mem->debug_range_idx; - mem->usage += end - start; + mem->usage += size; } inline diff --git a/memory/RingMemory.h b/memory/RingMemory.h index 5c7ea92..0c41fe8 100644 --- a/memory/RingMemory.h +++ b/memory/RingMemory.h @@ -26,6 +26,7 @@ struct RingMemory { uint64 size; uint64 pos; int alignment; + int element_alignment; // The following two indices are only used in special cases such as iterating through a portion // of the ring memory. In such cases it may be necessary to know where the start and end are. @@ -40,7 +41,7 @@ struct RingMemory { }; inline -void ring_alloc(RingMemory* ring, uint64 size, int alignment = 1) +void ring_alloc(RingMemory* ring, uint64 size, int alignment = 64) { ring->memory = alignment < 2 ? (byte *) playform_alloc(size) @@ -54,7 +55,7 @@ void ring_alloc(RingMemory* ring, uint64 size, int alignment = 1) } inline -void ring_create(RingMemory* ring, BufferMemory* buf, uint64 size, int alignment = 1) +void ring_create(RingMemory* ring, BufferMemory* buf, uint64 size, int alignment = 64) { ring->memory = buffer_get_memory(buf, size, alignment); @@ -76,8 +77,12 @@ void ring_free(RingMemory* buf) } inline -uint64 ring_calculate_position(const RingMemory* ring, uint64 pos, uint64 size, byte aligned = 1) +uint64 ring_calculate_position(const RingMemory* ring, uint64 pos, uint64 size, byte aligned = 0) { + if (aligned == 0) { + aligned = (byte) OMS_MAX(ring->element_alignment, 1); + } + if (aligned) { uintptr_t address = (uintptr_t) ring->memory; int64 adjustment = (aligned - ((address + ring->pos) & (aligned - 1))) % aligned; @@ -100,10 +105,14 @@ uint64 ring_calculate_position(const RingMemory* ring, uint64 pos, uint64 size, return pos; } -byte* ring_get_memory(RingMemory* ring, uint64 size, byte aligned = 1, bool zeroed = false) +byte* ring_get_memory(RingMemory* ring, uint64 size, byte aligned = 0, bool zeroed = false) { ASSERT_SIMPLE(size <= ring->size); + if (aligned == 0) { + aligned = (byte) OMS_MAX(ring->element_alignment, 1); + } + if (aligned > 1) { uintptr_t address = (uintptr_t) ring->memory; int64 adjustment = (aligned - ((address + ring->pos) & (aligned - 1))) % aligned; @@ -155,7 +164,7 @@ void ring_reset(RingMemory* ring) * Checks if one additional element can be inserted without overwriting the start index */ inline -bool ring_commit_safe(const RingMemory* ring, uint64 size, byte aligned = 1) +bool ring_commit_safe(const RingMemory* ring, uint64 size, byte aligned = 0) { uint64 pos = ring_calculate_position(ring, ring->pos, size, aligned); diff --git a/object/Texture.h b/object/Texture.h index 12d43df..0201e2f 100644 --- a/object/Texture.h +++ b/object/Texture.h @@ -34,7 +34,6 @@ #include "../stdlib/Types.h" #include "../image/Image.h" -#include "../models/Attrib.h" struct Texture { uint64 id; diff --git a/object/Vertex.h b/object/Vertex.h index f40b555..6a797a8 100644 --- a/object/Vertex.h +++ b/object/Vertex.h @@ -12,16 +12,38 @@ #include "../stdlib/Types.h" struct Vertex3D { - float position[3]; - float normal[3]; - uint32 tex_coord[2]; - float color[4]; + v3_f32 position; + v3_f32 normal; + v2_int32 tex_coord; + v4_f32 color; +}; + +struct Vertex3DColorIndex { + v3_f32 position; + uint32 color; }; struct Vertex2D { - float position[2]; - uint32 tex_coord[2]; - float color[4]; + v2_f32 position; + v2_int32 tex_coord; + v4_f32 color; +}; + +struct Vertex2DTexture { + v2_f32 position; + v2_int32 tex_coord; +}; + +struct Vertex2DColor { + v2_f32 position; + v4_f32 color; +}; + +struct Vertex2DColorIndex { + v2_f32 position; + // opengl shaders don't support individual bytes, + // otherwise we would use byte here for 256 color palettes + uint32 color; }; struct VertexRef { diff --git a/utils/MathUtils.h b/utils/MathUtils.h index e5714e0..9aa33eb 100644 --- a/utils/MathUtils.h +++ b/utils/MathUtils.h @@ -26,6 +26,7 @@ #define OMS_RAD2DEG(angle) ((angle) * 180.0f / OMS_PI) #define ROUND_TO_NEAREST(a, b) (((a) + ((b) - 1)) & ~((b) - 1)) #define CEIL_DIV(a, b) (((a) + (b) - 1) / (b)) +#define OMS_CEIL(x) ((x) == (int)(x) ? (int)(x) : ((x) > 0 ? (int)(x) + 1 : (int)(x))) #define SQRT_2 1.4142135623730950488016887242097f diff --git a/utils/SystemInfo.h b/utils/SystemInfo.h index c7ffe5d..a9d1ee4 100644 --- a/utils/SystemInfo.h +++ b/utils/SystemInfo.h @@ -47,7 +47,7 @@ struct CpuCacheInfo { int line_size; }; -void get_cache_info(int level, CpuCacheInfo* cache) { +void cache_info_get(int level, CpuCacheInfo* cache) { unsigned int eax, ebx, ecx, edx; int type; @@ -90,7 +90,7 @@ struct MainboardInfo { char serial_number[64]; }; -void get_mainboard_info(MainboardInfo* info) { +void mainboard_info_get(MainboardInfo* info) { info->name[63] = '\0'; info->serial_number[63] = '\0'; @@ -225,7 +225,7 @@ struct NetworkInfo { byte mac[8]; }; -int get_network_info(NetworkInfo* info) { +int network_info_get(NetworkInfo* info) { WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { return 0; @@ -295,16 +295,16 @@ struct CpuInfo { SIMDInfo simd; }; -void get_cpu_info(CpuInfo* info) { +void cpu_info_get(CpuInfo* info) { int temp; info->simd.sse = (temp = max_sse_supported()) > 9 ? temp / 10.0f : temp; info->simd.avx256 = max_avx256_supported(); info->simd.avx512 = max_avx512_supported(); - get_cache_info(1, &info->cache[0]); - get_cache_info(2, &info->cache[1]); - get_cache_info(3, &info->cache[2]); - get_cache_info(4, &info->cache[3]); + cache_info_get(1, &info->cache[0]); + cache_info_get(2, &info->cache[1]); + cache_info_get(3, &info->cache[2]); + cache_info_get(4, &info->cache[3]); SYSTEM_INFO sys_info; GetSystemInfo(&sys_info); @@ -353,7 +353,7 @@ struct OSInfo { int minor; }; -void get_os_info(OSInfo* info) { +void os_info_get(OSInfo* info) { info->vendor[15] = '\0'; info->name[63] = '\0'; @@ -382,7 +382,7 @@ struct RamInfo { int memory; }; -void get_ram_info(RamInfo* info) { +void ram_info_get(RamInfo* info) { MEMORYSTATUSEX statex; statex.dwLength = sizeof(statex); GlobalMemoryStatusEx(&statex); @@ -394,7 +394,7 @@ struct GpuInfo { int vram; }; -unsigned int get_gpu_info(GpuInfo* info) { +unsigned int gpu_info_get(GpuInfo* info) { IDXGIFactory *pFactory = NULL; IDXGIAdapter *pAdapter = NULL; DXGI_ADAPTER_DESC adapterDesc; @@ -432,18 +432,18 @@ struct DisplayInfo { int hz; }; -unsigned int get_display_info(DisplayInfo* info) { - DISPLAY_DEVICE device; - DEVMODE mode; +unsigned int display_info_get(DisplayInfo* info) { + DISPLAY_DEVICEA device; + DEVMODEA mode; - device.cb = sizeof(DISPLAY_DEVICE); + device.cb = sizeof(DISPLAY_DEVICEA); int i = 0; - while (EnumDisplayDevices(NULL, i, &device, 0)) { + while (EnumDisplayDevicesA(NULL, i, &device, 0)) { mode.dmSize = sizeof(mode); - if (EnumDisplaySettings(device.DeviceName, ENUM_CURRENT_SETTINGS, &mode)) { + if (EnumDisplaySettingsA(device.DeviceName, ENUM_CURRENT_SETTINGS, &mode)) { strcpy(info[i].name, device.DeviceName); info[i].width = mode.dmPelsWidth; info[i].height = mode.dmPelsHeight; @@ -473,7 +473,7 @@ struct SystemInfo { int display_count; }; -void render_system_info(char* buf, const SystemInfo* info) { +void system_info_render(char* buf, const SystemInfo* info) { const char avx512[8][12] = { "AVX-512F", "AVX-512DQ", @@ -559,15 +559,15 @@ void render_system_info(char* buf, const SystemInfo* info) { ); } -void get_system_info(SystemInfo* info) +void system_info_get(SystemInfo* info) { - get_os_info(&info->os); - get_mainboard_info(&info->mainboard); - info->network_count = get_network_info(info->network); - get_cpu_info(&info->cpu); - get_ram_info(&info->ram); - info->gpu_count = get_gpu_info(info->gpu); - info->display_count = get_display_info(info->display); + os_info_get(&info->os); + mainboard_info_get(&info->mainboard); + info->network_count = network_info_get(info->network); + cpu_info_get(&info->cpu); + ram_info_get(&info->ram); + info->gpu_count = gpu_info_get(info->gpu); + info->display_count = display_info_get(info->display); } #endif \ No newline at end of file