1
0
Fork 0
forked from suyu/suyu

spirv: Implement image buffers

This commit is contained in:
ReinUsesLisp 2021-04-14 21:36:36 -03:00 committed by ameerj
parent d8ec99dada
commit 416e1b7441
9 changed files with 142 additions and 49 deletions

View file

@ -54,28 +54,30 @@ Id ImageType(EmitContext& ctx, const TextureDescriptor& desc) {
throw InvalidArgument("Invalid texture type {}", desc.type);
}
spv::ImageFormat GetImageFormat(ImageFormat format) {
switch (format) {
case ImageFormat::Typeless:
return spv::ImageFormat::Unknown;
case ImageFormat::R8_UINT:
return spv::ImageFormat::R8ui;
case ImageFormat::R8_SINT:
return spv::ImageFormat::R8i;
case ImageFormat::R16_UINT:
return spv::ImageFormat::R16ui;
case ImageFormat::R16_SINT:
return spv::ImageFormat::R16i;
case ImageFormat::R32_UINT:
return spv::ImageFormat::R32ui;
case ImageFormat::R32G32_UINT:
return spv::ImageFormat::Rg32ui;
case ImageFormat::R32G32B32A32_UINT:
return spv::ImageFormat::Rgba32ui;
}
throw InvalidArgument("Invalid image format {}", format);
}
Id ImageType(EmitContext& ctx, const ImageDescriptor& desc) {
const spv::ImageFormat format{[&] {
switch (desc.format) {
case ImageFormat::Typeless:
return spv::ImageFormat::Unknown;
case ImageFormat::R8_UINT:
return spv::ImageFormat::R8ui;
case ImageFormat::R8_SINT:
return spv::ImageFormat::R8i;
case ImageFormat::R16_UINT:
return spv::ImageFormat::R16ui;
case ImageFormat::R16_SINT:
return spv::ImageFormat::R16i;
case ImageFormat::R32_UINT:
return spv::ImageFormat::R32ui;
case ImageFormat::R32G32_UINT:
return spv::ImageFormat::Rg32ui;
case ImageFormat::R32G32B32A32_UINT:
return spv::ImageFormat::Rgba32ui;
}
throw InvalidArgument("Invalid image format {}", desc.format);
}()};
const spv::ImageFormat format{GetImageFormat(desc.format)};
const Id type{ctx.U32[1]};
switch (desc.type) {
case TextureType::Color1D:
@ -388,6 +390,7 @@ EmitContext::EmitContext(const Profile& profile_, IR::Program& program, u32& bin
DefineConstantBuffers(program.info, binding);
DefineStorageBuffers(program.info, binding);
DefineTextureBuffers(program.info, binding);
DefineImageBuffers(program.info, binding);
DefineTextures(program.info, binding);
DefineImages(program.info, binding);
DefineAttributeMemAccess(program.info);
@ -883,6 +886,31 @@ void EmitContext::DefineTextureBuffers(const Info& info, u32& binding) {
}
}
void EmitContext::DefineImageBuffers(const Info& info, u32& binding) {
image_buffers.reserve(info.image_buffer_descriptors.size());
for (const ImageBufferDescriptor& desc : info.image_buffer_descriptors) {
if (desc.count != 1) {
throw NotImplementedException("Array of image buffers");
}
const spv::ImageFormat format{GetImageFormat(desc.format)};
const Id image_type{TypeImage(U32[4], spv::Dim::Buffer, false, false, false, 2, format)};
const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)};
const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
Decorate(id, spv::Decoration::Binding, binding);
Decorate(id, spv::Decoration::DescriptorSet, 0U);
Name(id, fmt::format("imgbuf{}_{:02x}", desc.cbuf_index, desc.cbuf_offset));
const ImageBufferDefinition def{
.id = id,
.image_type = image_type,
};
image_buffers.insert(image_buffers.end(), desc.count, def);
if (profile.supported_spirv >= 0x00010400) {
interfaces.push_back(id);
}
binding += desc.count;
}
}
void EmitContext::DefineTextures(const Info& info, u32& binding) {
textures.reserve(info.texture_descriptors.size());
for (const TextureDescriptor& desc : info.texture_descriptors) {

View file

@ -35,6 +35,11 @@ struct TextureDefinition {
Id image_type;
};
struct ImageBufferDefinition {
Id id;
Id image_type;
};
struct ImageDefinition {
Id id;
Id image_type;
@ -136,6 +141,7 @@ public:
std::array<UniformDefinitions, Info::MAX_CBUFS> cbufs{};
std::array<StorageDefinitions, Info::MAX_SSBOS> ssbos{};
std::vector<Id> texture_buffers;
std::vector<ImageBufferDefinition> image_buffers;
std::vector<TextureDefinition> textures;
std::vector<ImageDefinition> images;
@ -213,6 +219,7 @@ private:
void DefineConstantBuffers(const Info& info, u32& binding);
void DefineStorageBuffers(const Info& info, u32& binding);
void DefineTextureBuffers(const Info& info, u32& binding);
void DefineImageBuffers(const Info& info, u32& binding);
void DefineTextures(const Info& info, u32& binding);
void DefineImages(const Info& info, u32& binding);
void DefineAttributeMemAccess(const Info& info);

View file

@ -149,7 +149,8 @@ Id Image(EmitContext& ctx, const IR::Value& index, IR::TextureInstInfo info) {
throw NotImplementedException("Indirect image indexing");
}
if (info.type == TextureType::Buffer) {
throw NotImplementedException("Image buffer");
const ImageBufferDefinition def{ctx.image_buffers.at(index.U32())};
return ctx.OpLoad(def.image_type, def.id);
} else {
const ImageDefinition def{ctx.images.at(index.U32())};
return ctx.OpLoad(def.image_type, def.id);

View file

@ -158,9 +158,11 @@ TextureInst MakeInst(Environment& env, IR::Block* block, IR::Inst& inst) {
class Descriptors {
public:
explicit Descriptors(TextureBufferDescriptors& texture_buffer_descriptors_,
ImageBufferDescriptors& image_buffer_descriptors_,
TextureDescriptors& texture_descriptors_,
ImageDescriptors& image_descriptors_)
: texture_buffer_descriptors{texture_buffer_descriptors_},
image_buffer_descriptors{image_buffer_descriptors_},
texture_descriptors{texture_descriptors_}, image_descriptors{image_descriptors_} {}
u32 Add(const TextureBufferDescriptor& desc) {
@ -170,6 +172,13 @@ public:
});
}
u32 Add(const ImageBufferDescriptor& desc) {
return Add(image_buffer_descriptors, desc, [&desc](const auto& existing) {
return desc.format == existing.format && desc.cbuf_index == existing.cbuf_index &&
desc.cbuf_offset == existing.cbuf_offset;
});
}
u32 Add(const TextureDescriptor& desc) {
return Add(texture_descriptors, desc, [&desc](const auto& existing) {
return desc.cbuf_index == existing.cbuf_index &&
@ -200,6 +209,7 @@ private:
}
TextureBufferDescriptors& texture_buffer_descriptors;
ImageBufferDescriptors& image_buffer_descriptors;
TextureDescriptors& texture_descriptors;
ImageDescriptors& image_descriptors;
};
@ -224,6 +234,7 @@ void TexturePass(Environment& env, IR::Program& program) {
});
Descriptors descriptors{
program.info.texture_buffer_descriptors,
program.info.image_buffer_descriptors,
program.info.texture_descriptors,
program.info.image_descriptors,
};
@ -261,7 +272,13 @@ void TexturePass(Environment& env, IR::Program& program) {
case IR::Opcode::ImageWrite: {
const bool is_written{inst->GetOpcode() == IR::Opcode::ImageWrite};
if (flags.type == TextureType::Buffer) {
throw NotImplementedException("Image buffer");
index = descriptors.Add(ImageBufferDescriptor{
.format = flags.image_format,
.is_written = is_written,
.cbuf_index = cbuf.index,
.cbuf_offset = cbuf.offset,
.count = 1,
});
} else {
index = descriptors.Add(ImageDescriptor{
.type = flags.type,

View file

@ -67,6 +67,15 @@ struct TextureBufferDescriptor {
};
using TextureBufferDescriptors = boost::container::small_vector<TextureBufferDescriptor, 6>;
struct ImageBufferDescriptor {
ImageFormat format;
bool is_written;
u32 cbuf_index;
u32 cbuf_offset;
u32 count;
};
using ImageBufferDescriptors = boost::container::small_vector<ImageBufferDescriptor, 2>;
struct TextureDescriptor {
TextureType type;
bool is_depth;
@ -153,6 +162,7 @@ struct Info {
constant_buffer_descriptors;
boost::container::static_vector<StorageBufferDescriptor, MAX_SSBOS> storage_buffers_descriptors;
TextureBufferDescriptors texture_buffer_descriptors;
ImageBufferDescriptors image_buffer_descriptors;
TextureDescriptors texture_descriptors;
ImageDescriptors image_descriptors;
};

View file

@ -154,7 +154,7 @@ public:
void UnbindGraphicsTextureBuffers(size_t stage);
void BindGraphicsTextureBuffer(size_t stage, size_t tbo_index, GPUVAddr gpu_addr, u32 size,
PixelFormat format);
PixelFormat format, bool is_written);
void UnbindComputeStorageBuffers();
@ -163,8 +163,8 @@ public:
void UnbindComputeTextureBuffers();
void BindComputeTextureBuffer(size_t tbo_index, GPUVAddr gpu_addr, u32 size,
PixelFormat format);
void BindComputeTextureBuffer(size_t tbo_index, GPUVAddr gpu_addr, u32 size, PixelFormat format,
bool is_written);
void FlushCachedWrites();
@ -393,7 +393,9 @@ private:
u32 written_compute_storage_buffers = 0;
std::array<u32, NUM_STAGES> enabled_texture_buffers{};
std::array<u32, NUM_STAGES> written_texture_buffers{};
u32 enabled_compute_texture_buffers = 0;
u32 written_compute_texture_buffers = 0;
std::array<u32, NUM_STAGES> fast_bound_uniform_buffers{};
@ -700,12 +702,14 @@ void BufferCache<P>::BindGraphicsStorageBuffer(size_t stage, size_t ssbo_index,
template <class P>
void BufferCache<P>::UnbindGraphicsTextureBuffers(size_t stage) {
enabled_texture_buffers[stage] = 0;
written_texture_buffers[stage] = 0;
}
template <class P>
void BufferCache<P>::BindGraphicsTextureBuffer(size_t stage, size_t tbo_index, GPUVAddr gpu_addr,
u32 size, PixelFormat format) {
u32 size, PixelFormat format, bool is_written) {
enabled_texture_buffers[stage] |= 1U << tbo_index;
written_texture_buffers[stage] |= (is_written ? 1U : 0U) << tbo_index;
texture_buffers[stage][tbo_index] = GetTextureBufferBinding(gpu_addr, size, format);
}
@ -732,12 +736,14 @@ void BufferCache<P>::BindComputeStorageBuffer(size_t ssbo_index, u32 cbuf_index,
template <class P>
void BufferCache<P>::UnbindComputeTextureBuffers() {
enabled_compute_texture_buffers = 0;
written_compute_texture_buffers = 0;
}
template <class P>
void BufferCache<P>::BindComputeTextureBuffer(size_t tbo_index, GPUVAddr gpu_addr, u32 size,
PixelFormat format) {
PixelFormat format, bool is_written) {
enabled_compute_texture_buffers |= 1U << tbo_index;
written_compute_texture_buffers |= (is_written ? 1U : 0U) << tbo_index;
compute_texture_buffers[tbo_index] = GetTextureBufferBinding(gpu_addr, size, format);
}
@ -1274,6 +1280,10 @@ void BufferCache<P>::UpdateTextureBuffers(size_t stage) {
ForEachEnabledBit(enabled_texture_buffers[stage], [&](u32 index) {
Binding& binding = texture_buffers[stage][index];
binding.buffer_id = FindBuffer(binding.cpu_addr, binding.size);
// Mark buffer as written if needed
if (((written_texture_buffers[stage] >> index) & 1) != 0) {
MarkWrittenBuffer(binding.buffer_id, binding.cpu_addr, binding.size);
}
});
}
@ -1343,6 +1353,10 @@ void BufferCache<P>::UpdateComputeTextureBuffers() {
ForEachEnabledBit(enabled_compute_texture_buffers, [&](u32 index) {
Binding& binding = compute_texture_buffers[index];
binding.buffer_id = FindBuffer(binding.cpu_addr, binding.size);
// Mark as written if needed
if (((written_compute_texture_buffers >> index) & 1) != 0) {
MarkWrittenBuffer(binding.buffer_id, binding.cpu_addr, binding.size);
}
});
}

View file

@ -88,6 +88,7 @@ public:
Add(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, stage, info.constant_buffer_descriptors.size());
Add(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, stage, info.storage_buffers_descriptors.size());
Add(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, stage, info.texture_buffer_descriptors.size());
Add(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, stage, info.image_buffer_descriptors.size());
Add(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, stage, info.texture_descriptors.size());
Add(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, stage, info.image_descriptors.size());
}
@ -126,6 +127,7 @@ inline void PushImageDescriptors(const Shader::Info& info, const VkSampler*& sam
const ImageId*& image_view_ids, TextureCache& texture_cache,
VKUpdateDescriptorQueue& update_descriptor_queue) {
image_view_ids += info.texture_buffer_descriptors.size();
image_view_ids += info.image_buffer_descriptors.size();
for (const auto& desc : info.texture_descriptors) {
const VkSampler sampler{*(samplers++)};
ImageView& image_view{texture_cache.GetImageView(*(image_view_ids++))};

View file

@ -97,10 +97,12 @@ void ComputePipeline::Configure(Tegra::Engines::KeplerCompute& kepler_compute,
const u32 raw_handle{gpu_memory.Read<u32>(addr)};
return TextureHandle(raw_handle, via_header_index);
}};
for (const auto& desc : info.texture_buffer_descriptors) {
const auto add_image{[&](const auto& desc) {
const TextureHandle handle{read_handle(desc.cbuf_index, desc.cbuf_offset)};
image_view_indices.push_back(handle.image);
}
}};
std::ranges::for_each(info.texture_buffer_descriptors, add_image);
std::ranges::for_each(info.image_buffer_descriptors, add_image);
for (const auto& desc : info.texture_descriptors) {
const TextureHandle handle{read_handle(desc.cbuf_index, desc.cbuf_offset)};
image_view_indices.push_back(handle.image);
@ -108,24 +110,29 @@ void ComputePipeline::Configure(Tegra::Engines::KeplerCompute& kepler_compute,
Sampler* const sampler = texture_cache.GetComputeSampler(handle.sampler);
samplers.push_back(sampler->Handle());
}
for (const auto& desc : info.image_descriptors) {
const TextureHandle handle{read_handle(desc.cbuf_index, desc.cbuf_offset)};
image_view_indices.push_back(handle.image);
}
std::ranges::for_each(info.image_descriptors, add_image);
const std::span indices_span(image_view_indices.data(), image_view_indices.size());
texture_cache.FillComputeImageViews(indices_span, image_view_ids);
buffer_cache.UnbindComputeTextureBuffers();
ImageId* texture_buffer_ids{image_view_ids.data()};
size_t index{};
for (const auto& desc : info.texture_buffer_descriptors) {
const auto add_buffer{[&](const auto& desc) {
ASSERT(desc.count == 1);
bool is_written{false};
if constexpr (std::is_same_v<decltype(desc), const Shader::ImageBufferDescriptor&>) {
is_written = desc.is_written;
}
ImageView& image_view = texture_cache.GetImageView(*texture_buffer_ids);
buffer_cache.BindComputeTextureBuffer(index, image_view.GpuAddr(), image_view.BufferSize(),
image_view.format);
image_view.format, is_written);
++texture_buffer_ids;
++index;
}
}};
std::ranges::for_each(info.texture_buffer_descriptors, add_buffer);
std::ranges::for_each(info.image_buffer_descriptors, add_buffer);
buffer_cache.UpdateComputeBuffers();
buffer_cache.BindHostComputeBuffers();

View file

@ -175,10 +175,12 @@ void GraphicsPipeline::Configure(bool is_indexed) {
const u32 raw_handle{gpu_memory.Read<u32>(addr)};
return TextureHandle(raw_handle, via_header_index);
}};
for (const auto& desc : info.texture_buffer_descriptors) {
const auto add_image{[&](const auto& desc) {
const TextureHandle handle{read_handle(desc.cbuf_index, desc.cbuf_offset)};
image_view_indices.push_back(handle.image);
}
}};
std::ranges::for_each(info.texture_buffer_descriptors, add_image);
std::ranges::for_each(info.image_buffer_descriptors, add_image);
for (const auto& desc : info.texture_descriptors) {
const TextureHandle handle{read_handle(desc.cbuf_index, desc.cbuf_offset)};
image_view_indices.push_back(handle.image);
@ -186,28 +188,33 @@ void GraphicsPipeline::Configure(bool is_indexed) {
Sampler* const sampler{texture_cache.GetGraphicsSampler(handle.sampler)};
samplers.push_back(sampler->Handle());
}
for (const auto& desc : info.image_descriptors) {
const TextureHandle handle{read_handle(desc.cbuf_index, desc.cbuf_offset)};
image_view_indices.push_back(handle.image);
}
std::ranges::for_each(info.image_descriptors, add_image);
}
const std::span indices_span(image_view_indices.data(), image_view_indices.size());
texture_cache.FillGraphicsImageViews(indices_span, image_view_ids);
ImageId* texture_buffer_index{image_view_ids.data()};
for (size_t stage = 0; stage < Maxwell::MaxShaderStage; ++stage) {
const Shader::Info& info{stage_infos[stage]};
buffer_cache.UnbindGraphicsTextureBuffers(stage);
size_t index{};
for (const auto& desc : info.texture_buffer_descriptors) {
const auto add_buffer{[&](const auto& desc) {
ASSERT(desc.count == 1);
ImageView& image_view = texture_cache.GetImageView(*texture_buffer_index);
bool is_written{false};
if constexpr (std::is_same_v<decltype(desc), const Shader::ImageBufferDescriptor&>) {
is_written = desc.is_written;
}
ImageView& image_view{texture_cache.GetImageView(*texture_buffer_index)};
buffer_cache.BindGraphicsTextureBuffer(stage, index, image_view.GpuAddr(),
image_view.BufferSize(), image_view.format);
image_view.BufferSize(), image_view.format,
is_written);
++index;
++texture_buffer_index;
}
}};
const Shader::Info& info{stage_infos[stage]};
buffer_cache.UnbindGraphicsTextureBuffers(stage);
std::ranges::for_each(info.texture_buffer_descriptors, add_buffer);
std::ranges::for_each(info.image_buffer_descriptors, add_buffer);
texture_buffer_index += info.texture_descriptors.size();
texture_buffer_index += info.image_descriptors.size();
}
buffer_cache.UpdateGraphicsBuffers(is_indexed);