ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | Submodules | LICENSE

Commit: 3e15062ae0db1f2773bc09a5921a6fa133e4f253
Parent: 76c64c1d574f8c941edbb8b32bca576b240515fc
Author: Randy Palamar
Date:   Fri,  4 Oct 2024 09:10:43 -0600

remove usage of w32 headers

This avoids all the conflicting type nonsense with raylib and all
the other w32 headers junk.

Diffstat:
Mbeamformer.h | 50--------------------------------------------------
Mos_win32.c | 110++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Mutil.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 131 insertions(+), 79 deletions(-)

diff --git a/beamformer.h b/beamformer.h @@ -2,14 +2,6 @@ #ifndef _BEAMFORMER_H_ #define _BEAMFORMER_H_ -#include <immintrin.h> - -#include <glad.h> - -#define GRAPHICS_API_OPENGL_43 -#include <raylib.h> -#include <rlgl.h> - #include "util.h" #define BG_COLOUR (v4){.r = 0.15, .g = 0.12, .b = 0.13, .a = 1.0} @@ -28,34 +20,6 @@ #define RECT_BTN_ROUNDNESS 0.3f #define RECT_BTN_BORDER_WIDTH 6.0f -typedef union { - struct { f32 x, y; }; - struct { f32 w, h; }; - f32 E[2]; - Vector2 rl; -} v2; - -typedef union { - struct { f32 x, y, z, w; }; - struct { f32 r, g, b, a; }; - struct { v3 xyz; f32 _1; }; - struct { f32 _2; v3 yzw; }; - struct { v2 xy, zw; }; - f32 E[4]; - Vector4 rl; -} v4; - -typedef union { - struct { v3 x, y, z; }; - v3 c[3]; - f32 E[9]; -} m3; - -typedef union { - struct { v2 pos, size; }; - Rectangle rl; -} Rect; - enum program_flags { RELOAD_SHADERS = 1 << 0, GEN_MIPMAPS = 1 << 29, @@ -92,20 +56,6 @@ typedef struct { f32 cursor_blink_target; } InputState; -#include "beamformer_parameters.h" -typedef struct { - BeamformerParameters raw; - enum compute_shaders compute_stages[16]; - u32 compute_stages_count; - b32 upload; -} BeamformerParametersFull; - -typedef struct { - size filesize; - u64 timestamp; -} FileStats; -#define ERROR_FILE_STATS (FileStats){.filesize = -1} - #if defined(__unix__) #include "os_unix.c" diff --git a/os_win32.c b/os_win32.c @@ -1,29 +1,83 @@ /* See LICENSE for license details. */ -#include <fileapi.h> -#include <handleapi.h> -#include <libloaderapi.h> -#include <memoryapi.h> -#include <sysinfoapi.h> +#include "util.h" -/* NOTE: copied from wtypes.h; we don't actually use this type but winbase.h needs it defined */ -typedef void *HWND; -#include <winbase.h> +#define PAGE_READWRITE 0x04 +#define MEM_COMMIT 0x1000 +#define MEM_RESERVE 0x2000 +#define MEM_RELEASE 0x8000 +#define GENERIC_WRITE 0x40000000 +#define GENERIC_READ 0x80000000 -#include "util.h" +#define PIPE_TYPE_BYTE 0x00 +#define PIPE_ACCESS_INBOUND 0x01 + +#define FILE_MAP_ALL_ACCESS 0x000F001F + +#define INVALID_HANDLE_VALUE (void *)-1 + +#define CREATE_ALWAYS 2 +#define OPEN_EXISTING 3 + +typedef struct { + u16 wProcessorArchitecture; + u16 _pad1; + u32 dwPageSize; + size lpMinimumApplicationAddress; + size lpMaximumApplicationAddress; + u64 dwActiveProcessorMask; + u32 dwNumberOfProcessors; + u32 dwProcessorType; + u32 dwAllocationGranularity; + u16 wProcessorLevel; + u16 wProcessorRevision; +} w32_sys_info; + +typedef struct { + u32 dwFileAttributes; + u64 ftCreationTime; + u64 ftLastAccessTime; + u64 ftLastWriteTime; + u32 dwVolumeSerialNumber; + u32 nFileSizeHigh; + u32 nFileSizeLow; + u32 nNumberOfLinks; + u32 nFileIndexHigh; + u32 nFileIndexLow; +} w32_file_info; + +#define W32(r) __declspec(dllimport) r __stdcall +W32(b32) CloseHandle(void *); +W32(b32) CopyFileA(c8 *, c8 *, b32); +W32(void *) CreateFileA(c8 *, u32, u32, void *, u32, u32, void *); +W32(void *) CreateFileMappingA(void *, void *, u32, u32, u32, c8 *); +W32(void *) CreateNamedPipeA(c8 *, u32, u32, u32, u32, u32, u32, void *); +W32(b32) DeleteFileA(c8 *); +W32(b32) FreeLibrary(void *); +W32(b32) GetFileInformationByHandle(void *, void *); +W32(i32) GetLastError(void); +W32(void *) GetProcAddress(void *, c8 *); +W32(void) GetSystemInfo(void *); +W32(void *) LoadLibraryA(c8 *); +W32(void *) MapViewOfFile(void *, u32, u32, u32, u64); +W32(b32) PeekNamedPipe(void *, u8 *, i32, i32 *, i32 *, i32 *); +W32(b32) ReadFile(void *, u8 *, i32, i32 *, void *); +W32(b32) WriteFile(void *, u8 *, i32, i32 *, void *); +W32(void *) VirtualAlloc(u8 *, size, u32, u32); +W32(b32) VirtualFree(u8 *, size, u32); #define OS_INVALID_FILE (INVALID_HANDLE_VALUE) -typedef HANDLE os_file; +typedef void *os_file; typedef struct { os_file file; char *name; } os_pipe; -typedef HANDLE os_library_handle; +typedef void *os_library_handle; static Arena os_alloc_arena(Arena a, size capacity) { - SYSTEM_INFO Info; + w32_sys_info Info; GetSystemInfo(&Info); if (capacity % Info.dwPageSize != 0) @@ -50,13 +104,13 @@ os_read_file(Arena *a, char *fname, size fsize) die("os_read_file: %s\nHandling files >4GB is not yet " "handled in win32 code\n", fname); - HANDLE h = CreateFileA(fname, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); + void *h = CreateFileA(fname, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); if (h == INVALID_HANDLE_VALUE) die("os_read_file: couldn't open file: %s\n", fname); s8 ret = s8alloc(a, fsize); - DWORD rlen = 0; + i32 rlen = 0; if (!ReadFile(h, ret.data, ret.len, &rlen, 0) && rlen != ret.len) die("os_read_file: couldn't read file: %s\n", fname); CloseHandle(h); @@ -72,11 +126,11 @@ os_write_file(char *fname, s8 raw) return 0; } - HANDLE h = CreateFileA(fname, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); + void *h = CreateFileA(fname, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); if (h == INVALID_HANDLE_VALUE) return 0; - DWORD wlen = 0; + i32 wlen; WriteFile(h, raw.data, raw.len, &wlen, 0); CloseHandle(h); return wlen == raw.len; @@ -85,12 +139,12 @@ os_write_file(char *fname, s8 raw) static FileStats os_get_file_stats(char *fname) { - HANDLE h = CreateFileA(fname, 0, 0, 0, OPEN_EXISTING, 0, 0); + void *h = CreateFileA(fname, 0, 0, 0, OPEN_EXISTING, 0, 0); if (h == INVALID_HANDLE_VALUE) { return ERROR_FILE_STATS; } - BY_HANDLE_FILE_INFORMATION fileinfo; + w32_file_info fileinfo; if (!GetFileInformationByHandle(h, &fileinfo)) { fputs("os_get_file_stats: couldn't get file info\n", stderr); CloseHandle(h); @@ -100,10 +154,8 @@ os_get_file_stats(char *fname) size filesize = (size)fileinfo.nFileSizeHigh << 32; filesize |= (size)fileinfo.nFileSizeLow; - u64 timestamp = (u64)fileinfo.ftLastWriteTime.dwHighDateTime << 32; - timestamp |= (u64)fileinfo.ftLastWriteTime.dwLowDateTime; - return (FileStats){.filesize = filesize, .timestamp = timestamp}; + return (FileStats){.filesize = filesize, .timestamp = fileinfo.ftLastWriteTime}; } /* NOTE: win32 doesn't pollute the filesystem so no need to waste the user's time */ @@ -115,22 +167,22 @@ os_close_named_pipe(os_pipe p) static os_pipe os_open_named_pipe(char *name) { - HANDLE h = CreateNamedPipeA(name, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE, 1, - 0, 1 * MEGABYTE, 0, 0); + void *h = CreateNamedPipeA(name, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE, 1, + 0, 1 * MEGABYTE, 0, 0); return (os_pipe){.file = h, .name = name}; } static b32 os_poll_pipe(os_pipe p) { - DWORD bytes_available = 0; + i32 bytes_available = 0; return PeekNamedPipe(p.file, 0, 1 * MEGABYTE, 0, &bytes_available, 0) && bytes_available; } static size os_read_pipe_data(os_pipe p, void *buf, size len) { - DWORD total_read = 0; + i32 total_read = 0; ReadFile(p.file, buf, len, &total_read, 0); return total_read; } @@ -138,8 +190,8 @@ os_read_pipe_data(os_pipe p, void *buf, size len) static BeamformerParametersFull * os_open_shared_memory_area(char *name) { - HANDLE h = CreateFileMappingA(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, - sizeof(BeamformerParametersFull), name); + void *h = CreateFileMappingA(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, + sizeof(BeamformerParametersFull), name); if (h == INVALID_HANDLE_VALUE) return NULL; @@ -159,7 +211,7 @@ static os_library_handle os_load_library(char *name, char *temp_name) { if (temp_name) { - if (CopyFile(name, temp_name, 0)) + if (CopyFileA(name, temp_name, 0)) name = temp_name; } @@ -180,7 +232,7 @@ os_lookup_dynamic_symbol(os_library_handle h, char *name) return 0; void *res = GetProcAddress(h, name); if (!res) - TraceLog(LOG_WARNING, "os_lookup_dynamic_symbol(%s): %s\n", name, GetLastError()); + TraceLog(LOG_WARNING, "os_lookup_dynamic_symbol(%s): %d\n", name, GetLastError()); return res; } diff --git a/util.h b/util.h @@ -5,6 +5,14 @@ #include <stddef.h> #include <stdint.h> +#include <immintrin.h> + +#include <glad.h> + +#define GRAPHICS_API_OPENGL_43 +#include <raylib.h> +#include <rlgl.h> + #ifndef asm #define asm __asm__ #endif @@ -31,6 +39,7 @@ #define U32_MAX (0xFFFFFFFFUL) +typedef char c8; typedef uint8_t u8; typedef int16_t i16; typedef uint16_t u16; @@ -65,11 +74,52 @@ typedef union { } uv4; typedef union { + struct { f32 x, y; }; + struct { f32 w, h; }; + Vector2 rl; + f32 E[2]; +} v2; + +typedef union { struct { f32 x, y, z; }; struct { f32 w, h, d; }; f32 E[3]; } v3; +typedef union { + struct { f32 x, y, z, w; }; + struct { f32 r, g, b, a; }; + struct { v3 xyz; f32 _1; }; + struct { f32 _2; v3 yzw; }; + struct { v2 xy, zw; }; + f32 E[4]; +} v4; + +typedef union { + struct { v3 x, y, z; }; + v3 c[3]; + f32 E[9]; +} m3; + +typedef union { + struct { v2 pos, size; }; + Rectangle rl; +} Rect; + +typedef struct { + size filesize; + u64 timestamp; +} FileStats; +#define ERROR_FILE_STATS (FileStats){.filesize = -1} + +#include "beamformer_parameters.h" +typedef struct { + BeamformerParameters raw; + enum compute_shaders compute_stages[16]; + u32 compute_stages_count; + b32 upload; +} BeamformerParametersFull; + #include "util.c" #endif /* _UTIL_H_ */