Skip to content

Commit

Permalink
Merge pull request #8 from ancientjpeg/refactor-result
Browse files Browse the repository at this point in the history
Remove `rndr::Result` In Exchange For `ustd::expected`
  • Loading branch information
ancientjpeg authored Nov 9, 2024
2 parents 74a590d + 3c86ed7 commit e210261
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 94 deletions.
22 changes: 11 additions & 11 deletions example/example0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
constexpr int w = 640;
constexpr int h = 480;

rndr::Result performBufferCopies(rndr::Application &program_gpu)
ustd::result performBufferCopies(rndr::Application &program_gpu)
{
const wgpu::Device &device = program_gpu.getDevice();

Expand Down Expand Up @@ -65,10 +65,10 @@ rndr::Result performBufferCopies(rndr::Application &program_gpu)
buffer1.Destroy();
buffer2.Destroy();

return rndr::Result::success();
return {};
}

rndr::Result renderFrame(rndr::Application &program_gpu)
ustd::result renderFrame(rndr::Application &program_gpu)
{

const wgpu::Device &device = program_gpu.getDevice();
Expand All @@ -87,7 +87,7 @@ rndr::Result renderFrame(rndr::Application &program_gpu)
oss << "Surface did not provide next texture. "
"SurfaceGetCurrentTextureStatus:"
<< static_cast<uint32_t>(surface_tex.status);
return rndr::Result::error(oss.str());
return ustd::unexpected(oss.str());
}

wgpu::Texture next_tex_src = surface_tex.texture;
Expand All @@ -100,7 +100,7 @@ rndr::Result renderFrame(rndr::Application &program_gpu)
wgpu::TextureView next_tex = next_tex_src.CreateView(&next_tex_descriptor);

if (!next_tex) {
return rndr::Result::error("Cannot acquire next swap chain texture");
return ustd::unexpected("Cannot acquire next swap chain texture");
}

/* create the command encoder */
Expand Down Expand Up @@ -150,31 +150,31 @@ rndr::Result renderFrame(rndr::Application &program_gpu)
/* finally, present the next texture */
program_gpu.getSurface().Present();

return rndr::Result::success();
return {};
}

int main()
{
rndr::Application program_gpu;
rndr::Result init_result = program_gpu.initialize();
ustd::result init_result = program_gpu.initialize();

if (!init_result.ok()) {
std::cerr << "Initialization failed for reason: " << init_result;
return 1;
}

rndr::Result buffer_copy_result = performBufferCopies(program_gpu);
ustd::result buffer_copy_result = performBufferCopies(program_gpu);
if (!buffer_copy_result) {
std::cerr << buffer_copy_result << std::endl;
return 1;
}

rndr::Result render_result
= rndr::Result::error("Did not complete first frame.");
ustd::result render_result
= ustd::unexpected("Did not complete first frame.");

do {
if (glfwWindowShouldClose(program_gpu.getWindow())) {
render_result = rndr::Result::success("GLFW Requested Window Close");
render_result = {};
break;
}
// Check whether the user clicked on the close button (and any other
Expand Down
13 changes: 12 additions & 1 deletion src/rndr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@ add_library(rndr STATIC
${CMAKE_CURRENT_SOURCE_DIR}/application.cpp
)

Include(FetchContent)
FetchContent_Declare(
ustd
GIT_REPOSITORY https://github.com/ancientjpeg/ustd
GIT_TAG main
)

FetchContent_MakeAvailable(ustd)

add_subdirectory(math)
add_subdirectory(resources)
add_subdirectory(types)
add_subdirectory(utils)


target_include_directories(rndr PUBLIC ${PROJECT_SOURCE_DIR}/src)
target_link_libraries(rndr PUBLIC glfw3webgpu webgpu_cpp)
target_link_libraries(rndr PUBLIC glfw3webgpu webgpu_cpp ustd)



target_compile_definitions(rndr PUBLIC RNDR_SUPPORT_DIR=\"${RNDR_SUPPORT_DIR}\")
30 changes: 15 additions & 15 deletions src/rndr/types/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ void Globals::setRequiredLimits(wgpu::Limits required_limits)
required_limits_.limits = std::move(required_limits);
}

Result Globals::initializeWebGPU()
ustd::result Globals::initializeWebGPU()
{
wgpu::InstanceDescriptor instance_desc = {};
instance_desc.features.timedWaitAnyEnable = true;
instance_desc.features.timedWaitAnyMaxCount = 8;
instance_ = CreateInstance(&instance_desc);

if (instance_.Get() == nullptr) {
return Result::error("Failed to retrieve instance");
return ustd::unexpected("Failed to retrieve instance");
}

surface_
= wgpu::Surface::Acquire(glfwGetWGPUSurface(instance_.Get(), window_));

if (surface_.Get() == nullptr) {
return Result::error("Failed to retrieve surface");
return ustd::unexpected("Failed to retrieve surface");
}

/* Request adapter */
Expand All @@ -87,7 +87,7 @@ Result Globals::initializeWebGPU()
blockOnFuture(adapter_wait_future);

if (adapter_.Get() == nullptr) {
return Result::error("Failed to retrieve adapter");
return ustd::unexpected("Failed to retrieve adapter");
}

/* Get features */
Expand All @@ -100,8 +100,8 @@ Result Globals::initializeWebGPU()
for (wgpu::FeatureName feature : required_features_) {
if (std::find(features_.begin(), features_.end(), feature)
== features_.end()) {
return Result::error("Feature with code" + std::to_string((int)feature)
+ "unavailable");
return ustd::unexpected("Feature with code" + std::to_string((int)feature)
+ "unavailable");
}
}

Expand All @@ -111,7 +111,7 @@ Result Globals::initializeWebGPU()

if (!helpers::limits_supported(supported_limits.limits,
required_limits_.limits)) {
return Result::error("Cannot support required limits");
return ustd::unexpected("Cannot support required limits");
}

/* Request device */
Expand Down Expand Up @@ -141,11 +141,11 @@ Result Globals::initializeWebGPU()
blockOnFuture(device_future);

if (device_.Get() == nullptr) {
return Result::error("Failed to retrieve device");
return ustd::unexpected("Failed to retrieve device");
}

if (!error_msg.empty()) {
return Result::error(error_msg);
return ustd::unexpected(error_msg);
}

/* set default callbacks */
Expand All @@ -163,24 +163,24 @@ Result Globals::initializeWebGPU()

surface_.Configure(&surface_config);

return Result::success();
return {};
}

Result Globals::initializeGLFW()
ustd::result Globals::initializeGLFW()
{
/* GLFW init */
glfwInitHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
window_ = glfwCreateWindow(width_, height_, "RNDR", nullptr, nullptr);
if (window_ == nullptr) {
return Result::error("Failed to create GLFW window");
return ustd::unexpected("Failed to create GLFW window");
}

return Result::success();
return {};
}

Result Globals::initialize(int width, int height)
ustd::result Globals::initialize(int width, int height)
{
width_ = width;
height_ = height;
Expand All @@ -195,7 +195,7 @@ Result Globals::initialize(int width, int height)

initialized_ = true;

return Result::success();
return {};
}

bool Globals::blockOnFuture(wgpu::Future future)
Expand Down
18 changes: 9 additions & 9 deletions src/rndr/types/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef RNDR_GLOBALS_H_
#define RNDR_GLOBALS_H_

#include "rndr/types/types.h"
#include "ustd/expected.h"

#include <GLFW/glfw3.h>
#include <vector>
Expand Down Expand Up @@ -43,12 +43,12 @@ class Globals {
* @param width Initial width of the screen, in pixels
* @param height Initial height of the screen, in pixels
*/
[[nodiscard]] Result initialize(int width = 640, int height = 480);
bool isInitialized();
[[nodiscard]] ustd::result initialize(int width = 640, int height = 480);
bool isInitialized();

const wgpu::Device &getDevice();
const wgpu::Limits &getLimits();
const wgpu::Surface &getSurface();
const wgpu::Device &getDevice();
const wgpu::Limits &getLimits();
const wgpu::Surface &getSurface();
const std::vector<wgpu::FeatureName> &getFeatures();
const wgpu::Queue &getQueue();
GLFWwindow *getWindow();
Expand Down Expand Up @@ -82,10 +82,10 @@ class Globals {
int height_ = 0;

private:
Result initializeWebGPU();
Result initializeGLFW();
ustd::result initializeWebGPU();
ustd::result initializeGLFW();

bool initialized_ = false;
bool initialized_ = false;
};

class GlobalAccess {
Expand Down
56 changes: 0 additions & 56 deletions src/rndr/types/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,6 @@
#include <webgpu/webgpu_cpp.h>

namespace rndr {
/********* Error Type **********/
template <typename T>
struct is_string
: public std::disjunction<std::is_same<char *, std::decay_t<T>>,
std::is_same<const char *, std::decay_t<T>>,
std::is_same<std::string, std::decay_t<T>>> {};

struct Result {

static Result success(std::string message = {})
{
return Result(true, message);
}

static Result error(std::string message)
{
return Result(false, message);
}

friend std::ostream &operator<<(std::ostream &os, const Result &result)
{
const char *msg = result.success_ ? "Succeeded" : "Failed";
os << msg;
if (!result.message_.empty()) {
os << " with message: " << result.message_;
}
else {
os << " with no message";
}
return os;
}

std::string message()
{
return message_;
}

bool ok() const
{
return success_;
}

operator bool() const
{
return ok();
}

private:
Result(bool success, std::string message)
: success_(success), message_(message)
{
}

bool success_ = false;
std::string message_;
};

/********** Mesh Data **********/
struct MeshData {
Expand Down
1 change: 1 addition & 0 deletions src/rndr/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target_sources(rndr PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/helpers.h
${CMAKE_CURRENT_SOURCE_DIR}/helpers.cpp
${CMAKE_CURRENT_SOURCE_DIR}/result.h
)
Loading

0 comments on commit e210261

Please sign in to comment.