Skip to content
Draft
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
19 changes: 18 additions & 1 deletion src/client/classes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,29 @@ namespace builtin_scene
/**
* @namespace client_graphics
* The `client_graphics` namespace contains classes related to graphics rendering,
* such as `WebGLContext` and `WebGL2Context`.
* such as `WebGLContext`, `WebGL2Context`, and `WebGPUContext`.
*/
namespace client_graphics
{
class WebGLContext; // Represents a WebGL 1.0 rendering context
class WebGL2Context; // Represents a WebGL 2.0 rendering context

// WebGPU classes (split into individual files)
class WebGPUContextAttributes; // Context configuration attributes
class WebGPUAdapter; // Represents a WebGPU adapter
class WebGPUDevice; // Represents a WebGPU device
class WebGPUQueue; // Represents a WebGPU queue
class WebGPUContext; // Main WebGPU context interface
class WebGPUCommandEncoder; // Records GPU commands
class WebGPUCommandBuffer; // Container for recorded commands
class WebGPURenderPassEncoder; // Records render pass commands

// WebGPU resource classes (placeholders)
class WebGPUBuffer; // Represents a WebGPU buffer
class WebGPUTexture; // Represents a WebGPU texture
class WebGPUBindGroup; // Represents a WebGPU bind group
class WebGPURenderPipeline; // Represents a WebGPU render pipeline
class WebGPUComputePipeline; // Represents a WebGPU compute pipeline
}

/**
Expand Down
50 changes: 50 additions & 0 deletions src/client/graphics/webgpu_adapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "./webgpu_adapter.hpp"
#include "./webgpu_device.hpp"

#include <iostream>
#include <stdexcept>

namespace client_graphics
{
using namespace std;

// WebGPUAdapter implementation
WebGPUAdapter::WebGPUAdapter(const commandbuffers::GPUAdapterInfo &info)
: adapter_info_(info)
, features_()
, limits_()
{
// Initialize with default features and limits
// In a full implementation, these would be queried from the actual adapter
}

std::unique_ptr<WebGPUDevice> WebGPUAdapter::requestDevice(
std::optional<std::string> label,
const std::vector<std::string> &requiredFeatures,
const std::unordered_map<std::string, uint32_t> &requiredLimits)
{
// Validate required features
for (const auto &feature : requiredFeatures)
{
if (features_.find(feature) == features_.end())
{
// Feature not supported
return nullptr;
}
}

// Validate required limits
for (const auto &[limitName, requiredValue] : requiredLimits)
{
auto it = limits_.find(limitName);
if (it == limits_.end() || it->second < requiredValue)
{
// Limit not supported or insufficient
return nullptr;
}
}

// Create device with the requested configuration
return std::make_unique<WebGPUDevice>(adapter_info_, label);
}
}
53 changes: 53 additions & 0 deletions src/client/graphics/webgpu_adapter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include <common/command_buffers/gpu/gpu_adapter.hpp>

namespace client_graphics
{
/**
* The `WebGPUAdapter` class represents a WebGPU adapter which provides information
* about the underlying graphics hardware and its capabilities.
*/
class WebGPUAdapter
{
public:
WebGPUAdapter(const commandbuffers::GPUAdapterInfo &info);
~WebGPUAdapter() = default;

public:
const commandbuffers::GPUAdapterInfo &info() const
{
return adapter_info_;
}
const commandbuffers::GPUSupportedFeatures &features() const
{
return features_;
}
const commandbuffers::GPUSupportedLimits &limits() const
{
return limits_;
}

/**
* Request a device from this adapter.
* @param label Optional label for the device
* @param requiredFeatures Features required by the device
* @param requiredLimits Limits required by the device
* @returns A WebGPU device or nullptr if device creation fails
*/
std::unique_ptr<class WebGPUDevice> requestDevice(
std::optional<std::string> label = std::nullopt,
const std::vector<std::string> &requiredFeatures = {},
const std::unordered_map<std::string, uint32_t> &requiredLimits = {});

private:
commandbuffers::GPUAdapterInfo adapter_info_;
commandbuffers::GPUSupportedFeatures features_;
commandbuffers::GPUSupportedLimits limits_;
};
}
34 changes: 34 additions & 0 deletions src/client/graphics/webgpu_bind_group.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <string>
#include <common/command_buffers/gpu/gpu_base.hpp>

namespace client_graphics
{
/**
* Represents a WebGPU bind group.
* This is a placeholder implementation for the client-side bind group interface.
*/
class WebGPUBindGroup
{
public:
WebGPUBindGroup(std::string label = "WebGPUBindGroup")
: label_(std::move(label))
, id_(commandbuffers::GPUHandle("").id) // Generate unique ID
{
}

const std::string &label() const
{
return label_;
}
commandbuffers::GPUIdentifier id() const
{
return id_;
}

private:
std::string label_;
commandbuffers::GPUIdentifier id_;
};
}
34 changes: 34 additions & 0 deletions src/client/graphics/webgpu_buffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <string>
#include <common/command_buffers/gpu/gpu_base.hpp>

namespace client_graphics
{
/**
* Represents a WebGPU buffer resource.
* This is a placeholder implementation for the client-side buffer interface.
*/
class WebGPUBuffer
{
public:
WebGPUBuffer(std::string label = "WebGPUBuffer")
: label_(std::move(label))
, id_(commandbuffers::GPUHandle("").id) // Generate unique ID
{
}

const std::string &label() const
{
return label_;
}
commandbuffers::GPUIdentifier id() const
{
return id_;
}

private:
std::string label_;
commandbuffers::GPUIdentifier id_;
};
}
36 changes: 36 additions & 0 deletions src/client/graphics/webgpu_command_buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "./webgpu_command_buffer.hpp"

#include <iostream>

namespace client_graphics
{
using namespace std;

// WebGPUCommandBuffer implementation
WebGPUCommandBuffer::WebGPUCommandBuffer(std::optional<std::string> label)
: label_(label.value_or("WebGPUCommandBuffer"))
{
}

void WebGPUCommandBuffer::execute() const
{
// In the client-side implementation, this would typically transmit
// the recorded commands to the graphics server for execution.
// For now, we just log the execution for debugging purposes.

if (commands_.empty())
{
return;
}

// In a full implementation, this would serialize and send the commands
// to the server-side renderer via the command buffer system.
// Following the WebGL pattern in webgl_context.cpp

std::cout << "Executing WebGPU command buffer '" << label_
<< "' with " << commands_.size() << " commands" << std::endl;

// Placeholder for command transmission logic
// This would follow the same pattern as WebGLContext::sendCommandBufferRequest()
}
}
67 changes: 67 additions & 0 deletions src/client/graphics/webgpu_command_buffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include <common/command_buffers/gpu/gpu_command_buffer.hpp>

namespace client_graphics
{
/**
* The `WebGPUCommandBuffer` class represents a recorded sequence of GPU commands.
* This is the client-side implementation that holds recorded commands for later submission.
*/
class WebGPUCommandBuffer
{
public:
WebGPUCommandBuffer(std::optional<std::string> label = std::nullopt);
~WebGPUCommandBuffer() = default;

// Non-copyable but movable
WebGPUCommandBuffer(const WebGPUCommandBuffer &) = delete;
WebGPUCommandBuffer &operator=(const WebGPUCommandBuffer &) = delete;
WebGPUCommandBuffer(WebGPUCommandBuffer &&) = default;
WebGPUCommandBuffer &operator=(WebGPUCommandBuffer &&) = default;

public:
const std::string &label() const
{
return label_;
}
bool isEmpty() const
{
return commands_.empty();
}
size_t commandCount() const
{
return commands_.size();
}

/**
* Execute the recorded commands. In the client-side implementation,
* this would typically transmit the commands to the server.
* For now, this is a placeholder for the command recording pattern.
*/
void execute() const;

private:
friend class WebGPUCommandEncoder;
friend class WebGPURenderPassEncoder;

std::string label_;
std::vector<std::shared_ptr<commandbuffers::GPUCommand>> commands_;

/**
* Add a command to the command buffer.
* This follows the same pattern as the existing GPU command buffer implementation.
*/
template <typename T, typename... Args>
void addCommand(Args &&...args)
{
auto command = std::make_shared<T>(std::forward<Args>(args)...);
commands_.push_back(command);
}
};
}
Loading