opengles support

This commit is contained in:
Jeff Harris 2023-12-03 20:41:59 +13:00
parent 07d517307a
commit d812e6364a
11 changed files with 579 additions and 21924 deletions

View File

@ -153,6 +153,20 @@ typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
/*
* To support platform where unsigned long cannot be used interchangeably with
* inptr_t (e.g. CHERI-extended ISAs), we can use the stdint.h intptr_t.
* Ideally, we could just use (u)intptr_t everywhere, but this could result in
* ABI breakage if khronos_uintptr_t is changed from unsigned long to
* unsigned long long or similar (this results in different C++ name mangling).
* To avoid changes for existing platforms, we restrict usage of intptr_t to
* platforms where the size of a pointer is larger than the size of long.
*/
#if defined(__SIZEOF_LONG__) && defined(__SIZEOF_POINTER__)
#if __SIZEOF_POINTER__ > __SIZEOF_LONG__
#define KHRONOS_USE_INTPTR_T
#endif
#endif
#elif defined(__VMS ) || defined(__sgi)
@ -235,14 +249,21 @@ typedef unsigned short int khronos_uint16_t;
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
* to be the only LLP64 architecture in current use.
*/
#ifdef _WIN64
#ifdef KHRONOS_USE_INTPTR_T
typedef intptr_t khronos_intptr_t;
typedef uintptr_t khronos_uintptr_t;
#elif defined(_WIN64)
typedef signed long long int khronos_intptr_t;
typedef unsigned long long int khronos_uintptr_t;
typedef signed long long int khronos_ssize_t;
typedef unsigned long long int khronos_usize_t;
#else
typedef signed long int khronos_intptr_t;
typedef unsigned long int khronos_uintptr_t;
#endif
#if defined(_WIN64)
typedef signed long long int khronos_ssize_t;
typedef unsigned long long int khronos_usize_t;
#else
typedef signed long int khronos_ssize_t;
typedef unsigned long int khronos_usize_t;
#endif

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -12,3 +12,7 @@ target_sources(miniaudio PRIVATE
miniaudio.c
)
if (NOT MSVC)
target_link_libraries(miniaudio PUBLIC m dl pthread)
endif()

View File

@ -54,7 +54,7 @@ static void* create_window_and_renderer(char* title, int x, int y, int width, in
LOG_PANIC("SDL_INIT_VIDEO error: %s", SDL_GetError());
}
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) {
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES) != 0) {
LOG_PANIC("Failed to set SDL_GL_CONTEXT_PROFILE_MASK attribute. %s", SDL_GetError());
};
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);

View File

@ -111,7 +111,7 @@ static GLuint CreateShaderProgram(char* name, const char* vertex_shader, const i
GLint link_ok = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
glGetShaderInfoLog(program, 1024, NULL, log_buffer);
glGetProgramInfoLog(program, 1024, NULL, log_buffer);
LOG_PANIC("shader program %s failed to link: %s", name, log_buffer);
}
return program;
@ -380,10 +380,12 @@ void GLRenderer_BeginScene(br_actor* camera, br_pixelmap* colour_buffer, br_pixe
glUniformMatrix4fv(uniforms_3d.projection, 1, GL_FALSE, &projection.m[0][0]);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
CHECK_GL_ERROR("GLRenderer_BeginScene");
}
void GLRenderer_EndScene(void) {
// switch back to default fb and reset state
CHECK_GL_ERROR("GLRenderer_EndScene2");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDepthMask(GL_TRUE);
glDepthMask(GL_TRUE);
@ -392,12 +394,17 @@ void GLRenderer_EndScene(void) {
void GLRenderer_FullScreenQuad(uint8_t* screen_buffer) {
glViewport(vp_x, vp_y, vp_width, vp_height);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glViewport(vp_x, vp_y, vp_width, vp_height);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST);
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//CHECK_GL_ERROR("GLRenderer_RenderFullScreenQuad2");
glBindTexture(GL_TEXTURE_2D, fullscreen_quad_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8UI, render_width, render_height, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, screen_buffer);
glBindVertexArray(screen_buffer_vao);
@ -620,10 +627,10 @@ void GLRenderer_Model(br_actor* actor, br_model* model, br_material* material, b
switch (render_type) {
case BRT_TRIANGLE:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
break;
case BRT_LINE:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glUniform1ui(uniforms_3d.material_index_base, 255);
glUniform1ui(uniforms_3d.material_flags, 0);
break;
@ -691,18 +698,23 @@ void GLRenderer_BufferTexture(br_pixelmap* pm) {
}
void GLRenderer_FlushBuffer(tRenderer_flush_type flush_type) {
uint8_t* pm_pixels = last_colour_buffer->pixels;
if (!dirty_buffers) {
return;
}
// pull framebuffer into cpu memory to emulate BRender behavior
glBindTexture(GL_TEXTURE_2D, framebuffer_texture);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, screen_buffer_flip_pixels);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
glReadPixels(0, 0, render_width, render_height, GL_RED_INTEGER, GL_UNSIGNED_BYTE, screen_buffer_flip_pixels);
// pull framebuffer into cpu memory to emulate BRender behavior
//glBindTexture(GL_TEXTURE_2D, framebuffer_texture);
//glGetTexImage(GL_TEXTURE_2D, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, screen_buffer_flip_pixels);
CHECK_GL_ERROR("GLRenderer_FlushBuffer3");
// flip texture to match the expected orientation
int dest_y = render_height;
uint8_t* pm_pixels = last_colour_buffer->pixels;
uint8_t new_pixel;
for (int y = 0; y < render_height; y++) {
dest_y--;
@ -714,11 +726,15 @@ void GLRenderer_FlushBuffer(tRenderer_flush_type flush_type) {
}
}
CHECK_GL_ERROR("GLRenderer_FlushBuffer2");
if (flush_type == eFlush_all) {
// pull depthbuffer into cpu memory to emulate BRender behavior
glBindTexture(GL_TEXTURE_2D, depth_texture);
glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, depth_buffer_flip_pixels);
//glBindTexture(GL_TEXTURE_2D, depth_texture);
//glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, depth_buffer_flip_pixels);
glReadPixels(0, 0, render_width, render_height, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, depth_buffer_flip_pixels);
dest_y = last_colour_buffer->height;
int src_y = render_height - last_colour_buffer->base_y - last_colour_buffer->height;
@ -736,6 +752,7 @@ void GLRenderer_FlushBuffer(tRenderer_flush_type flush_type) {
glClear(GL_COLOR_BUFFER_BIT);
flush_counter++;
dirty_buffers = 0;
CHECK_GL_ERROR("GLRenderer_FlushBuffer");
}
void GLRenderer_FlushBuffers(void) {

View File

@ -28,12 +28,6 @@
case GL_OUT_OF_MEMORY: \
err = "GL_OUT_OF_MEMORY"; \
break; \
case GL_STACK_UNDERFLOW: \
err = "GL_STACK_UNDERFLOW"; \
break; \
case GL_STACK_OVERFLOW: \
err = "GL_STACK_OVERFLOW"; \
break; \
default: \
err = "unknown"; \
break; \

View File

@ -1,5 +1,8 @@
#version 140
#extension GL_ARB_explicit_attrib_location : require
#version 300 es
precision mediump float;
precision mediump int;
precision lowp usampler2D;
// Input, output variables
// =======================
@ -23,7 +26,7 @@ uniform usampler2D u_colour_buffer;
uniform uint u_viewport_height;
uniform vec4 u_clip_planes[6];
uniform int u_clip_plane_count = 0;
uniform int u_clip_plane_count;
uniform uint u_material_flags;
uniform mat2x3 u_material_uv_transform;
@ -31,7 +34,7 @@ uniform usampler2D u_material_texture_pixelmap;
uniform uint u_material_texture_enabled;
// material_blend_table is a 256x256 image which encodes 256 values of blending between new color and current color in framebuffer
uniform uint u_material_blend_enabled = 0u;
uniform uint u_material_blend_enabled;
uniform usampler2D u_material_blend_table;
// material_shade_table is a 256px-wide image which encodes material_shade_table_height lit shades for each color
@ -55,7 +58,7 @@ void main(void) {
// calculate signed plane-vertex distance
vec4 v4 = vec4(v_frag_pos.xyz, 1);
float d = dot(u_clip_planes[i], v4);
if (d < 0) {
if (d < 0.0) {
discard;
}
}
@ -96,7 +99,10 @@ void main(void) {
if (u_material_blend_enabled == 1u) {
// u_colour_buffer is upside down from opengl perspective. We need to sample it upside down.
uint current_framebuffer_color = texelFetch(u_colour_buffer, ivec2(gl_FragCoord.x, u_viewport_height - gl_FragCoord.y), 0).r;
int i = int(gl_FragCoord.x);
int i2 = int(u_viewport_height) - int(gl_FragCoord.y);
ivec2 coords = ivec2(gl_FragCoord.x, i2);
uint current_framebuffer_color = texelFetch(u_colour_buffer, coords, 0).r;
out_palette_index = texelFetch(u_material_blend_table, ivec2(out_palette_index, current_framebuffer_color), 0).r;
}

View File

@ -1,5 +1,7 @@
#version 140
#extension GL_ARB_explicit_attrib_location : require
#version 300 es
precision mediump float;
precision mediump int;
layout (location = 0) in vec3 a_pos;
layout (location = 1) in vec3 a_normal;

View File

@ -1,11 +1,16 @@
#version 140
#extension GL_ARB_explicit_attrib_location : require
#version 300 es
precision mediump float;
precision mediump int;
precision lowp usampler2D;
in vec2 v_tex_coord;
uniform usampler2D u_pixels;
uniform sampler2D u_palette;
layout (location = 0) out vec4 out_frag_color;
void main(void) {

View File

@ -1,10 +1,14 @@
#version 140
#extension GL_ARB_explicit_attrib_location:require
#version 300 es
precision highp float;
precision highp int;
layout (location = 0) in vec3 a_pos;
layout (location = 1) in vec3 a_color;
layout (location = 2) in vec2 a_uv;
out vec2 v_tex_coord;
void main(void) {