some progress

This commit is contained in:
veclavtlica
2026-10-07 11:21:21 +03:00
parent 14062e9d00
commit 8d502173a2
26 changed files with 705 additions and 212 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef PLR_DISPLAY_H
#define PLR_DISPLAY_H
extern int plr_display_x11_main(int argc, char *argv[]);
__attribute__((used)) extern int plr_display_x11_main(int argc, char *argv[]);
extern int plr_display_dos_main(int argc, char *argv[]);
#endif
+119 -10
View File
@@ -1,22 +1,79 @@
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "../../Common/bin.h"
#include "../../Common/buf.h"
#include "../../Common/cfg.h"
#include "../../Common/lzw.h"
#include "../../Common/mat.h"
#include "../../Common/std.h"
#include "../../Common/str.h"
#include "../draw.h"
#include "display.h"
/* https://hereket.com/posts/from-scratch-x11-windowing/ */
/* https://gaultier.github.io/blog/write_a_video_game_from_scratch_like_1987.html
*/
bool quited = false;
/* TODO: Have a cfg.h configuration header setting those. */
#define WIDTH 640
#define HEIGHT 480
bool fileread(void *ctx, char *into, uint32_t bytes) {
return com_std_syscall(0, (intptr_t)ctx, (intptr_t)into, bytes);
}
bool fileskip(void *ctx, uint32_t bytes) {
return com_std_syscall(8, (intptr_t)ctx, bytes, 1) >= 0;
}
static struct plr_x11_auth_entry {
uint16_t family;
struct com_buf *auth_name;
struct com_buf *auth_data;
bool success;
} plr_x11_read_auth_entry(struct com_bin *b, struct com_buf *buf) {
struct plr_x11_auth_entry result = {0};
if (!com_bin_read_u16(b, &result.family)) {
com_std_printf("Failure to initialize X11\n");
goto BREAK;
}
uint16_t addres_len;
if (!com_bin_read_u16(b, &addres_len))
goto BREAK;
if (!com_bin_skip(b, addres_len))
goto BREAK;
uint16_t display_len;
if (!com_bin_read_u16(b, &display_len))
goto BREAK;
if (!com_bin_skip(b, display_len))
goto BREAK;
uint16_t name_len;
if (!com_bin_read_u16(b, &name_len))
goto BREAK;
uint32_t name_idx;
if (!com_bin_read_bufbi(b, buf, &name_idx, name_len))
goto BREAK;
uint16_t data_len;
if (!com_bin_read_u16(b, &data_len))
goto BREAK;
uint32_t data_idx;
if (!com_bin_read_bufbi(b, buf, &data_idx, name_len))
goto BREAK;
result.auth_name = com_buf_index(struct com_buf, buf, name_idx);
result.auth_data = com_buf_index(struct com_buf, buf, data_idx);
result.success = true;
return result;
BREAK:
com_std_printf("Failure to initialize X11\n");
return result;
}
// TODO: correct error handling.
extern int plr_display_x11_main(int argc, char *argv[]) {
@@ -43,9 +100,61 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
// com_vec_t v0 = com_mat_vec_project(com_mat_reoder(m0), com_vec_identity());
// com_fix_print(v0.a[0]);
com_fix_run_tests();
com_fix_run_bench();
com_mat_run_bench();
// com_fix_run_tests();
// com_fix_run_bench();
// com_mat_run_bench();
/* TODO: Move init to separate func. */
/* Open X11 socket for communication. */
int const xfd = com_std_syscall(41, AF_UNIX, SOCK_STREAM, 0);
if (xfd < 0) {
com_std_printf("Failure to initialize X11\n");
return -1;
}
struct sockaddr_un const addr = {.sun_family = AF_UNIX,
.sun_path = "/tmp/.X11-unix/X0"};
if (com_std_syscall(42, xfd, (intptr_t)&addr, sizeof addr)) {
com_std_printf("Failure to initialize X11\n");
return -1;
}
/* First we need to authenticate X11 connection, which requires some funk. */
/* We assume that $XAUTHORITY is set and I hope it is true. */
char *xauthpath = NULL;
for (char **envp = &argv[argc + 1]; *envp != NULL; ++envp) {
static const struct com_str xauthlit = com_str_lit("XAUTHORITY=");
if (com_str_ext_prefixed(*envp, xauthlit)) {
xauthpath = &((*envp)[xauthlit.len]);
break;
}
}
/* Open it and parse. */
int const xauthfd = com_std_syscall(2, (intptr_t)xauthpath, O_RDONLY, 0);
if (xauthfd < 0) {
com_std_printf("Failure to initialize X11\n");
return -1;
}
com_buf_make_on_stack(char, xauthbuf, 1024, 0);
struct com_bin xauthbin = {
.ctx = (void *)(int64_t)xauthfd,
.flags = COM_BIN_BIG_ENDIAN,
.interface = {.readn = fileread, .skipn = fileskip}};
struct plr_x11_auth_entry auth_entry =
plr_x11_read_auth_entry(&xauthbin, xauthbuf);
com_std_print_str(auth_entry.auth_name->data, auth_entry.auth_name->len,
true);
com_std_print_str(auth_entry.auth_data->data, auth_entry.auth_data->len,
true);
com_buf_free(xauthbuf);
char sendbuf[16 * 1024] = {};
char readbuf[16 * 1024] = {};
// Display *display = XOpenDisplay(NULL);
// if (NULL == display) {
@@ -90,13 +199,13 @@ extern int plr_display_x11_main(int argc, char *argv[]) {
// GC gc = XCreateGC(display, window, 0, NULL);
// Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
// XSetWMProtocols(display, window, &wm_delete_window, 1);
// Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW",
// False); XSetWMProtocols(display, window, &wm_delete_window, 1);
// // TODO: can't be not true
// int bytes_per_pixel = 4;
// // char *pixel_buffer = (char *)malloc(WIDTH * HEIGHT * bytes_per_pixel);
// char pixel_buffer[WIDTH * HEIGHT * bytes_per_pixel];
// // char *pixel_buffer = (char *)malloc(WIDTH * HEIGHT *
// bytes_per_pixel); char pixel_buffer[WIDTH * HEIGHT * bytes_per_pixel];
// XImage *ximage = XCreateImage(display, visual, depth, ZPixmap, 0,
// pixel_buffer, WIDTH, HEIGHT, 32, 0);
+3
View File
@@ -0,0 +1,3 @@
#include "../Display/display.h"
extern int main(int argc, char **argv) { plr_display_x11_main(argc, argv); }
+10 -12
View File
@@ -1,15 +1,13 @@
#include "../Display/display.h"
__attribute__((force_align_arg_pointer)) __attribute__((noreturn)) extern void
/* No libc starting point. Jump straight into action! */
__attribute__((naked)) __attribute__((force_align_arg_pointer))
__attribute__((noreturn)) extern void
_start(void) {
plr_display_x11_main(0, 0);
__asm__ volatile("mov $60, %%rax\n\t"
"xor %%rdi, %%rdi\n\t"
"syscall"
:
:
: "%rax", "%rdi");
__builtin_unreachable();
__asm__ volatile("xor %rbp, %rbp\n"
"movq (%rsp), %rdi\n"
"leaq 8(%rsp), %rsi\n"
"call plr_display_x11_main\n"
"movq %rax, %rdi\n"
"movq $60, %rax\n"
"syscall");
}
+35 -4
View File
@@ -1,6 +1,37 @@
CC = clang
# todo: Only link to libm on debug.
CFLAGS = -Wall -std=c99 -g3 -O3 -flto=full
RELEASE ?= 0
SQUEEZE ?= 0
BITS ?= 64
CFLAGS = -Wall -std=gnu99 -flto=thin -ffast-math -fno-ident -fvisibility=hidden
ifeq ($(MAKECMDGOALS),linux)
ifeq ($(RELEASE), 1)
CFLAGS += -Wl,--ignore-data-address-equality -Wl,--ignore-function-address-equality \
-Wl,--optimize-bb-jumps
endif
ifeq ($(SQUEEZE), 1)
CFLAGS += -Wl,-T,tiny.ld -Wl,--icf=all -Wl,--bp-compression-sort=both \
-Wl,--nmagic
endif
ifeq ($(BITS), 64)
CFLAGS += -m64 -march=x86-64-v3 -mpopcnt
else
CFLAGS += -m32 -msse2
endif
endif
ifeq ($(MAKECMDGOALS),wasm)
ifeq ($(RELEASE), 1)
CFLAGS += -Wl,--compress-relocations
endif
endif
ifeq ($(RELEASE), 0)
CFLAGS += -g3 -O1
else
CFLAGS += -Wl,--strip-all -DNDEBUG -O3 -fuse-ld=lld -fmerge-all-constants -Wl,-O3 \
-ffunction-sections -fdata-sections -Wl,--gc-sections \
-Wl,-mllvm,--vectorize-slp=true \
-Wl,-mllvm,--enable-gvn-hoist=true
endif
# TODO: Just use amalgam build for all?
DEPS = ../Common/lzw.h ../Common/mat.h ../Common/vec.h ../Common/fix.h ../Common/def.h ../Common/bits.h \
../Common/gem.h \
../Common/Timer/time.h \
@@ -15,12 +46,12 @@ DOS = maindos.c Display/dos.c
$(CC) -c -o $@ $< $(CFLAGS)
# TODO: Make it work with no stdlib.
linux: CFLAGS += -msse2 -mbmi2 -nostdlib
linux: CFLAGS += -static -mbmi2 -nostdlib
linux: $(LINUX) | $(SRC)
$(CC) -o player $^ $(CFLAGS)
# Note: lld package might need to be downloaded.
wasm: CFLAGS += --target=wasm32 -nostdlib -Wl,--no-entry -msimd128
wasm: CFLAGS += --target=wasm32 -nostdlib -Wl,--no-entry -msimd128 -mbulk-memory
wasm:
$(CC) -o player.wasm $(WASM) $(CFLAGS)
+4
View File
@@ -2,6 +2,8 @@
Amalgamation file, compiling all in single unit.
*/
#include "../Common/bin.c"
#include "../Common/buf.c"
#include "../Common/def.h"
#include "../Common/fix.c"
#include "../Common/lzw.c"
@@ -14,6 +16,8 @@
#include "../Common/Timer/stub.c"
#include "./Display/x11.c"
#include "./Main/linux.c"
// #include "./Main/libc.c"
#elif COM_DEF_TARGET == COM_DEF_TARGET_WASM
#include "../Common/Std/stub.c"
#include "../Common/Timer/stub.c"
#endif
+7 -6
View File
@@ -1,4 +1,5 @@
#include "../Common/cfg.h"
#include "../Common/def.h"
#include "../Common/gem.h"
#include "../Common/std.h"
@@ -6,7 +7,7 @@
#include <stdint.h>
#if COM_DEF_TARGET == COM_DEF_TARGET_WASM
uint8_t plr_draw_buffer[640 * 480 * 4];
uint8_t plr_draw_buffer[COM_CFG_WIDTH * COM_CFG_HEIGHT * 4];
com_def_export_sym(
"plr_draw_get_pixel_buffer") uint8_t *plr_draw_get_pixel_buffer(void) {
@@ -16,9 +17,9 @@ com_def_export_sym(
com_def_export_sym("plr_draw_screen") void plr_draw_screen(
uint8_t *pixel_buffer) {
for (int y = 0; y < 480; y++) {
for (int x = 0; x < 640; x++) {
int pixel_index = (y * 640 + x) * 4;
for (int y = 0; y < COM_CFG_HEIGHT; y++) {
for (int x = 0; x < COM_CFG_WIDTH; x++) {
int pixel_index = (y * COM_CFG_WIDTH + x) * 4;
// Assuming standard Little-Endian 32-bit BGRA format
pixel_buffer[pixel_index + 0] = (x) % 256; // Blue
@@ -28,8 +29,8 @@ com_def_export_sym("plr_draw_screen") void plr_draw_screen(
}
}
com_mat_t proj =
com_mat_perspective(640, 480, 1, 100 * COM_FIX_FRACUNIT, COM_FIX_PIHALF);
com_mat_t proj = com_mat_perspective(COM_CFG_WIDTH, COM_CFG_HEIGHT, 1,
100 * COM_FIX_FRACUNIT, COM_FIX_PIHALF);
com_mat_t view = com_mat_look_at(com_vec_from(0, 0, 0),
com_vec_from(0, COM_FIX_FRACUNIT, 0),
com_vec_from(-COM_FIX_FRACUNIT, 0, 0));
+4 -1
View File
@@ -1,4 +1,5 @@
<!DOCTYPE html>
<!-- TODO: https://j9t.github.io/html-minifier-next/ -->
<html lang="en">
<head>
<meta charset="UTF-8">
@@ -16,6 +17,8 @@
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// TODO: Throttle FPS.
async function main() {
const wasm = await WebAssembly.instantiateStreaming(fetch("player.wasm"));
const { memory, plr_draw_screen, plr_draw_get_pixel_buffer } = wasm.instance.exports;
@@ -24,7 +27,7 @@
const wasmBuffer = new Uint8ClampedArray(
memory.buffer,
bufferPtr,
640 * 480 * 4
640 * 480 * 4 // TODO: Retrieve actual values.
);
const imgData = new ImageData(wasmBuffer, 640, 480);
+48
View File
@@ -0,0 +1,48 @@
OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64")
OUTPUT_ARCH(i386:x86-64)
ENTRY(_start)
PHDRS
{
text PT_LOAD FLAGS(5); /* RX: Read/Execute */
}
SECTIONS
{
PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000));
. = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS;
.text ALIGN(16) : {
*(.text*)
}
.rodata : {
*(.rodata*)
}
.data : {
*(.data*)
}
.bss : {
*(.bss*)
*(COMMON)
}
/* Discard bloat and unnecessary ELF sections */
/DISCARD/ : {
*(.note.GNU-stack)
*(.gnu_debuglink)
*(.gnu.lto_*)
*(.note.gnu.build-id)
*(.eh_frame_hdr)
*(.eh_frame_entry)
*(.eh_frame)
*(.comment)
*(.interp)
*(.dynsym)
*(.dynstr)
*(.hash)
*(.gnu.hash)
}
}