Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for undefined constants instead of zero values #218

Merged
merged 5 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,28 @@ lib-native-release: Cargo.lock Cargo.toml Makefile $(WILDCARD_SOURCE)
cargo build --release $(EXTRA_BUILD_ARGS)

example-compute: lib-native examples/compute/main.c
cd examples/compute && $(CREATE_BUILD_DIR) && cd build && cmake -DCMAKE_BUILD_TYPE=Debug .. $(GENERATOR_PLATFORM) && cmake --build .
cd examples/compute && $(CREATE_BUILD_DIR) && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .. $(GENERATOR_PLATFORM) && cmake --build .

run-example-compute: example-compute
cd examples/compute && "$(OUTPUT_DIR)/compute" 1 2 3 4

example-triangle: lib-native examples/triangle/main.c
cd examples/triangle && $(CREATE_BUILD_DIR) && cd build && cmake -DCMAKE_BUILD_TYPE=Debug .. $(GENERATOR_PLATFORM) && cmake --build .
cd examples/triangle && $(CREATE_BUILD_DIR) && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .. $(GENERATOR_PLATFORM) && cmake --build .

run-example-triangle: example-triangle
cd examples/triangle && "$(OUTPUT_DIR)/triangle"

example-triangle-release: lib-native-release examples/triangle/main.c
cd examples/triangle && $(CREATE_BUILD_DIR) && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .. $(GENERATOR_PLATFORM) && cmake --build .

run-example-triangle-release: example-triangle-release
cd examples/triangle && "$(OUTPUT_DIR)/triangle"

build-helper:
cargo build -p helper

example-capture: lib-native build-helper examples/capture/main.c
cd examples/capture && $(CREATE_BUILD_DIR) && cd build && cmake -DCMAKE_BUILD_TYPE=Debug .. $(GENERATOR_PLATFORM) && cmake --build .
cd examples/capture && $(CREATE_BUILD_DIR) && cd build && cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .. $(GENERATOR_PLATFORM) && cmake --build .
cwfitzgerald marked this conversation as resolved.
Show resolved Hide resolved

run-example-capture: example-capture
cd examples/capture && "$(OUTPUT_DIR)/capture"
40 changes: 22 additions & 18 deletions examples/capture/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
#include "unused.h"
#include "webgpu-headers/webgpu.h"
#include "wgpu.h"
#include <stdio.h>

static void handle_device_lost(WGPUDeviceLostReason reason, char const *message,
void *userdata) {
UNUSED(userdata);

printf("DEVICE LOST (%d): %s\n", reason, message);
}

static void handle_uncaptured_error(WGPUErrorType type, char const *message,
void *userdata) {
UNUSED(userdata);

printf("UNCAPTURED ERROR (%d): %s\n", type, message);
}

int main(int argc, char *argv[]) {
UNUSED(argc);
Expand Down Expand Up @@ -31,10 +46,7 @@ int main(int argc, char *argv[]) {
.requiredLimits =
&(WGPURequiredLimits){
.nextInChain = NULL,
.limits =
(WGPULimits){
.maxBindGroups = 1,
},
.limits = WGPULimits_DEFAULT,
},
.defaultQueue =
(WGPUQueueDescriptor){
Expand All @@ -44,6 +56,9 @@ int main(int argc, char *argv[]) {
},
request_device_callback, (void *)&device);

wgpuDeviceSetUncapturedErrorCallback(device, handle_uncaptured_error, NULL);
wgpuDeviceSetDeviceLostCallback(device, handle_device_lost, NULL);

BufferDimensions bufferDimensions = buffer_dimensions_new(width, height);
uint64_t bufferSize =
bufferDimensions.padded_bytes_per_row * bufferDimensions.height;
Expand Down Expand Up @@ -77,25 +92,14 @@ int main(int argc, char *argv[]) {
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(
device, &(WGPUCommandEncoderDescriptor){.label = NULL});

WGPUTextureView outputAttachment = wgpuTextureCreateView(
texture, &(WGPUTextureViewDescriptor){
.nextInChain = NULL,
.label = NULL,
.format = WGPUTextureFormat_Undefined,
.dimension = WGPUTextureViewDimension_Undefined,
.aspect = WGPUTextureAspect_All,
.arrayLayerCount = 0,
.baseArrayLayer = 0,
.baseMipLevel = 0,
.mipLevelCount = 0,
});
WGPUTextureView outputAttachment = wgpuTextureCreateView(texture, NULL);

WGPURenderPassEncoder renderPass = wgpuCommandEncoderBeginRenderPass(
encoder, &(WGPURenderPassDescriptor){
.colorAttachments =
&(WGPURenderPassColorAttachment){
.view = outputAttachment,
.resolveTarget = 0,
.resolveTarget = NULL,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue =
Expand Down Expand Up @@ -129,7 +133,7 @@ int main(int argc, char *argv[]) {
(WGPUTextureDataLayout){
.offset = 0,
.bytesPerRow = bufferDimensions.padded_bytes_per_row,
.rowsPerImage = 0,
.rowsPerImage = WGPU_COPY_STRIDE_UNDEFINED,
}},
&textureExtent);

Expand Down
28 changes: 23 additions & 5 deletions examples/compute/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@
#include "wgpu.h"

#include "framework.h"
#include "unused.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
static void handle_device_lost(WGPUDeviceLostReason reason, char const *message,
void *userdata) {
UNUSED(userdata);

printf("DEVICE LOST (%d): %s\n", reason, message);
}

static void handle_uncaptured_error(WGPUErrorType type, char const *message,
void *userdata) {
UNUSED(userdata);

printf("UNCAPTURED ERROR (%d): %s\n", type, message);
}

int main(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);

uint32_t numbers[] = {1, 2, 3, 4};
uint32_t numbersSize = sizeof(numbers);
uint32_t numbersLength = numbersSize / sizeof(uint32_t);
Expand All @@ -31,10 +49,7 @@ int main() {
.requiredLimits =
&(WGPURequiredLimits){
.nextInChain = NULL,
.limits =
(WGPULimits){
.maxBindGroups = 1,
},
.limits = WGPULimits_DEFAULT,
},
.defaultQueue =
(WGPUQueueDescriptor){
Expand All @@ -44,6 +59,9 @@ int main() {
},
request_device_callback, (void *)&device);

wgpuDeviceSetUncapturedErrorCallback(device, handle_uncaptured_error, NULL);
wgpuDeviceSetDeviceLostCallback(device, handle_device_lost, NULL);

WGPUShaderModuleDescriptor shaderSource = load_wgsl("shader.wgsl");
WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shaderSource);

Expand Down
2 changes: 1 addition & 1 deletion examples/framework.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void logCallback(WGPULogLevel level, const char *msg, void *userdata) {
printf("[%s] %s\n", level_str, msg);
}

void initializeLog() {
void initializeLog(void) {
wgpuSetLogCallback(logCallback, NULL);
wgpuSetLogLevel(WGPULogLevel_Warn);
}
Expand Down
32 changes: 31 additions & 1 deletion examples/framework.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
#include "wgpu.h"

#define WGPULimits_DEFAULT \
(WGPULimits) { \
.maxBindGroups = WGPU_LIMIT_U32_UNDEFINED, \
.maxTextureDimension1D = WGPU_LIMIT_U32_UNDEFINED, \
.maxTextureDimension2D = WGPU_LIMIT_U32_UNDEFINED, \
.maxTextureDimension3D = WGPU_LIMIT_U32_UNDEFINED, \
.maxTextureArrayLayers = WGPU_LIMIT_U32_UNDEFINED, \
.maxDynamicUniformBuffersPerPipelineLayout = WGPU_LIMIT_U32_UNDEFINED, \
.maxDynamicStorageBuffersPerPipelineLayout = WGPU_LIMIT_U32_UNDEFINED, \
.maxSampledTexturesPerShaderStage = WGPU_LIMIT_U32_UNDEFINED, \
.maxSamplersPerShaderStage = WGPU_LIMIT_U32_UNDEFINED, \
.maxStorageBuffersPerShaderStage = WGPU_LIMIT_U32_UNDEFINED, \
.maxStorageTexturesPerShaderStage = WGPU_LIMIT_U32_UNDEFINED, \
.maxUniformBuffersPerShaderStage = WGPU_LIMIT_U32_UNDEFINED, \
.maxUniformBufferBindingSize = WGPU_LIMIT_U64_UNDEFINED, \
.maxStorageBufferBindingSize = WGPU_LIMIT_U64_UNDEFINED, \
.minUniformBufferOffsetAlignment = WGPU_LIMIT_U32_UNDEFINED, \
.minStorageBufferOffsetAlignment = WGPU_LIMIT_U32_UNDEFINED, \
.maxVertexBuffers = WGPU_LIMIT_U32_UNDEFINED, \
.maxVertexAttributes = WGPU_LIMIT_U32_UNDEFINED, \
.maxVertexBufferArrayStride = WGPU_LIMIT_U32_UNDEFINED, \
.maxInterStageShaderComponents = WGPU_LIMIT_U32_UNDEFINED, \
.maxComputeWorkgroupStorageSize = WGPU_LIMIT_U32_UNDEFINED, \
.maxComputeInvocationsPerWorkgroup = WGPU_LIMIT_U32_UNDEFINED, \
.maxComputeWorkgroupSizeX = WGPU_LIMIT_U32_UNDEFINED, \
.maxComputeWorkgroupSizeY = WGPU_LIMIT_U32_UNDEFINED, \
.maxComputeWorkgroupSizeZ = WGPU_LIMIT_U32_UNDEFINED, \
.maxComputeWorkgroupsPerDimension = WGPU_LIMIT_U32_UNDEFINED, \
}

WGPUShaderModuleDescriptor load_wgsl(const char *name);

void request_adapter_callback(WGPURequestAdapterStatus status,
Expand All @@ -12,7 +42,7 @@ void request_device_callback(WGPURequestDeviceStatus status,

void readBufferMap(WGPUBufferMapAsyncStatus status, void *userdata);

void initializeLog();
void initializeLog(void);

void printGlobalReport(WGPUGlobalReport report);
void printAdapterFeatures(WGPUAdapter adapter);
11 changes: 5 additions & 6 deletions examples/triangle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ static void handleGlfwKey(GLFWwindow *window, int key, int scancode, int action,
}
}

int main() {
int main(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);
initializeLog();

if (!glfwInit()) {
Expand Down Expand Up @@ -186,10 +188,7 @@ int main() {
.requiredLimits =
&(WGPURequiredLimits){
.nextInChain = NULL,
.limits =
(WGPULimits){
.maxBindGroups = 1,
},
.limits = WGPULimits_DEFAULT,
},
.defaultQueue =
(WGPUQueueDescriptor){
Expand Down Expand Up @@ -325,7 +324,7 @@ int main() {
.colorAttachments =
&(WGPURenderPassColorAttachment){
.view = nextTexture,
.resolveTarget = 0,
.resolveTarget = NULL,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue =
Expand Down
33 changes: 27 additions & 6 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::native::{IntoHandle, IntoHandleWithContext, UnwrapId, Handle};
use crate::native::{Handle, IntoHandle, IntoHandleWithContext, UnwrapId};
use crate::{conv, handle_device_error, make_slice, native, Context, OwnedLabel};
use std::ffi::CStr;
use std::os::raw::c_char;
Expand Down Expand Up @@ -84,7 +84,12 @@ pub unsafe extern "C" fn wgpuCommandEncoderClearBuffer(
command_encoder,
buffer,
offset,
NonZeroU64::new(size)))
match size {
0 => panic!("invalid size"),
conv::WGPU_WHOLE_SIZE => None,
_ => Some(NonZeroU64::new_unchecked(size)),
}
))
.expect("Unable to clear buffer")
}

Expand Down Expand Up @@ -562,7 +567,11 @@ pub unsafe extern "C" fn wgpuRenderPassEncoderSetIndexBuffer(
buffer,
conv::map_index_format(index_format).expect("Index format cannot be undefined"),
offset,
NonZeroU64::new(size),
match size {
0 => panic!("invalid size"),
conv::WGPU_WHOLE_SIZE => None,
_ => Some(NonZeroU64::new_unchecked(size)),
},
);
}

Expand All @@ -582,7 +591,11 @@ pub unsafe extern "C" fn wgpuRenderPassEncoderSetVertexBuffer(
slot,
buffer,
offset,
NonZeroU64::new(size),
match size {
0 => panic!("invalid size"),
conv::WGPU_WHOLE_SIZE => None,
_ => Some(NonZeroU64::new_unchecked(size)),
},
);
}

Expand Down Expand Up @@ -848,7 +861,11 @@ pub unsafe extern "C" fn wgpuRenderBundleEncoderSetIndexBuffer(
buffer,
conv::map_index_format(format).unwrap(),
offset,
NonZeroU64::new(size),
match size {
0 => panic!("invalid size"),
conv::WGPU_WHOLE_SIZE => None,
_ => Some(NonZeroU64::new_unchecked(size)),
},
);
}

Expand Down Expand Up @@ -879,6 +896,10 @@ pub unsafe extern "C" fn wgpuRenderBundleEncoderSetVertexBuffer(
slot,
buffer,
offset,
NonZeroU64::new(size),
match size {
0 => panic!("invalid size"),
conv::WGPU_WHOLE_SIZE => None,
_ => Some(NonZeroU64::new_unchecked(size)),
},
);
}
Loading