Skip to content

Commit

Permalink
+ Added resource_access::static_access and dynamic_access. They both …
Browse files Browse the repository at this point in the history
…seem to work? Haven't tested too much. Also, buffer_resize now works on static_access resources. Static resources ought to be resizeable, just not instantly writeable - should use renderer edit instead
  • Loading branch information
harrand committed Jun 22, 2023
1 parent 07350e3 commit 5369e8a
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 43 deletions.
19 changes: 16 additions & 3 deletions demo/gl/tz_dynamic_triangle_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main()
TriangleVertexData{.position = {0.5f, -0.5f, 0.0f}, .texcoord = {1.0f, 0.0f}},
},
{
.access = tz::gl::resource_access::dynamic_variable
.access = tz::gl::resource_access::static_access
}
)
);
Expand All @@ -63,7 +63,7 @@ int main()
(
{0u, 1u, 2u},
{
.access = tz::gl::resource_access::dynamic_variable,
.access = tz::gl::resource_access::static_access,
.flags = {tz::gl::resource_flag::index_buffer}
}
)
Expand Down Expand Up @@ -97,7 +97,7 @@ int main()
{
.format = tz::gl::image_format::RGBA32,
.dimensions = {2u, 2u},
.access = tz::gl::resource_access::dynamic_variable,
.access = tz::gl::resource_access::dynamic_access,
.flags =
{
tz::gl::resource_flag::image_filter_linear,
Expand Down Expand Up @@ -168,6 +168,19 @@ int main()
std::random_shuffle(img_data.begin(), img_data.end());
// But set the first pixel (bottom left) to always be white.
std::fill(img_data.begin(), img_data.begin() + 4, std::byte{255});
renderer.edit
(tz::gl::RendererEditBuilder{}
.write
({
.resource = bufh,
.data = std::as_bytes(buf_data)
})
.write
({
.resource = ibufh,
.data = std::as_bytes(idx_data)
})
.build());
}
tz::end_frame();
}
Expand Down
14 changes: 11 additions & 3 deletions demo/gl/tz_terrain_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ int main()

tz::gl::buffer_resource buf = tz::gl::buffer_resource::from_one(BufferData{},
{
.access = tz::gl::resource_access::dynamic_fixed
.access = tz::gl::resource_access::dynamic_access
});
tz::gl::buffer_resource buf2 = tz::gl::buffer_resource::from_one(FeedbackData{},
{
tz::gl::resource_access::dynamic_fixed
tz::gl::resource_access::dynamic_access
});

tz::gl::renderer_info rinfo;
Expand Down Expand Up @@ -91,7 +91,7 @@ int main()
{
fixed_update.reset();
// Retrieve the dynamic buffer resource data.
BufferData& bufd = renderer.get_resource(bufh)->data_as<BufferData>().front();
BufferData bufd = renderer.get_resource(bufh)->data_as<BufferData>().front();
tz::vec3& camera_position = bufd.camera_position;

// Dragging the mouse influences the camera rotation.
Expand Down Expand Up @@ -145,6 +145,14 @@ int main()
float dist_to_terrain = output_vertex_height - camera_position[1];
camera_position[1] += std::clamp(dist_to_terrain * 0.3f, -5.0f, 5.0f);
}

std::span<const BufferData> new_data_span{&bufd, 1};
renderer.edit(tz::gl::RendererEditBuilder{}
.write
({
.resource = bufh,
.data = std::as_bytes(new_data_span)
}).build());
}
tz::end_frame();
}
Expand Down
8 changes: 4 additions & 4 deletions src/tz/dbgui/dbgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,24 +337,24 @@ namespace tz::dbgui

tz::gl::buffer_resource vertex_buffer = tz::gl::buffer_resource::from_one(Kibibyte{},
{
.access = tz::gl::resource_access::dynamic_variable
.access = tz::gl::resource_access::dynamic_access
});
tz::gl::buffer_resource index_buffer = tz::gl::buffer_resource::from_one(Kibibyte{},
{
.access = tz::gl::resource_access::dynamic_variable
.access = tz::gl::resource_access::dynamic_access
});
tz::gl::buffer_resource draw_buffer = tz::gl::buffer_resource::from_one(tz::gl::draw_indirect_command
{
.count = 0u,
.first = 0
},
{
.access = tz::gl::resource_access::dynamic_fixed,
.access = tz::gl::resource_access::dynamic_access,
.flags = {tz::gl::resource_flag::draw_indirect_buffer}
});
tz::gl::buffer_resource shader_data_buffer = tz::gl::buffer_resource::from_one(TopazShaderRenderData{},
{
.access = tz::gl::resource_access::dynamic_fixed
.access = tz::gl::resource_access::dynamic_access
});

tz::gl::image_resource font_image = tz::gl::image_resource::from_memory
Expand Down
4 changes: 4 additions & 0 deletions src/tz/gl/api/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ namespace tz::gl
*/
enum class resource_access
{
/// - resource data can only be written initially, or modified via a renderer edit.
static_access,
/// - resource data can be modified at anytime.
dynamic_access,
/// - resource data is written once initially, and cannot be resized.
static_fixed,
/// - resource data is always writable, but cannot be resized.
Expand Down
20 changes: 19 additions & 1 deletion src/tz/gl/impl/vulkan/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace tz::gl

void buffer_component_vulkan::resize(std::size_t sz)
{
tz::assert(this->resource->get_access() == resource_access::dynamic_variable, "Attempted to resize buffer_component_vulkan, but it not resource_access::dynamic_variable. Please submit a bug report.");
//tz::assert(this->resource->get_access() == resource_access::dynamic_variable, "Attempted to resize buffer_component_vulkan, but it not resource_access::dynamic_variable. Please submit a bug report.");
// Let's create a new buffer of the correct size.
vk2::Buffer& old_buf = this->vk_get_buffer();
vk2::Buffer new_buf
Expand All @@ -43,13 +43,23 @@ namespace tz::gl
.residency = old_buf.get_residency()
}};
// Copy the data over.
if(this->resource->get_access() == resource_access::dynamic_access)
{
auto old_data = old_buf.map_as<const std::byte>();
auto new_data = new_buf.map_as<std::byte>();
std::size_t copy_length = std::min(old_data.size_bytes(), new_data.size_bytes());
std::copy(old_data.begin(), old_data.begin() + copy_length, new_data.begin());
this->resource->set_mapped_data(new_data);
}
else
{
auto old_data = this->resource->data();
std::size_t copy_length = std::min(old_data.size_bytes(), sz);
std::vector<std::byte> new_data;
new_data.resize(sz);
std::copy(old_data.begin(), old_data.begin() + copy_length, new_data.begin());
this->resource->set_mapped_data(new_data);
}
new_buf.debug_set_name(old_buf.debug_get_name());
std::swap(old_buf, new_buf);
}
Expand Down Expand Up @@ -89,12 +99,16 @@ namespace tz::gl
tz::error("Unrecognised resource_access. Please submit a bug report.");
[[fallthrough]];
case resource_access::static_fixed:
[[fallthrough]];
case resource_access::static_access:
usage_field |= vk2::BufferUsage::TransferDestination;
residency = vk2::MemoryResidency::GPU;
break;
case resource_access::dynamic_fixed:
[[fallthrough]];
case resource_access::dynamic_variable:
[[fallthrough]];
case resource_access::dynamic_access:
residency = vk2::MemoryResidency::CPUPersistent;
break;
}
Expand Down Expand Up @@ -195,11 +209,15 @@ namespace tz::gl
tz::error("Unknown resource_access. Please submit a bug report.");
[[fallthrough]];
case resource_access::static_fixed:
[[fallthrough]];
case resource_access::static_access:
residency = vk2::MemoryResidency::GPU;
break;
case resource_access::dynamic_fixed:
[[fallthrough]];
case resource_access::dynamic_variable:
[[fallthrough]];
case resource_access::dynamic_access:
residency = vk2::MemoryResidency::CPUPersistent;
tiling = vk2::ImageTiling::Linear;
break;
Expand Down
59 changes: 34 additions & 25 deletions src/tz/gl/impl/vulkan/renderer2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ namespace tz::gl
{
TZ_PROFZONE("renderer_resource_manager - notify image dirty", 0xFFAAAA00);
// we need to know which image view index this corresponds to. just count resource image count until we reach rh vallue.
// a dirty image means that the underlying vk2::Image has been entirely replaced, and thus we need to replace the corresponding image view to refer to the new image.
std::size_t imgview_idx = 0;
for(std::size_t i = 0; i < static_cast<std::size_t>(static_cast<tz::hanval>(rh)); i++)
{
Expand Down Expand Up @@ -142,27 +143,26 @@ namespace tz::gl
tz::assert(comp != nullptr);
iresource* res = comp->get_resource();
tz::assert(res != nullptr);
if(res->get_access() == tz::gl::resource_access::static_fixed)
if(res->get_access() == tz::gl::resource_access::dynamic_access)
{
continue;
}
std::span<std::byte> underlying_component_data;
switch(res->get_type())
{
case tz::gl::resource_type::buffer:
underlying_component_data = static_cast<buffer_component_vulkan*>(comp)->vk_get_buffer().map_as<std::byte>();
break;
case tz::gl::resource_type::image:
underlying_component_data = static_cast<image_component_vulkan*>(comp)->vk_get_image().map_as<std::byte>();
break;
default:
tz::error("unrecognised resource_type");
break;
std::span<std::byte> underlying_component_data;
switch(res->get_type())
{
case tz::gl::resource_type::buffer:
underlying_component_data = static_cast<buffer_component_vulkan*>(comp)->vk_get_buffer().map_as<std::byte>();
break;
case tz::gl::resource_type::image:
underlying_component_data = static_cast<image_component_vulkan*>(comp)->vk_get_image().map_as<std::byte>();
break;
default:
tz::error("unrecognised resource_type");
break;
}
auto resdata = res->data();
tz::assert(resdata.size_bytes() <= underlying_component_data.size_bytes());
std::copy(resdata.begin(), resdata.end(), underlying_component_data.begin());
res->set_mapped_data(underlying_component_data);
}
auto resdata = res->data();
tz::assert(resdata.size_bytes() <= underlying_component_data.size_bytes());
std::copy(resdata.begin(), resdata.end(), underlying_component_data.begin());
res->set_mapped_data(underlying_component_data);
}
}

Expand Down Expand Up @@ -915,7 +915,7 @@ namespace tz::gl
tz::assert(cmp != nullptr);
const iresource* res = cmp->get_resource();
tz::assert(res != nullptr);
if(res->get_access() != tz::gl::resource_access::static_fixed)
if(res->get_access() != tz::gl::resource_access::static_access)
{
resource_staging_buffers.push_back(vk2::Buffer::null());
continue;
Expand Down Expand Up @@ -965,11 +965,11 @@ namespace tz::gl
{
iresource* res = renderer_resource_manager::get_resource(rwrite.resource);
tz::assert(res != nullptr);
if(res->get_access() != tz::gl::resource_access::static_fixed)
// if this is a dynamic resource, we can trivially do the write now.
tz::assert(res->data().size_bytes() >= (rwrite.offset + rwrite.data.size_bytes()), "resource write too long. resource is %zu bytes, write was (%zu + %zu) bytes", res->data().size_bytes(), rwrite.offset, rwrite.data.size_bytes());
std::memcpy(reinterpret_cast<char*>(res->data().data()) + rwrite.offset, rwrite.data.data(), rwrite.data.size_bytes());
if(res->get_access() == tz::gl::resource_access::dynamic_access)
{
// if this is a dynamic resource, we can trivially do the write now.
tz::assert(res->data().size_bytes() >= (rwrite.offset + rwrite.data.size_bytes()), "resource write too long. resource is %zu bytes, write was (%zu + %zu) bytes", res->data().size_bytes(), rwrite.offset, rwrite.data.size_bytes());
std::memcpy(reinterpret_cast<char*>(res->data().data()) + rwrite.offset, rwrite.data.data(), rwrite.data.size_bytes());
return;
}
auto id = static_cast<std::size_t>(static_cast<tz::hanval>(rwrite.resource));
Expand Down Expand Up @@ -1283,7 +1283,7 @@ namespace tz::gl
{
continue;
}
tz::assert(res->get_access() == tz::gl::resource_access::static_fixed, "while initialising static resources, detected non-static-fixed resource at handle %zu that somehow ended up with a non-null staging buffer. logic error. please submit a bug report.", i);
tz::assert(res->get_access() == tz::gl::resource_access::static_access, "while initialising static resources, detected non-static-fixed resource at handle %zu that somehow ended up with a non-null staging buffer. logic error. please submit a bug report.", i);
switch(res->get_type())
{
case tz::gl::resource_type::buffer:
Expand Down Expand Up @@ -1413,6 +1413,11 @@ namespace tz::gl
if(bufcomp->size() != arg.size)
{
bufcomp->resize(arg.size);
// if we're static, we need to write the resource data again.
if(bufcomp->get_resource()->get_access() == tz::gl::resource_access::static_access)
{
side_effects.rewrite_static_resources = true;
}
if(arg.buffer_handle == this->state.graphics.index_buffer || arg.buffer_handle == this->state.graphics.draw_buffer)
{
side_effects.rerecord_work_commands = true;
Expand All @@ -1431,6 +1436,10 @@ namespace tz::gl
{
imgcomp->resize(arg.dimensions);
renderer_resource_manager::notify_image_dirty(arg.image_handle);
if(imgcomp->get_resource()->get_access() == tz::gl::resource_access::static_access)
{
side_effects.rewrite_static_resources = true;
}
side_effects.rewrite_image_descriptors = true;
}
},
Expand Down
19 changes: 14 additions & 5 deletions src/tz/gl/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ namespace tz::gl
{
std::array<const char*, static_cast<int>(resource_access::Count)> resource_access_strings
{
"Static Fixed",
"Dynamic Fixed",
"Dynamic Variable"
"Static",
"Dynamic",
"Static Fixed (Legacy)",
"Dynamic Fixed (Legacy)",
"Dynamic Variable (Legacy)"
};

std::array<const char*, static_cast<int>(resource_flag::Count)> resource_flag_strings
Expand Down Expand Up @@ -123,8 +125,15 @@ namespace tz::gl

void resource::set_mapped_data(std::span<std::byte> mapped_resource_data)
{
tz::assert(this->get_access() == resource_access::dynamic_fixed || this->get_access() == resource_access::dynamic_variable, "Cannot set mapped data on a static resource.");
this->mapped_resource_data = mapped_resource_data;
if(this->get_access() == resource_access::dynamic_access)
{
this->mapped_resource_data = mapped_resource_data;
}
else
{
this->resource_data.resize(mapped_resource_data.size_bytes());
std::copy(mapped_resource_data.begin(), mapped_resource_data.end(), this->resource_data.begin());
}
}

bool buffer_resource::is_null() const
Expand Down
4 changes: 2 additions & 2 deletions src/tz/gl/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace tz::gl

struct buffer_info
{
resource_access access = resource_access::static_fixed;
resource_access access = resource_access::static_access;
resource_flags flags = {};
};

Expand Down Expand Up @@ -101,7 +101,7 @@ namespace tz::gl
/// Image dimensions, in pixels.
tz::vec2ui dimensions;
/// Access specifier. By default this is static fixed.
resource_access access = resource_access::static_fixed;
resource_access access = resource_access::static_access;
/// Flags specifying any special usages for the image. By default there are no flags.
resource_flags flags = {};
};
Expand Down

0 comments on commit 5369e8a

Please sign in to comment.