diff --git a/app/src/main/assets/shaders/bg.frag b/app/src/main/assets/shaders/bg.frag index ee29b3d..52a3257 100644 --- a/app/src/main/assets/shaders/bg.frag +++ b/app/src/main/assets/shaders/bg.frag @@ -9,7 +9,33 @@ layout(location = 0) out vec4 outColor; // 纹理采样器 layout(binding = 0) uniform sampler2D texSampler; +layout(push_constant) uniform PushConstants { + float zoom; + float r; + float g; + float b; + float radius; + float ux; + float uy; +} pushConstants; + void main() { - // 采样纹理 - outColor = texture(texSampler, inTexCoord); + // outFragColor = color; + // 获取当前片元的屏幕坐标(左下角为原点) + vec2 fragCoord = gl_FragCoord.xy; + + // 屏幕中心(480x480 的中心是 (240, 240)) + vec2 center = vec2(240.0, 240.0); + + // 计算到中心的距离(欧氏距离,单位:像素) + float dist = length(fragCoord - center); + + // 判断是否在半径之外 + if (dist > pushConstants.radius) { + // 使用 push constant 中的 RGB 颜色(注意 alpha 设为 1.0) + outColor = vec4(pushConstants.r, pushConstants.g, pushConstants.b, 1.0); + } else { + // 采样纹理 + outColor = texture(texSampler, inTexCoord); + } } \ No newline at end of file diff --git a/app/src/main/assets/shaders/bg.frag.spv b/app/src/main/assets/shaders/bg.frag.spv index 8642dc5..dcb02d2 100644 Binary files a/app/src/main/assets/shaders/bg.frag.spv and b/app/src/main/assets/shaders/bg.frag.spv differ diff --git a/app/src/main/assets/shaders/bg.vert b/app/src/main/assets/shaders/bg.vert index 5ff8f1b..da07cd8 100644 --- a/app/src/main/assets/shaders/bg.vert +++ b/app/src/main/assets/shaders/bg.vert @@ -38,7 +38,13 @@ const vec2 texCoords[6] = vec2[6]( // 定义 Push Constant,只有一个 float layout(push_constant) uniform PushConstants { - float myValue; + float zoom; + float r; + float g; + float b; + float radius; + float ux; + float uy; } pushConstants; @@ -48,7 +54,7 @@ layout(location = 0) out vec2 outTexCoord; void main() { // 获取顶点位置 vec2 position = positions[gl_VertexIndex]; - position = position * pushConstants.myValue; + position = position * pushConstants.zoom; // 设置输出位置(Vulkan使用不同的坐标系) gl_Position = vec4(position, 0.0, 1.0); diff --git a/app/src/main/assets/shaders/bg.vert.spv b/app/src/main/assets/shaders/bg.vert.spv index f261695..c90eedd 100644 Binary files a/app/src/main/assets/shaders/bg.vert.spv and b/app/src/main/assets/shaders/bg.vert.spv differ diff --git a/app/src/main/assets/shaders/texture.frag b/app/src/main/assets/shaders/texture.frag index 7fb8a9a..e228877 100644 --- a/app/src/main/assets/shaders/texture.frag +++ b/app/src/main/assets/shaders/texture.frag @@ -6,7 +6,11 @@ layout (location = 0) in vec2 inUV; layout (location = 1) in vec3 inNormal; layout(push_constant) uniform PushConstants { - float myValue; + float zoom; + float r; + float g; + float b; + float radius; float ux; float uy; } pushConstants; @@ -16,10 +20,25 @@ layout (location = 0) out vec4 outFragColor; void main() { - vec2 pos = vec2(pushConstants.ux/4096.f, pushConstants.uy/2048.f); - vec2 uv = pos + vec2(inUV.x/8.f, inUV.y/4.f); - vec4 color = texture(samplerColor, uv); + // outFragColor = color; + // 获取当前片元的屏幕坐标(左下角为原点) + vec2 fragCoord = gl_FragCoord.xy; - outFragColor = color; + // 屏幕中心(480x480 的中心是 (240, 240)) + vec2 center = vec2(240.0, 240.0); + + // 计算到中心的距离(欧氏距离,单位:像素) + float dist = length(fragCoord - center); + + // 判断是否在半径之外 + if (dist > pushConstants.radius) { + // 使用 push constant 中的 RGB 颜色(注意 alpha 设为 1.0) + outFragColor = vec4(pushConstants.r, pushConstants.g, pushConstants.b, 1.0); + } else { + vec2 pos = vec2(pushConstants.ux/4096.f, pushConstants.uy/2048.f); + vec2 uv = pos + vec2(inUV.x/8.f, inUV.y/4.f); + vec4 color = texture(samplerColor, uv); + outFragColor = color; + } } \ No newline at end of file diff --git a/app/src/main/assets/shaders/texture.frag.spv b/app/src/main/assets/shaders/texture.frag.spv index c9188ac..0c4f42b 100644 Binary files a/app/src/main/assets/shaders/texture.frag.spv and b/app/src/main/assets/shaders/texture.frag.spv differ diff --git a/app/src/main/assets/shaders/texture.vert b/app/src/main/assets/shaders/texture.vert index 1cd1180..fe8b0ac 100644 --- a/app/src/main/assets/shaders/texture.vert +++ b/app/src/main/assets/shaders/texture.vert @@ -17,7 +17,11 @@ layout (location = 0) out vec2 outUV; layout (location = 1) out vec3 outNormal; layout(push_constant) uniform PushConstants { - float myValue; + float zoom; + float r; + float g; + float b; + float radius; float ux; float uy; } pushConstants; @@ -32,7 +36,7 @@ void main() outUV = inUV; vec2 position = vec2(inPos.xy*2 -1); - position = position * pushConstants.myValue; + position = position * pushConstants.zoom; gl_Position = vec4(position, 0.5, 1.0); vec4 pos = ubo.model * vec4(inPos, 1.0); diff --git a/app/src/main/assets/shaders/texture.vert.spv b/app/src/main/assets/shaders/texture.vert.spv index 63c5499..2084859 100644 Binary files a/app/src/main/assets/shaders/texture.vert.spv and b/app/src/main/assets/shaders/texture.vert.spv differ diff --git a/vulkan/FaceApp.cpp b/vulkan/FaceApp.cpp index 0ccb824..dbf54e3 100644 --- a/vulkan/FaceApp.cpp +++ b/vulkan/FaceApp.cpp @@ -546,7 +546,7 @@ void FaceApp::setup_descriptor_set_layout() VkPushConstantRange pushConstantRanges[1]; pushConstantRanges[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT| VK_SHADER_STAGE_FRAGMENT_BIT; pushConstantRanges[0].offset = 0; - pushConstantRanges[0].size = sizeof(float)*3; + pushConstantRanges[0].size = sizeof(PushConstants); //pushConstantRanges[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; //pushConstantRanges[1].offset = sizeof(float); @@ -691,7 +691,7 @@ void FaceApp::render(VkCommandBuffer commandBuffer, long long frameTime) vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout_bg, 0, 1, &m_descriptor_set_bg, 0, NULL); vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_graphicsPipeline_bg); - vkCmdPushConstants(commandBuffer, m_pipelineLayout_bg, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(float), &myFloatValue); + vkCmdPushConstants(commandBuffer, m_pipelineLayout_bg, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(pushConstants), &pushConstants); @@ -713,21 +713,21 @@ void FaceApp::render(VkCommandBuffer commandBuffer, long long frameTime) // vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &m_descriptor_sets_right[_curMotionIndex], 0, NULL); //} - float fsValues[3] = { myFloatValue, 0 , 0}; + //float fsValues[3] = { myFloatValue, 0 , 0}; if (_curMotionIndex < _curMotions.size()) { if (_curFrameIndex < _curMotions[_curMotionIndex].frames.size()) { float ux = _curMotions[_curMotionIndex].frames[_curFrameIndex].x; float uy = _curMotions[_curMotionIndex].frames[_curFrameIndex].y; - fsValues[1] = ux; - fsValues[2] = uy; + pushConstants.ux = ux; + pushConstants.uy = uy; } } //fsValues[1] = 0; //fsValues[2] = 360; - vkCmdPushConstants(commandBuffer, m_pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT| VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(fsValues), &fsValues); + vkCmdPushConstants(commandBuffer, m_pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT| VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(pushConstants), &pushConstants); //vkCmdPushConstants(commandBuffer, m_pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(fsValues), fsValues); @@ -1169,7 +1169,7 @@ void FaceApp::setup_descriptor_set_layout_bg() VkPushConstantRange pushConstantRanges[1]; pushConstantRanges[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; pushConstantRanges[0].offset = 0; - pushConstantRanges[0].size = sizeof(float); + pushConstantRanges[0].size = sizeof(PushConstants); pipeline_layout_create_info.pushConstantRangeCount = 1; pipeline_layout_create_info.pPushConstantRanges = &pushConstantRanges[0]; @@ -1372,7 +1372,11 @@ void FaceApp::cleanup() void FaceApp::SetInitArg(const char* arg) { _initArg = json::parse(arg); - + pushConstants.zoom = _initArg.zoom; + pushConstants.r = _initArg.r; + pushConstants.g = _initArg.g; + pushConstants.b = _initArg.b; + pushConstants.radius = _initArg.radius; } void FaceApp::drawFrame(long long frameTime) diff --git a/vulkan/FaceApp.h b/vulkan/FaceApp.h index 8ee3ff8..da49e53 100644 --- a/vulkan/FaceApp.h +++ b/vulkan/FaceApp.h @@ -38,13 +38,23 @@ struct InitArg { int action_fps; float zoom; + float r; + float g; + float b; + float radius; //Motion motion; - NLOHMANN_DEFINE_TYPE_INTRUSIVE(InitArg, action_fps, zoom); + NLOHMANN_DEFINE_TYPE_INTRUSIVE(InitArg, action_fps, zoom, r, g, b, radius); }; struct PushConstants { - float myValue; + float zoom = 0.5f; + float r = 0; + float g = 0; + float b = 0; + float radius = 240; + float ux = 0; + float uy = 0; }; using Callback = std::function; @@ -149,7 +159,8 @@ private: - float myFloatValue = 1.5f; + //float myFloatValue = 1.5f; + PushConstants pushConstants; diff --git a/vulkan/main.cpp b/vulkan/main.cpp index cdc5d42..602e03b 100644 --- a/vulkan/main.cpp +++ b/vulkan/main.cpp @@ -150,6 +150,10 @@ int main() { init_arg.action_fps = 10; init_arg.zoom = 1.5f; + init_arg.r = 1; + init_arg.g = 0; + init_arg.b = 0; + init_arg.radius = 240; string json_str = json(init_arg).dump();