Skip to content

Commit

Permalink
* RendererInfoCommon now unique-clones every resource given to it via…
Browse files Browse the repository at this point in the history
… add_resource. This means that BufferResource/ImageResources no longer need to live until the renderers are created from the info. Note that this is still the case for add_component as those are expected to always be in their lifetime
  • Loading branch information
harrand committed Oct 6, 2022
1 parent 25bd29e commit 318d168
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 62 deletions.
101 changes: 53 additions & 48 deletions demo/gl/tz_dynamic_triangle_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,64 +39,69 @@ int main()
});
{
using namespace tz::literals;
tz::gl::ImageResource img = tz::gl::ImageResource::from_memory
std::size_t triangle_count = 1;

tz::gl::RendererInfo rinfo;
rinfo.shader().set_shader(tz::gl::ShaderStage::Vertex, ImportedShaderSource(tz_dynamic_triangle_demo, vertex));
rinfo.shader().set_shader(tz::gl::ShaderStage::Fragment, ImportedShaderSource(tz_dynamic_triangle_demo, fragment));
tz::gl::ResourceHandle imgh = rinfo.add_resource
(
{
0b1111'1111_uc,
0b0000'0000_uc,
0b0000'0000_uc,
0b1111'1111_uc,
tz::gl::ImageResource::from_memory
(
{
0b1111'1111_uc,
0b0000'0000_uc,
0b0000'0000_uc,
0b1111'1111_uc,

0b1111'1111_uc,
0b1111'1111_uc,
0b0000'0000_uc,
0b1111'1111_uc,
0b1111'1111_uc,
0b1111'1111_uc,
0b0000'0000_uc,
0b1111'1111_uc,

0b0000'0000_uc,
0b0000'0000_uc,
0b1111'1111_uc,
0b1111'1111_uc,
0b0000'0000_uc,
0b0000'0000_uc,
0b1111'1111_uc,
0b1111'1111_uc,

0b0000'0000_uc,
0b1111'1111_uc,
0b0000'0000_uc,
0b1111'1111_uc
},
{
.format = tz::gl::ImageFormat::RGBA32,
.dimensions = {2u, 2u},
.access = tz::gl::ResourceAccess::DynamicVariable
}
0b0000'0000_uc,
0b1111'1111_uc,
0b0000'0000_uc,
0b1111'1111_uc
},
{
.format = tz::gl::ImageFormat::RGBA32,
.dimensions = {2u, 2u},
.access = tz::gl::ResourceAccess::DynamicVariable
}
)
);

std::size_t triangle_count = 1;
tz::gl::BufferResource buf = tz::gl::BufferResource::from_many
tz::gl::ResourceHandle bufh = rinfo.add_resource
(
{
TriangleVertexData{.position = {-0.5f, -0.5f, 0.0f}, .texcoord = {0.0f, 0.0f}},
TriangleVertexData{.position = {0.0f, 0.5f, 0.0f}, .texcoord = {0.5f, 1.0f}},
TriangleVertexData{.position = {0.5f, -0.5f, 0.0f}, .texcoord = {1.0f, 0.0f}},
},
{
.access = tz::gl::ResourceAccess::DynamicVariable
}
tz::gl::BufferResource::from_many
(
{
TriangleVertexData{.position = {-0.5f, -0.5f, 0.0f}, .texcoord = {0.0f, 0.0f}},
TriangleVertexData{.position = {0.0f, 0.5f, 0.0f}, .texcoord = {0.5f, 1.0f}},
TriangleVertexData{.position = {0.5f, -0.5f, 0.0f}, .texcoord = {1.0f, 0.0f}},
},
{
.access = tz::gl::ResourceAccess::DynamicVariable
}
)
);
tz::gl::BufferResource ibuf = tz::gl::BufferResource::from_many
tz::gl::ResourceHandle ibufh = rinfo.add_resource
(
{0u, 1u, 2u},
{
.access = tz::gl::ResourceAccess::DynamicVariable,
.flags = {tz::gl::ResourceFlag::IndexBuffer}
}
tz::gl::BufferResource::from_many
(
{0u, 1u, 2u},
{
.access = tz::gl::ResourceAccess::DynamicVariable,
.flags = {tz::gl::ResourceFlag::IndexBuffer}
}
)
);

tz::gl::RendererInfo rinfo;
rinfo.shader().set_shader(tz::gl::ShaderStage::Vertex, ImportedShaderSource(tz_dynamic_triangle_demo, vertex));
rinfo.shader().set_shader(tz::gl::ShaderStage::Fragment, ImportedShaderSource(tz_dynamic_triangle_demo, fragment));
tz::gl::ResourceHandle imgh = rinfo.add_resource(img);
tz::gl::ResourceHandle bufh = rinfo.add_resource(buf);
tz::gl::ResourceHandle ibufh = rinfo.add_resource(ibuf);

tz::gl::Renderer renderer = tz::gl::device().create_renderer(rinfo);
std::default_random_engine rand;
tz::Delay fixed_update{50_ms};
Expand Down
4 changes: 2 additions & 2 deletions src/tz/gl/api/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ namespace tz::gl
using RendererOptions = tz::EnumField<RendererOption>;

template<typename T>
concept RendererInfoType = requires(T t, ResourceHandle r, IResource& resource, IComponent& component, IOutput& output, RendererOptions options, tz::Vec4 vec4, tz::Vec3ui vec3ui, std::string str)
concept RendererInfoType = requires(T t, ResourceHandle r, const IResource& resource, IComponent& component, IOutput& output, RendererOptions options, tz::Vec4 vec4, tz::Vec3ui vec3ui, std::string str)
{
{t.resource_count()} -> std::convertible_to<unsigned int>;
{t.get_resource(r)} -> std::convertible_to<const IResource*>;
{t.get_resources()} -> std::same_as<std::span<const IResource* const>>;
{t.get_resources()} -> std::same_as<std::vector<const IResource*>>;

{t.add_resource(resource)} -> std::same_as<ResourceHandle>;
{t.add_component(component)} -> std::same_as<ResourceHandle>;
Expand Down
21 changes: 13 additions & 8 deletions src/tz/gl/impl/frontend/common/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,34 @@ namespace tz::gl

const IResource* RendererInfoCommon::get_resource(ResourceHandle handle)
{
return this->resources[static_cast<std::size_t>(static_cast<tz::HandleValue>(handle))];
return this->resources[static_cast<std::size_t>(static_cast<tz::HandleValue>(handle))].get();
}

std::span<const IResource* const> RendererInfoCommon::get_resources() const
std::vector<const IResource*> RendererInfoCommon::get_resources() const
{
return this->resources;
std::vector<const IResource*> ret;
for(const auto& ptr : this->resources)
{
ret.push_back(ptr.get());
}
return ret;
}

std::span<const IComponent* const> RendererInfoCommon::get_components() const
{
return this->components;
}

ResourceHandle RendererInfoCommon::add_resource(IResource& resource)
ResourceHandle RendererInfoCommon::add_resource(const IResource& resource)
{
#if TZ_DEBUG
if(resource.get_flags().contains(ResourceFlag::IndexBuffer))
{
tz_assert(resource.get_type() == ResourceType::Buffer, "Attempting to add a resource with ResourceFlag::IndexBuffer specified, but the resource is not a buffer resource! Logic error/memory corruption? Please submit a bug report.");
tz_assert(!std::any_of(this->resources.begin(), this->resources.end(), [](const IResource* r)->bool{return r != nullptr && r->get_flags().contains(ResourceFlag::IndexBuffer);}), "Attempting to add a resource with ResourceFlag::IndexBuffer specified, but a resource was already added which is an index buffer. You cannot have more than one index buffer in a renderer. Logic error? Please submit a bug report.");
tz_assert(!std::any_of(this->resources.begin(), this->resources.end(), [](const auto& r)->bool{return r != nullptr && r->get_flags().contains(ResourceFlag::IndexBuffer);}), "Attempting to add a resource with ResourceFlag::IndexBuffer specified, but a resource was already added which is an index buffer. You cannot have more than one index buffer in a renderer. Logic error? Please submit a bug report.");
}
#endif
this->resources.push_back(&resource);
this->resources.push_back(resource.unique_clone());
return static_cast<tz::HandleValue>(this->real_resource_count() - 1);
}

Expand Down Expand Up @@ -158,7 +163,7 @@ namespace tz::gl
}
std::size_t bufc = 0, imgc = 0;
bufc = std::count_if(this->resources.begin(), this->resources.end(),
[](const IResource* res)
[](const auto& res)
{
return res != nullptr && res->get_type() == ResourceType::Buffer;
}) +
Expand All @@ -168,7 +173,7 @@ namespace tz::gl
return comp != nullptr && comp->get_resource()->get_type() == ResourceType::Buffer;
});
imgc = std::count_if(this->resources.begin(), this->resources.end(),
[](const IResource* res)
[](const auto& res)
{
return res != nullptr && res->get_type() == ResourceType::Image;
}) +
Expand Down
8 changes: 4 additions & 4 deletions src/tz/gl/impl/frontend/common/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ namespace tz::gl
/**
* Retrieve a span containing all of the specified resources. Size of the span is guaranteed to be equal to @ref resource_count()
*/
std::span<const IResource* const> get_resources() const;
std::vector<const IResource*> get_resources() const;
std::span<const IComponent* const> get_components() const;
/**
* Add a new resource, which will be used by a Renderer which is created from this helper struct.
*
* @param resource Reference to an existing resource. This will be copied by the Renderer upon construction, so its lifetime only needs to last until the desired Renderer has been created.
* @param resource Resource which will be owned by a renderer.
* @return Handle corresponding to the resource. If you want to retrieve the resource later, you should keep ahold of this handle.
*/
ResourceHandle add_resource(IResource& resource);
ResourceHandle add_resource(const IResource& resource);
ResourceHandle add_component(IComponent& component);
/**
* Renderers always render into something. By default, it renders to the window (only one window is supported so no confusion there). You can however set it to render into something else, such as a @ref TextureOutput if you want to render into the resource of another Renderer.
Expand Down Expand Up @@ -122,7 +122,7 @@ namespace tz::gl
private:
std::size_t real_resource_count() const;
/// Stores all provided resources. It is assumed that their lifetime is valid for the entirety of this helper struct's lifetime.
std::vector<IResource*> resources = {};
std::vector<std::unique_ptr<IResource>> resources = {};
/// Stores all provided components. In this context, components act as references to existing resources owned by another renderer.
std::vector<IComponent*> components = {};
/// Output. Can be null, which defaults to rendering into the main window.
Expand Down

0 comments on commit 318d168

Please sign in to comment.