Skip to content

Instantly share code, notes, and snippets.

View mmozeiko's full-sized avatar

Mārtiņš Možeiko mmozeiko

View GitHub Profile
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
#include <windows.h>
#include <d3d11.h>
// interface
mmozeiko / armv8_tsc.h
Last active April 2, 2024 08:34
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 / preview.c
Last active May 5, 2024 23:40
windows shell preview handler example
// example of using Windows Preview Handler
#include <windows.h>
#include <shlwapi.h>
#include <shellapi.h>
#include <shobjidl.h>
mmozeiko / win32_webgpu.c
Last active February 19, 2024 20:20
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:
// download pre-built Dawn webgpu.h/dll/lib files from
#include "webgpu.h"
#include <windows.h>
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 / !
Last active May 21, 2024 06:27
Download MSVC compiler/linker & Windows SDK without installing full Visual Studio

This downloads standalone 64-bit 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 64-bit native desktop app development.

Run python.exe and it will download output into msvc folder. By default it will download latest available MSVC & Windows SDK - currently v14.32.17.2 and v10.0.22621.0.

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

To use cl.exe/link.exe from output folder, first run setup.bat - after that PATH/INCLUDE/LIB env variables will be setup 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 / 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
#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 /
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 #
mmozeiko / tls_client.c
Last active March 26, 2024 02:27
simple example of TLS socket client using win32 schannel api
#include <winsock2.h>
#include <windows.h>
#define SECURITY_WIN32
#include <security.h>
#include <schannel.h>
#include <shlwapi.h>
#include <assert.h>
#include <stdio.h>
mmozeiko / win32_opengl_multi.c
Last active March 4, 2024 05:57
setting up modern OpenGL 4.5 context for drawing to multiple windows
// example how to set up OpenGL core context on Windows for rendering to multiple windows
#define WINDOW_COUNT 4 // how many windows we'll be opening?
// important extension functionality used here:
// (4.3) KHR_debug:
// (4.5) ARB_direct_state_access:
// (4.1) ARB_separate_shader_objects:
// (4.2) ARB_shading_language_420pack:
// (4.3) ARB_explicit_uniform_location: