Compare commits

...
Author SHA1 Message Date
nmzik 02578f3c6b docs: update screenshots 2026-09-24 12:18:55 +04:00
fcf0f3b67e shader: fold the lane test after a pixel shader's entry s_wqm (#801)
Preserve live lane predicates through RDNA2 whole-quad expansion

Keep the source-live predicate when WQM expands a scalar mask, so entry masks remain known live without an instruction pre-scan. Preserve scalar mask values, SCC, half writes, and raw-source behavior.

Cover entry pixel masks, cross-half copies, and scalar/high-word sources with existing shader regression suites.

Co-authored-by: nmzik <Nmzik@mail.ru>
Co-authored-by: Douglas <douglasproducoes21@gmail.com>
2026-09-24 12:02:19 +04:00
nmzik 84d015120c Remove duplicate image scratch setup from push constant test 2026-09-24 11:13:33 +04:00
nmzikandTarkusR a6248c7c7c Convert formatted half-float buffer stores from F32
Co-authored-by: TarkusR <90155059+TarkusR@users.noreply.github.com>
2026-09-24 11:13:33 +04:00
nmzik 3bd5de646d Reuse prepared shader bindings across draws and dispatches 2026-09-24 11:13:33 +04:00
nmzik 0382d0ca58 Publish reserved guest mappings atomically after backing commit 2026-09-24 11:13:33 +04:00
nmzik a670dc84b3 Test RDNA2 wave32 ReadLane material key selection 2026-09-24 11:13:33 +04:00
nmzikandTarkusR 963a0817dc Bound uniformized image keys by guarded material mask
Co-authored-by: TarkusR <90155059+TarkusR@users.noreply.github.com>
2026-09-24 11:13:33 +04:00
nmzik 1617ee77e5 Share CFG reachability for descriptor bounds analysis 2026-09-24 11:13:33 +04:00
nmzikandTarkusR 66cc2fe26d Track depth and stencil attachment feedback across graphics stages
Co-authored-by: TarkusR <90155059+TarkusR@users.noreply.github.com>
2026-09-24 11:13:33 +04:00
nmzik 51c34d1a24 Test zero scalar shift folding on RDNA2 compute shader 2026-09-24 11:13:33 +04:00
nmzikandTarkusR 3b89e36d1e Track bounded indirect image tables from signed shader loops
Co-authored-by: TarkusR <90155059+TarkusR@users.noreply.github.com>
2026-09-24 11:13:33 +04:00
João Lobo 3dc3fd0197 shader: implement DS_INC_U32 and DS_DEC_U32 (#790) 2026-09-24 00:45:12 +04:00
Deniz ŞAHİN fc86509aac shader: support DS_WRITE_B8_D16_HI (#794)
Decode GFX10 DS opcode 0xa0 as DS_WRITE_B8_D16_HI (next to
DS_WRITE_B16_D16_HI at 0xa1) and lower it through the existing 8-bit
shared-memory write. The stored byte is bits 23:16 of the data VGPR,
selected with SDWA BYTE_2 on src1.

Add captured-instruction decode coverage (from GTA V, PPSA04264) and a
runtime regression that writes one byte of a distinctive source value
into a pre-filled LDS dword.
2026-09-24 00:44:39 +04:00
569ec04d1a Fix save metadata and timestamps
Co-authored-by: Ali Almohaya <AliAlmohaya@gmail.com>
Co-authored-by: CheesyPoofs346 <jjs777406@gmail.com>
2026-09-23 22:50:51 +04:00
nmzik 7d7267cc56 Translate NGG mesh prolog with guest wave32 width 2026-09-23 22:50:51 +04:00
nmzik 2aa0a64f63 materialize compressed video-out DCC clears before flip
PPSA27624
2026-09-23 22:50:51 +04:00
nmzik fc04db7596 NGS2: preserve ATRAC9 config word byte order 2026-09-23 22:50:51 +04:00
nmzik 1275d65bb3 Audio: split ACM, Audio3D, and NGS2 implementations 2026-09-23 22:50:51 +04:00
nmzik 3742ad9980 NGS2: warn and skip unsupported custom audio modules 2026-09-23 22:50:51 +04:00
nmzik c437de9721 Presenter: avoid lock 2026-09-23 22:50:51 +04:00
42 changed files with 4937 additions and 2999 deletions

No files matched your search

+6 -6
View File
@@ -44,8 +44,8 @@ graphical glitches, low compatibility, and poor performance.
<table align="center">
<tr>
<td align="center">
<strong>Disgaea 6</strong><br>
<img src="docs/screenshots/ps5-01.png" width="300" alt="Disgaea 6 running in KytyPS5">
<strong>Astro Bot</strong><br>
<img src="docs/screenshots/ps5-01.png" width="300" alt="Astro Bot running in KytyPS5">
</td>
<td align="center">
<strong>Dreaming Sarah</strong><br>
@@ -64,12 +64,12 @@ graphical glitches, low compatibility, and poor performance.
</tr>
<tr>
<td align="center">
<strong>Hellboy</strong><br>
<img src="docs/screenshots/ps5-02.png" width="300" alt="Hellboy running in KytyPS5">
<strong>Demon's Souls</strong><br>
<img src="docs/screenshots/ps5-02.png" width="300" alt="Demon's Souls running in KytyPS5">
</td>
<td align="center">
<strong>Paleo Pines</strong><br>
<img src="docs/screenshots/ps5-06.png" width="300" alt="Paleo Pines running in KytyPS5">
<strong>Hellboy</strong><br>
<img src="docs/screenshots/ps5-06.png" width="300" alt="Hellboy running in KytyPS5">
</td>
</tr>
</table>
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.8 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 MiB

After

Width:  |  Height:  |  Size: 1.6 MiB

+15 -11
View File
@@ -1210,7 +1210,9 @@ void TextureCache::MaterializeDccClear(ImageId id, const ImageDesc& desc,
}
// Native expanded keys own consumption. Existing buffer tracking publishes this CPU
// write to future GPU readers; FillBuffer can fault and must run outside the texture lock.
m_buffer_cache.FillBuffer(address, slice_size, UINT32_MAX, false);
if (desc.type != BindingType::VideoOut) {
m_buffer_cache.FillBuffer(address, slice_size, UINT32_MAX, false);
}
}
}
@@ -1331,16 +1333,6 @@ ImageId TextureCache::FindImage(ImageDesc& desc, bool exact_format) {
}
}
auto& image = m_slot_images[result];
if (desc.type == BindingType::VideoOut &&
desc.info.metadata.compression != VideoOutCompression::Uncompressed) {
const bool guest_dirty = image.IsBufferModified() || image.IsCpuDirty();
const bool native_current =
(image.usage.render_target || image.IsGpuModified()) && !guest_dirty;
if (!native_current) {
EXIT("TextureCache: compressed video-out read requires clean native GPU "
"contents\n");
}
}
if (view_mip >= 0) {
desc.view_info.base_level = static_cast<uint32_t>(view_mip);
}
@@ -1351,6 +1343,18 @@ ImageId TextureCache::FindImage(ImageDesc& desc, bool exact_format) {
TouchImage(image);
}
MaterializeDccClear(result, desc, metadata_base_layer);
if (desc.type == BindingType::VideoOut &&
desc.info.metadata.compression != VideoOutCompression::Uncompressed) {
std::scoped_lock lock {m_lock};
const auto& image = m_slot_images[result];
const bool guest_dirty = image.IsBufferModified() || image.IsCpuDirty();
const bool native_current =
(image.usage.render_target || image.IsGpuModified()) && !guest_dirty;
if (!native_current) {
EXIT("TextureCache: compressed video-out read requires clean native GPU "
"contents\n");
}
}
return result;
}
@@ -41,13 +41,42 @@ struct RenderDepthInfo {
[[nodiscard]] vk::ImageAspectFlags AttachmentWriteAspects() const;
};
inline vk::ImageAspectFlags DepthFeedbackAspects(vk::ImageAspectFlags draw_writes,
const ImageViewInfo& target,
const ImageViewInfo& sampled) {
if (!ImageRangeOverlaps(target.base_level, target.level_count, sampled.base_level,
sampled.level_count) ||
!ImageRangeOverlaps(target.base_layer, target.layer_count, sampled.base_layer,
sampled.layer_count)) {
return {};
}
return draw_writes & sampled.aspect;
}
inline vk::ImageAspectFlags DepthReadableAspects(vk::ImageLayout layout) {
switch (layout) {
case vk::ImageLayout::eDepthReadOnlyOptimal:
case vk::ImageLayout::eDepthReadOnlyStencilAttachmentOptimal:
return vk::ImageAspectFlagBits::eDepth;
case vk::ImageLayout::eStencilReadOnlyOptimal:
case vk::ImageLayout::eDepthAttachmentStencilReadOnlyOptimal:
return vk::ImageAspectFlagBits::eStencil;
case vk::ImageLayout::eDepthStencilReadOnlyOptimal:
case vk::ImageLayout::eAttachmentFeedbackLoopOptimalEXT:
case vk::ImageLayout::eGeneral:
return vk::ImageAspectFlagBits::eDepth | vk::ImageAspectFlagBits::eStencil;
default:
return {};
}
}
inline vk::ImageLayout depth_attachment_layout(const RenderDepthInfo& depth) {
const auto available = ImageViewOps::DepthAspectMask(depth.desc.view_info.format);
const auto writes = depth.AttachmentWriteAspects();
const bool has_depth = static_cast<bool>(available & vk::ImageAspectFlagBits::eDepth);
const bool has_stencil = static_cast<bool>(available & vk::ImageAspectFlagBits::eStencil);
// LoadOp clears are separate from guest depth writes.
const bool depth_write = depth.depth_write_enable;
// The attachment layout must permit load clears as well as draw writes.
const bool depth_write = static_cast<bool>(writes & vk::ImageAspectFlagBits::eDepth);
const bool stencil_write = static_cast<bool>(writes & vk::ImageAspectFlagBits::eStencil);
if (!has_stencil) {
return depth_write ? vk::ImageLayout::eDepthAttachmentOptimal
@@ -17,6 +17,7 @@
#include "graphics/host_gpu/hostMemory.h"
#include "graphics/host_gpu/renderer/colorRenderTarget.h"
#include "graphics/host_gpu/renderer/debug.h"
#include "graphics/host_gpu/renderer/depthRenderTarget.h"
#include "graphics/host_gpu/renderer/image/imageView.h"
#include "graphics/host_gpu/renderer/image/textureCommon.h"
#include "graphics/host_gpu/renderer/pipeline/shaderResourceBarrier.h"
@@ -749,18 +750,27 @@ void RenderExecutor::ResetBindings() {
m_bound_images.clear();
}
PreparedBindings RenderExecutor::PrepareBindings(const ShaderStageRuntime& runtime) {
void RenderExecutor::PrepareBindings(const ShaderStageRuntime& runtime,
PreparedBindings& prepared) {
KYTY_PROFILER_FUNCTION();
EXIT_IF(!runtime);
const auto& program = *runtime.program;
const auto& snapshot = *runtime.resources;
PreparedBindings prepared;
prepared.runtime = &runtime;
prepared.images.reserve(program.info.images.size());
prepared.gds = {nullptr, 0, VK_WHOLE_SIZE};
prepared.flattened_srt = {};
prepared.shader_data_buffer = {};
prepared.buffer_sources.clear();
prepared.buffers.clear();
prepared.images.resize(program.info.images.size());
prepared.samplers.clear();
prepared.shader_data.clear();
for (uint32_t i = 0; i < program.info.images.size(); i++) {
auto binding = ResolveTexture(program.info.images[i], snapshot.images[i]);
BindImage(binding.image_id, binding.desc.type == TextureCache::BindingType::Storage);
prepared.images.push_back(std::move(binding));
binding.mip_views.swap(prepared.images[i].mip_views);
binding.mip_views.clear();
prepared.images[i] = std::move(binding);
}
prepared.samplers.reserve(program.info.samplers.size());
for (uint32_t i = 0; i < program.info.samplers.size(); i++) {
@@ -775,7 +785,6 @@ PreparedBindings RenderExecutor::PrepareBindings(const ShaderStageRuntime& runti
program.bindings, ShaderRecompiler::IR::DescriptorBindingKind::Gds) != nullptr) {
prepared.gds.buffer = m_context.GetBufferCache().GetGdsBuffer()->Handle();
}
return prepared;
}
void RenderExecutor::FindBuffers(PreparedBindings& prepared) {
@@ -989,19 +998,7 @@ void RenderExecutor::CommitBindings(CommandBuffer& buffer,
std::ranges::find(image.views, binding.image_view, &CachedImageView::view);
EXIT_IF(storage || host_view == image.views.end());
const auto aspect = host_view->info.aspect;
const bool depth_feedback =
layout == vk::ImageLayout::eAttachmentFeedbackLoopOptimalEXT &&
program.stage == ShaderType::Pixel;
const bool depth_read =
depth_feedback || layout == vk::ImageLayout::eDepthReadOnlyOptimal ||
layout == vk::ImageLayout::eDepthStencilReadOnlyOptimal ||
layout == vk::ImageLayout::eDepthReadOnlyStencilAttachmentOptimal;
const bool stencil_read =
layout == vk::ImageLayout::eStencilReadOnlyOptimal ||
layout == vk::ImageLayout::eDepthStencilReadOnlyOptimal ||
layout == vk::ImageLayout::eDepthAttachmentStencilReadOnlyOptimal;
if ((aspect & vk::ImageAspectFlagBits::eDepth && !depth_read) ||
(aspect & vk::ImageAspectFlagBits::eStencil && !stencil_read)) {
if (aspect & ~DepthReadableAspects(layout)) {
EXIT("sampling a writable depth/stencil attachment aspect\n");
}
}
+5 -2
View File
@@ -160,7 +160,7 @@ public:
void DispatchIndirect(uint64_t submit_id, CommandBuffer& buffer, uint64_t args_addr,
uint32_t mode);
[[nodiscard]] PreparedBindings PrepareBindings(const ShaderStageRuntime& runtime);
void PrepareBindings(const ShaderStageRuntime& runtime, PreparedBindings& prepared);
void FindBuffers(PreparedBindings& bindings);
void RebindBuffers(PreparedBindings& bindings);
void RebindImages(PreparedBindings& bindings);
@@ -196,7 +196,8 @@ private:
bool primitive_restart_enable);
[[nodiscard]] RenderState AcquireRenderTargets(CommandBuffer& buffer, RenderColorInfo* colors,
uint32_t color_count, RenderDepthInfo& depth,
const std::optional<PreparedBindings>& pixel = std::nullopt);
vk::ImageAspectFlags& feedback_aspects,
std::span<PreparedBindings* const> stages = {});
[[nodiscard]] bool ResolveColorTargets(CommandBuffer& buffer,
uint32_t render_target_slice_offset);
void BindImage(ImageId id, bool storage);
@@ -209,6 +210,8 @@ private:
uint32_t group_y, uint32_t group_z, uint32_t mode);
RenderContext& m_context;
GraphicsBindings m_graphics_bindings;
PreparedBindings m_compute_bindings;
std::vector<ImageId> m_bound_images;
std::vector<vk::DescriptorBufferInfo> m_descriptor_buffers;
std::vector<vk::DescriptorImageInfo> m_descriptor_images;
@@ -362,7 +362,8 @@ void RenderExecutor::DispatchDirect(uint64_t submit_id, CommandBuffer& buffer,
buffer.EndRendering();
auto& pipeline =
m_context.GetPipelineCache().GetComputePipeline(input_info, compute_program);
auto bindings = PrepareBindings(input_info.stage);
auto& bindings = m_compute_bindings;
PrepareBindings(input_info.stage, bindings);
FindBuffers(bindings);
if (program.info.uses_dma) {
m_context.PrepareBda();
@@ -414,7 +415,8 @@ void RenderExecutor::DispatchIndirect(uint64_t submit_id, CommandBuffer& buffer,
cs_regs, buffer.GetRegisters().GetShaderRegisters(), input_info);
buffer.EndRendering();
auto& pipeline = m_context.GetPipelineCache().GetComputePipeline(input_info, compute_program);
auto bindings = PrepareBindings(input_info.stage);
auto& bindings = m_compute_bindings;
PrepareBindings(input_info.stage, bindings);
FindBuffers(bindings);
const auto& program = *input_info.stage.program;
if (program.info.uses_dma) {
+32 -31
View File
@@ -453,8 +453,10 @@ struct DrawCallInfo {
RenderState RenderExecutor::AcquireRenderTargets(CommandBuffer& buffer, RenderColorInfo* colors,
uint32_t color_count, RenderDepthInfo& depth,
const std::optional<PreparedBindings>& pixel) {
vk::ImageAspectFlags& feedback_aspects,
std::span<PreparedBindings* const> stages) {
EXIT_IF(colors == nullptr || color_count > RENDER_COLOR_ATTACHMENTS_MAX);
feedback_aspects = {};
auto& cache = m_context.GetTextureCache();
RenderState state {};
state.width = std::numeric_limits<uint32_t>::max();
@@ -529,28 +531,29 @@ RenderState RenderExecutor::AcquireRenderTargets(CommandBuffer& buffer, RenderCo
image.info.data.address, depth.desc.view_info.base_layer,
depth.desc.view_info.layer_count);
EXIT_IF(image_view == nullptr || image.backing.samples != depth.desc.info.samples);
const bool feedback = depth.depth_write_enable && pixel &&
std::ranges::any_of(pixel->images, [&](const TextureBinding& binding) {
if (binding.image_id != depth.image_id ||
binding.desc.type != TextureCache::BindingType::Texture) {
return false;
}
const auto native =
std::ranges::find(image.views, binding.image_view, &CachedImageView::view);
EXIT_IF(native == image.views.end());
const auto& sampled = native->info;
const auto& target = depth.desc.view_info;
return (sampled.aspect & vk::ImageAspectFlagBits::eDepth) &&
ImageRangeOverlaps(sampled.base_level, sampled.level_count,
target.base_level, target.level_count) &&
ImageRangeOverlaps(sampled.base_layer, sampled.layer_count,
target.base_layer, target.layer_count);
});
if (feedback && !m_context.GetGraphics().attachment_feedback_loop_enabled) {
const auto draw_writes = depth.AttachmentWriteAspects();
vk::ImageAspectFlags sampled_aspects;
for (const auto* stage: stages) {
for (const auto& binding: stage->images) {
if (binding.image_id != depth.image_id ||
binding.desc.type != TextureCache::BindingType::Texture) continue;
const auto native =
std::ranges::find(image.views, binding.image_view, &CachedImageView::view);
EXIT_IF(native == image.views.end());
sampled_aspects |= native->info.aspect;
feedback_aspects |= DepthFeedbackAspects(draw_writes, depth.desc.view_info,
native->info);
}
}
if (feedback_aspects && !m_context.GetGraphics().attachment_feedback_loop_enabled) {
EXIT("depth attachment feedback loop is not supported by the host\n");
}
const auto layout = feedback ? vk::ImageLayout::eAttachmentFeedbackLoopOptimalEXT
: depth_attachment_layout(depth);
auto layout = depth_attachment_layout(depth);
if (sampled_aspects & ~DepthReadableAspects(layout)) {
layout = m_context.GetGraphics().attachment_feedback_loop_enabled
? vk::ImageLayout::eAttachmentFeedbackLoopOptimalEXT
: vk::ImageLayout::eGeneral;
}
// The attachment store writes even when guest depth/stencil tests do not.
const auto access = vk::AccessFlagBits2::eDepthStencilAttachmentRead |
vk::AccessFlagBits2::eDepthStencilAttachmentWrite;
@@ -1073,15 +1076,16 @@ void RenderExecutor::ExecutePreparedDraw(uint64_t submit_id, CommandBuffer& buff
index_source.guest_element_size);
}
LogDrawPhase(draw.Name(), "PrepareBindings");
GraphicsBindings bindings;
auto& bindings = m_graphics_bindings;
std::array<PreparedBindings*, 4> descriptor_stages {};
uint32_t stage_count = 0;
for (uint32_t i = 0; i < vertex_stages.size(); i++) {
bindings.vertex[i] = PrepareBindings(state.vertex_info[i].stage);
PrepareBindings(state.vertex_info[i].stage, bindings.vertex[i]);
descriptor_stages[stage_count++] = &bindings.vertex[i];
}
if (state.ps_active) {
bindings.pixel.emplace(PrepareBindings(state.ps_input_info.stage));
if (!bindings.pixel) bindings.pixel.emplace();
PrepareBindings(state.ps_input_info.stage, *bindings.pixel);
descriptor_stages[stage_count++] = &*bindings.pixel;
}
const auto stages = std::span {descriptor_stages.data(), stage_count};
@@ -1100,9 +1104,10 @@ void RenderExecutor::ExecutePreparedDraw(uint64_t submit_id, CommandBuffer& buff
std::span {state.color_info, state.color_count}, state.depth_info, vertex_stages, buffer,
state.ps_active ? &state.ps_input_info : nullptr, topology, primitive_restart_enable,
state.programs);
vk::ImageAspectFlags feedback_aspects;
const auto rendering =
AcquireRenderTargets(buffer, state.color_info, state.color_count, state.depth_info,
bindings.pixel);
feedback_aspects, stages);
// Resource preparation above may synchronously finish and restart the scheduler. From this
// point onward, every operation targets the current command buffer and cannot touch guest
@@ -1112,7 +1117,7 @@ void RenderExecutor::ExecutePreparedDraw(uint64_t submit_id, CommandBuffer& buff
if (!mesh_active) {
CommitVertexBuffers(vk_buffer, vertex_bindings);
}
if (bindings.pixel && !draw.IsIndexed()) {
if (state.ps_active && !draw.IsIndexed()) {
SetDrawDebugPhase(buffer, submit_id, draw, 0x300u);
}
CommitBindings(buffer, vk::PipelineBindPoint::eGraphics, pipeline, stages);
@@ -1134,11 +1139,7 @@ void RenderExecutor::ExecutePreparedDraw(uint64_t submit_id, CommandBuffer& buff
SetGraphicsDynamicParams(buffer, vk_buffer, vertex_stages.back(), state.depth_info, rendering);
if (m_context.GetGraphics().attachment_feedback_loop_enabled) {
vk_buffer.setAttachmentFeedbackLoopEnableEXT(
rendering.depth_stencil_attachment.image_layout ==
vk::ImageLayout::eAttachmentFeedbackLoopOptimalEXT
? vk::ImageAspectFlags {vk::ImageAspectFlagBits::eDepth}
: vk::ImageAspectFlags {});
vk_buffer.setAttachmentFeedbackLoopEnableEXT(feedback_aspects);
}
LogDrawPhase(draw.Name(), "BeginRendering");
+6 -1
View File
@@ -565,7 +565,12 @@ Graphics::ImageInfo BufferAttributeGroup::ImageInfo(const VideoOutBuffer& buffer
info.bgra16 = pixel_format.bgra16;
info.mip_layout[0] = {0, total.size, pitch, attribute.height};
if (compression != Graphics::VideoOutCompression::Uncompressed) {
info.metadata.range = {buffer.metadata_address, 0};
Graphics::TileSizeAlign dcc_size {};
if (!Graphics::TileGetDccSize(attribute.width, attribute.height, 1,
pixel_format.bytes_per_element, 1, tile_mode, dcc_size)) {
EXIT("invalid video-out DCC footprint\n");
}
info.metadata.range = {buffer.metadata_address, dcc_size.size};
info.metadata.kind = Graphics::ImageMetadataKind::Dcc;
info.metadata.control = attribute.dcc_control;
info.metadata.compression = compression;
@@ -171,6 +171,13 @@ void Presenter::Frame::Configure(GraphicContext& graphics, vk::Extent2D extent,
EXIT("unsupported prepared frame, extent=%ux%u format=%d\n", extent.width, extent.height,
static_cast<int>(format));
}
auto& dst = image;
const bool compatible = dst.image != nullptr && dst.extent.width == extent.width &&
dst.extent.height == extent.height && dst.format == format;
if (compatible) {
return;
}
const auto features = graphics.GetFormatProperties(format).optimalTilingFeatures;
const auto required =
vk::FormatFeatureFlagBits::eBlitSrc | vk::FormatFeatureFlagBits::eSampledImageFilterLinear |
@@ -180,12 +187,6 @@ void Presenter::Frame::Configure(GraphicContext& graphics, vk::Extent2D extent,
static_cast<int>(format), static_cast<vk::FormatFeatureFlags::MaskType>(features));
}
auto& dst = image;
const bool compatible = dst.image != nullptr && dst.extent.width == extent.width &&
dst.extent.height == extent.height && dst.format == format;
if (compatible) {
return;
}
if (dst.image != nullptr) {
graphics.DeleteImage(dst);
}
@@ -784,11 +784,11 @@ void EmitImage(ValueEmitContext& ctx, const IR::Inst& inst) {
? &state.program.descriptor_sources[image.source]
: nullptr;
if (handle == nullptr || source == nullptr || !source->indirect_image.has_value() ||
source->indirect_image->key_arg >= handle->NumArgs()) {
handle->NumArgs() == 0u) {
ctx.Fail(inst, "has invalid indirect image key provenance");
return;
}
const auto key = ctx.Def(handle->Arg(source->indirect_image->key_arg));
const auto key = ctx.Def(handle->Arg(0));
if (state.flattened_srt_variable == 0 || image.indirect_search_iterations == 0u ||
image.indirect_resources.size() < 2u) {
ctx.Fail(inst, "has no indirect image runtime mapping");
@@ -710,10 +710,14 @@ void StoreFormattedInBounds(ValueEmitContext& ctx, const IR::MemoryInfo& mem,
uint32_t data) {
if (component >= plan.info.component_count) return;
const auto bits = plan.info.component_bits[component];
if (plan.info.type == Format::ComponentType::Snorm && bits == 16u) {
if (bits == 16u && (plan.info.type == Format::ComponentType::Snorm ||
plan.info.type == Format::ComponentType::Float)) {
const auto value = EmitBitCastF32U32(ctx.state, data);
data = EmitPackSnorm2x16(
ctx.state, EmitCompositeConstructF32x2(ctx.state, value, ConstantF32Value(ctx.state, 0.0f)));
const auto pair = EmitCompositeConstructF32x2(ctx.state, value,
ConstantF32Value(ctx.state, 0.0f));
data = plan.info.type == Format::ComponentType::Float
? EmitPackHalf2x16(ctx.state, pair)
: EmitPackSnorm2x16(ctx.state, pair);
}
if (bits == 8u || bits == 16u) {
StoreSubwordInBounds(ctx, mem, plan.resource, plan.addresses[component],
@@ -86,6 +86,7 @@ constexpr MemoryOpcodeInfo FLAT_OPCODE_LIST[] = {
constexpr MemoryOpcodeInfo DS_OPCODE_LIST[] = {
{0x00u, Opcode::DS_ADD_U32, 1, 32}, {0x01u, Opcode::DS_SUB_U32, 1, 32},
{0x03u, Opcode::DS_INC_U32, 1, 32}, {0x04u, Opcode::DS_DEC_U32, 1, 32},
{0x05u, Opcode::DS_MIN_I32, 1, 32}, {0x06u, Opcode::DS_MAX_I32, 1, 32},
{0x07u, Opcode::DS_MIN_U32, 1, 32}, {0x08u, Opcode::DS_MAX_U32, 1, 32},
{0x09u, Opcode::DS_AND_B32, 1, 32}, {0x0au, Opcode::DS_OR_B32, 1, 32},
@@ -107,6 +108,7 @@ constexpr MemoryOpcodeInfo DS_OPCODE_LIST[] = {
{0x4du, Opcode::DS_WRITE_B64, 2, 32}, {0x4eu, Opcode::DS_WRITE2_B64, 4, 32},
{0x4fu, Opcode::DS_WRITE2ST64_B64, 4, 32}, {0x76u, Opcode::DS_READ_B64, 2, 32},
{0x77u, Opcode::DS_READ2_B64, 4, 32}, {0x78u, Opcode::DS_READ2ST64_B64, 4, 32},
{0xa0u, Opcode::DS_WRITE_B8_D16_HI, 1, 8},
{0xa1u, Opcode::DS_WRITE_B16_D16_HI, 1, 16},
{0xa6u, Opcode::DS_READ_U16_D16, 1, 16},
{0xa7u, Opcode::DS_READ_U16_D16_HI, 1, 16},
@@ -147,6 +149,7 @@ bool IsDsWriteOpcode(Opcode opcode) {
switch (opcode) {
case Opcode::DS_WRITE_B8:
case Opcode::DS_WRITE_B16:
case Opcode::DS_WRITE_B8_D16_HI:
case Opcode::DS_WRITE_B16_D16_HI:
case Opcode::DS_WRITE2_B32:
case Opcode::DS_WRITE2ST64_B32:
@@ -166,7 +169,9 @@ bool IsDsAtomicOpcode(Opcode opcode) {
case Opcode::DS_ADD_RTN_U32:
case Opcode::DS_SUB_U32:
case Opcode::DS_SUB_RTN_U32:
case Opcode::DS_INC_U32:
case Opcode::DS_INC_RTN_U32:
case Opcode::DS_DEC_U32:
case Opcode::DS_DEC_RTN_U32:
case Opcode::DS_MIN_I32:
case Opcode::DS_MIN_RTN_I32:
@@ -429,7 +434,9 @@ void DecodeDs(uint32_t pc, std::span<const uint32_t> code, uint32_t word_index,
}
DecodeVectorGpr(addr, inst.src0);
DecodeVectorGpr(data0, inst.src1);
if (inst.opcode == Opcode::DS_WRITE_B16_D16_HI) {
if (inst.opcode == Opcode::DS_WRITE_B8_D16_HI) {
inst.src1.sdwa_sel = 2u;
} else if (inst.opcode == Opcode::DS_WRITE_B16_D16_HI) {
inst.src1.sdwa_sel = 5u;
}
DecodeVectorGpr(data1, inst.src2);
@@ -659,7 +659,9 @@ std::string InstructionToString(const Instruction& inst) {
case Opcode::DS_ADD_RTN_U32:
case Opcode::DS_SUB_U32:
case Opcode::DS_SUB_RTN_U32:
case Opcode::DS_INC_U32:
case Opcode::DS_INC_RTN_U32:
case Opcode::DS_DEC_U32:
case Opcode::DS_DEC_RTN_U32:
case Opcode::DS_MIN_I32:
case Opcode::DS_MIN_RTN_I32:
@@ -694,6 +696,7 @@ std::string InstructionToString(const Instruction& inst) {
case Opcode::DS_READ_B128:
case Opcode::DS_WRITE_B8:
case Opcode::DS_WRITE_B16:
case Opcode::DS_WRITE_B8_D16_HI:
case Opcode::DS_WRITE_B16_D16_HI:
case Opcode::DS_WRITE2_B32:
case Opcode::DS_WRITE2ST64_B32:
@@ -502,7 +502,9 @@ enum class Opcode {
DS_ADD_RTN_U32,
DS_SUB_U32,
DS_SUB_RTN_U32,
DS_INC_U32,
DS_INC_RTN_U32,
DS_DEC_U32,
DS_DEC_RTN_U32,
DS_MIN_I32,
DS_MIN_RTN_I32,
@@ -541,6 +543,7 @@ enum class Opcode {
DS_READ_B128,
DS_WRITE_B8,
DS_WRITE_B16,
DS_WRITE_B8_D16_HI,
DS_WRITE_B16_D16_HI,
DS_WRITE2_B32,
DS_WRITE2ST64_B32,
@@ -302,23 +302,95 @@ void Translator::S_MOV_B64(const Decoder::Instruction& inst) {
}
void Translator::S_WQM(const Decoder::Instruction& inst, bool wide) {
const auto source = wide ? ReadU64(inst.src0)
: ir.ConstructU64(ReadU32(inst.src0), IR::U32(IR::Value(0u)));
const bool source_is_mask = inst.src0.kind == Decoder::OperandKind::Sgpr ||
inst.src0.kind == Decoder::OperandKind::ExecLo ||
inst.src0.kind == Decoder::OperandKind::VccLo ||
(!wide && IsExecOrVcc(inst.src0));
const auto source_valid = ReadMaskValid(inst.src0);
auto source_live = IR::U1(IR::Value(false));
if (source_is_mask) {
if (inst.src0.kind == Decoder::OperandKind::VccLo ||
inst.src0.kind == Decoder::OperandKind::VccHi) {
source_live = ir.GetVcc();
} else {
source_live = ReadMask(inst.src0);
}
}
const auto retained_live = ir.LogicalAnd(source_valid, source_live);
IR::U64 source;
if (wide) {
source = ReadU64(inst.src0);
} else {
source = ir.ConstructU64(ReadU32(inst.src0), IR::U32(IR::Value(0u)));
}
const auto result = IR::U64(ir.Emit(IR::ValueOpcode::WqmU64, {source}));
if (!wide) {
const auto low = ir.CompositeExtract(result, 0);
const bool destination_is_mask = IsExecOrVcc(inst.dst);
const bool destination_is_exec = inst.dst.kind == Decoder::OperandKind::ExecLo ||
inst.dst.kind == Decoder::OperandKind::ExecHi;
auto old_live = IR::U1(IR::Value(false));
if (destination_is_mask) {
if (destination_is_exec) {
old_live = ir.GetExec();
} else {
old_live = ir.GetVcc();
}
}
// A 32-bit write preserves the other EXEC/VCC half and invalidates any
// overlapping SGPR-pair mask provenance through the ordinary scalar path.
WriteRawU32(inst.dst, low);
if (destination_is_mask) {
const auto is_high_half = [](const Decoder::Operand& operand) {
return operand.kind == Decoder::OperandKind::ExecHi ||
operand.kind == Decoder::OperandKind::VccHi;
};
const bool destination_is_high = is_high_half(inst.dst);
auto affected_lanes = IR::U1(IR::Value(!destination_is_high));
if (program.wave_size == 64u) {
const auto lane = IR::U32(ir.Emit(IR::ValueOpcode::LaneId));
affected_lanes = ir.ULessThan(lane, IR::U32(IR::Value(32u)));
if (destination_is_high) {
affected_lanes = ir.LogicalNot(affected_lanes);
}
}
IR::U1 live;
if (destination_is_exec) {
live = ir.GetExec();
} else {
live = ir.GetVcc();
}
// A cross-half copy refers to different lanes, so only scalar bits apply.
if (is_high_half(inst.src0) == destination_is_high) {
live = ir.LogicalOr(retained_live, live);
}
live = IR::U1(ir.Emit(IR::ValueOpcode::SelectU1, {affected_lanes, live, old_live}));
if (destination_is_exec) {
ir.SetExec(live);
} else {
ir.SetVcc(live);
}
}
ir.SetScc(ir.INotEqual(low, IR::U32(IR::Value(0u))));
return;
}
const auto mask_valid = ReadMaskValid(inst.src0);
// WQM adds live lanes but never removes a source lane. Keep that fact separate
// from the scalar mask: reconstructing an entry-true predicate from a ballot
// otherwise introduces a spurious dependency into every vector write.
const auto live = ir.LogicalOr(retained_live, ThreadBit(ExtractU64(result)));
WriteOperand(DestinationOperand(inst), result);
if (inst.dst.kind == Decoder::OperandKind::Sgpr) {
if (inst.dst.kind == Decoder::OperandKind::ExecLo) {
ir.SetExec(live);
} else if (inst.dst.kind == Decoder::OperandKind::VccLo) {
ir.SetVcc(live);
} else if (inst.dst.kind == Decoder::OperandKind::Sgpr) {
const auto dst = static_cast<IR::ScalarReg>(inst.dst.reg);
ir.SetThreadBitScalarReg(dst, ThreadBit(ExtractU64(result)));
ir.SetScalarMaskTag(dst, mask_valid);
ir.SetThreadBitScalarReg(dst, live);
ir.SetScalarMaskTag(dst, source_valid);
}
ir.SetScc(IR::U1(ir.Emit(IR::ValueOpcode::INotEqual64, {result, IR::Value(uint64_t {0})})));
}
@@ -235,6 +235,7 @@ Decoder::Operand MemorySourceAt(const Decoder::Instruction& decoded, uint32_t in
return index == 0u ? decoded.src1 : index == 1u ? decoded.src0 : decoded.src2;
case Decoder::Opcode::DS_WRITE_B8:
case Decoder::Opcode::DS_WRITE_B16:
case Decoder::Opcode::DS_WRITE_B8_D16_HI:
case Decoder::Opcode::DS_WRITE_B16_D16_HI:
case Decoder::Opcode::DS_WRITE_B32:
case Decoder::Opcode::DS_WRITE_B64:
@@ -962,8 +963,12 @@ bool Translator::EmitMemory(const Decoder::Instruction& inst) {
return DS_ATOMIC(inst, IR::ValueOpcode::SharedAtomicISub32, false);
case Decoder::Opcode::DS_SUB_RTN_U32:
return DS_ATOMIC(inst, IR::ValueOpcode::SharedAtomicISub32, true);
case Decoder::Opcode::DS_INC_U32:
return DS_ATOMIC(inst, IR::ValueOpcode::SharedAtomicInc32, false);
case Decoder::Opcode::DS_INC_RTN_U32:
return DS_ATOMIC(inst, IR::ValueOpcode::SharedAtomicInc32, true);
case Decoder::Opcode::DS_DEC_U32:
return DS_ATOMIC(inst, IR::ValueOpcode::SharedAtomicDec32, false);
case Decoder::Opcode::DS_DEC_RTN_U32:
return DS_ATOMIC(inst, IR::ValueOpcode::SharedAtomicDec32, true);
case Decoder::Opcode::DS_MIN_I32:
@@ -1076,6 +1081,7 @@ bool Translator::EmitMemory(const Decoder::Instruction& inst) {
case Decoder::Opcode::DS_WRITE2ST64_B64: return DS_WRITE2(inst);
case Decoder::Opcode::DS_WRITE_B8:
case Decoder::Opcode::DS_WRITE_B16:
case Decoder::Opcode::DS_WRITE_B8_D16_HI:
case Decoder::Opcode::DS_WRITE_B16_D16_HI:
case Decoder::Opcode::DS_WRITE_B32:
case Decoder::Opcode::DS_WRITE_B64:
@@ -1063,8 +1063,8 @@ IR::Program TranslateProgram(const Decoder::Program& decoded, const CFG::Graph&
}
} else if (options.stage == ShaderType::Mesh) {
const auto& mesh = options.input_info.vertex->mesh;
EXIT_NOT_IMPLEMENTED(options.wave_size != 64u || mesh.primitives_per_group == 0u ||
mesh.vertices_per_group > 64u || total_threads > 15u * 64u);
EXIT_NOT_IMPLEMENTED(mesh.primitives_per_group == 0u || mesh.vertices_per_group > 64u ||
total_threads > 15u * options.wave_size);
const auto u32 = [](uint32_t value) { return IR::U32(IR::Value(value)); };
const auto draw = [&](uint32_t index) {
return IR::U32(
@@ -1089,12 +1089,15 @@ IR::Program TranslateProgram(const Decoder::Program& decoded, const CFG::Graph&
entry_ir.IAdd(IR::U32(entry_ir.Emit(IR::ValueOpcode::UDiv32,
{subtract_saturate(vertices, size), step})),
u32(1)));
const auto wave = entry_ir.ShiftRightLogical(local, u32(6));
const auto wave_base = entry_ir.BitwiseAnd(local, u32(~63u));
const auto vertex_count = minimum(subtract_saturate(vertices, wave_base), u32(64));
const auto primitive_count = minimum(subtract_saturate(primitives, wave_base), u32(64));
const auto wave = entry_ir.ShiftRightLogical(local, u32(options.wave_size == 32u ? 5u : 6u));
const auto wave_base = entry_ir.BitwiseAnd(local, u32(~(options.wave_size - 1u)));
const auto vertex_count =
minimum(subtract_saturate(vertices, wave_base), u32(options.wave_size));
const auto primitive_count =
minimum(subtract_saturate(primitives, wave_base), u32(options.wave_size));
const auto wave_info = entry_ir.BitwiseOr(entry_ir.ShiftLeftLogical(wave, u32(24)),
u32(((total_threads + 63u) / 64u) << 28u));
u32(((total_threads + options.wave_size - 1u) /
options.wave_size) << 28u));
entry_ir.SetScalarReg(
static_cast<IR::ScalarReg>(3),
entry_ir.BitwiseOr(wave_info, entry_ir.BitwiseOr(entry_ir.ShiftLeftLogical(
+4 -2
View File
@@ -463,9 +463,9 @@ struct DescriptorSource {
uint32_t table_source = 0;
uint32_t selector_stride = 0;
uint32_t selector_offset = 0;
uint32_t key_arg = 0;
uint32_t table_offset = 0;
uint32_t key_count = 0;
Value key_count;
Value selector_mask;
bool operator==(const IndirectImage& other) const = default;
};
@@ -541,6 +541,7 @@ struct ResourcePlan {
std::vector<SrtRead> srt_reads;
std::vector<uint8_t> clean_flat_slots;
bool requires_specialization_memory = false;
bool has_address_writes = false;
bool srt_plan_complete = false;
bool resource_tracking_complete = false;
ShaderInfo info;
@@ -553,6 +554,7 @@ struct ResourcePlan {
mutable std::vector<uint8_t> visited_blocks;
mutable std::vector<uint32_t> pending_blocks;
mutable std::vector<uint32_t> material_keys;
mutable std::vector<std::pair<uint64_t, uint64_t>> specialization_reads;
};
struct Program: ResourcePlan {
@@ -200,6 +200,13 @@ void FoldInstruction(Block& block, Block::iterator instruction,
switch (inst.GetOpcode()) {
case ValueOpcode::Phi: FoldPhi(inst); return;
case ValueOpcode::SelectU1:
if (!FoldSelect(inst) && IsImmediate(Arg(inst, 2), Type::U1) &&
!Arg(inst, 2).U1()) {
auto result = block.PrependNewInst(instruction, ValueOpcode::LogicalAnd,
{Arg(inst, 0), Arg(inst, 1)});
Replace(inst, Value(&*result));
}
return;
case ValueOpcode::SelectU32:
case ValueOpcode::SelectF32: FoldSelect(inst); return;
case ValueOpcode::BitFieldInsert: {
@@ -431,6 +438,10 @@ void FoldInstruction(Block& block, Block::iterator instruction,
return;
case ValueOpcode::ShiftRightLogical32:
if (!FoldU32(inst, [](uint32_t a, uint32_t b) { return a >> (b & 31u); })) {
if (IsImmediate(Arg(inst, 0), Type::U32) && Arg(inst, 0).U32() == 0u) {
Replace(inst, Value(0u));
return;
}
const auto shift = Arg(inst, 1);
if (IsImmediate(shift, Type::U32) && (shift.U32() & 31u) == 0u) {
Replace(inst, Arg(inst, 0));
@@ -573,6 +584,23 @@ void FoldInstruction(Block& block, Block::iterator instruction,
if (!FoldLogical(inst, [](bool a, bool b) { return a && b; })) {
const auto lhs = Arg(inst, 0);
const auto rhs = Arg(inst, 1);
const auto simplify = [&](Value assumption, Value expression) {
const auto* disjunction = expression.TryInstruction();
if (disjunction == nullptr || disjunction->GetOpcode() != ValueOpcode::LogicalOr) {
return false;
}
for (uint32_t i = 0; i < 2u; ++i) {
const auto* inverse = disjunction->Arg(i).Resolve().TryInstruction();
if (inverse != nullptr && inverse->GetOpcode() == ValueOpcode::LogicalNot &&
inverse->Arg(0).Resolve() == assumption) {
inst.SetArg(assumption == lhs ? 1u : 0u,
disjunction->Arg(i ^ 1u));
return true;
}
}
return false;
};
if (simplify(lhs, rhs) || simplify(rhs, lhs)) return;
if (IsImmediate(lhs, Type::U1)) {
Replace(inst, lhs.U1() ? rhs : lhs);
} else if (IsImmediate(rhs, Type::U1)) {
@@ -138,6 +138,50 @@ bool DecodeBufferDescriptor(const DescriptorValue& descriptor, ShaderBufferResou
return true;
}
struct ReadCapture {
SrtRuntime source;
std::vector<std::pair<uint64_t, uint64_t>>& ranges;
};
bool CaptureStrictRead(void* userdata, uint64_t address, std::span<uint32_t> values) {
auto& capture = *static_cast<ReadCapture*>(userdata);
if (!capture.source.read_specialization_memory(capture.source.userdata, address, values)) {
return false;
}
capture.ranges.emplace_back(address, values.size_bytes());
return true;
}
bool CaptureOrdinaryRead(void* userdata, uint64_t address, std::span<uint32_t> values) {
auto& capture = *static_cast<ReadCapture*>(userdata);
if (!capture.source.read_memory(capture.source.userdata, address, values)) {
return false;
}
capture.ranges.emplace_back(address, values.size_bytes());
return true;
}
bool WrittenBuffersDisjoint(const ResourcePlan& program, const ResourceSnapshot& snapshot,
std::span<const std::pair<uint64_t, uint64_t>> reads) {
for (uint32_t i = 0; i < program.info.buffers.size(); ++i) {
if (!program.info.buffers[i].written) continue;
ShaderBufferResource buffer;
if (!DecodeBufferDescriptor(snapshot.buffers[i], buffer)) return false;
const auto base = buffer.Base48();
const auto size = buffer.GetSize();
if (size == 0u) continue;
if (size - 1u > AddressMask - base) return false;
for (const auto [address, bytes]: reads) {
if (bytes == 0u) continue;
if (address > AddressMask || bytes - 1u > AddressMask - address ||
(address <= base ? base - address < bytes : address - base < size)) {
return false;
}
}
}
return true;
}
const DescriptorSource* Source(const ResourcePlan& program, uint32_t source) {
if (source >= program.descriptor_sources.size()) {
return nullptr;
@@ -146,12 +190,15 @@ const DescriptorSource* Source(const ResourcePlan& program, uint32_t source) {
}
void MarkCleanFlatSlots(const ResourcePlan& program, const DescriptorSource* source,
std::span<uint8_t> slots) {
if (source == nullptr) {
std::span<uint8_t> slots, Value extra = {}) {
if (source == nullptr && extra.IsEmpty()) {
return;
}
std::vector<Value> pending(source->dwords.begin(),
source->dwords.begin() + source->dword_count);
std::vector<Value> pending;
if (source != nullptr) {
pending.assign(source->dwords.begin(), source->dwords.begin() + source->dword_count);
}
if (!extra.IsEmpty()) pending.push_back(extra);
std::vector<const Inst*> visited;
while (!pending.empty()) {
auto value = pending.back().Resolve();
@@ -198,7 +245,8 @@ bool MaterializeIndirectImage(const ResourcePlan& program,
const DescriptorSource::IndirectImage& indirect,
const DescriptorValue& material_value,
const DescriptorValue& table_value, uint32_t image_index,
const SrtRuntime& runtime, ResourceSnapshot& snapshot,
const SrtRuntime& runtime, SrtWalker& clean,
ResourceSnapshot& snapshot,
ResourceSpecialization& specialization) {
uint64_t table_base = 0;
uint64_t table_size = UINT64_MAX; // Scalar addresses have no buffer descriptor bounds.
@@ -214,12 +262,41 @@ bool MaterializeIndirectImage(const ResourcePlan& program,
auto& keys = program.material_keys;
keys.clear();
if (indirect.material_source == UINT32_MAX) {
if (table_value.dword_count != 2u || indirect.key_count == 0u ||
indirect.key_count > MaxIndirectImageProbes) {
uint32_t key_count = 0;
const bool evaluated = clean.Evaluate(indirect.key_count, key_count);
if (std::bit_cast<int32_t>(key_count) <= 0) key_count = 0;
if (table_value.dword_count != 2u || !evaluated ||
key_count > MaxIndirectImageProbes ||
uint64_t {indirect.table_offset} + uint64_t {key_count} * 32u > UINT32_MAX + 1ull) {
return false;
}
keys.resize(indirect.key_count);
keys.resize(key_count);
std::iota(keys.begin(), keys.end(), 0u);
} else if (!indirect.selector_mask.IsEmpty()) {
uint32_t mask = 0;
uint32_t count = 0;
if (material_value.dword_count != 2u || table_value.dword_count != 2u ||
!clean.Evaluate(indirect.selector_mask, mask) ||
!clean.Evaluate(indirect.key_count, count) || count == 0u || count > 32u) {
return false;
}
if (count < 32u) mask &= (1u << count) - 1u;
const auto material_base =
(static_cast<uint64_t>(material_value.dwords[1]) << 32u) | material_value.dwords[0];
keys.reserve(std::popcount(mask));
while (mask != 0u) {
const auto index = std::countr_zero(mask);
const auto offset = static_cast<uint64_t>(indirect.selector_offset) +
static_cast<uint64_t>(index) * indirect.selector_stride;
if (offset > UINT32_MAX) return false;
uint32_t key = 0;
if (!ReadScalarTable(material_base, UINT64_MAX, static_cast<uint32_t>(offset),
runtime, {&key, 1})) return false;
keys.push_back(key);
mask &= mask - 1u;
}
std::ranges::sort(keys);
keys.erase(std::unique(keys.begin(), keys.end()), keys.end());
} else {
ShaderBufferResource material;
if (!DecodeBufferDescriptor(material_value, material) || table_value.dword_count != 4u ||
@@ -779,6 +856,7 @@ ResourcePlan ExtractResourcePlan(const Program& program) {
plan.memory_info = program.memory_info;
plan.srt_plan_complete = program.srt_plan_complete;
plan.resource_tracking_complete = program.resource_tracking_complete;
plan.has_address_writes = program.has_address_writes;
std::unordered_map<const Inst*, Inst*> cloned;
std::function<Value(Value)> Clone = [&](Value value) -> Value {
@@ -816,6 +894,10 @@ ResourcePlan ExtractResourcePlan(const Program& program) {
auto& target = plan.descriptor_sources.emplace_back();
target.dword_count = source.dword_count;
target.indirect_image = source.indirect_image;
if (target.indirect_image.has_value()) {
target.indirect_image->key_count = Clone(target.indirect_image->key_count);
target.indirect_image->selector_mask = Clone(target.indirect_image->selector_mask);
}
for (uint32_t dword = 0; dword < source.dword_count; dword++) {
target.dwords[dword] = Clone(source.dwords[dword]);
}
@@ -840,7 +922,7 @@ ResourcePlan ExtractResourcePlan(const Program& program) {
}
plan.requires_specialization_memory = true;
MarkCleanFlatSlots(plan, Source(plan, source->indirect_image->material_source),
plan.clean_flat_slots);
plan.clean_flat_slots, source->indirect_image->selector_mask);
MarkCleanFlatSlots(plan, Source(plan, source->indirect_image->table_source),
plan.clean_flat_slots);
}
@@ -853,8 +935,29 @@ bool MaterializeResources(const ResourcePlan& program, const SrtRuntime& runtime
(program.requires_specialization_memory && runtime.read_specialization_memory == nullptr)) {
return false;
}
SrtWalker clean(program, CleanRuntime(runtime));
SrtWalker walker(program, runtime, program.clean_flat_slots, &clean);
const bool masked_image = std::ranges::any_of(program.info.images, [&](const auto& image) {
const auto* source = Source(program, image.source);
return source != nullptr && source->indirect_image.has_value() &&
!source->indirect_image->selector_mask.IsEmpty();
});
if (masked_image &&
(program.has_address_writes ||
std::ranges::any_of(program.info.images, &ImageResource::written))) {
return false;
}
const bool capture_reads = masked_image &&
std::ranges::any_of(program.info.buffers, &BufferResource::written);
auto& reads = program.specialization_reads;
ReadCapture capture {runtime, reads};
SrtRuntime observed = runtime;
if (capture_reads) {
reads.clear();
observed.userdata = &capture;
observed.read_specialization_memory = CaptureStrictRead;
if (observed.read_memory != nullptr) observed.read_memory = CaptureOrdinaryRead;
}
SrtWalker clean(program, CleanRuntime(observed));
SrtWalker walker(program, observed, program.clean_flat_slots, &clean);
const auto active = clean.FindActiveSources();
if (!walker.RefreshFlatBuffer(snapshot.flattened_srt)) {
return false;
@@ -888,6 +991,15 @@ bool MaterializeResources(const ResourcePlan& program, const SrtRuntime& runtime
return false;
}
}
if (capture_reads) {
for (uint32_t i = 0; i < program.info.buffers.size(); ++i) {
const auto& buffer = program.info.buffers[i];
if (!buffer.written || (!active.empty() && !active[buffer.source])) continue;
DescriptorValue strict;
if (!clean.EvaluateDescriptor(buffer.source, strict) ||
strict != snapshot.buffers[i]) return false;
}
}
snapshot.images.resize(program.info.images.size());
specialization.images.clear();
specialization.images.reserve(program.info.images.size());
@@ -921,7 +1033,7 @@ bool MaterializeResources(const ResourcePlan& program, const SrtRuntime& runtime
if ((indirect.material_source != UINT32_MAX &&
!clean.EvaluateDescriptor(indirect.material_source, material)) ||
!clean.EvaluateDescriptor(indirect.table_source, table) ||
!MaterializeIndirectImage(program, indirect, material, table, i, runtime, snapshot,
!MaterializeIndirectImage(program, indirect, material, table, i, observed, clean, snapshot,
specialization)) {
return false;
}
@@ -940,6 +1052,7 @@ bool MaterializeResources(const ResourcePlan& program, const SrtRuntime& runtime
return false;
}
}
if (capture_reads && !WrittenBuffersDisjoint(program, snapshot, reads)) return false;
snapshot.user_data.assign(runtime.user_data.begin(), runtime.user_data.end());
return BuildResourceSpecialization(program, snapshot, specialization);
}
@@ -313,9 +313,20 @@ private:
for (uint32_t candidate = 0; candidate < m_sources.size(); candidate++) {
const auto& current = m_sources[candidate];
if (current.dword_count != descriptor.dword_count ||
current.indirect_image != descriptor.indirect_image) {
current.indirect_image.has_value() != descriptor.indirect_image.has_value()) {
continue;
}
if (current.indirect_image.has_value()) {
const auto& a = *current.indirect_image;
const auto& b = *descriptor.indirect_image;
if (a.material_source != b.material_source || a.table_source != b.table_source ||
a.selector_stride != b.selector_stride || a.selector_offset != b.selector_offset ||
a.table_offset != b.table_offset ||
!EquivalentValue(m_program, a.key_count, b.key_count) ||
a.selector_mask.IsEmpty() != b.selector_mask.IsEmpty() ||
(!a.selector_mask.IsEmpty() &&
!EquivalentValue(m_program, a.selector_mask, b.selector_mask))) continue;
}
bool same = true;
for (uint32_t i = 0; i < descriptor.dword_count; i++) {
same = same && EquivalentValue(m_program, current.dwords[i], descriptor.dwords[i]);
@@ -504,6 +515,355 @@ private:
}
}
const Block* FindBlock(uint32_t id) const {
const auto info = std::ranges::find(m_program.block_info, id, &BlockInfo::id);
return info == m_program.block_info.end()
? nullptr
: m_program.blocks[info - m_program.block_info.begin()];
}
bool CanReach(const Block* start, const Block* target, const Block* avoid) const {
std::vector<const Block*> pending {start};
std::vector<const Block*> visited;
while (!pending.empty()) {
const auto* block = pending.back();
pending.pop_back();
if (block == nullptr || block == avoid ||
std::ranges::find(visited, block) != visited.end()) continue;
if (block == target) return true;
visited.push_back(block);
for (const auto* next: block->ImmSuccessors()) pending.push_back(next);
}
return false;
}
bool ConditionalEdge(const Block* from, const Block* to, Value& condition,
bool& positive) const {
const auto position = std::ranges::find(m_program.blocks, from);
const auto target = std::ranges::find(m_program.blocks, to);
if (position == m_program.blocks.end() || target == m_program.blocks.end()) return false;
const auto& info = m_program.block_info[position - m_program.blocks.begin()];
const auto& term = info.terminator;
const auto id = m_program.block_info[target - m_program.blocks.begin()].id;
if (term.kind != CFG::TerminatorKind::ConditionalBranch ||
(term.true_block == id) == (term.false_block == id)) return false;
condition = info.condition;
positive = term.true_block == id;
while (const auto* inst = condition.Resolve().TryInstruction()) {
if (inst->GetOpcode() != ValueOpcode::LogicalNot) break;
condition = inst->Arg(0);
positive = !positive;
}
condition = condition.Resolve();
return true;
}
Value PositiveUseGuard(const Block* use) const {
if (use == nullptr || use->ImmPredecessors().size() != 1u) return {};
Value condition;
bool positive = false;
return ConditionalEdge(use->ImmPredecessors()[0], use, condition, positive) && positive
? condition : Value {};
}
Value SimplifyGuard(Value guard) const {
const auto invariant = ResolveInvariantPhi(m_program, guard);
return (invariant.IsEmpty() ? guard : invariant).Resolve();
}
bool Implies(Value guard, Value required) const {
guard = SimplifyGuard(guard);
required = required.Resolve();
if (EquivalentValue(m_program, guard, required)) return true;
const auto* inst = guard.TryInstruction();
return inst != nullptr && inst->GetOpcode() == ValueOpcode::LogicalAnd &&
(Implies(inst->Arg(0), required) || Implies(inst->Arg(1), required));
}
Value EqualLocalKey(Value guard, Value key) const {
guard = SimplifyGuard(guard);
const auto* inst = guard.TryInstruction();
if (inst == nullptr) return {};
if (inst->GetOpcode() == ValueOpcode::LogicalAnd) {
const auto left = EqualLocalKey(inst->Arg(0), key);
return left.IsEmpty() ? EqualLocalKey(inst->Arg(1), key) : left;
}
if (inst->GetOpcode() != ValueOpcode::IEqual32 || inst->NumArgs() != 2u) return {};
if (EquivalentValue(m_program, inst->Arg(0), key)) return inst->Arg(1).Resolve();
if (EquivalentValue(m_program, inst->Arg(1), key)) return inst->Arg(0).Resolve();
return {};
}
struct AffineOffset {
Value index;
uint64_t stride = 0;
uint64_t offset = 0;
};
bool MatchAffineOffset(Value value, Value guard, AffineOffset& out,
uint32_t depth = 0) const {
if (depth > 16u) return false;
value = value.Resolve();
uint32_t immediate = 0;
if (ImmediateU32(value, immediate)) {
out.offset = immediate;
return true;
}
const auto* inst = value.TryInstruction();
if (inst == nullptr) return false;
if (inst->GetOpcode() == ValueOpcode::SelectU32 && inst->NumArgs() == 3u &&
Implies(guard, inst->Arg(0))) {
return MatchAffineOffset(inst->Arg(1), guard, out, depth + 1u);
}
if (inst->GetOpcode() == ValueOpcode::IAdd32 && inst->NumArgs() == 2u) {
AffineOffset left, right;
if (!MatchAffineOffset(inst->Arg(0), guard, left, depth + 1u) ||
!MatchAffineOffset(inst->Arg(1), guard, right, depth + 1u) ||
(!left.index.IsEmpty() && !right.index.IsEmpty() &&
!EquivalentValue(m_program, left.index, right.index))) return false;
out.index = left.index.IsEmpty() ? right.index : left.index;
out.stride = left.stride + right.stride;
out.offset = left.offset + right.offset;
return out.stride <= UINT32_MAX && out.offset <= UINT32_MAX;
}
if (inst->GetOpcode() == ValueOpcode::ShiftLeftLogical32 && inst->NumArgs() == 2u &&
ImmediateU32(inst->Arg(1), immediate) && immediate < 32u) {
if (!MatchAffineOffset(inst->Arg(0), guard, out, depth + 1u)) return false;
out.stride <<= immediate;
out.offset <<= immediate;
return out.stride <= UINT32_MAX && out.offset <= UINT32_MAX;
}
out.index = value;
out.stride = 1u;
return value.GetType() == Type::U32;
}
bool ImpliesNonzero(Value guard, Value value) const {
guard = SimplifyGuard(guard);
const auto* inst = guard.TryInstruction();
if (inst == nullptr) return false;
if (inst->GetOpcode() == ValueOpcode::LogicalAnd) {
return ImpliesNonzero(inst->Arg(0), value) ||
ImpliesNonzero(inst->Arg(1), value);
}
if (inst->GetOpcode() != ValueOpcode::INotEqual32 || inst->NumArgs() != 2u)
return false;
uint32_t zero = 1u;
return (ImmediateU32(inst->Arg(0), zero) && zero == 0u &&
EquivalentValue(m_program, inst->Arg(1), value)) ||
(ImmediateU32(inst->Arg(1), zero) && zero == 0u &&
EquivalentValue(m_program, inst->Arg(0), value));
}
bool ImpliesIndexBelow32(Value guard, Value index) const {
guard = SimplifyGuard(guard);
const auto* inst = guard.TryInstruction();
if (inst == nullptr) return false;
if (inst->GetOpcode() == ValueOpcode::LogicalAnd) {
return ImpliesIndexBelow32(inst->Arg(0), index) ||
ImpliesIndexBelow32(inst->Arg(1), index);
}
if (inst->NumArgs() != 2u) return false;
uint32_t limit = 0;
return (inst->GetOpcode() == ValueOpcode::SLessThan32 &&
EquivalentValue(m_program, inst->Arg(0), index) &&
ImmediateU32(inst->Arg(1), limit) && limit == 32u) ||
(inst->GetOpcode() == ValueOpcode::SGreaterThan32 &&
ImmediateU32(inst->Arg(0), limit) && limit == 32u &&
EquivalentValue(m_program, inst->Arg(1), index));
}
bool ClearsFirstSetBit(Value value, Value mask) const {
const auto* update = value.Resolve().TryInstruction();
if (update == nullptr || update->GetOpcode() != ValueOpcode::BitwiseXor32 ||
update->NumArgs() != 2u) return false;
Value bit;
if (EquivalentValue(m_program, update->Arg(0), mask)) bit = update->Arg(1);
else if (EquivalentValue(m_program, update->Arg(1), mask)) bit = update->Arg(0);
else return false;
const auto* shift = bit.Resolve().TryInstruction();
uint32_t one = 0;
if (shift == nullptr || shift->GetOpcode() != ValueOpcode::ShiftLeftLogical32 ||
shift->NumArgs() != 2u || !ImmediateU32(shift->Arg(0), one) || one != 1u)
return false;
Value position = shift->Arg(1).Resolve();
const auto* masked = position.TryInstruction();
uint32_t lane_mask = 0;
if (masked != nullptr && masked->GetOpcode() == ValueOpcode::BitwiseAnd32 &&
masked->NumArgs() == 2u) {
if (ImmediateU32(masked->Arg(0), lane_mask) && lane_mask == 31u)
position = masked->Arg(1);
else if (ImmediateU32(masked->Arg(1), lane_mask) && lane_mask == 31u)
position = masked->Arg(0);
}
const auto* first = position.Resolve().TryInstruction();
return first != nullptr && first->GetOpcode() == ValueOpcode::FindILsb32 &&
first->NumArgs() == 1u && EquivalentValue(m_program, first->Arg(0), mask);
}
Value InitialClearingMask(Value value, const Block* update_block) const {
const auto* phi = value.Resolve().TryInstruction();
if (phi == nullptr || phi->GetOpcode() != ValueOpcode::Phi ||
phi->NumArgs() != 2u || phi->GetType() != Type::U32) return {};
for (uint32_t back = 0; back < 2u; ++back) {
if (phi->PhiBlock(back) != update_block ||
!ClearsFirstSetBit(phi->Arg(back), value)) continue;
const auto initial = phi->Arg(back ^ 1u).Resolve();
return ValidateRuntimeValue(m_program, initial, RuntimeValueType::Integer)
? initial : Value {};
}
return {};
}
bool EdgeOn(const Block* from, const Block* to, Value predicate, bool positive) const {
Value condition;
bool taken = false;
return ConditionalEdge(from, to, condition, taken) && taken == positive &&
EquivalentValue(m_program, condition, predicate);
}
Value BoundedSetBitMask(Value index, Value guard) const {
const auto* phi = index.Resolve().TryInstruction();
if (phi == nullptr || phi->GetOpcode() != ValueOpcode::Phi ||
phi->NumArgs() != 3u || phi->GetType() != Type::U32 ||
!ImpliesIndexBelow32(guard, index)) return {};
for (uint32_t bit_arm = 0; bit_arm < 3u; ++bit_arm) {
const auto bit_guard = PositiveUseGuard(phi->PhiBlock(bit_arm));
const auto* selected = phi->Arg(bit_arm).Resolve().TryInstruction();
if (selected == nullptr || selected->GetOpcode() != ValueOpcode::SelectU32 ||
selected->NumArgs() != 3u ||
!Implies(bit_guard, selected->Arg(0))) continue;
const auto* first = selected->Arg(1).Resolve().TryInstruction();
if (first == nullptr || first->GetOpcode() != ValueOpcode::FindILsb32 ||
first->NumArgs() != 1u || !ImpliesNonzero(bit_guard, first->Arg(0)))
continue;
const auto initial_mask = InitialClearingMask(first->Arg(0), phi->PhiBlock(bit_arm));
if (initial_mask.IsEmpty()) continue;
for (uint32_t sentinel_arm = 0; sentinel_arm < 3u; ++sentinel_arm) {
if (sentinel_arm == bit_arm) continue;
const auto sentinel_guard = PositiveUseGuard(phi->PhiBlock(sentinel_arm));
const auto* sentinel = phi->Arg(sentinel_arm).Resolve().TryInstruction();
uint32_t bound = 0;
if (sentinel == nullptr || sentinel->GetOpcode() != ValueOpcode::SelectU32 ||
sentinel->NumArgs() != 3u ||
!Implies(sentinel_guard, sentinel->Arg(0)) ||
!ImmediateU32(sentinel->Arg(1), bound) || bound != 32u) continue;
const auto loop_active = sentinel->Arg(0).Resolve();
const auto* active_phi = loop_active.TryInstruction();
if (active_phi == nullptr || active_phi->GetOpcode() != ValueOpcode::Phi ||
active_phi->NumArgs() != 2u) continue;
bool invariant = false;
for (uint32_t initial = 0; initial < 2u; ++initial) {
invariant = Implies(guard, active_phi->Arg(initial)) &&
EdgeOn(active_phi->PhiBlock(initial ^ 1u), active_phi->Parent(),
active_phi->Arg(initial ^ 1u), true);
if (invariant) break;
}
if (!invariant) continue;
const auto other_arm = 3u - bit_arm - sentinel_arm;
if (EdgeOn(phi->PhiBlock(other_arm), phi->Parent(), loop_active, false))
return initial_mask;
}
}
return {};
}
bool MatchUniformizedMaterialKey(Value key, const Inst& image,
DescriptorSource::IndirectImage& indirect,
DescriptorSource& material_source, uint32_t pc) {
const auto guard = PositiveUseGuard(image.Parent());
if (guard.IsEmpty()) return false;
const auto local = EqualLocalKey(guard, key);
const auto* selected = local.Resolve().TryInstruction();
if (selected == nullptr || selected->GetOpcode() != ValueOpcode::SelectU32 ||
selected->NumArgs() != 3u || !Implies(guard, selected->Arg(0))) return false;
const auto active = selected->Arg(0).Resolve();
const auto* read = selected->Arg(1).Resolve().TryInstruction();
if (read == nullptr || read->GetOpcode() != ValueOpcode::LoadAddressU32 ||
read->NumArgs() != 4u || !EquivalentValue(m_program, read->Arg(3), active))
return false;
uint32_t high = 1u;
if (!ImmediateU32(read->Arg(2), high) || high != 0u) return false;
const auto memory_index = read->Flags<MemoryFlags>().index;
if (memory_index >= m_program.memory_info.size()) return false;
const auto& memory = m_program.memory_info[memory_index];
if (memory.kind != ResourceKind::Global || memory.data_bits != 32u ||
memory.data_dwords != 1u || !MemoryIndexBelongsTo(memory_index, *read)) return false;
const auto* material_handle = read->Arg(0).Resolve().TryInstruction();
if (material_handle == nullptr ||
material_handle->GetOpcode() != ValueOpcode::GetAddressResource ||
!MakeRuntimeTableSource(*material_handle, pc, material_source)) return false;
AffineOffset offset;
if (!MatchAffineOffset(read->Arg(1), active, offset) || offset.index.IsEmpty() ||
offset.stride == 0u || offset.offset + memory.offset > UINT32_MAX ||
offset.offset + memory.offset + 31u * offset.stride + 4u >
static_cast<uint64_t>(UINT32_MAX) + 1u) return false;
const auto mask = BoundedSetBitMask(offset.index, active);
if (mask.IsEmpty()) return false;
indirect.material_source = InternSource(material_source);
indirect.selector_stride = static_cast<uint32_t>(offset.stride);
indirect.selector_offset = static_cast<uint32_t>(offset.offset + memory.offset);
indirect.key_count = Value(32u);
indirect.selector_mask = mask;
return true;
}
Value BoundedLoopCount(Value key, const Block* use) const {
const auto* phi = key.Resolve().TryInstruction();
if (m_shader_writes || phi == nullptr || phi->GetOpcode() != ValueOpcode::Phi ||
phi->GetType() != Type::U32 || phi->NumArgs() != 2u ||
m_program.blocks.size() != m_program.block_info.size()) {
return {};
}
bool induction = false;
for (uint32_t initial = 0; initial < 2u; ++initial) {
const auto zero = phi->Arg(initial).Resolve();
const auto* step = phi->Arg(initial ^ 1u).Resolve().TryInstruction();
if (!zero.IsImmediate() || zero.GetType() != Type::U32 || zero.U32() != 0u ||
step == nullptr || step->GetOpcode() != ValueOpcode::IAdd32 ||
step->Parent() != phi->PhiBlock(initial ^ 1u)) {
continue;
}
uint32_t increment = 0;
induction = (step->Arg(0).Resolve() == key &&
ImmediateU32(step->Arg(1), increment) && increment == 1u) ||
(step->Arg(1).Resolve() == key &&
ImmediateU32(step->Arg(0), increment) && increment == 1u);
if (induction) break;
}
if (!induction) return {};
const auto contains = [&](auto&& self, Value value, const Inst* comparison) -> bool {
value = value.Resolve();
if (value.TryInstruction() == comparison) return true;
const auto* inst = value.TryInstruction();
return inst != nullptr && inst->GetOpcode() == ValueOpcode::LogicalAnd &&
(self(self, inst->Arg(0), comparison) || self(self, inst->Arg(1), comparison));
};
for (const auto& use_of_key: phi->Uses()) {
const auto* compare = use_of_key.user;
if (compare->GetOpcode() != ValueOpcode::SLessThan32 || use_of_key.operand != 0u ||
!ValidateRuntimeValue(m_program, compare->Arg(1))) continue;
for (uint32_t i = 0; i < m_program.block_info.size(); ++i) {
const auto& info = m_program.block_info[i];
const auto* negated = info.condition.Resolve().TryInstruction();
if (info.terminator.kind != CFG::TerminatorKind::ConditionalBranch ||
compare->Parent() != m_program.blocks[i] || negated == nullptr ||
negated->GetOpcode() != ValueOpcode::LogicalNot ||
!contains(contains, negated->Arg(0), compare) ||
use == m_program.blocks[i] ||
CanReach(m_program.blocks.front(), use, m_program.blocks[i]) ||
CanReach(phi->Parent(), use, m_program.blocks[i]) ||
CanReach(FindBlock(info.terminator.true_block), use, phi->Parent()) ||
!CanReach(FindBlock(info.terminator.false_block), use, phi->Parent())) {
continue;
}
return compare->Arg(1);
}
}
return {};
}
bool TryMakeIndirectImage(Inst& handle, uint32_t pc, IndirectImagePlan& plan) {
if (handle.GetOpcode() != ValueOpcode::GetImageResource || handle.NumArgs() != 8u) {
return false;
@@ -562,13 +922,20 @@ private:
indirect.table_offset = table_offset;
if (table_source.dword_count == 2u) {
const auto* selector = key.Resolve().TryInstruction();
if (m_shader_writes || selector == nullptr ||
selector->GetOpcode() != ValueOpcode::FindILsb32 || selector->NumArgs() != 1u ||
!NonzeroOnEntry(selector->Arg(0), handle.Parent()) ||
(table_offset & 3u) != 0u || table_offset > UINT32_MAX - (32u * 32u - 1u)) {
const bool bitscan = selector != nullptr && selector->GetOpcode() == ValueOpcode::FindILsb32 &&
selector->NumArgs() == 1u && !m_shader_writes &&
NonzeroOnEntry(selector->Arg(0), handle.Parent());
if (bitscan) {
indirect.key_count = Value(32u);
} else {
indirect.key_count = BoundedLoopCount(key, handle.Parent());
}
if (indirect.key_count.IsEmpty() &&
!MatchUniformizedMaterialKey(key, handle, indirect, material_source, pc)) {
return false;
}
indirect.key_count = 32u;
if ((table_offset & 3u) != 0u ||
(bitscan && table_offset > UINT32_MAX - (32u * 32u - 1u))) return false;
} else {
auto* material_read = key.Resolve().TryInstruction();
uint32_t material_memory_index = 0;
@@ -873,6 +1240,9 @@ private:
return;
}
ValidateAddressHandle(inst.Arg(0), flags.pc);
if (address_info.access == AddressAccess::Write) {
m_program.has_address_writes = true;
}
m_info.uses_dma = true;
return;
}
+83 -112
View File
@@ -200,6 +200,10 @@ static bool IsPrivateCommittedRangeType(VirtualRangeType type) {
type == VirtualRangeType::Runtime;
}
#if defined(KYTY_VIRTUAL_MEMORY_ALLOCATION_TESTS)
static bool g_test_fail_next_range_replace = false;
#endif
class VirtualRanges {
public:
struct Range {
@@ -288,36 +292,22 @@ public:
return true;
}
bool ConsumeReserved(uint64_t start, uint64_t size,
VirtualRangeType type = VirtualRangeType::Reserved) {
bool ReplaceSpan(uint64_t start, uint64_t size, VirtualRangeType expected_type,
uint64_t offset, int protection, int memory_type, VirtualRangeType type,
const char* name, bool disallow_merge = false) {
Common::LockGuard lock(m_mutex);
auto end = End(start, size);
for (const auto& r: m_ranges) {
if (r.type == type && start >= r.start && end <= End(r.start, r.size)) {
RemoveUnlocked(start, size);
MergeUnlocked();
return true;
}
}
return false;
}
bool ConsumeReservedSpan(uint64_t start, uint64_t size, Range* first_range = nullptr,
VirtualRangeType type = VirtualRangeType::Reserved) {
Common::LockGuard lock(m_mutex);
if (size == 0) {
if (start == 0 || size == 0 || size > UINT64_MAX - start) {
return false;
}
auto current = start;
auto end = End(start, size);
const auto end = start + size;
while (current < end) {
const Range* candidate = nullptr;
for (const auto& r: m_ranges) {
if (r.type == type && current >= r.start && current < End(r.start, r.size)) {
if (r.type == expected_type && current >= r.start &&
current < End(r.start, r.size)) {
candidate = &r;
break;
}
@@ -325,14 +315,29 @@ public:
if (candidate == nullptr) {
return false;
}
if (current == start && first_range != nullptr) {
*first_range = *candidate;
}
current = std::min(end, End(candidate->start, candidate->size));
}
#if defined(KYTY_VIRTUAL_MEMORY_ALLOCATION_TESTS)
if (g_test_fail_next_range_replace) {
g_test_fail_next_range_replace = false;
return false;
}
#endif
Range replacement {};
replacement.start = start;
replacement.size = size;
replacement.offset = offset;
replacement.protection = protection;
replacement.memory_type = memory_type;
replacement.type = type;
replacement.disallow_merge = disallow_merge;
CopyVirtualRangeName(replacement.name, name);
RemoveUnlocked(start, size);
MergeUnlocked();
auto position = LowerBound(start);
const auto index = static_cast<size_t>(position - m_ranges.begin());
m_ranges.insert(position, replacement);
MergeAroundUnlocked(index);
return true;
}
@@ -2250,10 +2255,9 @@ int32_t KYTY_SYSV_ABI KernelMapNamedFlexibleMemory(void** addr_in_out, size_t le
EXIT("unknown prot: %d\n", prot);
}
auto in_addr = reinterpret_cast<uint64_t>(*addr_in_out);
uint64_t out_addr = 0;
bool consumed_reservation = false;
VirtualRanges::Range consumed_range {};
auto in_addr = reinterpret_cast<uint64_t>(*addr_in_out);
uint64_t out_addr = 0;
bool reserved_target = false;
if ((flags & GUEST_MAP_FIXED) != 0) {
if (in_addr == 0 || (in_addr & (PAGE_SIZE - 1)) != 0 ||
@@ -2269,16 +2273,12 @@ int32_t KYTY_SYSV_ABI KernelMapNamedFlexibleMemory(void** addr_in_out, size_t le
return range.type == VirtualRangeType::Reserved;
})) {
UnmapGpuRange(in_addr, len);
consumed_range = reserved_ranges.front();
if (g_virtual_ranges->ConsumeReservedSpan(in_addr, len)) {
consumed_reservation = true;
out_addr = in_addr;
}
reserved_target = true;
out_addr = in_addr;
}
if (!consumed_reservation && ReplaceFixedRangeWithReserved(in_addr, len) &&
g_virtual_ranges->ConsumeReservedSpan(in_addr, len, &consumed_range)) {
consumed_reservation = true;
out_addr = in_addr;
if (!reserved_target && ReplaceFixedRangeWithReserved(in_addr, len)) {
reserved_target = true;
out_addr = in_addr;
}
} else {
const auto search_addr = (in_addr != 0 ? in_addr : DEFAULT_PS5_BASE);
@@ -2291,30 +2291,25 @@ int32_t KYTY_SYSV_ABI KernelMapNamedFlexibleMemory(void** addr_in_out, size_t le
*addr_in_out = reinterpret_cast<void*>(out_addr);
if (out_addr == 0) {
if (consumed_reservation) {
g_virtual_ranges->Add(in_addr, len, 0, 0, 0, VirtualRangeType::Reserved,
consumed_range.name);
}
return KERNEL_ERROR_ENOMEM;
}
if (!g_flexible_memory->Map(out_addr, len, prot, mode, gpu_mode, name)) {
LOGF_COLOR(Log::Color::Red, "\t [Fail]\n");
if (consumed_reservation) {
EXIT_IF(!g_virtual_ranges->Add(out_addr, len, 0, 0, 0, VirtualRangeType::Reserved,
consumed_range.name));
}
return KERNEL_ERROR_ENOMEM;
}
if (!g_virtual_ranges->Add(out_addr, len, 0, prot, 0, VirtualRangeType::Flexible, name,
(map_flags & GUEST_MAP_NO_COALESCE) != 0)) {
const bool published = reserved_target
? g_virtual_ranges->ReplaceSpan(
out_addr, len, VirtualRangeType::Reserved, 0, prot, 0,
VirtualRangeType::Flexible, name,
(map_flags & GUEST_MAP_NO_COALESCE) != 0)
: g_virtual_ranges->Add(out_addr, len, 0, prot, 0,
VirtualRangeType::Flexible, name,
(map_flags & GUEST_MAP_NO_COALESCE) != 0);
if (!published) {
GpuAccessMode rollback_gpu_mode = GpuAccessMode::NoAccess;
EXIT_IF(!g_flexible_memory->Unmap(out_addr, len, &rollback_gpu_mode));
if (consumed_reservation) {
EXIT_IF(!g_virtual_ranges->Add(out_addr, len, 0, 0, 0, VirtualRangeType::Reserved,
consumed_range.name));
}
return KERNEL_ERROR_EBUSY;
}
@@ -2946,25 +2941,17 @@ int KYTY_SYSV_ABI KernelMapDirectMemory(void** addr, size_t len, int prot, int f
return KERNEL_ERROR_ENOMEM;
}
auto in_addr = reinterpret_cast<uint64_t>(*addr);
uint64_t out_addr = 0;
bool shared_backing = false;
bool consumed_reservation = false;
VirtualRanges::Range consumed_range {};
auto shared_failure = GuestBackingStore::FailureReason::None;
auto in_addr = reinterpret_cast<uint64_t>(*addr);
uint64_t out_addr = 0;
bool shared_backing = false;
bool reserved_target = false;
auto shared_failure = GuestBackingStore::FailureReason::None;
// Direct mappings must remain views of the single backing object. Anonymous fallbacks break
// aliasing and lose direct-memory contents when a range is unmapped and mapped again.
auto map_shared_fixed = [&](uint64_t target_addr) -> bool {
return g_guest_address_space->MapBacking(target_addr, len, direct_memory_start, mode,
&shared_failure);
};
auto map_consumed_reserved_fixed = [&]() {
if (map_shared_fixed(in_addr)) {
out_addr = in_addr;
shared_backing = true;
}
};
if (fixed) {
if (in_addr == 0 || (in_addr & (PAGE_SIZE - 1u)) != 0 ||
(alignment != 0 && in_addr % alignment != 0)) {
@@ -2980,20 +2967,18 @@ int KYTY_SYSV_ABI KernelMapDirectMemory(void** addr, size_t len, int prot, int f
return range.type == VirtualRangeType::Reserved;
})) {
UnmapGpuRange(in_addr, len);
consumed_range = reserved_ranges.front();
if (g_virtual_ranges->ConsumeReservedSpan(in_addr, len)) {
consumed_reservation = true;
map_consumed_reserved_fixed();
}
reserved_target = true;
}
if (!consumed_reservation && ReplaceFixedRangeWithReserved(in_addr, len) &&
g_virtual_ranges->ConsumeReservedSpan(in_addr, len, &consumed_range)) {
consumed_reservation = true;
map_consumed_reserved_fixed();
if (!reserved_target && ReplaceFixedRangeWithReserved(in_addr, len)) {
reserved_target = true;
}
if (!consumed_reservation) {
if (!reserved_target) {
return KERNEL_ERROR_ENOMEM;
}
if (map_shared_fixed(in_addr)) {
out_addr = in_addr;
shared_backing = true;
}
} else {
constexpr size_t DEFAULT_ALIGNMENT = 0x4000;
alignment = (alignment != 0 ? alignment : DEFAULT_ALIGNMENT);
@@ -3003,16 +2988,13 @@ int KYTY_SYSV_ABI KernelMapDirectMemory(void** addr, size_t len, int prot, int f
return range.type == VirtualRangeType::Reserved;
})) {
UnmapGpuRange(in_addr, len);
consumed_range = reserved_ranges.front();
if (g_virtual_ranges->ConsumeReservedSpan(in_addr, len)) {
consumed_reservation = true;
if (map_shared_fixed(in_addr)) {
out_addr = in_addr;
shared_backing = true;
}
reserved_target = true;
if (map_shared_fixed(in_addr)) {
out_addr = in_addr;
shared_backing = true;
}
}
if (!consumed_reservation) {
if (!reserved_target) {
out_addr = FindGuestFreeRange(in_addr, len, alignment);
if (out_addr != 0) {
UnmapGpuRange(out_addr, len);
@@ -3046,34 +3028,28 @@ int KYTY_SYSV_ABI KernelMapDirectMemory(void** addr, size_t len, int prot, int f
magic_enum::enum_name(gpu_mode), shared_backing ? "yes" : "no", shared_reason);
if (out_addr == 0) {
if (consumed_reservation) {
g_virtual_ranges->Add(in_addr, len, 0, 0, 0, VirtualRangeType::Reserved,
consumed_range.name);
}
return KERNEL_ERROR_ENOMEM;
}
if (!g_physical_memory->Map(out_addr, direct_memory_start, len, prot, mode, gpu_mode)) {
LOGF_COLOR(Log::Color::Red, "\t [Fail]\n");
EXIT_IF(!g_guest_address_space->UnmapBacking(out_addr, len));
if (consumed_reservation) {
EXIT_IF(!g_virtual_ranges->Add(in_addr, len, 0, 0, 0, VirtualRangeType::Reserved,
consumed_range.name));
}
return KERNEL_ERROR_EBUSY;
}
PhysicalMemory::AllocatedBlock mapped_block {};
g_physical_memory->Find(direct_memory_start, false, &mapped_block);
if (!g_virtual_ranges->Add(out_addr, len, direct_memory_start, prot, mapped_block.memory_type,
VirtualRangeType::Direct, "")) {
const bool published = reserved_target
? g_virtual_ranges->ReplaceSpan(
out_addr, len, VirtualRangeType::Reserved, direct_memory_start,
prot, mapped_block.memory_type, VirtualRangeType::Direct, "")
: g_virtual_ranges->Add(out_addr, len, direct_memory_start, prot,
mapped_block.memory_type,
VirtualRangeType::Direct, "");
if (!published) {
GpuAccessMode rollback_gpu_mode = GpuAccessMode::NoAccess;
EXIT_IF(!g_physical_memory->Unmap(out_addr, len, &rollback_gpu_mode));
EXIT_IF(!g_guest_address_space->UnmapBacking(out_addr, len));
if (consumed_reservation) {
EXIT_IF(!g_virtual_ranges->Add(in_addr, len, 0, 0, 0, VirtualRangeType::Reserved,
consumed_range.name));
}
return KERNEL_ERROR_EBUSY;
}
@@ -3442,6 +3418,10 @@ void TestFailNextFixedReserveRangeRegistration() {
g_test_fail_next_fixed_reserve_range_add = true;
}
void TestFailNextVirtualRangeReplacement() {
g_test_fail_next_range_replace = true;
}
bool TestPlaceholderRangeIsFree(uint64_t vaddr, uint64_t size) {
return g_guest_address_space->TestContainsFree(vaddr, size);
}
@@ -3965,12 +3945,8 @@ int KYTY_SYSV_ABI KernelMemoryPoolReserve(void* addr_in, size_t len, size_t alig
!IsReservedRangeType(reserved_range.type)) {
return KERNEL_ERROR_EBUSY;
}
g_virtual_ranges->Remove(out_vaddr, len);
if (!g_virtual_ranges->Add(out_vaddr, len, 0, 0, 0, VirtualRangeType::PoolReserved,
reserved_range.name)) {
g_virtual_ranges->Add(out_vaddr, len, reserved_range.offset, reserved_range.protection,
reserved_range.memory_type, reserved_range.type,
reserved_range.name);
if (!g_virtual_ranges->ReplaceSpan(out_vaddr, len, VirtualRangeType::Reserved, 0, 0, 0,
VirtualRangeType::PoolReserved, reserved_range.name)) {
return KERNEL_ERROR_EBUSY;
}
*addr_out = out_addr;
@@ -4011,17 +3987,13 @@ int KYTY_SYSV_ABI KernelMemoryPoolCommit(void* addr, size_t len, int type, int p
VirtualRanges::Range old_range {};
if (!g_virtual_ranges->Query(vaddr, 0, &old_range) ||
old_range.type != VirtualRangeType::PoolReserved) {
return KERNEL_ERROR_EACCES;
}
if (!g_virtual_ranges->ConsumeReserved(vaddr, len, VirtualRangeType::PoolReserved)) {
old_range.type != VirtualRangeType::PoolReserved ||
len > old_range.size - (vaddr - old_range.start)) {
return KERNEL_ERROR_EACCES;
}
std::vector<PooledMemory::Mapping> mappings;
if (!g_pooled_memory->Allocate(vaddr, len, gpu_mode, &mappings)) {
g_virtual_ranges->Add(vaddr, len, 0, 0, 0, VirtualRangeType::PoolReserved, old_range.name);
return KERNEL_ERROR_ENOMEM;
}
@@ -4034,7 +4006,6 @@ int KYTY_SYSV_ABI KernelMemoryPoolCommit(void* addr, size_t len, int type, int p
if (!g_pooled_memory->Release(vaddr, len, &rollback_gpu_mode)) {
EXIT("failed to release pooled-memory rollback allocation\n");
}
g_virtual_ranges->Add(vaddr, len, 0, 0, 0, VirtualRangeType::PoolReserved, old_range.name);
};
for (const auto& mapping: mappings) {
@@ -4050,8 +4021,8 @@ int KYTY_SYSV_ABI KernelMemoryPoolCommit(void* addr, size_t len, int type, int p
mapped.push_back(mapping);
}
if (!g_virtual_ranges->Add(vaddr, len, 0, prot, type, VirtualRangeType::Pooled,
old_range.name)) {
if (!g_virtual_ranges->ReplaceSpan(vaddr, len, VirtualRangeType::PoolReserved, 0, prot, type,
VirtualRangeType::Pooled, old_range.name)) {
rollback();
return KERNEL_ERROR_EBUSY;
}
+1
View File
@@ -190,6 +190,7 @@ void TestFailNextPhysicalMemoryUnmap();
void TestFailPhysicalMemoryUnmapAfter(uint32_t successful_unmaps);
void TestFailGuestBackingStoreUnmapAfter(uint32_t successful_unmaps);
void TestFailNextFixedReserveRangeRegistration();
void TestFailNextVirtualRangeReplacement();
bool TestPlaceholderRangeIsFree(uint64_t vaddr, uint64_t size);
bool TestGuestAddressRangeIsOwned(uint64_t vaddr, uint64_t size);
bool TestGuestBackingOutsideAddressSpace();
+177
View File
@@ -0,0 +1,177 @@
#include "libs/acm.h"
#include "common/assert.h"
#include "libs/errno.h"
#include "libs/libs.h"
#include <algorithm>
#include <atomic>
#include <cstring>
namespace Libs::Audio {
namespace Acm {
LIB_NAME("Acm", "Acm");
struct AcmBatchInfo {
void* buffer;
size_t offset;
size_t buffer_size;
};
struct AcmBatchError {
uint32_t reserved[8];
};
static std::atomic_uint32_t g_acm_next_context {1};
static std::atomic_uint32_t g_acm_next_batch {1};
static void acm_advance_batch(AcmBatchInfo* info, size_t bytes) {
if (info == nullptr || info->buffer == nullptr || info->buffer_size == 0) {
return;
}
info->offset = std::min(info->buffer_size, info->offset + bytes);
}
int KYTY_SYSV_ABI AcmContextCreate(AcmContextId* context) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(context == nullptr);
*context = g_acm_next_context.fetch_add(1, std::memory_order_relaxed);
LOGF("\t context = %" PRIu32 "\n", *context);
return OK;
}
int KYTY_SYSV_ABI AcmContextDestroy(AcmContextId context) {
PRINT_NAME();
LOGF("\t context = %" PRIu32 "\n", context);
return OK;
}
int KYTY_SYSV_ABI AcmBatchStartBuffer(AcmContextId context, const void* batch_commands,
size_t batch_size, AcmBatchError* batch_error,
AcmBatchId* batch) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(batch == nullptr);
if (batch_error != nullptr) {
std::memset(batch_error, 0, sizeof(AcmBatchError));
}
*batch = g_acm_next_batch.fetch_add(1, std::memory_order_relaxed);
return OK;
}
int KYTY_SYSV_ABI AcmBatchStartBuffers(AcmContextId context, uint32_t batch_info_count,
const AcmBatchInfo* const batch_info[],
AcmBatchError* batch_error, AcmBatchId* batch) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(batch_info_count != 0 && batch_info == nullptr);
EXIT_NOT_IMPLEMENTED(batch == nullptr);
if (batch_error != nullptr) {
std::memset(batch_error, 0, sizeof(AcmBatchError));
}
*batch = g_acm_next_batch.fetch_add(1, std::memory_order_relaxed);
return OK;
}
int KYTY_SYSV_ABI AcmBatchWait(AcmContextId context, AcmBatchId batch, uint32_t timeout) {
return OK;
}
int KYTY_SYSV_ABI AcmBatchJobNotification(AcmBatchInfo* batch_info) {
PRINT_NAME();
acm_advance_batch(batch_info, 2 * 16);
return OK;
}
int KYTY_SYSV_ABI AcmConvReverbSharedInput(AcmBatchInfo* batch_info, uint32_t block_count, void* in,
uint32_t count, const void* const ir[],
const float* gain, void* const out[]) {
PRINT_NAME();
(void)block_count;
(void)in;
(void)count;
(void)ir;
(void)gain;
(void)out;
acm_advance_batch(batch_info, 1024);
return OK;
}
int KYTY_SYSV_ABI AcmConvReverbSharedIr(AcmBatchInfo* batch_info, uint32_t block_count,
const void* ir, uint32_t count, void* const in[],
const float* gain, void* const out[]) {
PRINT_NAME();
(void)block_count;
(void)ir;
(void)count;
(void)in;
(void)gain;
(void)out;
acm_advance_batch(batch_info, 1024);
return OK;
}
int KYTY_SYSV_ABI AcmFft(AcmBatchInfo* batch_info, int size, int count, int input_format,
const void* const input[], int output_format, void* const output[],
uint32_t flags) {
PRINT_NAME();
(void)size;
(void)count;
(void)input_format;
(void)input;
(void)output_format;
(void)output;
(void)flags;
acm_advance_batch(batch_info, 256);
return OK;
}
int KYTY_SYSV_ABI AcmIfft(AcmBatchInfo* batch_info, int size, int count, int input_format,
const void* const input[], int output_format, void* const output[],
uint32_t flags) {
PRINT_NAME();
(void)size;
(void)count;
(void)input_format;
(void)input;
(void)output_format;
(void)output;
(void)flags;
acm_advance_batch(batch_info, 256);
return OK;
}
int KYTY_SYSV_ABI AcmPanner(AcmBatchInfo* batch_info, uint32_t in_count, const float* const in[],
uint32_t biquad_count, uint32_t biquad_update_count, uint32_t out_count,
const void* const parameter[], void* const state[],
const float* const out_init[], float* const out[]) {
PRINT_NAME();
(void)in_count;
(void)in;
(void)biquad_count;
(void)biquad_update_count;
(void)out_count;
(void)parameter;
(void)state;
(void)out_init;
(void)out;
acm_advance_batch(batch_info, 512);
return OK;
}
} // namespace Acm
} // namespace Libs::Audio
+48
View File
@@ -0,0 +1,48 @@
#ifndef EMULATOR_INCLUDE_EMULATOR_LIBS_ACM_H_
#define EMULATOR_INCLUDE_EMULATOR_LIBS_ACM_H_
#include "common/abi.h"
#include "common/common.h"
namespace Libs::Audio {
namespace Acm {
struct AcmBatchInfo;
struct AcmBatchError;
using AcmContextId = uint32_t;
using AcmBatchId = uint32_t;
int KYTY_SYSV_ABI AcmContextCreate(AcmContextId* context);
int KYTY_SYSV_ABI AcmContextDestroy(AcmContextId context);
int KYTY_SYSV_ABI AcmBatchStartBuffer(AcmContextId context, const void* batch_commands,
size_t batch_size, AcmBatchError* batch_error,
AcmBatchId* batch);
int KYTY_SYSV_ABI AcmBatchStartBuffers(AcmContextId context, uint32_t batch_info_count,
const AcmBatchInfo* const batch_info[],
AcmBatchError* batch_error, AcmBatchId* batch);
int KYTY_SYSV_ABI AcmBatchWait(AcmContextId context, AcmBatchId batch, uint32_t timeout);
int KYTY_SYSV_ABI AcmBatchJobNotification(AcmBatchInfo* batch_info);
int KYTY_SYSV_ABI AcmConvReverbSharedInput(AcmBatchInfo* batch_info, uint32_t block_count, void* in,
uint32_t count, const void* const ir[],
const float* gain, void* const out[]);
int KYTY_SYSV_ABI AcmConvReverbSharedIr(AcmBatchInfo* batch_info, uint32_t block_count,
const void* ir, uint32_t count, void* const in[],
const float* gain, void* const out[]);
int KYTY_SYSV_ABI AcmFft(AcmBatchInfo* batch_info, int size, int count, int input_format,
const void* const input[], int output_format, void* const output[],
uint32_t flags);
int KYTY_SYSV_ABI AcmIfft(AcmBatchInfo* batch_info, int size, int count, int input_format,
const void* const input[], int output_format, void* const output[],
uint32_t flags);
int KYTY_SYSV_ABI AcmPanner(AcmBatchInfo* batch_info, uint32_t in_count, const float* const in[],
uint32_t biquad_count, uint32_t biquad_update_count, uint32_t out_count,
const void* const parameter[], void* const state[],
const float* const out_init[], float* const out[]);
} // namespace Acm
} // namespace Libs::Audio
#endif /* EMULATOR_INCLUDE_EMULATOR_LIBS_ACM_H_ */
-2522
View File
File diff suppressed because it is too large. Load diff
-126
View File
@@ -190,43 +190,6 @@ const char* KYTY_SYSV_ABI AjmStrError(int error);
} // namespace Ajm
namespace Acm {
struct AcmBatchInfo;
struct AcmBatchError;
using AcmContextId = uint32_t;
using AcmBatchId = uint32_t;
int KYTY_SYSV_ABI AcmContextCreate(AcmContextId* context);
int KYTY_SYSV_ABI AcmContextDestroy(AcmContextId context);
int KYTY_SYSV_ABI AcmBatchStartBuffer(AcmContextId context, const void* batch_commands,
size_t batch_size, AcmBatchError* batch_error,
AcmBatchId* batch);
int KYTY_SYSV_ABI AcmBatchStartBuffers(AcmContextId context, uint32_t batch_info_count,
const AcmBatchInfo* const batch_info[],
AcmBatchError* batch_error, AcmBatchId* batch);
int KYTY_SYSV_ABI AcmBatchWait(AcmContextId context, AcmBatchId batch, uint32_t timeout);
int KYTY_SYSV_ABI AcmBatchJobNotification(AcmBatchInfo* batch_info);
int KYTY_SYSV_ABI AcmConvReverbSharedInput(AcmBatchInfo* batch_info, uint32_t block_count, void* in,
uint32_t count, const void* const ir[],
const float* gain, void* const out[]);
int KYTY_SYSV_ABI AcmConvReverbSharedIr(AcmBatchInfo* batch_info, uint32_t block_count,
const void* ir, uint32_t count, void* const in[],
const float* gain, void* const out[]);
int KYTY_SYSV_ABI AcmFft(AcmBatchInfo* batch_info, int size, int count, int input_format,
const void* const input[], int output_format, void* const output[],
uint32_t flags);
int KYTY_SYSV_ABI AcmIfft(AcmBatchInfo* batch_info, int size, int count, int input_format,
const void* const input[], int output_format, void* const output[],
uint32_t flags);
int KYTY_SYSV_ABI AcmPanner(AcmBatchInfo* batch_info, uint32_t in_count, const float* const in[],
uint32_t biquad_count, uint32_t biquad_update_count, uint32_t out_count,
const void* const parameter[], void* const state[],
const float* const out_init[], float* const out[]);
} // namespace Acm
namespace AvPlayer {
struct AvPlayerInitData;
@@ -271,95 +234,6 @@ int KYTY_SYSV_ABI AvPlayerSetLogCallback(void* callback, void* user_data);
} // namespace AvPlayer
namespace Audio3d {
struct Audio3dOpenParameters;
int KYTY_SYSV_ABI Audio3dInitialize(int64_t reserved);
void KYTY_SYSV_ABI Audio3dGetDefaultOpenParameters(Audio3dOpenParameters* p);
int KYTY_SYSV_ABI Audio3dPortOpen(int user_id, const Audio3dOpenParameters* parameters,
uint32_t* id);
int KYTY_SYSV_ABI Audio3dPortSetAttribute(uint32_t port_id, uint32_t attribute_id,
const void* attribute, size_t attribute_size);
int KYTY_SYSV_ABI Audio3dPortGetQueueLevel(uint32_t port_id, uint32_t* queue_level,
uint32_t* queue_available);
int KYTY_SYSV_ABI Audio3dPortAdvance(uint32_t port_id);
int KYTY_SYSV_ABI Audio3dPortPush(uint32_t port_id, uint32_t blocking);
} // namespace Audio3d
namespace Ngs2 {
struct Ngs2SystemOption;
struct Ngs2SystemInfo;
struct Ngs2RackOption;
struct Ngs2BufferAllocator;
struct Ngs2VoiceParamHeader;
struct Ngs2RenderBufferInfo;
struct Ngs2ContextBufferInfo;
struct Ngs2VoiceState;
struct Ngs2WaveformFormat;
struct Ngs2WaveformBlock;
struct Ngs2WaveformInfo;
struct Ngs2PanWork;
struct Ngs2PanParam;
struct Ngs2GeomListenerParam;
struct Ngs2GeomListenerWork;
struct Ngs2GeomSourceParam;
struct Ngs2GeomAttribute;
int KYTY_SYSV_ABI Ngs2SystemResetOption(Ngs2SystemOption* option);
int KYTY_SYSV_ABI Ngs2SystemQueryBufferSize(const Ngs2SystemOption* option,
Ngs2ContextBufferInfo* buffer_info);
int KYTY_SYSV_ABI Ngs2SystemCreate(const Ngs2SystemOption* option,
const Ngs2ContextBufferInfo* buffer_info, uintptr_t* handle);
int KYTY_SYSV_ABI Ngs2SystemGetInfo(uintptr_t system_handle, Ngs2SystemInfo* info,
size_t info_size);
int KYTY_SYSV_ABI Ngs2SystemSetGrainSamples(uintptr_t system_handle, uint32_t num_samples);
int KYTY_SYSV_ABI Ngs2RackQueryBufferSize(uint32_t rack_id, const Ngs2RackOption* option,
Ngs2ContextBufferInfo* buffer_info);
int KYTY_SYSV_ABI Ngs2RackCreate(uintptr_t system_handle, uint32_t rack_id,
const Ngs2RackOption* option,
const Ngs2ContextBufferInfo* buffer_info, uintptr_t* handle);
int KYTY_SYSV_ABI Ngs2SystemCreateWithAllocator(const Ngs2SystemOption* option,
const Ngs2BufferAllocator* allocator,
uintptr_t* handle);
int KYTY_SYSV_ABI Ngs2SystemDestroy(uintptr_t system_handle, Ngs2ContextBufferInfo* buffer_info);
int KYTY_SYSV_ABI Ngs2RackCreateWithAllocator(uintptr_t system_handle, uint32_t rack_id,
const Ngs2RackOption* option,
const Ngs2BufferAllocator* allocator,
uintptr_t* handle);
int KYTY_SYSV_ABI Ngs2RackDestroy(uintptr_t rack_handle, Ngs2ContextBufferInfo* buffer_info);
int KYTY_SYSV_ABI Ngs2RackLock(uintptr_t rack_handle);
int KYTY_SYSV_ABI Ngs2RackUnlock(uintptr_t rack_handle);
int KYTY_SYSV_ABI Ngs2RackGetVoiceHandle(uintptr_t rack_handle, uint32_t voice_id,
uintptr_t* handle);
int KYTY_SYSV_ABI Ngs2VoiceControl(uintptr_t voice_handle, const Ngs2VoiceParamHeader* param_list);
int KYTY_SYSV_ABI Ngs2VoiceRunCommands(uintptr_t voice_handle, const void* commands,
size_t num_commands);
int KYTY_SYSV_ABI Ngs2VoiceGetState(uintptr_t voice_handle, Ngs2VoiceState* state,
size_t state_size);
int KYTY_SYSV_ABI Ngs2VoiceGetStateFlags(uintptr_t voice_handle, uint32_t* state_flags);
int KYTY_SYSV_ABI Ngs2SystemRender(uintptr_t system_handle, const Ngs2RenderBufferInfo* buffer_info,
uint32_t num_buffer_info);
int KYTY_SYSV_ABI Ngs2ParseWaveformData(const void* data, size_t data_size, Ngs2WaveformInfo* info);
int KYTY_SYSV_ABI Ngs2CalcWaveformBlock(const Ngs2WaveformFormat* format, uint32_t sample_pos,
uint32_t num_samples, Ngs2WaveformBlock* block);
int KYTY_SYSV_ABI Ngs2PanInit(Ngs2PanWork* work, const float* speaker_angles, float unit_angle,
uint32_t num_speakers);
int KYTY_SYSV_ABI Ngs2PanGetVolumeMatrix(Ngs2PanWork* work, const Ngs2PanParam* params,
uint32_t num_params, uint32_t matrix_format,
float* out_volume_matrix);
int KYTY_SYSV_ABI Ngs2GeomResetListenerParam(Ngs2GeomListenerParam* out_listener_param);
int KYTY_SYSV_ABI Ngs2GeomResetSourceParam(Ngs2GeomSourceParam* out_source_param);
int KYTY_SYSV_ABI Ngs2GeomCalcListener(const Ngs2GeomListenerParam* param,
Ngs2GeomListenerWork* out_work, uint32_t flags);
int KYTY_SYSV_ABI Ngs2GeomApply(const Ngs2GeomListenerWork* listener,
const Ngs2GeomSourceParam* source, Ngs2GeomAttribute* out_attrib,
uint32_t flags);
} // namespace Ngs2
} // namespace Libs::Audio
#endif /* EMULATOR_INCLUDE_EMULATOR_AUDIO_H_ */
+319
View File
@@ -0,0 +1,319 @@
#include "libs/audio3d.h"
#include "common/assert.h"
#include "common/threads.h"
#include "kernel/semaphore.h"
#include "libs/errno.h"
#include "libs/libs.h"
#include <atomic>
namespace Libs::Audio {
namespace Audio3d {
LIB_NAME("Audio3d", "Audio3d");
namespace Semaphore = LibKernel::Semaphore;
struct Audio3dOpenParameters {
size_t size = 0x20;
uint32_t granularity = 256;
uint32_t rate = 0;
uint32_t max_objects = 512;
uint32_t queue_depth = 2;
uint32_t buffer_mode = 2;
uint32_t pad = 0;
// uint32_t num_beds;
};
struct Audio3dData {
enum class State { Empty, Ready, Play };
std::atomic<State> state = State::Empty;
};
struct Audio3dInternal {
Audio3dData* data = nullptr;
Common::Mutex* data_mutex = nullptr;
uint64_t data_delay = 0;
Semaphore::KernelSema playback_sema = nullptr;
Audio3dOpenParameters params = {};
int user_id = 0;
float late_reverb_level = 0.0f;
float downmix_spread_radius = 2.0f;
int downmix_spread_height_aware = 0;
uint32_t data_index = 0;
bool used = false;
std::atomic_bool playback_finished = false;
};
constexpr uint32_t MAX_PORTS = 4;
static Audio3dInternal g_ports[MAX_PORTS] = {};
static void playback_simulate(void* arg) {
auto* port = static_cast<Audio3dInternal*>(arg);
EXIT_IF(port == nullptr);
EXIT_IF(port->data_mutex == nullptr);
EXIT_IF(port->data == nullptr);
for (;;) {
int result = Semaphore::KernelWaitSema(port->playback_sema, 1, nullptr);
if (result != OK) {
break;
}
Audio3dData* play_data = nullptr;
port->data_mutex->Lock();
{
for (uint32_t i = 0; i < port->params.queue_depth; i++) {
uint32_t index = (port->data_index + i) % port->params.queue_depth;
if (port->data[index].state == Audio3dData::State::Play) {
play_data = &port->data[index];
break;
}
}
}
port->data_mutex->Unlock();
EXIT_IF(play_data == nullptr);
if (play_data != nullptr) {
// TODO(): Audio output is not yet implemented, so simulate audio delay
Common::Thread::SleepMicro(port->data_delay);
play_data->state = Audio3dData::State::Empty;
}
}
port->playback_finished = true;
}
int KYTY_SYSV_ABI Audio3dInitialize(int64_t reserved) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(reserved != 0);
return OK;
}
void KYTY_SYSV_ABI Audio3dGetDefaultOpenParameters(Audio3dOpenParameters* p) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(sizeof(Audio3dOpenParameters) != 0x20);
*p = Audio3dOpenParameters();
}
int KYTY_SYSV_ABI Audio3dPortOpen(int user_id, const Audio3dOpenParameters* parameters,
uint32_t* id) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(parameters == nullptr);
EXIT_NOT_IMPLEMENTED(id == nullptr);
EXIT_NOT_IMPLEMENTED(parameters->size != 0x20);
LOGF("\t user_id = %d\n"
"\t granularity = %u\n"
"\t rate = %u\n"
"\t max_objects = %u\n"
"\t queue_depth = %u\n"
"\t buffer_mode = %u\n",
user_id, parameters->granularity, parameters->rate, parameters->max_objects,
parameters->queue_depth, parameters->buffer_mode);
EXIT_NOT_IMPLEMENTED(parameters->buffer_mode != 2);
EXIT_NOT_IMPLEMENTED(user_id != 255 && user_id != 1);
uint32_t port = 0;
for (; port < MAX_PORTS; port++) {
if (!g_ports[port].used) {
break;
}
}
EXIT_NOT_IMPLEMENTED(port >= MAX_PORTS);
g_ports[port].user_id = user_id;
g_ports[port].params = *parameters;
g_ports[port].used = true;
EXIT_IF(g_ports[port].data != nullptr);
EXIT_IF(g_ports[port].data_mutex != nullptr);
EXIT_IF(g_ports[port].playback_sema != nullptr);
g_ports[port].data = new Audio3dData[parameters->queue_depth];
g_ports[port].data_index = 0;
g_ports[port].data_mutex = new Common::Mutex;
g_ports[port].data_delay = (1000000 * static_cast<uint64_t>(parameters->granularity)) / 48000;
for (uint32_t d = 0; d < parameters->queue_depth; d++) {
g_ports[port].data[d].state = Audio3dData::State::Empty;
}
int result = Semaphore::KernelCreateSema(&g_ports[port].playback_sema, "audio3d_play", 0x01, 0,
static_cast<int>(parameters->queue_depth), nullptr);
EXIT_NOT_IMPLEMENTED(result != OK);
g_ports[port].playback_finished = false;
Common::Thread playback_thread(playback_simulate, &g_ports[port]);
playback_thread.Detach();
*id = port;
return OK;
}
int KYTY_SYSV_ABI Audio3dPortSetAttribute(uint32_t port_id, uint32_t attribute_id,
const void* attribute, size_t attribute_size) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(port_id >= MAX_PORTS);
EXIT_NOT_IMPLEMENTED(!g_ports[port_id].used);
EXIT_NOT_IMPLEMENTED(attribute == nullptr);
LOGF("\t attribute_id = 0x%" PRIx32 "\n", attribute_id);
switch (attribute_id) {
case 0x10001:
EXIT_NOT_IMPLEMENTED(attribute_size != 4);
g_ports[port_id].late_reverb_level = *static_cast<const float*>(attribute);
LOGF("\t late_reverb_level = %f\n", g_ports[port_id].late_reverb_level);
break;
case 0x10002:
EXIT_NOT_IMPLEMENTED(attribute_size != 4);
g_ports[port_id].downmix_spread_radius = *static_cast<const float*>(attribute);
LOGF("\t downmix_spread_radius = %f\n", g_ports[port_id].downmix_spread_radius);
break;
case 0x10003:
EXIT_NOT_IMPLEMENTED(attribute_size != 4);
g_ports[port_id].downmix_spread_height_aware = *static_cast<const int*>(attribute);
LOGF("\t downmix_spread_height_aware = %d\n",
g_ports[port_id].downmix_spread_height_aware);
break;
default: EXIT("unknown attribute: 0x%" PRIx32 "\n", attribute_id);
}
return OK;
}
int KYTY_SYSV_ABI Audio3dPortGetQueueLevel(uint32_t port_id, uint32_t* queue_level,
uint32_t* queue_available) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(port_id >= MAX_PORTS);
EXIT_NOT_IMPLEMENTED(!g_ports[port_id].used);
EXIT_NOT_IMPLEMENTED(queue_level == nullptr && queue_available == nullptr);
auto* port = &g_ports[port_id];
uint32_t empty_num = 0;
port->data_mutex->Lock();
{
for (uint32_t i = 0; i < port->params.queue_depth; i++) {
uint32_t index = (port->data_index + i) % port->params.queue_depth;
if (port->data[index].state == Audio3dData::State::Empty) {
empty_num++;
} else {
break;
}
}
}
port->data_mutex->Unlock();
EXIT_IF(empty_num > port->params.queue_depth);
LOGF("\t queue_available = %u\n", empty_num);
if (queue_level != nullptr) {
*queue_level = port->params.queue_depth - empty_num;
}
if (queue_available != nullptr) {
*queue_available = empty_num;
}
return OK;
}
int KYTY_SYSV_ABI Audio3dPortAdvance(uint32_t port_id) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(port_id >= MAX_PORTS);
EXIT_NOT_IMPLEMENTED(!g_ports[port_id].used);
auto* port = &g_ports[port_id];
port->data_mutex->Lock();
{
uint32_t current_index = port->data_index;
uint32_t next_index = (current_index + 1) % port->params.queue_depth;
if (port->data[current_index].state == Audio3dData::State::Empty) {
port->data[current_index].state = Audio3dData::State::Ready;
}
EXIT_NOT_IMPLEMENTED(port->data[current_index].state != Audio3dData::State::Ready);
port->data_index = next_index;
LOGF("\t %u -> %u\n", current_index, next_index);
}
port->data_mutex->Unlock();
return OK;
}
int KYTY_SYSV_ABI Audio3dPortPush(uint32_t port_id, uint32_t blocking) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(port_id >= MAX_PORTS);
EXIT_NOT_IMPLEMENTED(!g_ports[port_id].used);
auto* port = &g_ports[port_id];
EXIT_NOT_IMPLEMENTED(blocking != 1);
LOGF("\t blocking = %u\n", blocking);
int data_num = 0;
Audio3dData* first_data = nullptr;
port->data_mutex->Lock();
{
first_data = port->data + port->data_index;
for (uint32_t i = 0; i < port->params.queue_depth; i++) {
uint32_t index = (port->data_index + i) % port->params.queue_depth;
if (port->data[index].state == Audio3dData::State::Ready) {
port->data[index].state = Audio3dData::State::Play;
data_num++;
}
}
}
port->data_mutex->Unlock();
LOGF("\t push num = %d\n", data_num);
if (data_num > 0) {
Semaphore::KernelSignalSema(port->playback_sema, data_num);
if (blocking == 1) {
auto wait_time = port->data_delay / 8;
while (first_data->state != Audio3dData::State::Empty) {
Common::Thread::SleepMicro(wait_time);
}
}
}
return OK;
}
} // namespace Audio3d
} // namespace Libs::Audio
+28
View File
@@ -0,0 +1,28 @@
#ifndef EMULATOR_INCLUDE_EMULATOR_LIBS_AUDIO3D_H_
#define EMULATOR_INCLUDE_EMULATOR_LIBS_AUDIO3D_H_
#include "common/abi.h"
#include "common/common.h"
namespace Libs::Audio {
namespace Audio3d {
struct Audio3dOpenParameters;
int KYTY_SYSV_ABI Audio3dInitialize(int64_t reserved);
void KYTY_SYSV_ABI Audio3dGetDefaultOpenParameters(Audio3dOpenParameters* p);
int KYTY_SYSV_ABI Audio3dPortOpen(int user_id, const Audio3dOpenParameters* parameters,
uint32_t* id);
int KYTY_SYSV_ABI Audio3dPortSetAttribute(uint32_t port_id, uint32_t attribute_id,
const void* attribute, size_t attribute_size);
int KYTY_SYSV_ABI Audio3dPortGetQueueLevel(uint32_t port_id, uint32_t* queue_level,
uint32_t* queue_available);
int KYTY_SYSV_ABI Audio3dPortAdvance(uint32_t port_id);
int KYTY_SYSV_ABI Audio3dPortPush(uint32_t port_id, uint32_t blocking);
} // namespace Audio3d
} // namespace Libs::Audio
#endif /* EMULATOR_INCLUDE_EMULATOR_LIBS_AUDIO3D_H_ */
+3
View File
@@ -1,6 +1,9 @@
#include "common/abi.h"
#include "libs/ajm/aac_decoder.h"
#include "libs/acm.h"
#include "libs/audio3d.h"
#include "libs/audio.h"
#include "libs/ngs2.h"
#include "libs/libs.h"
#include "loader/symbolDatabase.h"
+171 -55
View File
@@ -122,6 +122,20 @@ struct SaveDataParam {
uint8_t reserved[32];
};
struct SaveParamTextField {
char* data;
size_t size;
};
static SaveParamTextField param_text_field(SaveDataParam& param, uint32_t type) {
switch (type) {
case 1: return {param.title, sizeof(param.title)};
case 2: return {param.sub_title, sizeof(param.sub_title)};
case 3: return {param.detail, sizeof(param.detail)};
default: return {nullptr, 0};
}
}
struct SaveDataMountInfo {
uint64_t blocks;
uint64_t free_blocks;
@@ -345,6 +359,62 @@ static int read_save_blocks(const std::filesystem::path& directory, uint64_t* bl
return OK;
}
static int read_save_param(const std::filesystem::path& path, SaveDataParam* param) {
std::error_code error;
const bool exists = std::filesystem::exists(path, error);
if (error) {
return SAVE_DATA_ERROR_INTERNAL;
}
if (!exists) {
return SAVE_DATA_ERROR_NOT_FOUND;
}
Common::File file(path, Common::File::Mode::Read);
if (file.IsInvalid()) {
return SAVE_DATA_ERROR_INTERNAL;
}
if (file.Size() != sizeof(*param)) {
return SAVE_DATA_ERROR_BROKEN;
}
uint32_t read = 0;
file.Read(param, sizeof(*param), &read);
return read == sizeof(*param) ? OK : SAVE_DATA_ERROR_BROKEN;
}
static std::filesystem::path save_param_path(const std::filesystem::path& directory) {
return directory / "sce_sys" / "param.bin";
}
static int64_t newest_save_time(const std::filesystem::path& directory) {
int64_t mtime = 0;
std::error_code error;
for (std::filesystem::recursive_directory_iterator it(
directory, std::filesystem::directory_options::skip_permission_denied, error),
end;
it != end; it.increment(error)) {
if (error) {
error.clear();
continue;
}
if (it->is_regular_file(error) && !error) {
const auto time = Common::File::GetLastWriteTimeUTC(it->path());
if (!time.IsInvalid()) {
mtime = std::max(mtime, static_cast<int64_t>(time.ToUnix()));
}
}
error.clear();
}
return mtime;
}
static int load_save_param(const std::filesystem::path& directory, SaveDataParam* param) {
*param = {};
const int status = read_save_param(save_param_path(directory), param);
if (status == OK) {
param->mtime = newest_save_time(directory);
}
return status;
}
static int save_memory(const std::filesystem::path& directory, const SaveDataMemory& memory) {
std::error_code error;
std::filesystem::create_directories(directory, error);
@@ -385,23 +455,7 @@ static int load_memory(const std::filesystem::path& directory, SaveDataMemory* m
return SAVE_DATA_ERROR_BROKEN;
}
file.Close();
const bool has_param = std::filesystem::exists(directory / "param.bin", error);
if (error) {
return SAVE_DATA_ERROR_INTERNAL;
}
if (has_param) {
if (!file.Open(directory / "param.bin", Common::File::Mode::Read)) {
return SAVE_DATA_ERROR_INTERNAL;
}
if (file.Size() != sizeof(memory->param)) {
return SAVE_DATA_ERROR_BROKEN;
}
file.Read(&memory->param, sizeof(memory->param), &read);
if (read != sizeof(memory->param)) {
return SAVE_DATA_ERROR_BROKEN;
}
}
return OK;
return read_save_param(directory / "param.bin", &memory->param);
}
static void queue_save_data_event(uint32_t type, int32_t user_id,
@@ -451,11 +505,11 @@ static bool dir_name_match(const char* str, const char* pattern) {
return *str == '\0' && *pattern == '\0';
}
static int mount_save_data(int slot, std::string_view dir_name, const std::string& directory,
static int mount_save_data(int slot, const std::filesystem::path& directory,
uint32_t status, SaveDataMountResult* result) {
const std::string mount_point = SaveDataMountSlots::MountPoint(static_cast<size_t>(slot));
LibKernel::FileSystem::Mount(directory, mount_point);
g_mount_slots.Mount(static_cast<size_t>(slot), dir_name);
g_mount_slots.Mount(static_cast<size_t>(slot), directory);
std::snprintf(result->mount_point.data, sizeof(result->mount_point.data), "%s",
mount_point.c_str());
result->required_blocks = 0;
@@ -542,7 +596,8 @@ int KYTY_SYSV_ABI SaveDataDirNameSearch(const SaveDataDirNameSearchCond* cond,
if (Common::File::IsDirectoryExisting(root)) {
for (const auto& entry: Common::File::GetDirEntries(root)) {
if (!entry.is_file && entry.name != "." && entry.name != ".." &&
!entry.name.starts_with("sce_") && valid_path_component(entry.name)) {
!entry.name.starts_with("sce_") && valid_path_component(entry.name) &&
Common::File::IsFileExisting(save_param_path(root / entry.name))) {
if (cond->dir_name == nullptr || cond->dir_name->data[0] == '\0' ||
dir_name_match(Common::ToLower(entry.name).c_str(),
Common::ToLower(std::string(cond->dir_name->data)).c_str())) {
@@ -574,7 +629,10 @@ int KYTY_SYSV_ABI SaveDataDirNameSearch(const SaveDataDirNameSearchCond* cond,
std::snprintf(result->dir_names[i].data, sizeof(result->dir_names[i].data), "%s",
dir_list[i].c_str());
if (result->params != nullptr) {
result->params[i] = {};
const int status = load_save_param(root / dir_list[i], &result->params[i]);
if (status != OK) {
return status;
}
}
if (result->infos != nullptr) {
auto& info = result->infos[i];
@@ -615,12 +673,12 @@ int KYTY_SYSV_ABI SaveDataMount3(const SaveDataMount3* mount, SaveDataMountResul
Common::LockGuard lock(g_mount_mutex);
const std::string dir_name = mount->dir_name->data;
const auto mount_dir = save_directory(get_title_id(), dir_name, mount->user_id).string();
const auto mount_dir = save_directory(get_title_id(), dir_name, mount->user_id);
const bool create = ((mount->mount_mode & 4u) != 0);
const bool create2 = ((mount->mount_mode & 32u) != 0);
const bool open = (!create && !create2 && ((mount->mount_mode & 3u) != 0));
const int slot = g_mount_slots.FindAvailable(dir_name);
const int slot = g_mount_slots.FindAvailable(mount_dir);
if (slot == SaveDataMountSlots::BUSY) {
return SAVE_DATA_ERROR_BUSY;
}
@@ -645,7 +703,7 @@ int KYTY_SYSV_ABI SaveDataMount3(const SaveDataMount3* mount, SaveDataMountResul
if (mount->blocks < SAVE_DATA_BLOCKS_MIN || mount->blocks > SAVE_DATA_BLOCKS_MAX) {
return SAVE_DATA_ERROR_PARAMETER;
}
const auto metadata = std::filesystem::path(mount_dir) / "sce_sys";
const auto metadata = mount_dir / "sce_sys";
std::error_code error;
std::filesystem::create_directories(metadata, error);
if (error) {
@@ -657,10 +715,17 @@ int KYTY_SYSV_ABI SaveDataMount3(const SaveDataMount3* mount, SaveDataMountResul
std::filesystem::remove_all(mount_dir, error);
return status;
}
const SaveDataParam initial {};
const int param_status = write_save_file(save_param_path(mount_dir), &initial,
sizeof(initial));
if (param_status != OK) {
std::filesystem::remove_all(mount_dir, error);
return param_status;
}
created = true;
}
return mount_save_data(slot, dir_name, mount_dir, created ? 1u : 0u, mount_result);
return mount_save_data(slot, mount_dir, created ? 1u : 0u, mount_result);
}
int KYTY_SYSV_ABI SaveDataSetupSaveDataMemory2(const SaveDataMemorySetup2* setup_param,
@@ -686,7 +751,7 @@ int KYTY_SYSV_ABI SaveDataSetupSaveDataMemory2(const SaveDataMemorySetup2* setup
Common::LockGuard lock(g_mount_mutex);
const auto directory = memory_directory(setup_param->user_id, setup_param->slot_id);
if (g_save_data_memory.contains(directory) ||
g_mount_slots.FindAvailable(memory_dir_name(setup_param->slot_id)) ==
g_mount_slots.FindAvailable(directory) ==
SaveDataMountSlots::BUSY) {
return SAVE_DATA_ERROR_BUSY;
}
@@ -848,8 +913,8 @@ int KYTY_SYSV_ABI SaveDataTransferringMount(const SaveDataTransferringMount* mou
Common::LockGuard lock(g_mount_mutex);
const std::string dir_name = mount->dir_name->data;
const auto mount_dir = save_directory(mount->title_id->data, dir_name, mount->user_id).string();
const int slot = g_mount_slots.FindAvailable(dir_name);
const auto mount_dir = save_directory(mount->title_id->data, dir_name, mount->user_id);
const int slot = g_mount_slots.FindAvailable(mount_dir);
if (slot == SaveDataMountSlots::BUSY) {
return SAVE_DATA_ERROR_BUSY;
}
@@ -861,7 +926,7 @@ int KYTY_SYSV_ABI SaveDataTransferringMount(const SaveDataTransferringMount* mou
return SAVE_DATA_ERROR_NOT_FOUND;
}
return mount_save_data(slot, dir_name, mount_dir, 0, mount_result);
return mount_save_data(slot, mount_dir, 0, mount_result);
}
int KYTY_SYSV_ABI SaveDataUmount2(uint32_t mode, const SaveDataMountPoint* mount_point) {
@@ -946,7 +1011,7 @@ int KYTY_SYSV_ABI SaveDataDelete(const SaveDataDelete* del) {
Common::LockGuard lock(g_mount_mutex);
const auto dir = save_directory(del->title_id != nullptr ? del->title_id->data : get_title_id(),
del->dir_name->data, del->user_id);
if (g_mount_slots.FindAvailable(del->dir_name->data) == SaveDataMountSlots::BUSY ||
if (g_mount_slots.FindAvailable(dir) == SaveDataMountSlots::BUSY ||
g_save_data_memory.contains(dir)) {
return SAVE_DATA_ERROR_BUSY;
}
@@ -960,7 +1025,8 @@ int KYTY_SYSV_ABI SaveDataGetParam(const SaveDataMountPoint* mount_point, uint32
void* param_buf, size_t param_buf_size, size_t* got_size) {
PRINT_NAME();
if (mount_point == nullptr || param_buf == nullptr) {
if (mount_point == nullptr || param_buf == nullptr || param_type > 5 ||
std::memchr(mount_point->data, 0, sizeof(mount_point->data)) == nullptr) {
return SAVE_DATA_ERROR_PARAMETER;
}
@@ -968,21 +1034,44 @@ int KYTY_SYSV_ABI SaveDataGetParam(const SaveDataMountPoint* mount_point, uint32
"\t param_type = %u\n"
"\t param_buf_size = %" PRIu64 "\n",
mount_point->data, param_type, param_buf_size);
if (param_type == 0) {
if (param_buf_size < sizeof(SaveDataParam)) {
return SAVE_DATA_ERROR_PARAMETER;
}
std::memset(param_buf, 0, sizeof(SaveDataParam));
if (got_size != nullptr) {
*got_size = sizeof(SaveDataParam);
}
return OK;
Common::LockGuard lock(g_mount_mutex);
const int slot = g_mount_slots.Find(mount_point->data);
if (slot == SaveDataMountSlots::FULL) {
return SAVE_DATA_ERROR_NOT_MOUNTED;
}
std::memset(param_buf, 0, param_buf_size);
SaveDataParam param {};
const int status = load_save_param(g_mount_slots.Directory(static_cast<size_t>(slot)), &param);
if (status != OK) {
return status;
}
size_t size = 0;
if (param_type == 0) {
size = sizeof(param);
if (param_buf_size < size) {
return SAVE_DATA_ERROR_PARAMETER;
}
std::memcpy(param_buf, &param, size);
} else if (param_type == 4 || param_type == 5) {
size = param_type == 4 ? sizeof(param.user_param) : sizeof(param.mtime);
if (param_buf_size < size) {
return SAVE_DATA_ERROR_PARAMETER;
}
std::memcpy(param_buf, param_type == 4 ? static_cast<const void*>(&param.user_param)
: static_cast<const void*>(&param.mtime), size);
} else {
if (param_buf_size == 0) {
return SAVE_DATA_ERROR_PARAMETER;
}
const auto field = param_text_field(param, param_type);
size = std::min(static_cast<size_t>(
std::find(field.data, field.data + field.size, '\0') - field.data),
param_buf_size - 1);
std::memcpy(param_buf, field.data, size);
static_cast<char*>(param_buf)[size++] = '\0';
}
if (got_size != nullptr) {
*got_size = param_buf_size;
*got_size = size;
}
return OK;
@@ -1035,7 +1124,7 @@ int KYTY_SYSV_ABI SaveDataSyncSaveDataMemory(const SaveDataMemorySync* sync_para
return SAVE_DATA_ERROR_MEMORY_NOT_READY;
}
const auto dir_name = memory_dir_name(sync_param->slot_id);
if (g_mount_slots.FindAvailable(dir_name) == SaveDataMountSlots::BUSY) {
if (g_mount_slots.FindAvailable(it->first) == SaveDataMountSlots::BUSY) {
return SAVE_DATA_ERROR_BUSY;
}
const int status = save_memory(it->first, it->second);
@@ -1108,26 +1197,53 @@ int KYTY_SYSV_ABI SaveDataSetParam(const SaveDataMountPoint* mount_point, uint32
const void* param_buf, size_t param_buf_size) {
PRINT_NAME();
EXIT_NOT_IMPLEMENTED(mount_point == nullptr);
if (mount_point == nullptr || param_buf == nullptr || param_type > 4 ||
std::memchr(mount_point->data, 0, sizeof(mount_point->data)) == nullptr ||
(param_type == 0 && param_buf_size < sizeof(SaveDataParam)) ||
(param_type == 4 && param_buf_size < sizeof(uint32_t)) ||
(param_type >= 1 && param_type <= 3 && param_buf_size == 0)) {
return SAVE_DATA_ERROR_PARAMETER;
}
LOGF("\t mount_point = %s\n"
"\t param_type = %u\n"
"\t param_buf_size = %" PRIu64 "\n",
mount_point->data, param_type, param_buf_size);
Common::LockGuard lock(g_mount_mutex);
const int slot = g_mount_slots.Find(mount_point->data);
if (slot == SaveDataMountSlots::FULL) {
return SAVE_DATA_ERROR_NOT_MOUNTED;
}
if (param_type == 0) {
const auto* p = static_cast<const SaveDataParam*>(param_buf);
LOGF("\t title = %s\n"
"\t sub_title = %s\n"
"\t detail = %s\n"
LOGF("\t title = %.128s\n"
"\t sub_title = %.128s\n"
"\t detail = %.1024s\n"
"\t user_param = %u\n",
p->title, p->sub_title, p->detail, p->user_param);
} else {
LOGF("\t unsupported param_type, accepting as no-op\n");
return write_save_file(save_param_path(g_mount_slots.Directory(static_cast<size_t>(slot))),
p, sizeof(*p));
}
return OK;
SaveDataParam param {};
const auto path = save_param_path(g_mount_slots.Directory(static_cast<size_t>(slot)));
const int status = read_save_param(path, &param);
if (status != OK) {
return status;
}
if (param_type == 4) {
std::memcpy(&param.user_param, param_buf, sizeof(param.user_param));
} else {
const auto field = param_text_field(param, param_type);
const auto* text = static_cast<const char*>(param_buf);
const size_t length = std::min(param_buf_size, field.size - 1);
const auto* end = static_cast<const char*>(std::memchr(text, '\0', length));
const size_t count = end != nullptr ? static_cast<size_t>(end - text) : length;
std::memset(field.data, 0, field.size);
std::memcpy(field.data, text, count);
}
return write_save_file(path, &param, sizeof(param));
}
int KYTY_SYSV_ABI SaveDataGetMountInfo(const SaveDataMountPoint* mount_point,
@@ -1139,12 +1255,12 @@ int KYTY_SYSV_ABI SaveDataGetMountInfo(const SaveDataMountPoint* mount_point,
return SAVE_DATA_ERROR_PARAMETER;
}
Common::LockGuard lock(g_mount_mutex);
if (g_mount_slots.Find(mount_point->data) == SaveDataMountSlots::FULL) {
const int slot = g_mount_slots.Find(mount_point->data);
if (slot == SaveDataMountSlots::FULL) {
return SAVE_DATA_ERROR_NOT_MOUNTED;
}
uint64_t blocks = 0;
const int status = read_save_blocks(
LibKernel::FileSystem::GetRealFilename(mount_point->data), &blocks);
const int status = read_save_blocks(g_mount_slots.Directory(static_cast<size_t>(slot)), &blocks);
if (status != OK) {
return status;
}
+2091
View File
File diff suppressed because it is too large. Load diff
+83
View File
@@ -0,0 +1,83 @@
#ifndef EMULATOR_INCLUDE_EMULATOR_LIBS_NGS2_H_
#define EMULATOR_INCLUDE_EMULATOR_LIBS_NGS2_H_
#include "common/abi.h"
#include "common/common.h"
namespace Libs::Audio {
namespace Ngs2 {
struct Ngs2SystemOption;
struct Ngs2SystemInfo;
struct Ngs2RackOption;
struct Ngs2BufferAllocator;
struct Ngs2VoiceParamHeader;
struct Ngs2RenderBufferInfo;
struct Ngs2ContextBufferInfo;
struct Ngs2VoiceState;
struct Ngs2WaveformFormat;
struct Ngs2WaveformBlock;
struct Ngs2WaveformInfo;
struct Ngs2PanWork;
struct Ngs2PanParam;
struct Ngs2GeomListenerParam;
struct Ngs2GeomListenerWork;
struct Ngs2GeomSourceParam;
struct Ngs2GeomAttribute;
int KYTY_SYSV_ABI Ngs2SystemResetOption(Ngs2SystemOption* option);
int KYTY_SYSV_ABI Ngs2SystemQueryBufferSize(const Ngs2SystemOption* option,
Ngs2ContextBufferInfo* buffer_info);
int KYTY_SYSV_ABI Ngs2SystemCreate(const Ngs2SystemOption* option,
const Ngs2ContextBufferInfo* buffer_info, uintptr_t* handle);
int KYTY_SYSV_ABI Ngs2SystemGetInfo(uintptr_t system_handle, Ngs2SystemInfo* info,
size_t info_size);
int KYTY_SYSV_ABI Ngs2SystemSetGrainSamples(uintptr_t system_handle, uint32_t num_samples);
int KYTY_SYSV_ABI Ngs2RackQueryBufferSize(uint32_t rack_id, const Ngs2RackOption* option,
Ngs2ContextBufferInfo* buffer_info);
int KYTY_SYSV_ABI Ngs2RackCreate(uintptr_t system_handle, uint32_t rack_id,
const Ngs2RackOption* option,
const Ngs2ContextBufferInfo* buffer_info, uintptr_t* handle);
int KYTY_SYSV_ABI Ngs2SystemCreateWithAllocator(const Ngs2SystemOption* option,
const Ngs2BufferAllocator* allocator,
uintptr_t* handle);
int KYTY_SYSV_ABI Ngs2SystemDestroy(uintptr_t system_handle, Ngs2ContextBufferInfo* buffer_info);
int KYTY_SYSV_ABI Ngs2RackCreateWithAllocator(uintptr_t system_handle, uint32_t rack_id,
const Ngs2RackOption* option,
const Ngs2BufferAllocator* allocator,
uintptr_t* handle);
int KYTY_SYSV_ABI Ngs2RackDestroy(uintptr_t rack_handle, Ngs2ContextBufferInfo* buffer_info);
int KYTY_SYSV_ABI Ngs2RackLock(uintptr_t rack_handle);
int KYTY_SYSV_ABI Ngs2RackUnlock(uintptr_t rack_handle);
int KYTY_SYSV_ABI Ngs2RackGetVoiceHandle(uintptr_t rack_handle, uint32_t voice_id,
uintptr_t* handle);
int KYTY_SYSV_ABI Ngs2VoiceControl(uintptr_t voice_handle, const Ngs2VoiceParamHeader* param_list);
int KYTY_SYSV_ABI Ngs2VoiceRunCommands(uintptr_t voice_handle, const void* commands,
size_t num_commands);
int KYTY_SYSV_ABI Ngs2VoiceGetState(uintptr_t voice_handle, Ngs2VoiceState* state,
size_t state_size);
int KYTY_SYSV_ABI Ngs2VoiceGetStateFlags(uintptr_t voice_handle, uint32_t* state_flags);
int KYTY_SYSV_ABI Ngs2SystemRender(uintptr_t system_handle, const Ngs2RenderBufferInfo* buffer_info,
uint32_t num_buffer_info);
int KYTY_SYSV_ABI Ngs2ParseWaveformData(const void* data, size_t data_size, Ngs2WaveformInfo* info);
int KYTY_SYSV_ABI Ngs2CalcWaveformBlock(const Ngs2WaveformFormat* format, uint32_t sample_pos,
uint32_t num_samples, Ngs2WaveformBlock* block);
int KYTY_SYSV_ABI Ngs2PanInit(Ngs2PanWork* work, const float* speaker_angles, float unit_angle,
uint32_t num_speakers);
int KYTY_SYSV_ABI Ngs2PanGetVolumeMatrix(Ngs2PanWork* work, const Ngs2PanParam* params,
uint32_t num_params, uint32_t matrix_format,
float* out_volume_matrix);
int KYTY_SYSV_ABI Ngs2GeomResetListenerParam(Ngs2GeomListenerParam* out_listener_param);
int KYTY_SYSV_ABI Ngs2GeomResetSourceParam(Ngs2GeomSourceParam* out_source_param);
int KYTY_SYSV_ABI Ngs2GeomCalcListener(const Ngs2GeomListenerParam* param,
Ngs2GeomListenerWork* out_work, uint32_t flags);
int KYTY_SYSV_ABI Ngs2GeomApply(const Ngs2GeomListenerWork* listener,
const Ngs2GeomSourceParam* source, Ngs2GeomAttribute* out_attrib,
uint32_t flags);
} // namespace Ngs2
} // namespace Libs::Audio
#endif /* EMULATOR_INCLUDE_EMULATOR_LIBS_NGS2_H_ */
+16 -5
View File
@@ -2,9 +2,11 @@
#define EMULATOR_INCLUDE_EMULATOR_LIBS_SAVEDATAMOUNTSLOTS_H_
#include <array>
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <system_error>
namespace Libs::SaveData {
@@ -14,12 +16,15 @@ public:
static constexpr int FULL = -1;
static constexpr size_t COUNT = 16;
[[nodiscard]] int FindAvailable(std::string_view directory) const {
[[nodiscard]] int FindAvailable(const std::filesystem::path& directory) const {
int available = FULL;
for (size_t index = 0; index < m_directories.size(); index++) {
const auto& mounted = m_directories[index];
if (mounted == directory) {
return BUSY;
if (mounted.has_value()) {
std::error_code error;
if (*mounted == directory || std::filesystem::equivalent(*mounted, directory, error)) {
return BUSY;
}
}
if (!mounted.has_value() && available == FULL) {
available = static_cast<int>(index);
@@ -28,7 +33,13 @@ public:
return available;
}
void Mount(size_t slot, std::string_view directory) { m_directories[slot] = directory; }
void Mount(size_t slot, const std::filesystem::path& directory) {
m_directories[slot] = directory;
}
[[nodiscard]] const std::filesystem::path& Directory(size_t slot) const {
return *m_directories[slot];
}
void Release(size_t slot) {
if (slot < m_directories.size()) {
@@ -59,7 +70,7 @@ public:
}
private:
std::array<std::optional<std::string>, COUNT> m_directories;
std::array<std::optional<std::filesystem::path>, COUNT> m_directories;
};
} // namespace Libs::SaveData
+388 -1
View File
@@ -614,7 +614,8 @@ void TestGuardedDirectImageTable() {
const auto &indirect = fixture.program.descriptor_sources[source].indirect_image;
Check(indirect && indirect->material_source == UINT32_MAX &&
indirect->selector_stride == 0u && indirect->table_offset == 344u &&
indirect->key_count == 32u &&
indirect->key_count.Resolve().IsImmediate() &&
indirect->key_count.Resolve().U32() == 32u &&
fixture.program.descriptor_sources[indirect->table_source].dword_count == 2u,
"guarded direct image table lost its pointer or proven selector range");
return ExtractResourcePlan(fixture.program);
@@ -699,6 +700,390 @@ void TestGuardedDirectImageTable() {
"batched descriptor read crossed the 48-bit endpoint");
}
void TestBoundedComputeImageLoop() {
namespace CFG = Libs::Graphics::ShaderRecompiler::CFG;
enum class Variant {
Bounded, WrongGuard, EntryBypass, ExitBypass, GuardBlock, WrongStep
};
const auto make_plan = [](Variant variant) {
Fixture fixture;
auto *entry = fixture.block;
auto *header = fixture.AddBlock();
auto *body = fixture.AddBlock();
auto *latch = fixture.AddBlock();
auto *exit = fixture.AddBlock();
entry->AddBranch(header);
header->AddBranch(exit);
header->AddBranch(body);
body->AddBranch(latch);
latch->AddBranch(header);
if (variant == Variant::EntryBypass) entry->AddBranch(body);
if (variant == Variant::ExitBypass) exit->AddBranch(body);
fixture.program.block_info[0].terminator = {
.kind = variant == Variant::EntryBypass ? CFG::TerminatorKind::ConditionalBranch
: CFG::TerminatorKind::Branch,
.true_block = 1u, .false_block = 2u};
fixture.program.block_info[1].terminator = {
.kind = CFG::TerminatorKind::ConditionalBranch,
.true_block = 4u, .false_block = 2u};
fixture.program.block_info[2].terminator = {
.kind = CFG::TerminatorKind::Branch, .true_block = 3u};
fixture.program.block_info[3].terminator = {
.kind = CFG::TerminatorKind::Branch, .true_block = 1u};
fixture.program.block_info[4].terminator = {
.kind = variant == Variant::ExitBypass ? CFG::TerminatorKind::Branch
: CFG::TerminatorKind::Return,
.true_block = 2u};
auto &phi = header->AppendNewInst(ValueOpcode::Phi, {},
static_cast<uint64_t>(Type::U32));
const auto key = Value(&phi);
const auto count = fixture.UserData(2);
const auto in_range = fixture.Emit(ValueOpcode::SLessThan32,
{variant == Variant::WrongGuard
? Value(0u) : key,
count}, 0, header);
const auto allowed = fixture.Emit(ValueOpcode::LogicalAnd,
{in_range, Value(true)}, 0, header);
fixture.program.block_info[1].condition =
fixture.Emit(ValueOpcode::LogicalNot, {allowed}, 0, header);
const auto step = fixture.Emit(ValueOpcode::IAdd32,
{key, Value(variant == Variant::WrongStep ? 2u : 1u)},
0, latch);
phi.AddPhiOperand(entry, Value(0u));
phi.AddPhiOperand(latch, step);
fixture.block = variant == Variant::GuardBlock ? header : body;
const auto table = fixture.Address(fixture.UserData(0), fixture.UserData(1));
const auto offset = fixture.Emit(ValueOpcode::IAdd32,
{fixture.Emit(ValueOpcode::ShiftLeftLogical32, {key, Value(5u)}),
Value(0x6b0u)});
std::array<Value, 8> words;
for (uint32_t word = 0; word < words.size(); ++word) {
MemoryInfo memory;
memory.kind = ResourceKind::ScalarAddress;
memory.offset = word * sizeof(uint32_t);
words[word] = fixture.Emit(
ValueOpcode::LoadAddressU32,
{table, offset, Value(0u), Value(true)},
fixture.AddMemory(memory, 0x29c));
}
const auto image = fixture.Image(words, 0x29c);
const auto sampler = fixture.Sampler({Value(0u), Value(0u), Value(0u), Value(0u)});
MemoryInfo sample;
sample.kind = ResourceKind::Image;
sample.image_dimension = Decoder::ImageDimension::Dim2D;
fixture.Emit(ValueOpcode::ImageSampleRaw,
{image, sampler, fixture.ImageAddress()},
fixture.AddMemory(sample, 0x29c));
fixture.PlanAndTrack();
const auto source = fixture.program.info.images[0].source;
const auto &indirect = fixture.program.descriptor_sources[source].indirect_image;
Check(indirect && indirect->material_source == UINT32_MAX &&
indirect->table_offset == 0x6b0u &&
indirect->key_count.Resolve() == count.Resolve(),
"bounded compute loop lost its runtime image count");
return ExtractResourcePlan(fixture.program);
};
auto plan = make_plan(Variant::Bounded);
CheckFatal([&] { make_plan(Variant::WrongGuard); },
"not a valid runtime value",
"compute image loop accepted an unrelated guard");
CheckFatal([&] { make_plan(Variant::EntryBypass); },
"not a valid runtime value",
"compute image loop accepted an entry bypass");
CheckFatal([&] { make_plan(Variant::ExitBypass); },
"not a valid runtime value",
"compute image loop accepted an exit bypass");
CheckFatal([&] { make_plan(Variant::GuardBlock); },
"not a valid runtime value",
"compute image loop accepted a descriptor read before the guard");
CheckFatal([&] { make_plan(Variant::WrongStep); },
"not a valid runtime value",
"compute image loop accepted a two-step induction");
LinearTestMemory memory;
const auto table = 0x1800u + 0x6b0u;
for (uint32_t key = 0; key < 3u; ++key) {
const auto word = (table - memory.base) / 4u + key * 8u;
memory.words[word] = 0x100u + key;
memory.words[word + 1u] = static_cast<uint32_t>(
Libs::Graphics::Prospero::BufferFormat::k32_32_32_32Float) << 20u;
memory.words[word + 3u] = Libs::Graphics::DstSel(4, 5, 6, 7) |
(static_cast<uint32_t>(Libs::Graphics::Prospero::ImageType::kColor2D) << 28u);
}
std::array<uint32_t, 3> user_data{0x1800u, 0u, 2u};
SrtRuntime runtime{.user_data = user_data,
.read_memory = ReadLinearTestMemory,
.userdata = &memory,
.read_specialization_memory = ReadLinearTestMemory};
ResourceSnapshot snapshot;
ResourceSpecialization specialization;
for (const uint32_t count : {2u, 3u, 2u}) {
user_data[2] = count;
Check(MaterializeResources(plan, runtime, snapshot, specialization) &&
snapshot.images.size() == count &&
specialization.images.size() == count &&
snapshot.flattened_srt[
specialization.images[0].indirect_mapping_offset] == count &&
snapshot.images.back().dwords[0] == 0x100u + count - 1u,
"compute image table did not refresh for a changed loop bound");
}
for (const uint32_t count : {0u, UINT32_MAX}) {
user_data[2] = count;
Check(MaterializeResources(plan, runtime, snapshot, specialization) &&
snapshot.images.size() == 1u &&
specialization.images.size() == 1u &&
std::ranges::all_of(snapshot.images[0].dwords,
[](uint32_t word) { return word == 0u; }) &&
specialization.images[0].indirect_root ==
ImageResource::NoIndirectImage &&
specialization.images[0].indirect_search_iterations == 0u,
"empty compute loop bound retained unreachable image candidates");
}
user_data[2] = 65537u;
Check(!MaterializeResources(plan, runtime, snapshot, specialization),
"oversized compute loop bound was accepted for image enumeration");
}
void TestUniformizedMaterialImageKeys() {
namespace CFG = Libs::Graphics::ShaderRecompiler::CFG;
const auto make_plan = [](bool wrong_update, bool wrong_equality) {
Fixture fixture;
auto *entry = fixture.block;
auto *header = fixture.AddBlock();
auto *inactive = fixture.AddBlock();
auto *sentinel = fixture.AddBlock();
auto *bit = fixture.AddBlock();
auto *merge = fixture.AddBlock();
auto *choose = fixture.AddBlock();
auto *sample = fixture.AddBlock();
auto *done = fixture.AddBlock();
entry->AddBranch(header);
header->AddBranch(inactive);
inactive->AddBranch(merge);
inactive->AddBranch(sentinel);
sentinel->AddBranch(merge);
sentinel->AddBranch(bit);
bit->AddBranch(header);
bit->AddBranch(merge);
merge->AddBranch(choose);
choose->AddBranch(sample);
choose->AddBranch(done);
sample->AddBranch(done);
fixture.program.block_info[0].terminator = {
.kind = CFG::TerminatorKind::Branch, .true_block = 1u};
fixture.program.block_info[1].terminator = {
.kind = CFG::TerminatorKind::Branch, .true_block = 2u};
const auto active_on_entry = fixture.Emit(
ValueOpcode::INotEqual32, {fixture.UserData(5u), Value(0u)}, 0, entry);
auto &active_phi = header->AppendNewInst(
ValueOpcode::Phi, {}, static_cast<uint64_t>(Type::U1));
auto &mask_phi = header->AppendNewInst(
ValueOpcode::Phi, {}, static_cast<uint64_t>(Type::U32));
const auto mask = Value(&mask_phi);
const auto active = Value(&active_phi);
fixture.program.block_info[2].condition = fixture.Emit(
ValueOpcode::LogicalNot, {active}, 0, inactive);
fixture.program.block_info[2].terminator = {
.kind = CFG::TerminatorKind::ConditionalBranch,
.true_block = 5u, .false_block = 3u};
const auto nonzero = fixture.Emit(
ValueOpcode::INotEqual32, {Value(0u), mask}, 0, sentinel);
const auto bit_guard = fixture.Emit(
ValueOpcode::LogicalAnd, {active, nonzero}, 0, sentinel);
fixture.program.block_info[3].condition = fixture.Emit(
ValueOpcode::LogicalNot, {bit_guard}, 0, sentinel);
fixture.program.block_info[3].terminator = {
.kind = CFG::TerminatorKind::ConditionalBranch,
.true_block = 5u, .false_block = 4u};
const auto first = fixture.Emit(ValueOpcode::FindILsb32, {mask}, 0, bit);
const auto position = fixture.Emit(
ValueOpcode::BitwiseAnd32, {first, Value(31u)}, 0, bit);
const auto one_bit = fixture.Emit(
ValueOpcode::ShiftLeftLogical32, {Value(1u), position}, 0, bit);
const auto cleared = fixture.Emit(
wrong_update ? ValueOpcode::BitwiseOr32 : ValueOpcode::BitwiseXor32,
{mask, one_bit}, 0, bit);
const auto continuation = fixture.Emit(
ValueOpcode::LogicalAnd, {bit_guard, active_on_entry}, 0, bit);
fixture.program.block_info[4].condition = continuation;
fixture.program.block_info[4].terminator = {
.kind = CFG::TerminatorKind::ConditionalBranch,
.true_block = 1u, .false_block = 5u};
active_phi.AddPhiOperand(entry, active_on_entry);
active_phi.AddPhiOperand(bit, continuation);
mask_phi.AddPhiOperand(entry, fixture.UserData(4u));
mask_phi.AddPhiOperand(bit, cleared);
const auto arbitrary = fixture.UserData(6u);
const auto sentinel_index = fixture.Emit(
ValueOpcode::SelectU32, {active, Value(32u), arbitrary}, 0, sentinel);
const auto bit_index = fixture.Emit(
ValueOpcode::SelectU32, {bit_guard, first, sentinel_index}, 0, bit);
auto &index_phi = merge->AppendNewInst(
ValueOpcode::Phi, {}, static_cast<uint64_t>(Type::U32));
index_phi.AddPhiOperand(inactive, arbitrary);
index_phi.AddPhiOperand(sentinel, sentinel_index);
index_phi.AddPhiOperand(bit, bit_index);
const auto index = Value(&index_phi);
const auto below = fixture.Emit(
ValueOpcode::SGreaterThan32, {Value(32u), index}, 0, merge);
const auto material_guard = fixture.Emit(
ValueOpcode::LogicalAnd, {active_on_entry, below}, 0, merge);
fixture.program.block_info[5].terminator = {
.kind = CFG::TerminatorKind::Branch, .true_block = 6u};
const auto scaled = fixture.Emit(
ValueOpcode::ShiftLeftLogical32, {index, Value(4u)}, 0, choose);
const auto selected_scale = fixture.Emit(
ValueOpcode::SelectU32, {material_guard, scaled, Value(0u)}, 0, choose);
const auto times_eight = fixture.Emit(
ValueOpcode::ShiftLeftLogical32, {selected_scale, Value(3u)}, 0, choose);
const auto times_nine = fixture.Emit(
ValueOpcode::IAdd32, {times_eight, selected_scale}, 0, choose);
const auto material_offset = fixture.Emit(
ValueOpcode::SelectU32,
{material_guard,
fixture.Emit(ValueOpcode::IAdd32,
{times_nine, Value(0xc00u)}, 0, choose),
times_nine}, 0, choose);
const auto base = fixture.Address(fixture.UserData(0u), fixture.UserData(1u));
MemoryInfo material_memory;
material_memory.kind = ResourceKind::Global;
const auto loaded = fixture.Emit(
ValueOpcode::LoadAddressU32,
{base, material_offset, Value(0u), material_guard},
fixture.AddMemory(material_memory, 0x1a88u), choose);
const auto local = fixture.Emit(
ValueOpcode::SelectU32, {material_guard, loaded, arbitrary}, 0, choose);
const auto key = fixture.Emit(
ValueOpcode::ReadLane, {local, Value(0u)}, 0, choose);
const auto compared = fixture.Emit(
ValueOpcode::IEqual32,
{key, wrong_equality ? arbitrary : local}, 0, choose);
const auto sample_guard = fixture.Emit(
ValueOpcode::LogicalAnd, {material_guard, compared}, 0, choose);
fixture.program.block_info[6].condition = fixture.Emit(
ValueOpcode::LogicalNot, {sample_guard}, 0, choose);
fixture.program.block_info[6].terminator = {
.kind = CFG::TerminatorKind::ConditionalBranch,
.true_block = 8u, .false_block = 7u};
const auto table_offset = fixture.Emit(
ValueOpcode::IAdd32,
{fixture.Emit(ValueOpcode::ShiftLeftLogical32,
{key, Value(5u)}, 0, sample), Value(0x20e0u)}, 0, sample);
std::array<Value, 8> words;
for (uint32_t word = 0; word < words.size(); ++word) {
MemoryInfo memory;
memory.kind = ResourceKind::ScalarAddress;
memory.offset = word * 4u;
words[word] = fixture.Emit(
ValueOpcode::LoadAddressU32,
{base, table_offset, Value(0u), Value(true)},
fixture.AddMemory(memory, 0x1accu), sample);
}
const auto image = fixture.Emit(
ValueOpcode::GetImageResource,
{words[0], words[1], words[2], words[3],
words[4], words[5], words[6], words[7]}, 0, sample);
const auto sampler = fixture.Emit(
ValueOpcode::GetSamplerResource,
{Value(0u), Value(0u), Value(0u), Value(0u)}, 0, sample);
const auto address = fixture.Emit(
ValueOpcode::MakeImageAddress,
{Value(0u), Value(0u), Value(0u), Value(0u), Value(0u), Value(0u),
Value(0u), Value(0u), Value(0u), Value(0u), Value(0u), Value(0u),
Value(0u)}, 0, sample);
MemoryInfo image_memory;
image_memory.kind = ResourceKind::Image;
image_memory.image_dimension = Decoder::ImageDimension::Dim2D;
fixture.Emit(ValueOpcode::ImageSampleRaw, {image, sampler, address},
fixture.AddMemory(image_memory, 0x1aecu), sample);
fixture.program.block_info[7].terminator = {
.kind = CFG::TerminatorKind::Branch, .true_block = 8u};
fixture.program.block_info[8].terminator.kind = CFG::TerminatorKind::Return;
const auto output = fixture.Emit(
ValueOpcode::GetBufferResource,
{fixture.UserData(8u), fixture.UserData(9u),
fixture.UserData(10u), fixture.UserData(11u)}, 0, done);
MemoryInfo output_memory;
output_memory.kind = ResourceKind::Buffer;
fixture.Emit(ValueOpcode::StoreBufferU32,
{output, Value(0u), Value(0u), Value(0u),
Value(1u), Value(true)},
fixture.AddMemory(output_memory, 0x1b00u), done);
fixture.PlanAndTrack();
const auto source = fixture.program.info.images[0].source;
const auto &indirect = fixture.program.descriptor_sources[source].indirect_image;
Check(indirect && indirect->selector_stride == 0x90u &&
indirect->selector_offset == 0xc00u &&
indirect->table_offset == 0x20e0u &&
!indirect->selector_mask.IsEmpty() &&
indirect->key_count.Resolve().IsImmediate() &&
indirect->key_count.Resolve().U32() == 32u,
"uniformized image key lost its finite material range");
return ExtractResourcePlan(fixture.program);
};
auto plan = make_plan(false, false);
Check(plan.requires_specialization_memory &&
plan.descriptor_sources[plan.info.images[0].source]
.indirect_image->selector_mask.Resolve().TryInstruction() != nullptr,
"uniformized image mask did not survive extraction");
LinearTestMemory memory;
memory.words.resize(0x23000u / 4u);
constexpr uint64_t base = 0x1000u;
constexpr uint64_t first_material = base + 0xc00u + 2u * 0x90u;
constexpr uint64_t second_material = base + 0xc00u + 29u * 0x90u;
constexpr uint64_t first_table = base + 0x20e0u + 7u * 32u;
constexpr uint64_t second_table = base + 0x20e0u + 4096u * 32u;
memory.words[(first_material - base) / 4u] = 7u;
memory.words[(second_material - base) / 4u] = 4096u;
const auto set_descriptor = [&](uint64_t address, uint32_t color) {
const auto word = (address - base) / 4u;
memory.words[word] = color;
memory.words[word + 1u] = static_cast<uint32_t>(
Libs::Graphics::Prospero::BufferFormat::k32_32_32_32Float) << 20u;
memory.words[word + 3u] = Libs::Graphics::DstSel(4, 5, 6, 7) |
(static_cast<uint32_t>(Libs::Graphics::Prospero::ImageType::kColor2D) << 28u);
};
set_descriptor(first_table, 0x200u);
set_descriptor(second_table, 0x400u);
std::array<uint32_t, 12> user_data{};
user_data[0] = base;
user_data[4] = (1u << 2u) | (1u << 29u);
user_data[5] = 1u;
user_data[8] = 0x400000u;
user_data[9] = 16u << 16u;
user_data[10] = 1u;
memory.fail_address = base + 0xc00u + 3u * 0x90u;
const SrtRuntime runtime{
.user_data = user_data, .read_memory = ReadLinearTestMemory,
.userdata = &memory,
.read_specialization_memory = ReadLinearTestMemory};
ResourceSnapshot snapshot;
ResourceSpecialization specialization;
Check(MaterializeResources(plan, runtime, snapshot, specialization) &&
snapshot.images.size() == 2u &&
memory.reads == 4u && memory.descriptor_reads == 2u &&
snapshot.flattened_srt[
specialization.images[0].indirect_mapping_offset] == 2u &&
snapshot.flattened_srt[
specialization.images[0].indirect_mapping_offset + 1u] == 7u &&
snapshot.flattened_srt[
specialization.images[0].indirect_mapping_offset + 3u] == 4096u,
"material mask did not limit sparse descriptor reads");
user_data[8] = first_material;
Check(!MaterializeResources(plan, runtime, snapshot, specialization),
"written buffer alias with a material key was accepted");
user_data[8] = first_table;
Check(!MaterializeResources(plan, runtime, snapshot, specialization),
"written buffer alias with an image record was accepted");
CheckFatal([&] { make_plan(true, false); }, "not a valid runtime value",
"non-clearing material mask was accepted");
CheckFatal([&] { make_plan(false, true); }, "not a valid runtime value",
"unrelated ReadLane key was accepted");
}
void TestImageDescriptorFields() {
constexpr std::array<std::pair<uint32_t, uint32_t>, 5> reserved{
{{1u, 0x20000000u}, {2u, 0x70003000u}, {4u, 0xe000e000u},
@@ -2506,6 +2891,8 @@ int main() {
Run("dynamic storage mips", TestDynamicStorageMipTracking);
Run("invariant indirect images", TestInvariantIndirectImageMaterialization);
Run("guarded direct image table", TestGuardedDirectImageTable);
Run("bounded compute image loop", TestBoundedComputeImageLoop);
Run("uniformized material image keys", TestUniformizedMaterialImageKeys);
Run("image descriptor fields", TestImageDescriptorFields);
Run("draw-uniform scalar image", TestUniformScalarBufferImage);
Run("SRT runtime", TestSrtFlatteningAndRuntimeMemoization);
+128 -5
View File
@@ -330,11 +330,16 @@ std::vector<std::string> Search(int32_t user, const SceSaveDataTitleId* title =
void TestClassicSavePaths() {
Reset("CLASSIC");
const std::vector<std::string> names {"save.1", "save1", "slot@A", "slotA"};
fs::create_directories("_SaveData/CLASSIC/save.1");
{
std::ofstream saved("_SaveData/CLASSIC/save.1/progress");
saved << "save.1";
}
const auto existing = DirName("save.1");
struct SaveDataMount3 initial {};
initial.user_id = 1;
initial.dir_name = &existing;
initial.mount_mode = 4;
initial.blocks = 48;
SaveDataMountResult first {};
CHECK(SaveDataMount3(&initial, &first) == OK);
std::ofstream(g_mounted_directory / "progress") << "save.1";
CHECK(SaveDataUmount2(0, &first.mount_point) == OK);
CHECK(Search(1) == std::vector<std::string> {"save.1"});
for (const auto& text: names) {
auto name = DirName(text.c_str());
@@ -455,6 +460,123 @@ void TestSaveAllocations() {
CHECK(!fs::exists(metadata));
}
void TestClassicSaveParams() {
Reset("PARAMS");
const auto name = DirName("slot");
struct SaveDataMount3 mount {};
mount.user_id = 1;
mount.dir_name = &name;
mount.mount_mode = 4;
mount.blocks = 48;
SaveDataMountResult mounted {};
CHECK(SaveDataMount3(&mount, &mounted) == OK);
SaveDataParam param {};
std::strcpy(param.title, "First save");
std::strcpy(param.sub_title, "Chapter 2");
std::strcpy(param.detail, "Progress");
param.user_param = 42;
CHECK(SaveDataSetParam(&mounted.mount_point, 0, nullptr, sizeof(param)) ==
SAVE_DATA_ERROR_PARAMETER);
CHECK(SaveDataSetParam(&mounted.mount_point, 0, &param, sizeof(param) - 1) ==
SAVE_DATA_ERROR_PARAMETER);
CHECK(SaveDataSetParam(&mounted.mount_point, 0, &param, sizeof(param)) == OK);
const fs::path saved = "_SaveData/PARAMS/slot";
CHECK(fs::exists(saved / "sce_sys/param.bin"));
fs::create_directory(saved / "nested");
std::ofstream(saved / "nested/progress") << "payload";
SaveDataParam loaded {};
size_t got = 0;
CHECK(SaveDataGetParam(&mounted.mount_point, 0, &loaded, sizeof(loaded), &got) == OK);
CHECK(got == sizeof(loaded) && std::string(loaded.title) == "First save");
CHECK(std::string(loaded.sub_title) == "Chapter 2" && loaded.user_param == 42);
CHECK(loaded.mtime > 0);
fs::last_write_time(saved / "nested/progress",
fs::last_write_time(saved / "sce_sys/param.bin") + std::chrono::hours(2));
CHECK(SaveDataGetParam(&mounted.mount_point, 0, &loaded, sizeof(loaded), nullptr) == OK);
const int64_t nested_mtime = loaded.mtime;
CHECK(nested_mtime > static_cast<int64_t>(std::time(nullptr)) + 3600);
fs::create_directory(saved / "sce_sys/param.bin.tmp");
std::strcpy(param.title, "Failed update");
CHECK(SaveDataSetParam(&mounted.mount_point, 0, &param, sizeof(param)) ==
SAVE_DATA_ERROR_INTERNAL);
CHECK(fs::remove(saved / "sce_sys/param.bin.tmp"));
CHECK(SaveDataGetParam(&mounted.mount_point, 0, &loaded, sizeof(loaded), nullptr) == OK);
CHECK(std::string(loaded.title) == "First save");
std::strcpy(param.title, "Updated save");
CHECK(SaveDataSetParam(&mounted.mount_point, 0, &param, sizeof(param)) == OK);
CHECK(SaveDataGetParam(&mounted.mount_point, 0, &loaded, sizeof(loaded), nullptr) == OK);
CHECK(std::string(loaded.title) == "Updated save");
CHECK(SaveDataUmount2(0, &mounted.mount_point) == OK);
CHECK(SaveDataGetParam(&mounted.mount_point, 0, &loaded, sizeof(loaded), nullptr) ==
SAVE_DATA_ERROR_NOT_MOUNTED);
Reset("OTHER");
SceSaveDataTitleId source_title {};
std::strcpy(source_title.data, "PARAMS");
struct SaveDataTransferringMount transfer {};
transfer.user_id = 1;
transfer.title_id = &source_title;
transfer.dir_name = &name;
SaveDataMountResult transferred {};
CHECK(SaveDataTransferringMount(&transfer, &transferred) == OK);
CHECK(SaveDataGetParam(&transferred.mount_point, 0, &loaded, sizeof(loaded), nullptr) == OK);
CHECK(std::string(loaded.title) == "Updated save");
std::error_code alias_error;
fs::create_directory_symlink("PARAMS", "_SaveData/P_ALIAS", alias_error);
if (!alias_error) {
SceSaveDataTitleId alias_title {};
std::strcpy(alias_title.data, "P_ALIAS");
transfer.title_id = &alias_title;
SaveDataMountResult duplicate {};
CHECK(SaveDataTransferringMount(&transfer, &duplicate) == SAVE_DATA_ERROR_BUSY);
transfer.title_id = &source_title;
CHECK(fs::remove("_SaveData/P_ALIAS"));
}
#ifdef _WIN32
SceSaveDataTitleId lowercase_title {};
std::strcpy(lowercase_title.data, "params");
transfer.title_id = &lowercase_title;
SaveDataMountResult duplicate {};
CHECK(SaveDataTransferringMount(&transfer, &duplicate) == SAVE_DATA_ERROR_BUSY);
transfer.title_id = &source_title;
#endif
SaveDataMountResult other {};
CHECK(SaveDataMount3(&mount, &other) == OK);
std::strcpy(param.title, "Other title");
CHECK(SaveDataSetParam(&other.mount_point, 0, &param, sizeof(param)) == OK);
constexpr char typed_title[] = "Typed title";
CHECK(SaveDataSetParam(&other.mount_point, 1, typed_title, sizeof(typed_title)) == OK);
char title[32] {};
CHECK(SaveDataGetParam(&other.mount_point, 1, title, sizeof(title), &got) == OK);
CHECK(std::string(title) == typed_title && got == sizeof(typed_title));
const uint32_t user_param = 77;
CHECK(SaveDataSetParam(&other.mount_point, 4, &user_param, sizeof(user_param)) == OK);
CHECK(SaveDataGetParam(&other.mount_point, 0, &loaded, sizeof(loaded), nullptr) == OK);
CHECK(std::string(loaded.title) == typed_title && loaded.user_param == user_param);
CHECK(SaveDataGetParam(&other.mount_point, 5, &loaded.mtime, sizeof(loaded.mtime), nullptr) == OK);
CHECK(loaded.mtime > 0);
CHECK(SaveDataSetParam(&other.mount_point, 5, &loaded.mtime, sizeof(loaded.mtime)) ==
SAVE_DATA_ERROR_PARAMETER);
CHECK(SaveDataGetParam(&transferred.mount_point, 0, &loaded, sizeof(loaded), nullptr) == OK);
CHECK(std::string(loaded.title) == "Updated save");
CHECK(SaveDataUmount2(0, &other.mount_point) == OK);
CHECK(SaveDataUmount2(0, &transferred.mount_point) == OK);
std::array<SceSaveDataDirName, 1> names {};
std::array<SaveDataParam, 1> params {};
SaveDataDirNameSearchCond cond {};
cond.user_id = 1;
cond.title_id = &source_title;
SaveDataDirNameSearchResult result {};
result.dir_names = names.data();
result.dir_names_num = names.size();
result.params = params.data();
CHECK(SaveDataDirNameSearch(&cond, &result) == OK && result.set_num == 1);
CHECK(std::string(names[0].data) == "slot" && std::string(params[0].title) == "Updated save");
CHECK(params[0].mtime == nested_mtime);
}
void RunChild(const fs::path& executable, const char* mode) {
#ifdef _WIN32
CHECK(_spawnl(_P_WAIT, executable.string().c_str(), executable.string().c_str(), mode,
@@ -496,6 +618,7 @@ int main(int argc, char** argv) {
TestFailedWritePreservesSave();
TestClassicSavePaths();
TestSaveAllocations();
TestClassicSaveParams();
CHECK(SaveDataTerminate() == OK);
fs::current_path(previous);
fs::remove_all(temp);
+491 -52
View File
@@ -408,10 +408,11 @@ struct RenderExecutorTestAccess {
const ShaderStageRuntime &pixel,
bool pixel_active) {
RenderExecutor::GraphicsBindings result;
result.vertex[0] = executor.PrepareBindings(vertex);
executor.PrepareBindings(vertex, result.vertex[0]);
std::array<PreparedBindings *, 2> stages{&result.vertex[0], nullptr};
if (pixel_active) {
result.pixel.emplace(executor.PrepareBindings(pixel));
result.pixel.emplace();
executor.PrepareBindings(pixel, *result.pixel);
stages[1] = &*result.pixel;
}
executor.PrepareGraphicsBindings(
@@ -543,8 +544,15 @@ struct RenderExecutorTestAccess {
RenderColorInfo *colors,
uint32_t color_count,
RenderDepthInfo &depth,
const std::optional<PreparedBindings> &pixel = std::nullopt) {
return executor.AcquireRenderTargets(buffer, colors, color_count, depth, pixel);
std::span<PreparedBindings *const> stages = {},
vk::ImageAspectFlags *feedback_out = nullptr) {
vk::ImageAspectFlags feedback;
auto state = executor.AcquireRenderTargets(buffer, colors, color_count, depth,
feedback, stages);
if (feedback_out != nullptr) {
*feedback_out = feedback;
}
return state;
}
static void ResetBindings(RenderExecutor &executor) {
@@ -1976,16 +1984,37 @@ public:
ShaderRecompiler::IR::ResourceSnapshot pixel_snapshot;
pixel_snapshot.user_data = {0x33333333u, 0x44444444u};
ShaderStageRuntime pixel_runtime{&pixel_program, &pixel_snapshot};
auto vertex = context.GetRenderExecutor().PrepareBindings(vertex_runtime);
auto pixel = context.GetRenderExecutor().PrepareBindings(pixel_runtime);
PreparedBindings vertex;
PreparedBindings pixel;
auto &executor = context.GetRenderExecutor();
executor.PrepareBindings(vertex_runtime, vertex);
executor.PrepareBindings(pixel_runtime, pixel);
const auto *vertex_storage = vertex.shader_data.data();
const auto *pixel_storage = pixel.shader_data.data();
vertex.buffer_sources.push_back({});
vertex.buffers.push_back({});
vertex.gds.offset = 1;
vertex.flattened_srt.offset = 1;
vertex.shader_data_buffer.offset = 1;
vertex_snapshot.user_data = {0x55555555u, 0x66666666u};
pixel_snapshot.user_data = {0x77777777u, 0x88888888u};
executor.PrepareBindings(vertex_runtime, vertex);
executor.PrepareBindings(pixel_runtime, pixel);
Require(name, "prepared binding scratch",
vertex.shader_data.data() == vertex_storage &&
pixel.shader_data.data() == pixel_storage &&
vertex.buffer_sources.empty() && vertex.buffers.empty() &&
vertex.gds.offset == 0 && vertex.flattened_srt.offset == 0 &&
vertex.shader_data_buffer.offset == 0,
"repeated shader binding preparation allocated or retained stale state");
const auto pipeline = RenderExecutorTestAccess::CommitBindings(
context.GetRenderExecutor(), scheduler.Current(), vertex, pixel);
Require(name, "shared push data",
vertex.shader_data ==
std::vector<uint32_t>{0x11111111u, 0x22222222u} &&
std::vector<uint32_t>{0x55555555u, 0x66666666u} &&
pixel.shader_data ==
std::vector<uint32_t>{0x33333333u, 0x44444444u},
std::vector<uint32_t>{0x77777777u, 0x88888888u},
"graphics stages did not commit their shared push data");
scheduler.Finish();
RenderExecutorTestAccess::DestroyDescriptorPipelines(
@@ -10085,7 +10114,8 @@ public:
std::memcpy(value.dwords.data(), buffer_descriptor.fields,
sizeof(buffer_descriptor.fields));
value.dword_count = 4;
auto buffer_bindings = executor.PrepareBindings(buffer_runtime);
PreparedBindings buffer_bindings;
executor.PrepareBindings(buffer_runtime, buffer_bindings);
executor.FindBuffers(buffer_bindings);
const auto original_id = buffer_bindings.buffer_sources[0].id;
@@ -10152,7 +10182,8 @@ public:
null_info.info = std::move(null_program.info);
null_info.bindings = std::move(null_program.bindings);
ShaderStageRuntime null_runtime{&null_info, &null_snapshot};
auto null_bindings = executor.PrepareBindings(null_runtime);
PreparedBindings null_bindings;
executor.PrepareBindings(null_runtime, null_bindings);
executor.RebindImages(null_bindings);
Require(name, "null descriptor count",
null_bindings.images.size() == 3,
@@ -10414,6 +10445,19 @@ public:
sampled_overwide_binding.mip_views.empty() &&
sampled_overwide_binding.image_view != nullptr,
"sampled view lost addressable mips in the allocated tail");
const auto *image_storage = mipped_prepared.images.data();
const auto *mip_storage = mipped_prepared.images[1].mip_views.data();
executor.PrepareBindings(mipped_runtime, mipped_prepared);
Require(name, "prepared mip scratch",
mipped_prepared.images.data() == image_storage &&
mipped_prepared.images[1].mip_views.empty() &&
mipped_prepared.images[1].mip_views.capacity() >= 3,
"repeated image preparation retained stale mip views or reallocated scratch");
executor.RebindImages(mipped_prepared);
Require(name, "prepared mip views rebound",
mipped_prepared.images[1].mip_views.size() == 3 &&
mipped_prepared.images[1].mip_views.data() == mip_storage,
"reused image scratch did not rebuild dynamic storage mip views");
// Streaming T# clamps apply after S# max LOD, without changing the view
// base.
{
@@ -10658,7 +10702,8 @@ public:
storage_info.info = std::move(storage_program.info);
storage_info.bindings = std::move(storage_program.bindings);
ShaderStageRuntime storage_runtime{&storage_info, &storage_snapshot};
auto storage_discovery = executor.PrepareBindings(storage_runtime);
PreparedBindings storage_discovery;
executor.PrepareBindings(storage_runtime, storage_discovery);
const auto storage_id = storage_discovery.images[0].image_id;
Require(name, "storage prefetch purity",
storage_discovery.images[0].image_view == nullptr &&
@@ -10886,6 +10931,95 @@ public:
"descriptor layouts were queried after a later mip transition "
"instead of being captured at each binding");
auto disjoint_depth_desc = split_desc;
disjoint_depth_desc.type = BindingType::DepthTarget;
disjoint_depth_desc.info.data.address = base + 0xe00000;
disjoint_depth_desc.info.pixel_format = vk::Format::eD32Sfloat;
disjoint_depth_desc.info.guest_format = Prospero::BufferFormat::k32Float;
disjoint_depth_desc.view_info.format = vk::Format::eD32Sfloat;
disjoint_depth_desc.view_info.aspect = vk::ImageAspectFlagBits::eDepth;
disjoint_depth_desc.view_info.level_count = 1;
disjoint_depth_desc.view_info.usage =
vk::ImageUsageFlagBits::eDepthStencilAttachment;
const auto disjoint_depth_id = texture_cache.FindImage(disjoint_depth_desc);
auto disjoint_sampled_desc = disjoint_depth_desc;
disjoint_sampled_desc.type = BindingType::Texture;
disjoint_sampled_desc.view_info.base_level = 1;
disjoint_sampled_desc.view_info.usage = vk::ImageUsageFlagBits::eSampled;
const auto disjoint_sampled_view =
texture_cache.FindTexture(disjoint_depth_id, disjoint_sampled_desc);
RenderDepthInfo disjoint_depth{};
disjoint_depth.desc = disjoint_depth_desc;
disjoint_depth.image_id = disjoint_depth_id;
disjoint_depth.depth_test_enable = true;
disjoint_depth.depth_write_enable = true;
disjoint_depth.depth_compare_op = vk::CompareOp::eAlways;
PreparedBindings disjoint_binding{};
disjoint_binding.runtime = &sampled_runtime;
disjoint_binding.images.push_back(
{disjoint_depth_id, disjoint_sampled_view, disjoint_sampled_desc});
RenderExecutorTestAccess::BindRenderTarget(executor, disjoint_depth_id);
vk::ImageAspectFlags disjoint_feedback;
std::array<PreparedBindings *, 1> disjoint_stages{&disjoint_binding};
RenderColorInfo no_disjoint_color{};
const auto disjoint_rendering = RenderExecutorTestAccess::AcquireRenderTargets(
executor, scheduler.Current(), &no_disjoint_color, 0, disjoint_depth,
disjoint_stages, &disjoint_feedback);
descriptor_pipelines.push_back(RenderExecutorTestAccess::CommitBindings(
executor, scheduler.Current(), disjoint_binding));
const auto &disjoint_image = texture_cache.GetImage(disjoint_depth_id);
Require(name, "disjoint depth sampling during depth writes",
!disjoint_feedback &&
disjoint_rendering.depth_stencil_attachment.image_layout ==
vk::ImageLayout::eAttachmentFeedbackLoopOptimalEXT &&
disjoint_binding.images[0].layout ==
disjoint_rendering.depth_stencil_attachment.image_layout &&
disjoint_image.backing.state.layout ==
disjoint_rendering.depth_stencil_attachment.image_layout &&
disjoint_image.backing.subresource_states.empty(),
"disjoint sampling used inconsistent image layouts or enabled feedback");
RenderExecutorTestAccess::ResetBindings(executor);
auto overlapping_sampled_desc = disjoint_sampled_desc;
overlapping_sampled_desc.view_info.base_level = 0;
const auto overlapping_view = texture_cache.FindTexture(
disjoint_depth_id, overlapping_sampled_desc);
ShaderRecompiler::IR::Program depth_views_ir{};
depth_views_ir.stage = ShaderType::Pixel;
depth_views_ir.resource_tracking_complete = true;
depth_views_ir.info.images = {sampled_resource, sampled_resource};
allocate_bindings(depth_views_ir);
ShaderRecompiler::IR::CompiledShaderInfo depth_views_program{};
depth_views_program.stage = depth_views_ir.stage;
depth_views_program.info = std::move(depth_views_ir.info);
depth_views_program.bindings = std::move(depth_views_ir.bindings);
ShaderRecompiler::IR::ResourceSnapshot depth_views_snapshot;
ShaderStageRuntime depth_views_runtime{&depth_views_program, &depth_views_snapshot};
PreparedBindings depth_views_binding{};
depth_views_binding.runtime = &depth_views_runtime;
depth_views_binding.images.push_back(
{disjoint_depth_id, overlapping_view, overlapping_sampled_desc});
depth_views_binding.images.push_back(
{disjoint_depth_id, disjoint_sampled_view, disjoint_sampled_desc});
RenderExecutorTestAccess::BindRenderTarget(executor, disjoint_depth_id);
std::array<PreparedBindings *, 1> depth_views_stages{&depth_views_binding};
vk::ImageAspectFlags depth_views_feedback;
const auto depth_views_rendering = RenderExecutorTestAccess::AcquireRenderTargets(
executor, scheduler.Current(), &no_disjoint_color, 0, disjoint_depth,
depth_views_stages, &depth_views_feedback);
descriptor_pipelines.push_back(RenderExecutorTestAccess::CommitBindings(
executor, scheduler.Current(), depth_views_binding));
Require(name, "overlapping and disjoint depth views",
overlapping_view != nullptr &&
depth_views_feedback == vk::ImageAspectFlagBits::eDepth &&
depth_views_rendering.depth_stencil_attachment.image_layout ==
vk::ImageLayout::eAttachmentFeedbackLoopOptimalEXT &&
MakeImageInfo(depth_views_binding.images[0]).imageLayout ==
depth_views_rendering.depth_stencil_attachment.image_layout &&
MakeImageInfo(depth_views_binding.images[1]).imageLayout ==
depth_views_rendering.depth_stencil_attachment.image_layout,
"two depth views chose different descriptor layouts or feedback aspects");
Libs::LibKernel::Memory::WriteBacking(
storage_address, &storage_stale_value, sizeof(storage_stale_value));
texture_cache.ProcessDownloadImages();
@@ -11212,26 +11346,35 @@ public:
executor, scheduler.Current(), bounds_depth);
auto bounds_bindings = RenderExecutorTestAccess::PrepareGraphicsBindings(
executor, bounds_vertex, bounds_pixel, true);
std::array<PreparedBindings *, 2> bounds_stages{
&bounds_bindings.vertex[0], &*bounds_bindings.pixel};
vk::ImageAspectFlags bounds_feedback;
const auto bounds_rendering = RenderExecutorTestAccess::AcquireRenderTargets(
executor, scheduler.Current(), &no_color, 0, bounds_depth, bounds_bindings.pixel);
executor, scheduler.Current(), &no_color, 0, bounds_depth,
bounds_stages, &bounds_feedback);
descriptor_pipelines.push_back(RenderExecutorTestAccess::CommitBindings(
executor, scheduler.Current(), bounds_bindings.vertex[0], *bounds_bindings.pixel));
const auto &vertex_depth = bounds_bindings.vertex[0].images[0];
const auto &pixel_depth = bounds_bindings.pixel->images[0];
constexpr auto readonly_layout = vk::ImageLayout::eDepthReadOnlyOptimal;
Require(name, "deferred clear with sampled read-only depth bounds",
const auto expected_layout = pass == 0
? vk::ImageLayout::eAttachmentFeedbackLoopOptimalEXT
: vk::ImageLayout::eDepthReadOnlyOptimal;
Require(name, "deferred clear with sampled depth bounds",
bounds_depth.image_id == depth_only.image_id &&
bounds_depth.depth_bounds_test_enable &&
!bounds_depth.depth_write_enable &&
bounds_depth.depth_load_clear_enable == (pass == 0) &&
!texture_cache.IsMetaCleared(depth_only_htile_address, 0) &&
bounds_rendering.depth_stencil_attachment.image_layout == readonly_layout &&
bounds_feedback == (pass == 0
? vk::ImageAspectFlagBits::eDepth
: vk::ImageAspectFlags{}) &&
bounds_rendering.depth_stencil_attachment.image_layout == expected_layout &&
bounds_rendering.depth_stencil_attachment.depth_clear == (pass == 0) &&
vertex_depth.image_id == depth_only.image_id &&
pixel_depth.image_id == depth_only.image_id &&
MakeImageInfo(vertex_depth).imageLayout == readonly_layout &&
MakeImageInfo(pixel_depth).imageLayout == readonly_layout,
"deferred clear changed guest depth writes, sampled layouts, or repeated");
MakeImageInfo(vertex_depth).imageLayout == expected_layout &&
MakeImageInfo(pixel_depth).imageLayout == expected_layout,
"a deferred clear sampled by vertex and pixel stages missed depth feedback");
scheduler.BeginRendering(bounds_rendering);
scheduler.EndRendering();
RenderExecutorTestAccess::ResetBindings(executor);
@@ -11554,35 +11697,50 @@ public:
auto shared_bindings =
RenderExecutorTestAccess::PrepareGraphicsBindings(
executor, shared_depth_vertex, shared_depth_pixel, true);
auto& shared_image = texture_cache.GetImage(shared_depth.image_id);
auto stencil_view_info = shared_depth.desc.view_info;
stencil_view_info.aspect = vk::ImageAspectFlagBits::eStencil;
stencil_view_info.usage = vk::ImageUsageFlagBits::eSampled;
const auto stencil_view = shared_image.FindView(stencil_view_info);
auto& pixel_image = shared_bindings.pixel->images[0];
pixel_image.image_view = stencil_view;
pixel_image.desc.view_info = stencil_view_info;
std::array<PreparedBindings *, 2> shared_stages{
&shared_bindings.vertex[0], &*shared_bindings.pixel};
vk::ImageAspectFlags shared_feedback;
const auto shared_rendering =
RenderExecutorTestAccess::AcquireRenderTargets(
executor, scheduler.Current(), &no_color, 0, shared_depth, shared_bindings.pixel);
executor, scheduler.Current(), &no_color, 0, shared_depth,
shared_stages, &shared_feedback);
descriptor_pipelines.push_back(RenderExecutorTestAccess::CommitBindings(
executor, scheduler.Current(), shared_bindings.vertex[0],
*shared_bindings.pixel));
const auto expected_layout =
stencil_write
? vk::ImageLayout::eDepthReadOnlyStencilAttachmentOptimal
? vk::ImageLayout::eAttachmentFeedbackLoopOptimalEXT
: vk::ImageLayout::eDepthStencilReadOnlyOptimal;
const auto expected_access =
vk::AccessFlagBits2::eShaderRead |
vk::AccessFlagBits2::eDepthStencilAttachmentRead |
vk::AccessFlagBits2::eDepthStencilAttachmentWrite;
const auto &shared_image = texture_cache.GetImage(shared_depth.image_id);
const auto &vertex_image = shared_bindings.vertex[0].images[0];
const auto &pixel_image = shared_bindings.pixel->images[0];
Require(name, "sampled depth and stencil attachment layout",
shared_depth.image_id == phased_depth.image_id &&
stencil_view != nullptr &&
shared_feedback == (stencil_write
? vk::ImageAspectFlagBits::eStencil
: vk::ImageAspectFlags{}) &&
vertex_image.image_id == shared_depth.image_id &&
pixel_image.image_id == shared_depth.image_id &&
pixel_image.image_view == stencil_view &&
MakeImageInfo(vertex_image).imageLayout == expected_layout &&
MakeImageInfo(pixel_image).imageLayout == expected_layout &&
shared_rendering.depth_stencil_attachment.image_layout ==
expected_layout &&
shared_image.backing.state.layout == expected_layout &&
shared_image.backing.state.access_mask == expected_access,
"a shader alias replaced the depth/stencil attachment layout "
"or dropped an attachment access");
"stencil sampling and writes lost the feedback aspect, shared "
"layout, or attachment access");
scheduler.BeginRendering(shared_rendering);
scheduler.EndRendering();
RenderExecutorTestAccess::ResetBindings(executor);
@@ -11678,7 +11836,8 @@ public:
array_snapshot.images.push_back(array_descriptor);
ShaderStageRuntime array_runtime{&array_program, &array_snapshot};
auto array_binding = executor.PrepareBindings(array_runtime);
PreparedBindings array_binding;
executor.PrepareBindings(array_runtime, array_binding);
executor.RebindImages(array_binding);
const auto expanded_array_id = array_binding.images[0].image_id;
const auto &expanded_array = texture_cache.GetImage(expanded_array_id);
@@ -11766,7 +11925,8 @@ public:
auto &value = buffer_snapshot.buffers.emplace_back();
std::memcpy(value.dwords.data(), descriptor.fields, sizeof(descriptor.fields));
value.dword_count = 4;
auto buffer_bindings = executor.PrepareBindings(buffer_runtime);
PreparedBindings buffer_bindings;
executor.PrepareBindings(buffer_runtime, buffer_bindings);
std::array stages{&buffer_bindings};
RenderColorInfo target{};
target.desc = array_target;
@@ -11851,8 +12011,8 @@ public:
colliding_msaa_snapshot.images.push_back(colliding_msaa_descriptor);
ShaderStageRuntime colliding_msaa_runtime{
&colliding_msaa_program, &colliding_msaa_snapshot};
auto colliding_msaa_binding =
executor.PrepareBindings(colliding_msaa_runtime);
PreparedBindings colliding_msaa_binding;
executor.PrepareBindings(colliding_msaa_runtime, colliding_msaa_binding);
executor.RebindImages(colliding_msaa_binding);
const auto &resolved_colliding_msaa =
colliding_msaa_binding.images[0];
@@ -11910,7 +12070,8 @@ public:
ShaderRecompiler::IR::ResourceSnapshot msaa_snapshot{};
msaa_snapshot.images.push_back(msaa_descriptor);
ShaderStageRuntime msaa_runtime{&msaa_program, &msaa_snapshot};
auto msaa_binding = executor.PrepareBindings(msaa_runtime);
PreparedBindings msaa_binding;
executor.PrepareBindings(msaa_runtime, msaa_binding);
executor.RebindImages(msaa_binding);
const auto &resolved_msaa = msaa_binding.images[0];
Require(
@@ -11949,7 +12110,8 @@ public:
ShaderRecompiler::IR::ResourceSnapshot msaa_array_snapshot{};
msaa_array_snapshot.images.push_back(msaa_array_descriptor);
ShaderStageRuntime msaa_array_runtime{&msaa_array_program, &msaa_array_snapshot};
auto msaa_array_binding = executor.PrepareBindings(msaa_array_runtime);
PreparedBindings msaa_array_binding;
executor.PrepareBindings(msaa_array_runtime, msaa_array_binding);
executor.RebindImages(msaa_array_binding);
const auto &resolved_msaa_array = msaa_array_binding.images[0];
Require(name, "MSAA array backing expansion",
@@ -12213,7 +12375,8 @@ public:
snapshot.images.push_back(descriptor);
ShaderStageRuntime runtime{&program, &snapshot};
auto prepared = context.GetRenderExecutor().PrepareBindings(runtime);
PreparedBindings prepared;
context.GetRenderExecutor().PrepareBindings(runtime, prepared);
const auto sampled_stencil_id = prepared.images[0].image_id;
Require(name, "first stencil discovery",
prepared.images.size() == 1 &&
@@ -12245,7 +12408,8 @@ public:
"at the stencil guest address");
RenderExecutorTestAccess::ResetBindings(executor);
auto redirected = context.GetRenderExecutor().PrepareBindings(runtime);
PreparedBindings redirected;
context.GetRenderExecutor().PrepareBindings(runtime, redirected);
Require(name, "redirected owner discovery",
redirected.images.size() == 1 &&
redirected.images[0].image_id == depth_id &&
@@ -12435,7 +12599,8 @@ public:
sizeof(plane_descriptor.fields));
plane_value.dword_count = 4;
const ShaderStageRuntime plane_runtime{&plane_program, &plane_snapshot};
auto plane_bindings = executor.PrepareBindings(plane_runtime);
PreparedBindings plane_bindings;
executor.PrepareBindings(plane_runtime, plane_bindings);
vk::MemoryBarrier2 plane_barrier{};
plane_barrier.srcStageMask = plane_barrier.dstStageMask =
vk::PipelineStageFlagBits2::eAllCommands;
@@ -13501,12 +13666,18 @@ public:
!depth_feedback || bindings.pixel->images[0].image_id == depth.image_id,
"the fragment must sample the depth attachment's native image");
auto &command = scheduler.Current();
vk::ImageAspectFlags feedback_aspects;
std::array<PreparedBindings *, 2> stages{
&bindings.vertex[0], &*bindings.pixel};
auto rendering = RenderExecutorTestAccess::AcquireRenderTargets(
executor, command, &color, 1, depth, bindings.pixel);
executor, command, &color, 1, depth, stages, &feedback_aspects);
const bool feedback_enabled = rendering.depth_stencil_attachment.image_layout ==
vk::ImageLayout::eAttachmentFeedbackLoopOptimalEXT;
Require(name, "per-draw depth feedback",
feedback_enabled == (depth_feedback && depth.depth_write_enable),
feedback_enabled == (depth_feedback && depth.depth_write_enable) &&
feedback_aspects == (feedback_enabled
? vk::ImageAspectFlagBits::eDepth
: vk::ImageAspectFlags{}),
"feedback was not selected only for an overlapping fragment depth read/write");
RenderExecutorTestAccess::CommitBindings(
executor, command, selected, bindings.vertex[0], *bindings.pixel);
@@ -13539,9 +13710,7 @@ public:
}
const vk::Bool32 write = true;
cmd.setColorWriteEnableEXT(1, &write);
cmd.setAttachmentFeedbackLoopEnableEXT(
feedback_enabled ? vk::ImageAspectFlags{vk::ImageAspectFlagBits::eDepth}
: vk::ImageAspectFlags{});
cmd.setAttachmentFeedbackLoopEnableEXT(feedback_aspects);
const vk::DeviceSize offset = 0;
cmd.bindVertexBuffers(0, 1, &buffer.buffer, &offset);
cmd.draw(vertex_count, 1, 0, 0);
@@ -16416,6 +16585,7 @@ CoverageClass ClassifyOpcode(ShaderOpcode opcode,
case Opcode::DS_READ_B128:
case Opcode::DS_WRITE_B8:
case Opcode::DS_WRITE_B16:
case Opcode::DS_WRITE_B8_D16_HI:
case Opcode::DS_WRITE_B16_D16_HI:
case Opcode::DS_WRITE2_B32:
case Opcode::DS_WRITE2ST64_B32:
@@ -16609,6 +16779,32 @@ TestCase Shifts() {
O::BUFFER_STORE_DWORD, O::S_ENDPGM}};
}
TestCase ScalarZeroShiftWithRuntimeCount() {
using O = ShaderOpcode;
std::vector<u32> code = {
EncodeSop2(0x20, 8, InlineU32(0), 4),
EncodeSop2(0x20, 9, 6, 4),
};
AppendStoreSgpr(&code, 8, 0);
AppendStoreSgpr(&code, 9, 1);
AppendEnd(&code);
TestCase test;
test.name = "ScalarZeroShiftWithRuntimeCount";
test.code = std::move(code);
test.expected = {0, 0x40000000u};
test.opcodes = {O::S_LSHR_B32, O::V_MOV_B32, O::BUFFER_STORE_DWORD,
O::S_ENDPGM};
test.user_data = MakeStructuredStorageBufferData(0, 2 * sizeof(u32));
test.user_data[4] = 33;
test.user_data[6] = 0x80000000u;
test.has_user_data = true;
test.ir_counts = {{" = ShiftRightLogical32 ", 1}};
test.required_spirv = {"OpShiftRightLogical"};
return test;
}
TestCase ExactPushConstantExtent() {
using O = ShaderOpcode;
@@ -18331,6 +18527,25 @@ TestCase ScalarWqmB32Masks(u32 wave_size) {
store_marker(lo, hi);
}
}
for (u32 base : {106u, 126u}) {
for (u32 half : {0u, 1u}) {
AppendVMovU32(&code, 2, 0);
code.push_back(EncodeSMovB32(107, InlineU32(0)));
code.push_back(EncodeVopc(0xc2, InlineU32(3), 0)); // Only lane 3.
code.push_back(EncodeSop1(0x04, base, 106));
code.push_back(EncodeSop1(0x09, base + half, base + (half ^ 1u)));
code.push_back(EncodeSMovB32(20, base));
code.push_back(EncodeSMovB32(21, base + 1));
code.push_back(EncodeSop1(0x04, 126, base));
AppendVMovU32(&code, 2, 9);
code.push_back(EncodeSop1(0x04, 126, 193));
const u32 lo = half == 0 ? 0u : 8u;
const u32 hi = half == 0 ? 0u : 0xfu;
store_scalar(20, lo);
store_scalar(21, hi);
store_marker(lo, hi);
}
}
AppendEnd(&code);
test.initial.resize(test.expected.size());
test.opcodes = {O::S_WQM_B32, O::S_MOV_B32, O::S_MOV_B64, O::S_CMP_EQ_U32,
@@ -18400,8 +18615,21 @@ TestCase ScalarWqmB64PreservesPartialMasks() {
code.push_back(EncodeSop1(0x0a, 10, 8));
code.push_back(EncodeSop1(0x04, 126, 10));
AppendStoreVgprAtLaneDwordOffset(&code, 2, 0, offset);
code.push_back(EncodeSop1(0x04, 126, 16));
for (u32 source : {253u, 127u, 107u}) {
if (source == 253u) {
code.push_back(EncodeSopc(0x06, InlineU32(1), InlineU32(1))); // SCC=1.
} else {
AppendSMovLiteral(&code, source - 1, ~0u);
code.push_back(EncodeSMovB32(source, InlineU32(1)));
}
code.push_back(EncodeSop1(0x0a, 126, source));
offset += 64;
AppendStoreVgprAtLaneDwordOffset(&code, 2, 0, offset);
code.push_back(EncodeSop1(0x04, 126, 16));
}
AppendEnd(&code);
test.initial.assign(256, 0xdeadbeef);
test.initial.assign(448, 0xdeadbeef);
test.expected = test.initial;
for (u32 lane = 0; lane < 64; ++lane) {
for (u32 region = 0; region < 3; ++region) {
@@ -18410,9 +18638,12 @@ TestCase ScalarWqmB64PreservesPartialMasks() {
if ((lane >= 4 && lane < 8) || (lane >= 32 && lane < 36)) {
test.expected[192 + lane] = 9;
}
for (u32 region = 4; region < 7; ++region) {
if (lane < 4) test.expected[region * 64 + lane] = 9;
}
}
test.opcodes = {O::S_MOV_B64, O::S_MOV_B32, O::S_WQM_B64, O::V_MOV_B32,
O::V_CMP_EQ_U32, O::V_LSHLREV_B32, O::BUFFER_STORE_DWORD,
O::V_CMP_EQ_U32, O::S_CMP_EQ_U32, O::V_LSHLREV_B32, O::BUFFER_STORE_DWORD,
O::S_ENDPGM};
test.compute_info.threads_num[0] = 64;
test.compute_info.threads_num[1] = test.compute_info.threads_num[2] = 1;
@@ -20472,6 +20703,43 @@ TestCase VectorLaneWave32RuntimeSelectorWraps() {
return test;
}
TestCase VectorReadlaneSelectsTwoKeysWithinWave() {
using O = ShaderOpcode;
std::vector<u32> code;
code.push_back(EncodeVop2(0x16, 1, InlineU32(4), 0)); // lanes 0..15: 0, 16..31: 1
AppendVop3(&code, 0x360, 20, Vgpr(1), InlineU32(0));
AppendVop3(&code, 0x360, 21, Vgpr(1), InlineU32(16));
AppendVMovU32(&code, 2, 0);
AppendVMovU32(&code, 3, 11);
AppendVMovU32(&code, 4, 22);
code.push_back(EncodeVopc(0xc2, 20, 1)); // selected lane 0 key == local key
code.push_back(EncodeVop2(0x01, 2, Vgpr(2), 3));
code.push_back(EncodeVopc(0xc2, 21, 1)); // selected lane 16 key == local key
code.push_back(EncodeVop2(0x01, 2, Vgpr(2), 4));
AppendStoreVgprAtLaneDwordOffset(&code, 2, 0, 0);
AppendEnd(&code);
TestCase test;
test.name = "VectorReadlaneSelectsTwoKeysWithinWave";
test.code = std::move(code);
test.expected.resize(32);
std::fill_n(test.expected.begin(), 16, 11u);
std::fill_n(test.expected.begin() + 16, 16, 22u);
test.opcodes = {O::V_LSHRREV_B32, O::V_READLANE_B32, O::V_MOV_B32,
O::V_CMP_EQ_U32, O::V_CNDMASK_B32,
O::BUFFER_STORE_DWORD, O::S_ENDPGM};
test.decoded_counts = {{"V_READLANE_B32", 2}};
test.required_spirv = {"OpGroupNonUniformShuffle"};
test.compute_info.threads_num[0] = 32;
test.compute_info.threads_num[1] = 1;
test.compute_info.threads_num[2] = 1;
test.compute_info.wave_size = 32;
test.compute_info.thread_ids_num = 1;
test.has_compute_info = true;
return test;
}
TestCase VectorPermlanex16() {
using O = ShaderOpcode;
@@ -23446,6 +23714,38 @@ TestCase BufferStoreFormatXResource16UintPreservesAdjacentLanes() {
return test;
}
TestCase BufferStoreFormatXyzwFloat16ConvertsComponents() {
using O = ShaderOpcode;
// This finite F32 has low bits 0x7e63, which form an F16 NaN if truncated.
constexpr std::array<float, 8> values = {
std::bit_cast<float>(0x3f807e63u), -2.0f, 0.5f, 0.25f,
-4.0f, 3.0f, 0.0f, 2.0f};
std::vector<u32> code;
for (u32 record = 0; record < 2; record++) {
for (u32 component = 0; component < 4; component++) {
AppendVMovLiteral(&code, component,
std::bit_cast<u32>(values[record * 4 + component]));
}
AppendVMovU32(&code, 20, record);
code.push_back(EncodeMubuf0(0x07u, 0, true, false));
code.push_back(EncodeMubuf1(0, 0, 20));
}
AppendEnd(&code);
TestCase test;
test.name = "BufferStoreFormatXyzwFloat16ConvertsComponents";
test.code = std::move(code);
test.initial = std::vector<u32>(4, 0xdeadbeefu);
test.expected = {0xc0003c04u, 0x34003800u, 0x4200c400u, 0x40000000u};
test.user_data = MakeStructuredStorageBufferData(
8, 2, false, BufferFormat(Prospero::BufferFormat::k16_16_16_16Float));
test.has_user_data = true;
test.opcodes = {O::V_MOV_B32, O::BUFFER_STORE_FORMAT_XYZW, O::S_ENDPGM};
test.required_spirv = {"PackHalf2x16"};
return test;
}
TestCase BufferStoreFormatXyzwSnorm16CapturedSkinningVectors() {
using O = ShaderOpcode;
@@ -24688,6 +24988,38 @@ TestCase DsWriteB16D16HiCapturedUsesHighHalf() {
return test;
}
TestCase DsWriteB8D16HiWritesByteTwo() {
using O = ShaderOpcode;
std::vector<u32> code;
AppendVMovU32(&code, 1, 0);
AppendVMovLiteral(&code, 3, 0xdeadbeefu);
code.push_back(EncodeDs0(0x0d));
code.push_back(EncodeDs1(0, 3, 1)); // ds_write_b32 v1, v3
// Every byte of the source differs, so selecting any byte but 23:16 fails.
AppendVMovU32(&code, 20, 1);
AppendVMovLiteral(&code, 2, 0x11a22233u);
AppendVMovLiteral(&code, 0, 0x87654321u);
code.push_back(EncodeDs0(0xa0));
code.push_back(EncodeDs1(0, 2, 20)); // ds_write_b8_d16_hi v20, v2
code.push_back(EncodeDs0(0x36));
code.push_back(EncodeDs1(4, 0, 1)); // ds_read_b32 v4, v1
AppendStoreVgpr(&code, 4, 0);
AppendStoreVgpr(&code, 0, 1);
AppendEnd(&code);
TestCase test{"DsWriteB8D16HiWritesByteTwo",
code,
std::vector<u32>(2, 0),
{0xdeada2efu, 0x87654321u},
{O::V_MOV_B32, O::DS_WRITE_B32, O::DS_WRITE_B8_D16_HI,
O::DS_READ_B32, O::BUFFER_STORE_DWORD, O::S_ENDPGM}};
test.decoded_counts = {{"DS_WRITE_B8_D16_HI", 1}};
return test;
}
TestCase DsReadU16D16HiCapturedPreservesLowHalf() {
using O = ShaderOpcode;
@@ -24902,10 +25234,12 @@ TestCase DsAtomicNoReturnVariants() {
std::vector<u32> code;
AppendVMovU32(&code, 1, 0);
const u32 initial[] = {10, 10, 0xfffffff0u, 0xfffffff0u, 10,
10, 0xf0f0u, 0xf000u, 0xf00fu};
const u32 values[] = {5, 3, 5, 5, 5, 20, 0x0ff0u, 0x0f00u, 0x00ffu};
const u32 ops[] = {0x00, 0x01, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b};
const u32 initial[] = {10, 10, 2, 1, 0, 3, 9, 0xfffffff0u, 0xfffffff0u,
10, 10, 0xf0f0u, 0xf000u, 0xf00fu};
const u32 values[] = {5, 3, 2, 5, 2, 5, 5, 5, 5,
5, 20, 0x0ff0u, 0x0f00u, 0x00ffu};
const u32 ops[] = {0x00, 0x01, 0x03, 0x03, 0x04, 0x04, 0x04,
0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b};
for (u32 i = 0; i < static_cast<u32>(std::size(values)); i++) {
AppendVMovLiteral(&code, 2, initial[i]);
code.push_back(EncodeDs0(0x0d, i * 4u));
@@ -24923,12 +25257,14 @@ TestCase DsAtomicNoReturnVariants() {
return {"DsAtomicNoReturnVariants",
code,
std::vector<u32>(9, 0),
{15, 7, 0xfffffff0u, 5, 5, 20, 0x00f0u, 0xff00u, 0xf0f0u},
std::vector<u32>(14, 0),
{15, 7, 0, 2, 2, 2, 5, 0xfffffff0u, 5, 5, 20, 0x00f0u, 0xff00u,
0xf0f0u},
{O::V_MOV_B32, O::DS_WRITE_B32, O::DS_ADD_U32, O::DS_SUB_U32,
O::DS_MIN_I32, O::DS_MAX_I32, O::DS_MIN_U32, O::DS_MAX_U32,
O::DS_AND_B32, O::DS_OR_B32, O::DS_XOR_B32, O::DS_READ_B32,
O::BUFFER_STORE_DWORD, O::S_ENDPGM}};
O::DS_INC_U32, O::DS_DEC_U32, O::DS_MIN_I32, O::DS_MAX_I32,
O::DS_MIN_U32, O::DS_MAX_U32, O::DS_AND_B32, O::DS_OR_B32,
O::DS_XOR_B32, O::DS_READ_B32, O::BUFFER_STORE_DWORD,
O::S_ENDPGM}};
}
TestCase DsAtomicReturnVariants() {
@@ -27933,6 +28269,7 @@ std::vector<TestCase> MakeCases() {
AddCase(IntegerAddSubMul);
AddCase(BitwiseOps);
AddCase(Shifts);
AddCase(ScalarZeroShiftWithRuntimeCount);
AddCase(ExactPushConstantExtent);
AddCase(ScalarShiftCountsMaskLowBits);
AddCase(Rdna2ScalarOpcodes);
@@ -28039,6 +28376,7 @@ std::vector<TestCase> MakeCases() {
AddCase(VectorWritelaneIgnoresExecMask);
AddCase(VectorReadlaneFromInactiveWrittenLane);
AddCase(VectorLaneWave32RuntimeSelectorWraps);
AddCase(VectorReadlaneSelectsTwoKeysWithinWave);
AddCase(VectorPermlanex16);
AddCase(VectorPermlane16FetchInactiveZero);
AddCase(VectorPermlane16FetchInactiveFi);
@@ -28132,6 +28470,7 @@ std::vector<TestCase> MakeCases() {
AddCase(BufferFormatStoreVariants);
AddCase(BufferStoreFormatXResource16UintWritesHalfword);
AddCase(BufferStoreFormatXResource16UintPreservesAdjacentLanes);
AddCase(BufferStoreFormatXyzwFloat16ConvertsComponents);
AddCase(BufferStoreFormatXyzwSnorm16CapturedSkinningVectors);
AddCase(BufferStoreFormatXSnorm16ClampsRoundsAndPreservesHalfwords);
AddCase(BufferLoadFormatXResource8UintZeroExtendsByte);
@@ -28177,6 +28516,7 @@ std::vector<TestCase> MakeCases() {
AddCase(ScratchIsPrivatePerInvocation);
AddCase(DsReadWriteVariants);
AddCase(DsWriteB16D16HiCapturedUsesHighHalf);
AddCase(DsWriteB8D16HiWritesByteTwo);
AddCase(DsReadU16D16CapturedPreservesHighHalf);
AddCase(DsReadU16D16HiCapturedPreservesLowHalf);
AddCase(DsAppendConsumeUsesEncodedLdsSelector);
@@ -31018,6 +31358,81 @@ void CheckDepthAttachmentWrites() {
std::printf("[host] %-32s ok\n", "DepthAttachmentWrites");
}
void CheckDepthFeedbackAspects() {
RenderDepthInfo target{};
target.desc.view_info.format = vk::Format::eD32SfloatS8Uint;
target.desc.view_info.base_level = 1;
target.desc.view_info.level_count = 2;
target.desc.view_info.base_layer = 3;
target.desc.view_info.layer_count = 2;
target.depth_test_enable = true;
target.depth_write_enable = true;
target.depth_compare_op = vk::CompareOp::eAlways;
target.stencil_test_enable = true;
target.stencil_front = {vk::StencilOp::eKeep, vk::StencilOp::eReplace,
vk::StencilOp::eKeep, vk::CompareOp::eAlways, 0xff, 0xff, 0};
target.stencil_back = target.stencil_front;
auto sampled = target.desc.view_info;
const auto feedback = [&](const ImageViewInfo& view) {
return DepthFeedbackAspects(target.AttachmentWriteAspects(),
target.desc.view_info, view);
};
sampled.aspect = vk::ImageAspectFlagBits::eDepth;
Require("DepthFeedbackAspects", "sampled writable depth",
feedback(sampled) == vk::ImageAspectFlagBits::eDepth,
"overlapping depth sampling did not request depth feedback");
sampled.aspect = vk::ImageAspectFlagBits::eStencil;
Require("DepthFeedbackAspects", "sampled writable stencil",
feedback(sampled) == vk::ImageAspectFlagBits::eStencil,
"overlapping stencil sampling did not request stencil feedback");
sampled.aspect = vk::ImageAspectFlagBits::eDepth | vk::ImageAspectFlagBits::eStencil;
Require("DepthFeedbackAspects", "both writable aspects",
feedback(sampled) == sampled.aspect,
"combined depth/stencil sampling lost a writable aspect");
target.depth_write_enable = false;
Require("DepthFeedbackAspects", "read-only depth",
feedback(sampled) == vk::ImageAspectFlagBits::eStencil,
"read-only depth was included in stencil feedback");
target.depth_write_enable = true;
target.stencil_test_enable = false;
Require("DepthFeedbackAspects", "read-only stencil",
feedback(sampled) == vk::ImageAspectFlagBits::eDepth,
"read-only stencil was included in depth feedback");
sampled.aspect = vk::ImageAspectFlagBits::eStencil;
Require("DepthFeedbackAspects", "sampled read-only aspect",
!feedback(sampled),
"sampling a read-only stencil requested feedback for depth writes");
sampled.aspect = vk::ImageAspectFlagBits::eDepth;
sampled.base_level = 3;
Require("DepthFeedbackAspects", "disjoint mip",
!feedback(sampled),
"disjoint mip ranges requested depth feedback");
sampled.base_level = 2;
sampled.base_layer = 5;
Require("DepthFeedbackAspects", "disjoint layer",
!feedback(sampled),
"disjoint layer ranges requested depth feedback");
sampled.base_layer = 4;
Require("DepthFeedbackAspects", "overlapping range boundary",
feedback(sampled) == vk::ImageAspectFlagBits::eDepth,
"overlapping mip and layer boundaries missed depth feedback");
target.depth_write_enable = false;
target.depth_load_clear_enable = true;
Require("DepthFeedbackAspects", "depth clear only",
feedback(sampled) == vk::ImageAspectFlagBits::eDepth,
"a sampled depth attachment clear missed feedback");
target.depth_load_clear_enable = false;
target.stencil_clear_enable = true;
sampled.aspect = vk::ImageAspectFlagBits::eStencil;
Require("DepthFeedbackAspects", "stencil clear only",
feedback(sampled) == vk::ImageAspectFlagBits::eStencil,
"a sampled stencil attachment clear missed feedback");
std::printf("[host] %-32s ok\n", "DepthFeedbackAspects");
}
void CheckDynamicRenderingState() {
RenderState first{};
first.width = 64;
@@ -31065,10 +31480,10 @@ void CheckDynamicRenderingState() {
vk::ImageLayout::eDepthStencilReadOnlyOptimal,
"fully read-only depth/stencil used a writable layout");
attachment.depth_load_clear_enable = true;
Require("DynamicRenderingState", "read-only deferred depth clear",
Require("DynamicRenderingState", "deferred depth clear layout",
depth_attachment_layout(attachment) ==
vk::ImageLayout::eDepthStencilReadOnlyOptimal,
"a load clear changed the guest depth-write layout");
vk::ImageLayout::eDepthAttachmentStencilReadOnlyOptimal,
"a depth load clear used a read-only attachment layout");
attachment.depth_test_enable = true;
attachment.depth_write_enable = true;
Require("DynamicRenderingState", "depth-write stencil-read layout",
@@ -32784,6 +33199,11 @@ int main(int argc, char **argv) {
RunCase(&vulkan, BufferFormatStoreVariants());
return 0;
}
if (argc == 2 && std::strcmp(argv[1], "--buffer-float16-store-only") == 0) {
VulkanHarness vulkan;
RunCase(&vulkan, BufferStoreFormatXyzwFloat16ConvertsComponents());
return 0;
}
if (argc == 2 && std::strcmp(argv[1], "--s-ashr-i64-only") == 0) {
VulkanHarness vulkan;
RunCase(&vulkan, ScalarAshrI64Edges(false));
@@ -33007,6 +33427,11 @@ int main(int argc, char **argv) {
RunCase(&vulkan, VectorAlignByteUsesFiveBitByteOffset());
return 0;
}
if (argc == 2 && std::strcmp(argv[1], "--zero-shift-only") == 0) {
VulkanHarness vulkan;
RunCase(&vulkan, ScalarZeroShiftWithRuntimeCount());
return 0;
}
if (argc == 2 && std::strcmp(argv[1], "--sdwa-mov-only") == 0) {
VulkanHarness vulkan;
RunCase(&vulkan, Vop1SdwaMovByteDestinations());
@@ -33083,6 +33508,7 @@ int main(int argc, char **argv) {
}
if (argc == 2 && std::strcmp(argv[1], "--image-overlap-only") == 0) {
CheckDepthAttachmentWrites();
CheckDepthFeedbackAspects();
CheckDynamicRenderingState();
VulkanHarness vulkan;
vulkan.CheckRasterization(false);
@@ -33100,6 +33526,12 @@ int main(int argc, char **argv) {
CheckEmbeddedFetchVertexOffset();
return 0;
}
if (argc == 2 && std::strcmp(argv[1], "--depth-feedback-only") == 0) {
CheckDepthAttachmentWrites();
CheckDepthFeedbackAspects();
CheckDynamicRenderingState();
return 0;
}
if (argc == 2 && std::strcmp(argv[1], "--polygon-mode-only") == 0) {
VulkanHarness vulkan;
vulkan.CheckRasterization(false);
@@ -33173,6 +33605,11 @@ int main(int argc, char **argv) {
RunCase(&vulkan, ImageCubeGradientsPreserveDerivatives());
return 0;
}
if (argc == 2 && std::strcmp(argv[1], "--readlane-key-guard-only") == 0) {
VulkanHarness vulkan;
RunCase(&vulkan, VectorReadlaneSelectsTwoKeysWithinWave());
return 0;
}
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
if (argc == 2 && std::strcmp(argv[1], "--reverse-rt-death") == 0) {
RunReverseRenderTargetDeathCase();
@@ -33255,6 +33692,7 @@ int main(int argc, char **argv) {
CheckPs5DepthRegisterDecoding();
CheckDepthHtileStencilCompatibility();
CheckDepthAttachmentWrites();
CheckDepthFeedbackAspects();
CheckDynamicRenderingState();
CheckDepthTargetFootprints();
CheckSlotVectorLifetime();
@@ -33264,6 +33702,7 @@ int main(int argc, char **argv) {
return 2;
}
CheckShaderRecompilerFatalContracts();
CheckDepthFeedbackAspects();
VulkanHarness vulkan;
#endif
CheckImageSamplerSpecialization();
+84
View File
@@ -462,6 +462,89 @@ void TestPrtBackingReadPreservesSparseResidency() {
std::printf("[host] %-48s ok\n", test);
}
void TestPrtReadDuringDirectCommit() {
const char* test = "PrtReadDuringDirectCommit";
constexpr uint64_t chunk_size = SceKernelMemoryPoolCommitLen;
constexpr uint64_t chunks = 32;
constexpr uint64_t size = chunk_size * chunks;
int64_t physical = -1;
CheckOk(test,
Libs::LibKernel::Memory::KernelAllocateDirectMemory(
0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(), size, chunk_size,
SceKernelMtypeC, &physical),
"KernelAllocateDirectMemory");
void* arena = reinterpret_cast<void*>(0x1000000000ull);
CheckOk(test, Libs::LibKernel::Memory::KernelReserveVirtualRange(&arena, size, 0, size),
"KernelReserveVirtualRange");
const auto base = reinterpret_cast<uint64_t>(arena);
CheckOk(test, Libs::LibKernel::Memory::KernelSetPrtAperture(2, arena, size),
"KernelSetPrtAperture");
const auto map_chunk = [&](uint64_t index) {
void* address = reinterpret_cast<void*>(base + index * chunk_size);
return Libs::LibKernel::Memory::KernelMapNamedDirectMemory(
&address, chunk_size, SceKernelProtCpuRw, SceKernelMapFixed,
physical + static_cast<int64_t>(index * chunk_size), chunk_size, "prt_direct");
};
CheckOk(test, map_chunk(0), "KernelMapNamedDirectMemory(first)");
CheckOk(test, map_chunk(chunks - 1), "KernelMapNamedDirectMemory(last)");
std::memset(reinterpret_cast<void*>(base), 0x3c, chunk_size);
std::memset(reinterpret_cast<void*>(base + (chunks - 1) * chunk_size), 0xa7, chunk_size);
std::vector<uint8_t> bytes(size, 0x5a);
Check(test, Libs::LibKernel::Memory::TryReadPrtBacking(base, bytes.data(), size),
"PRT read rejected a partly committed direct mapping");
Check(test, bytes.front() == 0x3c && bytes[chunk_size] == 0 && bytes.back() == 0xa7,
"PRT read lost resident bytes or sparse zeros");
Libs::LibKernel::Memory::TestFailNextVirtualRangeReplacement();
CheckFailed(test, map_chunk(1), "KernelMapNamedDirectMemory(injected replacement failure)");
ExpectRange(test, Query(test, base + chunk_size), base + chunk_size,
base + (chunks - 1) * chunk_size, 0, 0, 0, 0, 0, "anon");
Check(test, !Libs::LibKernel::Memory::TryReadBacking(base + chunk_size, bytes.data(),
chunk_size) &&
Libs::LibKernel::Memory::TryReadPrtBacking(base, bytes.data(), size) &&
bytes[chunk_size] == 0,
"failed direct publication did not restore sparse reservation and backing");
std::atomic<bool> started {false};
std::atomic<bool> stop {false};
std::atomic<bool> read_failed {false};
std::atomic<uint32_t> reads {0};
std::thread reader([&] {
std::vector<uint8_t> snapshot(size);
started.store(true, std::memory_order_release);
while (!stop.load(std::memory_order_acquire)) {
if (!Libs::LibKernel::Memory::TryReadPrtBacking(base, snapshot.data(), size) ||
snapshot.front() != 0x3c || snapshot.back() != 0xa7) {
read_failed.store(true, std::memory_order_relaxed);
}
reads.fetch_add(1, std::memory_order_relaxed);
}
});
while (!started.load(std::memory_order_acquire)) {
std::this_thread::yield();
}
int map_result = OK;
for (uint64_t index = 1; index + 1 < chunks; ++index) {
map_result = map_chunk(index);
if (map_result != OK) {
break;
}
}
stop.store(true, std::memory_order_release);
reader.join();
CheckOk(test, Libs::LibKernel::Memory::KernelSetPrtAperture(2, nullptr, 0),
"KernelSetPrtAperture(clear)");
CheckOk(test, Libs::LibKernel::Memory::KernelMunmap(base, size), "KernelMunmap");
CheckOk(test, Libs::LibKernel::Memory::KernelReleaseDirectMemory(physical, size),
"KernelReleaseDirectMemory");
CheckOk(test, map_result, "KernelMapNamedDirectMemory(middle)");
Check(test, reads.load(std::memory_order_relaxed) != 0 &&
!read_failed.load(std::memory_order_relaxed),
"PRT read observed a gap while direct backing was committed");
std::printf("[host] %-48s ok\n", test);
}
void TestGuestAddressSpaceHasNoFixedFallback() {
const char* test = "GuestAddressSpaceHasNoFixedFallback";
const auto unowned_address = reinterpret_cast<void*>(0x10000);
@@ -3031,6 +3114,7 @@ int main(int argc, char** argv) {
RunTest(TestProsperoArgumentAndInfoSizeContracts);
RunTest(TestGuestAddressSpaceOwnsReservationsBeforeBacking);
RunTest(TestPrtBackingReadPreservesSparseResidency);
RunTest(TestPrtReadDuringDirectCommit);
RunTest(TestGuestAddressSpaceHasNoFixedFallback);
RunTest(TestGuestFreeRangeSearchDoesNotUnderflow);
RunTest(TestFlexibleMemoryCapacityIsBootFixed);
+55 -3
View File
@@ -4458,6 +4458,23 @@ void TestNewShaderDecoderArchitecture() {
d16_hi_write.src1.sdwa_sel == 5u,
"DS decoder rejected the captured high-half D16 write");
// Captured from GTA V (PPSA04264) compute shader, pc 0x1598.
const uint32_t d16_hi_byte_write_ds[] = {0xda800200u, 0x00001413u};
Instruction d16_hi_byte_write;
ShaderRecompiler::Decoder::DecodeInstruction(d16_hi_byte_write_ds, 0u,
d16_hi_byte_write);
Check(d16_hi_byte_write.family == Family::DS &&
d16_hi_byte_write.opcode == Opcode::DS_WRITE_B8_D16_HI &&
d16_hi_byte_write.word_count == 2u &&
d16_hi_byte_write.src_count == 2u &&
d16_hi_byte_write.data_dwords == 1u &&
d16_hi_byte_write.data_bits == 8u &&
d16_hi_byte_write.offset == 0x200u && !d16_hi_byte_write.gds &&
d16_hi_byte_write.src0.reg == 19u &&
d16_hi_byte_write.src1.reg == 20u &&
d16_hi_byte_write.src1.sdwa_sel == 2u,
"DS decoder rejected the captured high-half byte write");
constexpr uint32_t packed_source_selectors[][2] = {
{0xcc0e0000u, 0x0c0a0300u}, // Source 0: instruction bit 59.
{0xcc0e0000u, 0x140a0300u}, // Source 1: instruction bit 60.
@@ -6694,9 +6711,9 @@ void TestNewShaderRecompilerFormattedStoreUsesRuntimeArrayLengthOnly() {
Check(result.resources.buffers.size() == 1 &&
result.resources.buffers[0].dwords[2] == 5u,
"formatted store test did not preserve descriptor NumRecords");
Check((result.decoded_dump.find("buffer_store_format_x") != std::string::npos),
Check((result.decoded_dump.find("BUFFER_STORE_FORMAT_X") != std::string::npos),
"formatted store regression did not decode buffer_store_format_x");
Check((result.ir_dump.find("typed=0 formatted=1") != std::string::npos),
Check((result.decoded_dump.find("typed=0 formatted=1") != std::string::npos),
"formatted store regression did not preserve formatted metadata");
CheckSpirvBinaryValidates(result.spirv);
@@ -6706,6 +6723,15 @@ void TestNewShaderRecompilerFormattedStoreUsesRuntimeArrayLengthOnly() {
Check(
!SpirvSourceHasInstructionUsing(source, "OpULessThan", "%uint_5"),
"formatted store SPIR-V baked descriptor NumRecords into a store guard");
user_data[1] = 8u << 16u;
user_data[3] = static_cast<uint32_t>(Prospero::BufferFormat::k16_16_16_16Float) << 12u;
options.user_data = user_data;
result = RecompileForTest(shader, options);
Check((DisassembleSpirvBinary(result.spirv).find("PackHalf2x16") !=
std::string::npos),
"formatted half-float store did not convert F32 to F16");
CheckSpirvBinaryValidates(result.spirv);
}
void TestNewShaderRecompilerTypedBufferTranslation() {
@@ -9885,6 +9911,7 @@ void TestMeshInputAssembly() {
uint32_t capacity, count, group, lane, width, address_low, base_vertex;
uint32_t wave_info, first, second, third, byte_offset, vertex_id;
bool fetch;
uint32_t wave_size = 64;
};
const Case cases[] = {
{Prospero::PrimitiveType::kTriList, 14, 177, 14, 2, 2, 0x1002, UINT32_MAX,
@@ -9931,11 +9958,14 @@ void TestMeshInputAssembly() {
0x40000c0c, 1, 0, 0, 52, 0xabcd0128, true},
{Prospero::PrimitiveType::kPointList, 12, 265, 1, 64, 4, 0x1000, 0,
0x41000000, 64, 0, 0, 304, 0, false},
{Prospero::PrimitiveType::kTriStrip, 40, 40, 0, 32, 0, 0, 11,
0x81000608, 32, 33, 34, 0, 43, false, 32},
};
for (const auto &test : cases) {
ShaderVertexInputInfo input{};
auto &mesh = input.mesh;
mesh.input_primitive = static_cast<uint32_t>(test.topology);
mesh.wave_size = test.wave_size;
mesh.primitives_per_group = mesh.InputPrimitiveCount(test.capacity);
mesh.vertices_per_group = mesh.InputVertexCount(mesh.primitives_per_group);
mesh.threads_num[0] = 256;
@@ -9949,7 +9979,7 @@ void TestMeshInputAssembly() {
graph.entry_block = 0;
Frontend::TranslateOptions options{};
options.stage = ShaderType::Mesh;
options.wave_size = 64;
options.wave_size = test.wave_size;
options.user_data_count = 0;
options.input_info.vertex = &input;
auto program = Frontend::TranslateProgram(decoded, graph, options);
@@ -11478,6 +11508,27 @@ void TestNewShaderRecompilerVertexExportUsesInvocationExecMask() {
}
void TestNewShaderRecompilerPerInvocationMasksWithoutMirrors() {
for (uint32_t wave_size : {32u, 64u}) {
for (uint32_t opcode : {0x09u, 0x0au}) {
for (uint32_t source : {126u, 8u}) {
const uint32_t pixel_shader[] = {
EncodeSop1(0x04, 8, 126), // Save the entry live mask.
EncodeSop1(opcode, 126, source),
EncodeVop1(0x01, 0, 242), // v_mov_b32 v0, 1.0
EncodeExp0(0x00, 0x1), EncodeExp1(0, 0, 0, 0),
EncodeSopp(0x01),
};
auto pixel_options = MakeCompileOptions(ShaderType::Pixel);
pixel_options.wave_size = wave_size;
const auto pixel_result = RecompileForTest(pixel_shader, pixel_options);
CheckSpirvBinaryValidates(pixel_result.spirv);
Check(DisassembleSpirvBinary(pixel_result.spirv).find("OpGroupNonUniformBallot") ==
std::string::npos,
"entry WQM lost the known live predicate through a scalar ballot");
}
}
}
const uint32_t local_shader[] = {
EncodeVopc(0xc1, 5 + 256, 8), // v_cmp_lt_u32 vcc, v5, v8
EncodeSop2(0x0f, 2, 126, 106), // s_and_b64 s[2:3], exec, vcc
@@ -13494,6 +13545,7 @@ int main() {
TestNewShaderRecompilerNativeWideBufferIr();
TestNewShaderRecompilerScalarB64LaneTranslation();
TestNewShaderRecompilerMubufFormatTranslation();
TestNewShaderRecompilerFormattedStoreUsesRuntimeArrayLengthOnly();
TestNewShaderRecompilerTypedBufferTranslation();
TestNewShaderRecompilerDsReadWrite2Translation();
TestNewShaderRecompilerDsWideAndAtomicTranslation();