Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ jobs:
sudo apt-get update && sudo apt-get install -y \
libx11-dev \
libxrandr-dev \
mesa-common-dev \
glslang-tools
mesa-common-dev
- name: package
run: CXX=clang++ ./src_build/make.sh
- name: release
Expand Down
6 changes: 1 addition & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ jobs:
sudo apt-get update && sudo apt-get install -y \
libx11-dev \
libxrandr-dev \
mesa-common-dev \
glslang-tools
mesa-common-dev
- name: build
run: CXX=clang++ ./src_build/make.sh build
- name: analyze
if: ${{ !contains(matrix.os, '-arm') }}
uses: github/codeql-action/analyze@f09c1c0a94de965c15400f5634aa42fac8fb8f88 # v3
33 changes: 22 additions & 11 deletions src/carbon_memory.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) Wasym A. Alonso. All Rights Reserved.

#define CARBON_MEMORY__HDR_SZ (sizeof(usz))
#define CARBON_MEMORY__MAX_ALIGN 16
#define CARBON_MEMORY__HDR_SZ ((sizeof(usz) + CARBON_MEMORY__MAX_ALIGN - 1) & ~(CARBON_MEMORY__MAX_ALIGN - 1))

#ifdef _WIN32
#define CARBON_MEMORY__ALLOC(sz) _aligned_malloc((sz), CARBON_MEMORY__MAX_ALIGN)
#define CARBON_MEMORY__REALLOC(p, sz) _aligned_realloc((p), (sz), CARBON_MEMORY__MAX_ALIGN)
#define CARBON_MEMORY__FREE(p) _aligned_free((p))
#else
#define CARBON_MEMORY__ALLOC(sz) __builtin_malloc((sz))
#define CARBON_MEMORY__REALLOC(p, sz) __builtin_realloc((p), (sz))
#define CARBON_MEMORY__FREE(p) __builtin_free((p))
#endif

static usz carbon_memory__usage;

void *carbon_memory_alloc(usz size) {
u8 *p = __builtin_malloc(size + CARBON_MEMORY__HDR_SZ);
u8 *p = CARBON_MEMORY__ALLOC(size + CARBON_MEMORY__HDR_SZ);
CBN_ASSERT(p && "failed to allocate memory");
*(usz *)p = size;
carbon_memory__usage += size;
Expand All @@ -23,7 +34,7 @@ void *carbon_memory_realloc(void *p, usz size) {
if (!p) return carbon_memory_alloc(size);
u8 *raw_p = (u8 *)p - CARBON_MEMORY__HDR_SZ;
usz old_sz = *(usz *)raw_p;
u8 *new_p = __builtin_realloc(raw_p, size + CARBON_MEMORY__HDR_SZ);
u8 *new_p = CARBON_MEMORY__REALLOC(raw_p, size + CARBON_MEMORY__HDR_SZ);
CBN_ASSERT(new_p && "failed to reallocate memory");
*(usz *)new_p = size;
carbon_memory__usage -= old_sz;
Expand All @@ -35,37 +46,37 @@ void carbon_memory_free(void *p) {
if (!p) return;
u8 *raw_p = (u8 *)p - CARBON_MEMORY__HDR_SZ;
carbon_memory__usage -= *(usz *)raw_p;
__builtin_free(raw_p);
CARBON_MEMORY__FREE(raw_p);
}

usz carbon_memory_usage(void) {
return carbon_memory__usage;
}

void *carbon_memory_copy(void *dst, const void *src, usz n) {
u8 *d = (u8 *) dst;
const u8 *s = (const u8 *) src;
u8 *d = (u8 *)dst;
const u8 *s = (const u8 *)src;
if (d == s || !n) return dst;
if (d < s) while (n--) *d++ = *s++;
else while (n--) d[n] = s[n];
return dst;
}

void *carbon_memory_copy_raw(void *dst, const void *src, usz n) {
u8 *d = (u8 *) dst;
const u8 *s = (const u8 *) src;
u8 *d = (u8 *)dst;
const u8 *s = (const u8 *)src;
while (n--) *d++ = *s++;
return dst;
}

i32 carbon_memory_cmp(const void *v1, const void *v2, usz n) {
const u8 *l = (const u8 *) v1, *r = (const u8 *) v2;
const u8 *l = (const u8 *)v1, *r = (const u8 *)v2;
for (; n && *l == *r; --n, ++l, ++r);
return n ? *l - *r : 0;
}

void *carbon_memory_set(void *dst, i32 c, usz n) {
u8 *d = (u8 *) dst;
while (n--) *d++ = (u8) c;
u8 *d = (u8 *)dst;
while (n--) *d++ = (u8)c;
return dst;
}
4 changes: 2 additions & 2 deletions src/carbon_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (C) Wasym A. Alonso. All Rights Reserved.

static bool carbon_win__should_close;
static usz carbon_win__renderer_w, carbon_win__renderer_h;

static CBN_Image carbon_win__icon;
static bool carbon_win__cursor_visible = true;
Expand All @@ -26,8 +27,7 @@ CBNINL bool carbon_win__poll_event(void);
#if defined(__APPLE__)
#include "carbon_win_macos.m"
#elif defined(_WIN32)
// ...
#error Not implemented yet
#include "carbon_win_win32.inl"
#elif defined(__linux__) || defined(__FreeBSD__)
#include "carbon_win_x11.inl"
#else
Expand Down
139 changes: 139 additions & 0 deletions src/carbon_win_gl.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) Wasym A. Alonso. All Rights Reserved.

#pragma once

#ifndef GL_VERTEX_SHADER
#define GL_VERTEX_SHADER 0x8B31
#define GL_FRAGMENT_SHADER 0x8B30
#define GL_COMPILE_STATUS 0x8B81
#define GL_LINK_STATUS 0x8B82
#define GL_UNSIGNED_INT_8_8_8_8 0x8035
#define GL_TEXTURE0 0x84C0
#endif

typedef char GLchar;

static GLuint carbon_win__gl_tex;
static GLuint carbon_win__gl_vao;

#define CARBON_WIN__GL_PROCS \
x(GLuint, glCreateShader, GLenum) \
x(void, glShaderSource, GLuint, GLsizei, const GLchar **, const GLint *) \
x(void, glCompileShader, GLuint) \
x(GLuint, glCreateProgram, void) \
x(void, glAttachShader, GLuint, GLuint) \
x(void, glLinkProgram, GLuint) \
x(void, glUseProgram, GLuint) \
x(void, glGenVertexArrays, GLsizei, GLuint *) \
x(void, glBindVertexArray, GLuint) \
x(void, glGetShaderiv, GLuint, GLenum, GLint *) \
x(void, glGetProgramiv, GLuint, GLenum, GLint *) \
x(void, glGetShaderInfoLog, GLuint, GLsizei, GLsizei *, GLchar *) \
x(void, glGetProgramInfoLog, GLuint, GLsizei, GLsizei *, GLchar *)

#define x(ret, name, ...) \
typedef ret (*name ## _t)(__VA_ARGS__); \
static name ## _t name;
CARBON_WIN__GL_PROCS;
#ifdef _WIN32
x(void, glActiveTexture, GLenum);
#endif
#undef x

CBNINL void *carbon_win__gl_func_loader(const char *name);

CBNINL void carbon_win__gl_load_funcs(void) {
#define x(ret, name, ...) \
name = (name ## _t) carbon_win__gl_func_loader(#name); \
CBN_ASSERT(name && #name " failed to load");
CARBON_WIN__GL_PROCS;
#ifdef _WIN32
x(void, glActiveTexture, GLenum);
#endif
#undef x
}

CBNINL void carbon_win__gl_init(usz w, usz h) {
carbon_win__renderer_w = w;
carbon_win__renderer_h = h;
carbon_win__gl_load_funcs();
GLuint vert = glCreateShader(GL_VERTEX_SHADER); {
const GLchar *src = (const GLchar *)__src_carbon_win_shader_vert_glsl;
const GLint len = (GLint)__src_carbon_win_shader_vert_glsl_len;
glShaderSource(vert, 1, &src, &len);
glCompileShader(vert);
GLint ok;
glGetShaderiv(vert, GL_COMPILE_STATUS, &ok);
if (!ok) {
char log[512];
glGetShaderInfoLog(vert, 512, 0, log);
CBN_ERROR("vert compile error: %s", log);
CARBON_UNREACHABLE;
}
}
GLuint frag = glCreateShader(GL_FRAGMENT_SHADER); {
const GLchar *src = (const GLchar *)__src_carbon_win_shader_frag_glsl;
const GLint len = (GLint)__src_carbon_win_shader_frag_glsl_len;
glShaderSource(frag, 1, &src, &len);
glCompileShader(frag);
GLint ok;
glGetShaderiv(frag, GL_COMPILE_STATUS, &ok);
if (!ok) {
char log[512];
glGetShaderInfoLog(frag, 512, 0, log);
CBN_ERROR("frag compile error: %s", log);
CARBON_UNREACHABLE;
}
}
GLuint prog = glCreateProgram(); {
glAttachShader(prog, vert);
glAttachShader(prog, frag);
glLinkProgram(prog);
GLint ok;
glGetProgramiv(prog, GL_LINK_STATUS, &ok);
if (!ok) {
char log[512];
glGetProgramInfoLog(prog, 512, 0, log);
CBN_ERROR("prog link error: %s", log);
CARBON_UNREACHABLE;
}
}
glUseProgram(prog);
glGenVertexArrays(1, &carbon_win__gl_vao);
glBindVertexArray(carbon_win__gl_vao);
glGenTextures(1, &carbon_win__gl_tex);
glBindTexture(GL_TEXTURE_2D, carbon_win__gl_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
carbon_win__renderer_w,
carbon_win__renderer_h,
0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 0);
}

CBNINL void carbon_win__gl_render(const u32 *pixels, usz w, usz h) {
if (w != carbon_win__renderer_w || h != carbon_win__renderer_h) {
carbon_win__renderer_w = w;
carbon_win__renderer_h = h;
glViewport(0, 0, carbon_win__renderer_w, carbon_win__renderer_h);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
carbon_win__renderer_w,
carbon_win__renderer_h,
0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 0);
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, carbon_win__gl_tex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
carbon_win__renderer_w,
carbon_win__renderer_h,
GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,
pixels);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(carbon_win__gl_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
}

// Local Variables:
// mode: c
// End:
2 changes: 0 additions & 2 deletions src/carbon_win_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ - (BOOL)windowShouldClose:(NSWindow *)win {
static NSWindow *carbon_win__window;
static CBNDelegate *carbon_win__delegate;

static usz carbon_win__renderer_w, carbon_win__renderer_h;

static id<MTLDevice> carbon_win__mtl_device;
static id<MTLCommandQueue> carbon_win__mtl_queue;
static CAMetalLayer *carbon_win__mtl_layer;
Expand Down
8 changes: 4 additions & 4 deletions src/carbon_win_shader.frag.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#version 450
#version 330 core

layout(location = 0) in vec2 uv;
layout(location = 0) out vec4 color;
layout(binding = 0) uniform sampler2D tex;
in vec2 uv;
out vec4 color;
uniform sampler2D tex;

void main() {
color = texture(tex, uv);
Expand Down
4 changes: 2 additions & 2 deletions src/carbon_win_shader.vert.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#version 450
#version 330 core

layout(location = 0) out vec2 uv;
out vec2 uv;

void main() {
vec2 pos[] = vec2[3](vec2(-1, -1), vec2(3, -1), vec2(-1, 3));
Expand Down
Loading
Loading