Make Vulkan shader/UBO/layout path real enough for end-to-end blits

- Add shared UBO layout across vertex + fragment stages so vertex
  uniforms like ove_mvpmat compile and bind correctly.
- Rewrite uniform extraction/injection to avoid corrupting the UBO
  block and to keep explicit sampler bindings stable.
- Make the UBO and sampler descriptor bindings visible to both vertex
  and fragment stages.
- Add layout locations for vertex varyings (ove_texcoord) and fragment
  inputs so SPIR-V generation succeeds.
- Extend TransitionImageLayout() to cover all layout pairs used by
  upload/download/clear/blit.
- Add subpass dependencies to the cached render pass and leave
  destinations in SHADER_READ_ONLY_OPTIMAL after Blit().
- Move descriptor allocation before command recording and abort cleanly
  if allocation fails; switch Blit() to the persistent linear sampler.
- Add a gtest that creates a Vulkan backend, uploads a red U8 RGBA
  texture, blits through the default pass-through shader, and verifies
  the downloaded pixel is red. This test passes on the current system.

ctest passes 4/4; DynamicRenderBackend.* passes 3/1 (Vulkan fallback
skipped because Vulkan is available).
This commit is contained in:
2026-07-13 10:19:30 +08:00
parent f1d19a03f8
commit f4906862a1
3 changed files with 468 additions and 205 deletions
@@ -1,6 +1,14 @@
#include <gtest/gtest.h>
#include <QByteArray>
#include <QMatrix4x4>
#include "node/value.h"
#include "render/backend/dynamicrenderer.h"
#include "render/job/shaderjob.h"
#include "render/shadercode.h"
#include "render/texture.h"
#include "render/videoparams.h"
TEST(DynamicRenderBackend, LoadsExperimentalOpenGLBackend)
{
@@ -62,3 +70,79 @@ TEST(DynamicRenderBackend, FallsBackWhenExperimentalVulkanUnavailable)
EXPECT_STREQ(info.name, "opengl");
#endif
}
TEST(DynamicRenderBackend, VulkanUploadBlitDownload)
{
#ifndef OAK_ENABLE_DYNAMIC_RENDER_BACKEND
GTEST_SKIP() << "Dynamic render backend is not enabled in this build";
#else
olive::DynamicRenderer renderer(QStringLiteral("vulkan"));
ASSERT_TRUE(renderer.Load());
OakRenderBackendInfo info = {};
ASSERT_TRUE(renderer.GetBackendInfo(&info));
if (info.kind != OAK_RENDER_BACKEND_VULKAN) {
GTEST_SKIP() << "Vulkan backend is not available on this system";
}
ASSERT_TRUE(renderer.Init());
renderer.PostInit();
const int kSize = 64;
olive::VideoParams params(kSize, kSize, olive::PixelFormat::U8,
olive::VideoParams::kRGBAChannelCount);
olive::TexturePtr src = renderer.CreateTexture(params);
ASSERT_NE(src, nullptr);
ASSERT_FALSE(src->IsDummy());
QByteArray src_data(kSize * kSize * 4, 0);
for (int i = 0; i < kSize * kSize; ++i) {
src_data[i * 4 + 0] = static_cast<char>(255); // R
src_data[i * 4 + 1] = static_cast<char>(0); // G
src_data[i * 4 + 2] = static_cast<char>(0); // B
src_data[i * 4 + 3] = static_cast<char>(255); // A
}
src->Upload(src_data.data(), kSize * 4);
olive::TexturePtr dst = renderer.CreateTexture(params);
ASSERT_NE(dst, nullptr);
ASSERT_FALSE(dst->IsDummy());
const QString vert = QStringLiteral(
"uniform mat4 ove_mvpmat;\n"
"in vec4 a_position;\n"
"in vec2 a_texcoord;\n"
"out vec2 ove_texcoord;\n"
"void main() {\n"
" gl_Position = ove_mvpmat * a_position;\n"
" ove_texcoord = a_texcoord;\n"
"}\n");
const QString frag = QStringLiteral(
"uniform sampler2D ove_maintex;\n"
"in vec2 ove_texcoord;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = texture(ove_maintex, ove_texcoord);\n"
"}\n");
QVariant shader = renderer.CreateNativeShader(olive::ShaderCode(frag, vert));
ASSERT_FALSE(shader.isNull());
olive::ShaderJob job;
job.Insert(QStringLiteral("ove_maintex"),
olive::NodeValue(olive::NodeValue::kTexture,
QVariant::fromValue(src)));
job.Insert(QStringLiteral("ove_mvpmat"),
olive::NodeValue(olive::NodeValue::kMatrix, QMatrix4x4()));
renderer.BlitToTexture(shader, job, dst.get(), true);
QByteArray dst_data(kSize * kSize * 4, 0);
dst->Download(dst_data.data(), kSize * 4);
// The default pass-through shader should reproduce the red source pixel.
EXPECT_EQ(static_cast<uint8_t>(dst_data[0]), 255u);
EXPECT_EQ(static_cast<uint8_t>(dst_data[1]), 0u);
EXPECT_EQ(static_cast<uint8_t>(dst_data[2]), 0u);
EXPECT_EQ(static_cast<uint8_t>(dst_data[3]), 255u);
#endif
}