From c711a90592ab6f5ef710e61c2c32042710dd7001 Mon Sep 17 00:00:00 2001 From: Xiang Silian Date: Wed, 24 Sep 2025 23:23:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=83=8C=E6=99=AF=E7=9A=84?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/texture_loading/texture_loading.cpp | 34 ++++++++++++++++--- samples/api/texture_loading/texture_loading.h | 6 ++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/samples/api/texture_loading/texture_loading.cpp b/samples/api/texture_loading/texture_loading.cpp index 070a855..e98f626 100644 --- a/samples/api/texture_loading/texture_loading.cpp +++ b/samples/api/texture_loading/texture_loading.cpp @@ -562,6 +562,19 @@ void TextureLoading::build_command_buffers() vkCmdBindDescriptorSets(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout_bg, 0, 1, &descriptor_set_bg, 0, NULL); vkCmdBindPipeline(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.background); + + // 在命令缓冲区记录时 + float myFloatValue = 1.5f; // 你的 float 值 + + vkCmdPushConstants( + draw_cmd_buffers[i], // 命令缓冲区 + pipeline_layout_bg, // 管线布局 + VK_SHADER_STAGE_VERTEX_BIT, // + 0, // 偏移量(字节) + sizeof(float), // 数据大小(字节) + &myFloatValue // 数据指针 + ); + vkCmdDraw(draw_cmd_buffers[i], 6, 1, 0, 0); //vkCmdBindDescriptorSets(draw_cmd_buffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_layout, 0, 1, &descriptor_set, 0, NULL); @@ -759,6 +772,14 @@ void TextureLoading::setup_descriptor_set_layout_bg() &descriptor_set_layout_bg, 1); + VkPushConstantRange pushConstantRange{}; + pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; // 只在片段着色器中使用 + pushConstantRange.offset = 0; + pushConstantRange.size = sizeof(float); // 或者 sizeof(PushConstants) + + pipeline_layout_create_info.pushConstantRangeCount = 1; + pipeline_layout_create_info.pPushConstantRanges = &pushConstantRange; + VK_CHECK(vkCreatePipelineLayout(get_device().get_handle(), &pipeline_layout_create_info, nullptr, &pipeline_layout_bg)); } @@ -1427,18 +1448,21 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options) size_t dataSize = testImage.size(); processWithVulkan(testImage.data(), width, height, rowStride, dataSize, texture_point); +#ifndef _WIN32 width = 640; height = 480; rowStride = width * 4; auto arrowImage = generateSimpleTestImage(width, height, 80); dataSize = arrowImage.size(); processWithVulkan(arrowImage.data(), width, height, rowStride, dataSize, cam_text); +#else - //const char* filename = "face.png"; - //std::vector image; - //unsigned w, h; - //unsigned error = lodepng::decode(image, w, h, filename, LCT_RGBA, 8); - //processWithVulkan(image.data(), w, h, w*4, image.size(), cam_text); + const char* filename = "face.png"; + std::vector image; + unsigned w, h; + unsigned error = lodepng::decode(image, w, h, filename, LCT_RGBA, 8); + processWithVulkan(image.data(), w, h, w*4, image.size(), cam_text); +#endif width = 256; height = 256; diff --git a/samples/api/texture_loading/texture_loading.h b/samples/api/texture_loading/texture_loading.h index 8f5bf5b..d1886ea 100644 --- a/samples/api/texture_loading/texture_loading.h +++ b/samples/api/texture_loading/texture_loading.h @@ -47,6 +47,12 @@ struct LineVertex { glm::vec3 lineEnd; // 该顶点所属线段的终点 }; +// 最简单的结构体 - 只有一个 float +struct PushConstants { + float myValue; +}; + + class TextureLoading : public ApiVulkanSample { public: