Skip to content

Commit

Permalink
gpu render example tweaks, makefile simplificaiton
Browse files Browse the repository at this point in the history
  • Loading branch information
austinvhuang committed Jun 15, 2024
1 parent c163673 commit 9af7a65
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
29 changes: 16 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ USE_WGPU=-DWEBGPU_TAG=wgpu

.PHONY: demo tests libgpu debug build check-entr watch-demo watch-tests clean

FLAGS = -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_CXX_COMPILER=$(CXX)
FLAGS = --trace -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_CXX_COMPILER=$(CXX)

# TODO(avh): decide whether to use wgpu as default
FASTBUILD_FLAGS = $(FLAGS) -DFASTBUILD:BOOL=ON
Expand All @@ -19,53 +19,56 @@ DEBUG_FLAGS = $(FLAGS) -DDEBUG:BOOL=ON
EMSCRIPTEN_FLAGS = -DIMPLEMENTATION=emscripten -DCMAKE_CXX_COMPILER=em++
LOCAL_FLAGS = -DUSE_LOCAL_LIBS=ON

# CMAKE command variable
CMAKE_CMD = mkdir -p build && cd build && cmake ..

demo: check-dependencies
mkdir -p build && cd build && cmake .. $(FASTBUILD_FLAGS) && make -j$(NUM_JOBS) $(TARGET_DEMO) && ./$(TARGET_DEMO)
$(CMAKE_CMD) $(FASTBUILD_FLAGS) && make -j$(NUM_JOBS) $(TARGET_DEMO) && ./$(TARGET_DEMO)

# check for the existence of clang++ and cmake
check-dependencies:
@command -v clang++ >/dev/null 2>&1 || { echo >&2 "Please install clang++ with 'sudo apt-get install clang' or 'brew install llvm'"; exit 1; }
@command -v cmake >/dev/null 2>&1 || { echo >&2 "Please install cmake with 'sudo apt-get install cmake' or 'brew install cmake'"; exit 1; }

tests: check-dependencies
mkdir -p build && cd build && cmake .. $(FASTBUILD_FLAGS) && make -j$(NUM_JOBS) $(TARGET_TESTS) && ./$(TARGET_TESTS)
$(CMAKE_CMD) $(FASTBUILD_FLAGS) && make -j$(NUM_JOBS) $(TARGET_TESTS) && ./$(TARGET_TESTS)

libgpu: check-dependencies
mkdir -p build && cd build && cmake .. $(RELEASE_FLAGS) && make -j$(NUM_JOBS) gpu
$(CMAKE_CMD) $(RELEASE_FLAGS) && make -j$(NUM_JOBS) gpu

debug: check-dependencies
mkdir -p build && cd build && cmake .. $(DEBUG_FLAGS) && make -j$(NUM_JOBS) $(TARGET_ALL)
$(CMAKE_CMD) $(DEBUG_FLAGS) && make -j$(NUM_JOBS) $(TARGET_ALL)

build: check-dependencies
mkdir -p build && cd build && cmake .. $(RELEASE_FLAGS) && make -j$(NUM_JOBS) $(TARGET_ALL)
$(CMAKE_CMD) $(RELEASE_FLAGS) && make -j$(NUM_JOBS) $(TARGET_ALL)

emscripten: check-dependencies
mkdir -p build && cd build && cmake .. $(EMSCRIPTEN_FLAGS) -DIMPLEMENTATION=emscripten && make -j$(NUM_JOBS) $(TARGET_ALL)
$(CMAKE_CMD) $(EMSCRIPTEN_FLAGS) -DIMPLEMENTATION=emscripten && make -j$(NUM_JOBS) $(TARGET_ALL)

check-entr:
@command -v entr >/dev/null 2>&1 || { echo >&2 "Please install entr with 'brew install entr' or 'sudo apt-get install entr'"; exit 1; }

watch-demo: check-entr check-dependencies
mkdir -p build && cd build && cmake .. $(FASTBUILD_FLAGS) && ls ../* ../utils/* | entr -s "rm -f $(TARGET_DEMO) && make -j$(NUM_JOBS) $(TARGET_DEMO) && ./$(TARGET_DEMO)"
$(CMAKE_CMD) $(FASTBUILD_FLAGS) && ls ../* ../utils/* | entr -s "rm -f $(TARGET_DEMO) && make -j$(NUM_JOBS) $(TARGET_DEMO) && ./$(TARGET_DEMO)"

watch-tests: check-entr check-dependencies
mkdir -p build && cd build && cmake .. $(FASTBUILD_FLAGS) && ls ../* ../utils/* | entr -s "rm -f $(TARGET_TESTS) && make -j$(NUM_JOBS) $(TARGET_TESTS) && ./$(TARGET_TESTS)"
$(CMAKE_CMD) $(FASTBUILD_FLAGS) && ls ../* ../utils/* | entr -s "rm -f $(TARGET_TESTS) && make -j$(NUM_JOBS) $(TARGET_TESTS) && ./$(TARGET_TESTS)"

# experimental
watch-tests-wgpu: check-entr check-dependencies
mkdir -p build && cd build && cmake .. $(FASTBUILD_FLAGS) $(USE_WGPU) && ls ../* ../utils/* | entr -s "rm -f $(TARGET_TESTS) && make -j$(NUM_JOBS) $(TARGET_TESTS) && ./$(TARGET_TESTS)"
$(CMAKE_CMD) $(FASTBUILD_FLAGS) $(USE_WGPU) && ls ../* ../utils/* | entr -s "rm -f $(TARGET_TESTS) && make -j$(NUM_JOBS) $(TARGET_TESTS) && ./$(TARGET_TESTS)"

watch-demo-local: check-entr check-dependencies
mkdir -p build && cd build && cmake .. $(FASTBUILD_FLAGS) $(LOCAL_FLAGS) && ls ../* ../utils/* | entr -s "rm -f $(TARGET_DEMO) && make -j$(NUM_JOBS) $(TARGET_DEMO) && ./$(TARGET_DEMO)"
$(CMAKE_CMD) $(FASTBUILD_FLAGS) $(LOCAL_FLAGS) && ls ../* ../utils/* | entr -s "rm -f $(TARGET_DEMO) && make -j$(NUM_JOBS) $(TARGET_DEMO) && ./$(TARGET_DEMO)"

watch-tests-local: check-entr check-dependencies
mkdir -p build && cd build && cmake .. $(FASTBUILD_FLAGS) $(LOCAL_FLAGS) && ls ../* ../utils/* | entr -s "rm -f $(TARGET_TESTS) && make -j$(NUM_JOBS) $(TARGET_TESTS) && ./$(TARGET_TESTS)"
$(CMAKE_CMD) $(FASTBUILD_FLAGS) $(LOCAL_FLAGS) && ls ../* ../utils/* | entr -s "rm -f $(TARGET_TESTS) && make -j$(NUM_JOBS) $(TARGET_TESTS) && ./$(TARGET_TESTS)"

clean-build:
read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/*

clean:
read -r -p "This will delete the contents of build/* and third_party/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* third_party/fetchcontent/*
read -r -p "This will delete the contents of build/* and third_party/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* third_party/fetchcontent/* third_party/gpu-build third_party/gpu-subbuild third_party/gpu-src

all: build
cd examples/gpu_puzzles && make
Expand Down
65 changes: 36 additions & 29 deletions examples/raymarch/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,46 @@ fn main(@builtin(global_invocation_id) GlobalInvocationID: vec3<u32>) {
return;
}
// Screen coordinates for this thread.
let x: f32 = f32(GlobalInvocationID.x);
let y: f32 = f32(GlobalInvocationID.y);
let p: vec3<f32> = vec3<f32>((x / f32(params.screenWidth)) - 0.5,
(y / f32(params.screenHeight)) - 0.5,
// ray position, starting at the camera
var p: vec3<f32> = vec3<f32>((x / f32(params.screenWidth)) * 2.0 - 1.0,
(y / f32(params.screenHeight)) * 2.0 - 1.0,
params.focalLength);
let offsetX: f32 = sin(f32(params.time) / 1000) * 0.2;
let offsetY: f32 = cos(f32(params.time) / 1000) * 0.2;
let offsetZ: f32 = cos(f32(params.time) / 1000) * 0.1;
let len: f32 = length(p);
let dir: vec3<f32> = vec3<f32>(p.x / len, p.y / len, p.z / len);
// object dynamics
// let offsetX: f32 = sin(f32(params.time) / 200) * 6.0;
// let offsetY: f32 = cos(f32(params.time) / 200) * 6.0;
let offsetX: f32 = 0.0;
let offsetY: f32 = 0.0;
let offsetZ: f32 = sin(f32(params.time) / 400) * 2.0;
// let offsetZ: f32 = 0.0;
let c: vec3<f32> = vec3<f32>(params.sphereCenterX + offsetX,
params.sphereCenterY + offsetY,
params.sphereCenterZ + offsetZ);
let len: f32 = length(p);
let dir: vec3<f32> = vec3<f32>(p.x / len, p.y / len, p.z / len);
let dist: f32 = 0.0;
out[GlobalInvocationID.y * params.screenWidth + GlobalInvocationID.x] = 0.0;
let maxIter: u32 = 40;
let maxIter: u32 = 10;
// march the ray in the direction of dir by length derived by the SDF
for (var i: u32 = 0; i < maxIter; i++) {
let dist: f32 = sdf(p, c, params.sphereRadius);
if (abs(dist) < .001) {
// largest step we can take w/o intersection is = SDF value at point
let step : f32 = sdf(p, c, params.sphereRadius);
if (abs(step) < .001) {
return;
}
out[GlobalInvocationID.y * params.screenWidth + GlobalInvocationID.x] =
max(0, min(10.0, out[GlobalInvocationID.y * params.screenWidth + GlobalInvocationID.x] + step));
if (out[GlobalInvocationID.y * params.screenWidth + GlobalInvocationID.x] == 10.0) {
return;
}
out[GlobalInvocationID.y * params.screenWidth + GlobalInvocationID.x] += dist;
// TODO(avh) : march the ray - comment for now until we get the scaling right
// p = p + dir * step;
}
p = p + dir * step;
}
}
Expand All @@ -80,7 +90,7 @@ std::uint32_t getCurrentTimeInMilliseconds() {
int main(int argc, char **argv) {

constexpr size_t NROWS = 16;
constexpr size_t NCOLS = 96;
constexpr size_t NCOLS = 64;

std::array<float, NROWS * NCOLS> screen;

Expand All @@ -96,9 +106,9 @@ int main(int argc, char **argv) {
} params = {/* focal length */ 0.2,
NCOLS,
NROWS,
/* radius */ 1.5,
0.0,
0.0,
/* radius */ 2.0,
/* x */ 0.0,
/* y */ 0.0,
/* z */ 5.0,
0};

Expand All @@ -119,17 +129,17 @@ int main(int argc, char **argv) {
ToCPU(ctx, devScreen, screen.data(), sizeof(screen));

static const char intensity[] = "@%#*+=-:. ";
// clear the screen
printf("\033[2J");
// clear the screen, move cursor to the top
printf("\033[2J\033[H");

fprintf(stdout, "%s",
show<float, NROWS, NCOLS>(screen, "Raw values").c_str());

// normalize values
float min = *std::min_element(screen.begin(), screen.end());
float max = *std::max_element(screen.begin(), screen.end());
// float min = 0.0;
// float max = 5.0;
// float min = *std::min_element(screen.begin(), screen.end());
// float max = *std::max_element(screen.begin(), screen.end());
float min = 0.0;
float max = 10.0;

for (size_t i = 0; i < screen.size(); ++i) {
screen[i] = (screen[i] - min) / (max - min);
Expand All @@ -150,8 +160,5 @@ int main(int argc, char **argv) {
}
printf("\n");
}

// wait for key
// getchar();
}
}
2 changes: 1 addition & 1 deletion third_party/local/WebGPU-distribution

0 comments on commit 9af7a65

Please sign in to comment.