Add renderer resource factory contract
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
#include "renderer_api/recording_renderer.h"
|
||||
|
||||
#include <new>
|
||||
#include <utility>
|
||||
|
||||
namespace pp::renderer {
|
||||
|
||||
namespace {
|
||||
@@ -18,6 +21,20 @@ void push_command(
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Resource, typename Interface, typename... Args>
|
||||
[[nodiscard]] pp::foundation::Result<std::unique_ptr<Interface>> make_recording_resource(
|
||||
Args&&... args) noexcept
|
||||
{
|
||||
auto resource = std::unique_ptr<Resource>(new (std::nothrow) Resource(std::forward<Args>(args)...));
|
||||
if (!resource) {
|
||||
return pp::foundation::Result<std::unique_ptr<Interface>>::failure(
|
||||
pp::foundation::Status::out_of_range("renderer resource allocation failed"));
|
||||
}
|
||||
|
||||
std::unique_ptr<Interface> erased = std::move(resource);
|
||||
return pp::foundation::Result<std::unique_ptr<Interface>>::success(std::move(erased));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RecordingTexture2D::RecordingTexture2D(TextureDesc desc) noexcept
|
||||
@@ -457,6 +474,71 @@ const char* RecordingRenderDevice::backend_name() const noexcept
|
||||
return "recording";
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::unique_ptr<ITexture2D>> RecordingRenderDevice::create_texture(
|
||||
TextureDesc desc) noexcept
|
||||
{
|
||||
const auto bytes = texture_byte_size(desc);
|
||||
if (!bytes.ok()) {
|
||||
return pp::foundation::Result<std::unique_ptr<ITexture2D>>::failure(bytes.status());
|
||||
}
|
||||
|
||||
return make_recording_resource<RecordingTexture2D, ITexture2D>(desc);
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::unique_ptr<IRenderTarget>> RecordingRenderDevice::create_render_target(
|
||||
TextureDesc color_desc) noexcept
|
||||
{
|
||||
if (!color_desc.render_target) {
|
||||
return pp::foundation::Result<std::unique_ptr<IRenderTarget>>::failure(
|
||||
pp::foundation::Status::invalid_argument("render target texture must be flagged as render_target"));
|
||||
}
|
||||
|
||||
const auto bytes = texture_byte_size(color_desc);
|
||||
if (!bytes.ok()) {
|
||||
return pp::foundation::Result<std::unique_ptr<IRenderTarget>>::failure(bytes.status());
|
||||
}
|
||||
|
||||
return make_recording_resource<RecordingRenderTarget, IRenderTarget>(color_desc);
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::unique_ptr<IShaderProgram>> RecordingRenderDevice::create_shader_program(
|
||||
ShaderProgramDesc desc) noexcept
|
||||
{
|
||||
const auto status = validate_shader_program_desc(desc);
|
||||
if (!status.ok()) {
|
||||
return pp::foundation::Result<std::unique_ptr<IShaderProgram>>::failure(status);
|
||||
}
|
||||
|
||||
return make_recording_resource<RecordingShaderProgram, IShaderProgram>(desc.debug_name);
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::unique_ptr<IMesh>> RecordingRenderDevice::create_mesh(
|
||||
MeshDesc desc) noexcept
|
||||
{
|
||||
const auto status = validate_mesh_desc(desc);
|
||||
if (!status.ok()) {
|
||||
return pp::foundation::Result<std::unique_ptr<IMesh>>::failure(status);
|
||||
}
|
||||
|
||||
return make_recording_resource<RecordingMesh, IMesh>(desc);
|
||||
}
|
||||
|
||||
pp::foundation::Result<std::unique_ptr<IReadbackBuffer>> RecordingRenderDevice::create_readback_buffer(
|
||||
std::uint64_t size_bytes) noexcept
|
||||
{
|
||||
if (size_bytes == 0U) {
|
||||
return pp::foundation::Result<std::unique_ptr<IReadbackBuffer>>::failure(
|
||||
pp::foundation::Status::invalid_argument("readback buffer size must be greater than zero"));
|
||||
}
|
||||
|
||||
if (size_bytes > max_texture_bytes) {
|
||||
return pp::foundation::Result<std::unique_ptr<IReadbackBuffer>>::failure(
|
||||
pp::foundation::Status::out_of_range("readback buffer size exceeds the configured limit"));
|
||||
}
|
||||
|
||||
return make_recording_resource<RecordingReadbackBuffer, IReadbackBuffer>(size_bytes);
|
||||
}
|
||||
|
||||
ICommandContext& RecordingRenderDevice::immediate_context() noexcept
|
||||
{
|
||||
return context_;
|
||||
|
||||
@@ -163,6 +163,16 @@ public:
|
||||
RecordingRenderDevice() noexcept;
|
||||
|
||||
[[nodiscard]] const char* backend_name() const noexcept override;
|
||||
[[nodiscard]] pp::foundation::Result<std::unique_ptr<ITexture2D>> create_texture(
|
||||
TextureDesc desc) noexcept override;
|
||||
[[nodiscard]] pp::foundation::Result<std::unique_ptr<IRenderTarget>> create_render_target(
|
||||
TextureDesc color_desc) noexcept override;
|
||||
[[nodiscard]] pp::foundation::Result<std::unique_ptr<IShaderProgram>> create_shader_program(
|
||||
ShaderProgramDesc desc) noexcept override;
|
||||
[[nodiscard]] pp::foundation::Result<std::unique_ptr<IMesh>> create_mesh(
|
||||
MeshDesc desc) noexcept override;
|
||||
[[nodiscard]] pp::foundation::Result<std::unique_ptr<IReadbackBuffer>> create_readback_buffer(
|
||||
std::uint64_t size_bytes) noexcept override;
|
||||
[[nodiscard]] ICommandContext& immediate_context() noexcept override;
|
||||
[[nodiscard]] IRenderTrace* trace() noexcept override;
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
|
||||
namespace pp::renderer {
|
||||
@@ -237,6 +238,16 @@ class IRenderDevice {
|
||||
public:
|
||||
virtual ~IRenderDevice() = default;
|
||||
[[nodiscard]] virtual const char* backend_name() const noexcept = 0;
|
||||
[[nodiscard]] virtual pp::foundation::Result<std::unique_ptr<ITexture2D>> create_texture(
|
||||
TextureDesc desc) noexcept = 0;
|
||||
[[nodiscard]] virtual pp::foundation::Result<std::unique_ptr<IRenderTarget>> create_render_target(
|
||||
TextureDesc color_desc) noexcept = 0;
|
||||
[[nodiscard]] virtual pp::foundation::Result<std::unique_ptr<IShaderProgram>> create_shader_program(
|
||||
ShaderProgramDesc desc) noexcept = 0;
|
||||
[[nodiscard]] virtual pp::foundation::Result<std::unique_ptr<IMesh>> create_mesh(
|
||||
MeshDesc desc) noexcept = 0;
|
||||
[[nodiscard]] virtual pp::foundation::Result<std::unique_ptr<IReadbackBuffer>> create_readback_buffer(
|
||||
std::uint64_t size_bytes) noexcept = 0;
|
||||
[[nodiscard]] virtual ICommandContext& immediate_context() noexcept = 0;
|
||||
[[nodiscard]] virtual IRenderTrace* trace() noexcept = 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user