From 9af7a656455227d80a716529b5d4e565aec78464 Mon Sep 17 00:00:00 2001 From: austinvhuang Date: Sat, 15 Jun 2024 13:48:56 -0400 Subject: [PATCH] gpu render example tweaks, makefile simplificaiton --- Makefile | 29 ++++++------ examples/raymarch/run.cpp | 65 +++++++++++++++------------ third_party/local/WebGPU-distribution | 2 +- 3 files changed, 53 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index e3d7e7d..7e27753 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -19,8 +19,11 @@ 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: @@ -28,44 +31,44 @@ check-dependencies: @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 diff --git a/examples/raymarch/run.cpp b/examples/raymarch/run.cpp index c7963c2..c67033c 100644 --- a/examples/raymarch/run.cpp +++ b/examples/raymarch/run.cpp @@ -35,36 +35,46 @@ fn main(@builtin(global_invocation_id) GlobalInvocationID: vec3) { return; } - // Screen coordinates for this thread. let x: f32 = f32(GlobalInvocationID.x); let y: f32 = f32(GlobalInvocationID.y); - let p: vec3 = vec3((x / f32(params.screenWidth)) - 0.5, - (y / f32(params.screenHeight)) - 0.5, + // ray position, starting at the camera + var p: vec3 = vec3((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 = vec3(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 = vec3(params.sphereCenterX + offsetX, params.sphereCenterY + offsetY, params.sphereCenterZ + offsetZ); - let len: f32 = length(p); - - let dir: vec3 = vec3(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; } } @@ -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 screen; @@ -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}; @@ -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(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); @@ -150,8 +160,5 @@ int main(int argc, char **argv) { } printf("\n"); } - - // wait for key - // getchar(); } } diff --git a/third_party/local/WebGPU-distribution b/third_party/local/WebGPU-distribution index 1025b97..34ed834 160000 --- a/third_party/local/WebGPU-distribution +++ b/third_party/local/WebGPU-distribution @@ -1 +1 @@ -Subproject commit 1025b977e1927b6d0327e67352f90feb4bcf8274 +Subproject commit 34ed83417a8454917d4fb3dc1a1c4bba8a55bc0b