shader: Implement texture buffers
This commit is contained in:
		| @@ -46,6 +46,8 @@ Id ImageType(EmitContext& ctx, const TextureDescriptor& desc) { | ||||
|         return ctx.TypeImage(type, spv::Dim::Cube, true, false, false, 1, format); | ||||
|     case TextureType::ShadowArrayCube: | ||||
|         return ctx.TypeImage(type, spv::Dim::Cube, true, true, false, 1, format); | ||||
|     case TextureType::Buffer: | ||||
|         break; | ||||
|     } | ||||
|     throw InvalidArgument("Invalid texture type {}", desc.type); | ||||
| } | ||||
| @@ -129,6 +131,7 @@ EmitContext::EmitContext(const Profile& profile_, IR::Program& program, u32& bin | ||||
|     DefineConstantBuffers(program.info, binding); | ||||
|     DefineStorageBuffers(program.info, binding); | ||||
|     DefineTextures(program.info, binding); | ||||
|     DefineTextureBuffers(program.info, binding); | ||||
|     DefineAttributeMemAccess(program.info); | ||||
|     DefineLabels(program); | ||||
| } | ||||
| @@ -541,6 +544,32 @@ void EmitContext::DefineTextures(const Info& info, u32& binding) { | ||||
|     } | ||||
| } | ||||
|  | ||||
| void EmitContext::DefineTextureBuffers(const Info& info, u32& binding) { | ||||
|     if (info.texture_buffer_descriptors.empty()) { | ||||
|         return; | ||||
|     } | ||||
|     const spv::ImageFormat format{spv::ImageFormat::Unknown}; | ||||
|     image_buffer_type = TypeImage(F32[1], spv::Dim::Buffer, 0U, false, false, 1, format); | ||||
|     sampled_texture_buffer_type = TypeSampledImage(image_buffer_type); | ||||
|  | ||||
|     const Id type{TypePointer(spv::StorageClass::UniformConstant, sampled_texture_buffer_type)}; | ||||
|     texture_buffers.reserve(info.texture_buffer_descriptors.size()); | ||||
|     for (const TextureBufferDescriptor& desc : info.texture_buffer_descriptors) { | ||||
|         if (desc.count != 1) { | ||||
|             throw NotImplementedException("Array of texture buffers"); | ||||
|         } | ||||
|         const Id id{AddGlobalVariable(type, spv::StorageClass::UniformConstant)}; | ||||
|         Decorate(id, spv::Decoration::Binding, binding); | ||||
|         Decorate(id, spv::Decoration::DescriptorSet, 0U); | ||||
|         Name(id, fmt::format("texbuf{}_{:02x}", desc.cbuf_index, desc.cbuf_offset)); | ||||
|         texture_buffers.insert(texture_buffers.end(), desc.count, id); | ||||
|         if (profile.supported_spirv >= 0x00010400) { | ||||
|             interfaces.push_back(id); | ||||
|         } | ||||
|         binding += desc.count; | ||||
|     } | ||||
| } | ||||
|  | ||||
| void EmitContext::DefineLabels(IR::Program& program) { | ||||
|     for (IR::Block* const block : program.blocks) { | ||||
|         block->SetDefinition(OpLabel()); | ||||
|   | ||||
| @@ -90,9 +90,13 @@ public: | ||||
|  | ||||
|     Id storage_u32{}; | ||||
|  | ||||
|     Id image_buffer_type{}; | ||||
|     Id sampled_texture_buffer_type{}; | ||||
|  | ||||
|     std::array<UniformDefinitions, Info::MAX_CBUFS> cbufs{}; | ||||
|     std::array<Id, Info::MAX_SSBOS> ssbos{}; | ||||
|     std::vector<TextureDefinition> textures; | ||||
|     std::vector<Id> texture_buffers; | ||||
|  | ||||
|     Id workgroup_id{}; | ||||
|     Id local_invocation_id{}; | ||||
| @@ -151,6 +155,7 @@ private: | ||||
|     void DefineConstantBuffers(const Info& info, u32& binding); | ||||
|     void DefineStorageBuffers(const Info& info, u32& binding); | ||||
|     void DefineTextures(const Info& info, u32& binding); | ||||
|     void DefineTextureBuffers(const Info& info, u32& binding); | ||||
|     void DefineAttributeMemAccess(const Info& info); | ||||
|     void DefineLabels(IR::Program& program); | ||||
|  | ||||
|   | ||||
| @@ -249,6 +249,7 @@ void SetupCapabilities(const Profile& profile, const Info& info, EmitContext& ct | ||||
|     // TODO: Track this usage | ||||
|     ctx.AddCapability(spv::Capability::ImageGatherExtended); | ||||
|     ctx.AddCapability(spv::Capability::ImageQuery); | ||||
|     ctx.AddCapability(spv::Capability::SampledBuffer); | ||||
| } | ||||
|  | ||||
| Id PhiArgDef(EmitContext& ctx, IR::Inst* inst, size_t index) { | ||||
|   | ||||
| @@ -128,12 +128,18 @@ Id Texture(EmitContext& ctx, const IR::Value& index) { | ||||
|     throw NotImplementedException("Indirect texture sample"); | ||||
| } | ||||
|  | ||||
| Id TextureImage(EmitContext& ctx, const IR::Value& index) { | ||||
|     if (index.IsImmediate()) { | ||||
| Id TextureImage(EmitContext& ctx, const IR::Value& index, IR::TextureInstInfo info) { | ||||
|     if (!index.IsImmediate()) { | ||||
|         throw NotImplementedException("Indirect texture sample"); | ||||
|     } | ||||
|     if (info.type == TextureType::Buffer) { | ||||
|         const Id sampler_id{ctx.texture_buffers.at(index.U32())}; | ||||
|         const Id id{ctx.OpLoad(ctx.sampled_texture_buffer_type, sampler_id)}; | ||||
|         return ctx.OpImage(ctx.image_buffer_type, id); | ||||
|     } else { | ||||
|         const TextureDefinition def{ctx.textures.at(index.U32())}; | ||||
|         return ctx.OpImage(def.image_type, ctx.OpLoad(def.sampled_type, def.id)); | ||||
|     } | ||||
|     throw NotImplementedException("Indirect texture sample"); | ||||
| } | ||||
|  | ||||
| Id Decorate(EmitContext& ctx, IR::Inst* inst, Id sample) { | ||||
| @@ -297,17 +303,22 @@ Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, | ||||
|                 ctx.F32[4], Texture(ctx, index), coords, dref, operands.Mask(), operands.Span()); | ||||
| } | ||||
|  | ||||
| #pragma optimize("", off) | ||||
|  | ||||
| Id EmitImageFetch(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id offset, | ||||
|                   Id lod, Id ms) { | ||||
|     const auto info{inst->Flags<IR::TextureInstInfo>()}; | ||||
|     if (info.type == TextureType::Buffer) { | ||||
|         lod = Id{}; | ||||
|     } | ||||
|     const ImageOperands operands(offset, lod, ms); | ||||
|     return Emit(&EmitContext::OpImageSparseFetch, &EmitContext::OpImageFetch, ctx, inst, ctx.F32[4], | ||||
|                 TextureImage(ctx, index), coords, operands.Mask(), operands.Span()); | ||||
|                 TextureImage(ctx, index, info), coords, operands.Mask(), operands.Span()); | ||||
| } | ||||
|  | ||||
| Id EmitImageQueryDimensions(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id lod) { | ||||
|     const auto info{inst->Flags<IR::TextureInstInfo>()}; | ||||
|     const Id image{TextureImage(ctx, index)}; | ||||
|     const Id image{TextureImage(ctx, index, info)}; | ||||
|     const Id zero{ctx.u32_zero_value}; | ||||
|     const auto mips{[&] { return ctx.OpImageQueryLevels(ctx.U32[1], image); }}; | ||||
|     switch (info.type) { | ||||
| @@ -331,6 +342,9 @@ Id EmitImageQueryDimensions(EmitContext& ctx, IR::Inst* inst, const IR::Value& i | ||||
|     case TextureType::ShadowArrayCube: | ||||
|         return ctx.OpCompositeConstruct(ctx.U32[4], ctx.OpImageQuerySizeLod(ctx.U32[3], image, lod), | ||||
|                                         mips()); | ||||
|     case TextureType::Buffer: | ||||
|         return ctx.OpCompositeConstruct(ctx.U32[4], ctx.OpImageQuerySize(ctx.U32[1], image), zero, | ||||
|                                         zero, mips()); | ||||
|     } | ||||
|     throw LogicError("Unspecified image type {}", info.type.Value()); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user