Skip to content

Instantly share code, notes, and snippets.

@cdwfs
cdwfs / http_server.py
Last active February 16, 2018 21:36
Python script to run an HTTP server that only accepts connections from localhost. Primarily intended as a workaround for cross-origin requests preventing you from loading local assets in your local JS scripts.
import argparse
import os
import os.path
import sys
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", type=int, help="Port to open for HTTP connections", default=80)
parser.add_argument("-d", "--dir", help="Root directory to serve", default=".")
args = parser.parse_args()
@cdwfs
cdwfs / track_followers.py
Last active February 9, 2024 22:47
Python script to track changes to a user's list of Twitter followers
#!/usr/bin/env python
# Usage:
# 1) Install the Python Twitter Tools (PTT) from http://mike.verdone.ca/twitter/
# (or just "easy_install twitter").
# 2) Run "twitter-follow -o blahblahblah" and follow the instructions to set up
# OAuth. This gives the twitter-follow script read-only access to your account.
# Technically, it doesn't even need to be *your* account that you authorize,
# since the script is only querying public follower information.
# 3) Modify the value of "my_handle" to the username of the Twitter user whose
@cdwfs
cdwfs / retval_check.h
Created July 18, 2014 23:30
Return value error-checking macro generator
/**
* Handy meta-macro to simplify repetitive error checking for APIs where every function returns
* an error code (e.g. CUDA, C11 threads, most of the Windows API, etc.)
*
* Example usage:
* #define CUDA_CHECK(expr) RETVAL_CHECK(cudaSuccess, expr)
* ...
* CUDA_CHECK( cudaMemcpy(dst, src, size, cudaMemcpyHostToDevice) );
*
* To disable the checks in release builds, just redefine the macro:
@cdwfs
cdwfs / cube-strip
Last active December 22, 2023 15:39
Rendering a cube as a 14-index triangle strip
; How to render a cube using a single 14-index triangle strip
; (Reposted from http://www.asmcommunity.net/forums/topic/?id=6284#post-45209)
; Drawbacks: no support for per-vertex normals, UVs, etc.
;
; 1-------3-------4-------2 Cube = 8 vertices
; | E __/|\__ A | H __/| =================
; | __/ | \__ | __/ | Single Strip: 4 3 7 8 5 3 1 4 2 7 6 5 2 1
; |/ D | B \|/ I | 12 triangles: A B C D E F G H I J K L
; 5-------8-------7-------6
; | C __/|
@cdwfs
cdwfs / QtCreator.ini
Last active June 10, 2016 17:07
QtCreator config
# These blocks go in .config/QtProject/QtCreator.ini.
# Not storing the file here due to potentially sensitive contents in file/search history.
[KeyboardShortcuts]
Coreplugin.OutputPane.nextitem=F4
CppEditor.FindUsages=Alt+Shift+F
CppEditor.RenameSymbolUnderCursor=Alt+Shift+R
CppTools.SwitchHeaderSource=Alt+O
Debugger.AttachToRemoteServer=Ctrl+F5
Find.Dialog=Alt+Shift+S
@cdwfs
cdwfs / vk_cpu_gpu_timestamp.cpp
Last active December 12, 2021 06:01
Vulkan function to get a pair of timestamps (one CPU, one GPU) corresponding to (very nearly) the same point in absolute wall time.
struct CpuGpuTimestampInfo {
VkDevice device;
VkQueue queue;
uint32_t queue_family_index;
float timestamp_period; // Copy from VkPhysicalDeviceLimits::timestampPeriod
uint32_t timestamp_valid_bits; // Copy from VkQueueFamilyProperties::timestampValidBits
};
VkResult GetCpuGpuTimestamp(const CpuGpuTimestampInfo *info,
std::chrono::high_resolution_clock::time_point *out_cpu_time, uint64_t *out_gpu_time) {
if (info->timestamp_valid_bits == 0) {
@cdwfs
cdwfs / windows_build_vvl.bat
Last active June 25, 2018 17:55
Windows batch script to build the Vulkan-ValidationLayers repo
REM Variables: Release/Debug, VS version, VS arch, path to python?
IF NOT EXIST Vulkan-Headers (
git clone https://github.com/KhronosGroup/Vulkan-Headers.git
)
IF NOT EXIST glslang (
git clone https://github.com/KhronosGroup/glslang.git
)
REM Update Vulkan-Headers & dependencies to known-good revisions and build
@cdwfs
cdwfs / permute.cpp
Last active November 20, 2018 22:48
Generate a permutation of 1..P values from a set of N (N>=P) using O(N) space, but with strict O(1) initialization and O(1) cost per selected element (not amortized)
// g++ -std=c++11 -o permute permute.cpp
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
const int kElemCount = 1024*1024;
const int kOutElemCount = kElemCount;
static_assert(kOutElemCount <= kElemCount, "can't output more than input");
@cdwfs
cdwfs / EntityHierarchy.cs
Last active January 14, 2019 22:47
Simple, pseudo-psuedocodey demonstration of how to communicate data from "parent" entities to "child" entities
/// The goal here is to have some processing in one set of entities (the parents) whose results need to
/// be consumed by a different set of entities (the children), where it's not possible to iterate over both
/// sets simultaneously.
/// In this specific example, each parent entity has two unique child entities.
/// Different approaches would be necessary for one-to-many relationships, many-to-many relationships, etc.
///
/// NB I've never actually compiled this code; there may be minor errors due to typos or API changes.
struct ParentData : IComponentData
{
Entity child1;
Shader "ProcHexGridSurfShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_AlbedoMaps("Albedo (RGB)", 2DArray) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_HexScale("Hex Scale", Range(0,1)) = 1.0
}