Skip to content

Instantly share code, notes, and snippets.

View mmozeiko's full-sized avatar

Mārtiņš Možeiko mmozeiko

View GitHub Profile
@mmozeiko
mmozeiko / MagicRingBuffer.h
Last active October 2, 2024 12:32
Magic RingBuffer for Windows, Linux and macOS
#pragma once
//
// Magic Ring Buffer
// https://gist.github.com/mmozeiko/3b09a340f3c53e5eaed699a1aea95250
//
// Sets up memory mapping so the same memory block follows itself in virtual address space:
//
// [abcd...xyz][abc...xyz]
//
@mmozeiko
mmozeiko / wic.h
Last active April 29, 2024 00:06
simple wrapper to load or save D3D11 texture from/to image file with Windows Imaging Component
#pragma once
#define COBJMACROS
#include <windows.h>
#include <d3d11.h>
//
// interface
//
@mmozeiko
mmozeiko / armv8_tsc.h
Last active August 31, 2024 13:06
armv8 timer & cycle counter
#pragma once
#define _GNU_SOURCE
#include <stdint.h>
#include <stdbool.h>
#include <sched.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/perf_event.h>
@mmozeiko
mmozeiko / preview.c
Last active July 23, 2024 06:37
windows shell preview handler example
// example of using Windows Preview Handler
// https://learn.microsoft.com/en-us/windows/win32/shell/preview-handlers
#define COBJMACROS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlwapi.h>
#include <shellapi.h>
#include <shobjidl.h>
@mmozeiko
mmozeiko / win32_webgpu.c
Last active September 20, 2024 04:24
setting up and using WebGPU in C using Dawn
// example how to set up WebGPU rendering on Windows in C
// uses Dawn implementation of WebGPU: https://dawn.googlesource.com/dawn/
// download pre-built Dawn webgpu.h/dll/lib files from https://github.com/mmozeiko/build-dawn/releases/latest
#include "webgpu.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define _USE_MATH_DEFINES
@mmozeiko
mmozeiko / upng.h
Last active February 25, 2024 08:08
uncompressed png writer & reader
#pragma once
// uncompressed png writer & reader
// supports only 8-bit and 16-bit formats
// Performance comparison for 8192x8192 BGRA8 image (256MB)
// Compiled with "clang -O2", AVX2 requires extra "-mavx2" or "/arch:AVX2" argument
//
// For libpng (compressed) uses default libpng/zlib compression settings
// For libpng (uncompressed) case following two functions are used:
@mmozeiko
mmozeiko / !README.md
Last active October 4, 2024 03:37
Download MSVC compiler/linker & Windows SDK without installing full Visual Studio

This downloads standalone MSVC compiler, linker & other tools, also headers/libraries from Windows SDK into portable folder, without installing Visual Studio. Has bare minimum components - no UWP/Store/WindowsRT stuff, just files & tools for native desktop app development.

Run py.exe portable-msvc.py and it will download output into msvc folder. By default it will download latest available MSVC & Windows SDK - currently v14.40.33807 and v10.0.26100.0.

You can list available versions with py.exe portable-msvc.py --show-versions and then pass versions you want with --msvc-version and --sdk-version arguments.

To use cl.exe/link.exe first run setup_TARGET.bat - after that PATH/INCLUDE/LIB env variables will be updated to use all the tools as usual. You can also use clang-cl.exe with these includes & libraries.

To use clang-cl.exe without running setup.bat, pass extra /winsysroot msvc argument (msvc is folder name where output is stored).

@mmozeiko
mmozeiko / FlushFileCache.c
Last active March 14, 2024 00:14
flush file cache on Windows
// clang.exe -Os -nostdlib -fuse-ld=lld -Wl,-fixed,-subsystem:console FlushFileCache.c -o FlushFileCache.exe
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment (lib, "kernel32.lib")
#pragma comment (lib, "advapi32.lib")
#define error(str) do { DWORD written; WriteFile(GetStdHandle(STD_ERROR_HANDLE), str, sizeof(str)-1, &written, NULL); } while (0)
@mmozeiko
mmozeiko / images2ico.py
Created January 10, 2022 02:24
packs multiple images into ico file
#!/usr/bin/env python3
# packs multiple images (bmp/png/...) into ico file
# width and height of images must be <= 256
# pixel format of images must be 32-bit RGBA
import argparse
import struct
import os
from PIL import Image # https://python-pillow.org/
@mmozeiko
mmozeiko / tls_client.c
Last active September 27, 2024 19:55
simple example of TLS socket client using win32 schannel api
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#define SECURITY_WIN32
#include <security.h>
#include <schannel.h>
#include <shlwapi.h>
#include <assert.h>
#include <stdio.h>