Skip to content

Commit

Permalink
add texture_arrays example
Browse files Browse the repository at this point in the history
  • Loading branch information
rajveermalviya committed Oct 4, 2023
1 parent e9bc15a commit 2543cc2
Show file tree
Hide file tree
Showing 9 changed files with 972 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ jobs:
make example-compute
make example-triangle
make example-enumerate_adapters
make example-texture_arrays
- if: ${{ matrix.run_examples }}
name: Run examples debug
run: |
Expand All @@ -205,10 +206,10 @@ jobs:
make example-compute-release
make example-triangle-release
make example-enumerate_adapters-release
make example-texture_arrays-release
- if: ${{ matrix.run_examples }}
name: Run examples release
run: |
make run-example-capture-release
make run-example-compute-release
make run-example-enumerate_adapters-release
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ example-enumerate_adapters-release: examples-release
run-example-enumerate_adapters-release: example-enumerate_adapters-release
cd examples/triangle && "../build/RelWithDebInfo/enumerate_adapters/enumerate_adapters"

example-texture_arrays: examples-debug
cd examples/build/Debug && cmake --build . --target texture_arrays

run-example-texture_arrays: example-texture_arrays
cd examples/texture_arrays && "../build/Debug/texture_arrays/texture_arrays"

example-texture_arrays-release: examples-release
cd examples/build/RelWithDebInfo && cmake --build . --target texture_arrays

run-example-texture_arrays-release: example-texture_arrays-release
cd examples/texture_arrays && "../build/RelWithDebInfo/texture_arrays/texture_arrays"

example-triangle: examples-debug
cd examples/build/Debug && cmake --build . --target triangle

Expand Down
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.20)
project(examples LANGUAGES C)

set(CMAKE_C_STANDARD 99)

if(APPLE)
add_compile_options(-x objective-c)
endif()
Expand Down Expand Up @@ -30,6 +32,7 @@ add_subdirectory(framework)
add_subdirectory(capture)
add_subdirectory(compute)
add_subdirectory(enumerate_adapters)
add_subdirectory(texture_arrays)
add_subdirectory(triangle)

set(GLFW_USE_WAYLAND 1)
Expand Down
36 changes: 32 additions & 4 deletions examples/framework/framework.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#include "framework.h"
#include "wgpu.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

static void log_callback(WGPULogLevel level, char const *message,
void *userdata) {
Expand Down Expand Up @@ -88,6 +84,38 @@ WGPUShaderModule frmwrk_load_shader_module(WGPUDevice device,
return shader_module;
}

#define COPY_BUFFER_ALIGNMENT 4
#define MAX(A, B) ((A) > (B) ? (A) : (B))

WGPUBuffer frmwrk_device_create_buffer_init(
WGPUDevice device, frmwrk_buffer_init_descriptor descriptor[static 1]) {
if (descriptor->content_size == 0) {
return wgpuDeviceCreateBuffer(device, &(WGPUBufferDescriptor){
.label = descriptor->label,
.size = 0,
.usage = descriptor->usage,
.mappedAtCreation = false,
});
}

size_t unpadded_size = descriptor->content_size;
size_t align_mask = COPY_BUFFER_ALIGNMENT - 1;
size_t padded_size =
MAX((unpadded_size + align_mask) & ~align_mask, COPY_BUFFER_ALIGNMENT);
WGPUBuffer buffer =
wgpuDeviceCreateBuffer(device, &(WGPUBufferDescriptor){
.label = descriptor->label,
.size = padded_size,
.usage = descriptor->usage,
.mappedAtCreation = true,
});
void *buf = wgpuBufferGetMappedRange(buffer, 0, unpadded_size);
memcpy(buf, descriptor->content, unpadded_size);
wgpuBufferUnmap(buffer);

return buffer;
}

#define print_storage_report(report, prefix) \
printf("%snumOccupied=%zu\n", prefix, report.numOccupied); \
printf("%snumVacant=%zu\n", prefix, report.numVacant); \
Expand Down
14 changes: 14 additions & 0 deletions examples/framework/framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@
#define FRAMEWORK_H

#include "wgpu.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define UNUSED(x) (void)x;

typedef struct frmwrk_buffer_init_descriptor {
WGPU_NULLABLE char const *label;
WGPUBufferUsageFlags usage;
void *content;
size_t content_size;
} frmwrk_buffer_init_descriptor;

void frmwrk_setup_logging(WGPULogLevel level);
WGPUShaderModule frmwrk_load_shader_module(WGPUDevice device, const char *name);
void frmwrk_print_global_report(WGPUGlobalReport report);
WGPUBuffer frmwrk_device_create_buffer_init(
WGPUDevice device, frmwrk_buffer_init_descriptor descriptor[static 1]);

#endif // FRAMEWORK_H
27 changes: 27 additions & 0 deletions examples/texture_arrays/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 3.20)
project(texture_arrays LANGUAGES C)

add_executable(texture_arrays main.c)

if (MSVC)
add_compile_options(/W4)
else()
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

include_directories(${CMAKE_SOURCE_DIR}/../ffi)
include_directories(${CMAKE_SOURCE_DIR}/framework)
include_directories(${DEP_GLFW_DIR}/include)

if (WIN32)
add_definitions(-DWGPU_TARGET_WINDOWS)
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll)
elseif(UNIX AND NOT APPLE)
add_definitions(-DWGPU_TARGET_LINUX_X11)
set(OS_LIBRARIES "-lm -ldl")
elseif(APPLE)
add_definitions(-DWGPU_TARGET_MACOS)
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal")
endif()

target_link_libraries(texture_arrays framework glfw ${WGPU_LIBRARY} ${OS_LIBRARIES})
61 changes: 61 additions & 0 deletions examples/texture_arrays/indexing.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
struct VertexInput {
@location(0) position: vec2<f32>,
@location(1) tex_coord: vec2<f32>,
@location(2) index: i32,
}

struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) tex_coord: vec2<f32>,
@location(1) index: i32,
}

@vertex
fn vert_main(vertex: VertexInput) -> VertexOutput {
var outval: VertexOutput;
outval.position = vec4<f32>(vertex.position.x, vertex.position.y, 0.0, 1.0);
outval.tex_coord = vertex.tex_coord;
outval.index = vertex.index;
return outval;
}

struct FragmentInput {
@location(0) tex_coord: vec2<f32>,
@location(1) index: i32,
}

@group(0) @binding(0)
var texture_array_top: binding_array<texture_2d<f32>>;
@group(0) @binding(1)
var texture_array_bottom: binding_array<texture_2d<f32>>;
@group(0) @binding(2)
var sampler_array: binding_array<sampler>;

struct Uniforms {
index: u32,
}

@group(0) @binding(3)
var<uniform> uniforms: Uniforms;

@fragment
fn uniform_main(fragment: FragmentInput) -> @location(0) vec4<f32> {
var outval: vec3<f32>;
if fragment.tex_coord.y <= 0.5 {
outval = textureSampleLevel(
texture_array_top[uniforms.index],
sampler_array[uniforms.index],
fragment.tex_coord,
0.0
).rgb;
} else {
outval = textureSampleLevel(
texture_array_bottom[uniforms.index],
sampler_array[uniforms.index],
fragment.tex_coord,
0.0
).rgb;
}

return vec4<f32>(outval.x, outval.y, outval.z, 1.0);
}
Loading

0 comments on commit 2543cc2

Please sign in to comment.