Skip to content

Commit

Permalink
添加计算着色器, 处理图片
Browse files Browse the repository at this point in the history
  • Loading branch information
luxiaodong committed Aug 29, 2022
1 parent ef20302 commit 287749a
Show file tree
Hide file tree
Showing 21 changed files with 809 additions and 23 deletions.
14 changes: 14 additions & 0 deletions Vulkan/Vulkan_Sample/Vulkan.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
B0272D7128B4C609002D3602 /* deferredmutisampling.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272D7028B4C609002D3602 /* deferredmutisampling.cpp */; };
B0272D7628B4E294002D3602 /* deferredshadows.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272D7528B4E294002D3602 /* deferredshadows.cpp */; };
B0272D8128B71F82002D3602 /* ssao.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272D7A28B70A45002D3602 /* ssao.cpp */; };
B0272D8628B899F9002D3602 /* computershader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B0272D8428B899F9002D3602 /* computershader.cpp */; };
B066A145286213A600763515 /* assets in CopyFiles */ = {isa = PBXBuildFile; fileRef = B066A1422862125900763515 /* assets */; };
B066DE1E28A4FF5D00726A95 /* thread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B066DE1D28A4FF5D00726A95 /* thread.cpp */; };
B066DE2328A5074600726A95 /* multithread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B066DE2128A5074600726A95 /* multithread.cpp */; };
Expand Down Expand Up @@ -111,6 +112,8 @@
B0272D7528B4E294002D3602 /* deferredshadows.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = deferredshadows.cpp; sourceTree = "<group>"; };
B0272D7928B70A45002D3602 /* ssao.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ssao.h; sourceTree = "<group>"; };
B0272D7A28B70A45002D3602 /* ssao.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ssao.cpp; sourceTree = "<group>"; };
B0272D8428B899F9002D3602 /* computershader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = computershader.cpp; sourceTree = "<group>"; };
B0272D8528B899F9002D3602 /* computershader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = computershader.h; sourceTree = "<group>"; };
B066A1422862125900763515 /* assets */ = {isa = PBXFileReference; lastKnownFileType = folder; path = assets; sourceTree = "<group>"; };
B066DE1C28A4FF5D00726A95 /* thread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = thread.h; sourceTree = "<group>"; };
B066DE1D28A4FF5D00726A95 /* thread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = thread.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -308,6 +311,15 @@
path = ssao;
sourceTree = "<group>";
};
B0272D8328B899F9002D3602 /* computershader */ = {
isa = PBXGroup;
children = (
B0272D8428B899F9002D3602 /* computershader.cpp */,
B0272D8528B899F9002D3602 /* computershader.h */,
);
path = computershader;
sourceTree = "<group>";
};
B066DE2028A5074600726A95 /* multithread */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -737,6 +749,7 @@
B0E13A182861729300D1D2B6 /* sample */ = {
isa = PBXGroup;
children = (
B0272D8328B899F9002D3602 /* computershader */,
B0272D7828B70A45002D3602 /* ssao */,
B0272D7328B4E294002D3602 /* deferredshadows */,
B0272D6E28B4C609002D3602 /* deferredmutisampling */,
Expand Down Expand Up @@ -888,6 +901,7 @@
B066DE5C28ACD54300726A95 /* svpng.c in Sources */,
B0B5D0C5287ECA78003A175D /* specializationconstants.cpp in Sources */,
B0B5D01F2875293B003A175D /* ui.cpp in Sources */,
B0272D8628B899F9002D3602 /* computershader.cpp in Sources */,
B0B5D10B288A7935003A175D /* separatevertexattributes.cpp in Sources */,
B0B5D0BA287D50A3003A175D /* dynamicuniformbuffer.cpp in Sources */,
B0B5D09728783317003A175D /* gltfModel.cpp in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#version 450

layout (local_size_x = 16, local_size_y = 16) in;
layout (binding = 0, rgba8) uniform readonly image2D inputImage;
layout (binding = 1, rgba8) uniform image2D resultImage;

float conv(in float[9] kernel, in float[9] data, in float denom, in float offset)
{
float res = 0.0;
for (int i=0; i<9; ++i)
{
res += kernel[i] * data[i];
}
return clamp(res/denom + offset, 0.0, 1.0);
}

struct ImageData
{
float avg[9];
} imageData;

void main()
{
// Fetch neighbouring texels
int n = -1;
for (int i=-1; i<2; ++i)
{
for(int j=-1; j<2; ++j)
{
n++;
vec3 rgb = imageLoad(inputImage, ivec2(gl_GlobalInvocationID.x + i, gl_GlobalInvocationID.y + j)).rgb;
imageData.avg[n] = (rgb.r + rgb.g + rgb.b) / 3.0;
}
}

float[9] kernel;
kernel[0] = -1.0/8.0; kernel[1] = -1.0/8.0; kernel[2] = -1.0/8.0;
kernel[3] = -1.0/8.0; kernel[4] = 1.0; kernel[5] = -1.0/8.0;
kernel[6] = -1.0/8.0; kernel[7] = -1.0/8.0; kernel[8] = -1.0/8.0;

vec4 res = vec4(vec3(conv(kernel, imageData.avg, 0.1, 0.0)), 1.0);

imageStore(resultImage, ivec2(gl_GlobalInvocationID.xy), res);
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#version 450

layout (local_size_x = 16, local_size_y = 16) in;
layout (binding = 0, rgba8) uniform readonly image2D inputImage;
layout (binding = 1, rgba8) uniform image2D resultImage;

float conv(in float[9] kernel, in float[9] data, in float denom, in float offset)
{
float res = 0.0;
for (int i=0; i<9; ++i)
{
res += kernel[i] * data[i];
}
return clamp(res/denom + offset, 0.0, 1.0);
}

struct ImageData
{
float avg[9];
} imageData;

void main()
{
// Fetch neighbouring texels
int n = -1;
for (int i=-1; i<2; ++i)
{
for(int j=-1; j<2; ++j)
{
n++;
vec3 rgb = imageLoad(inputImage, ivec2(gl_GlobalInvocationID.x + i, gl_GlobalInvocationID.y + j)).rgb;
imageData.avg[n] = (rgb.r + rgb.g + rgb.b) / 3.0;
}
}

float[9] kernel;
kernel[0] = -1.0; kernel[1] = 0.0; kernel[2] = 0.0;
kernel[3] = 0.0; kernel[4] = -1.0; kernel[5] = 0.0;
kernel[6] = 0.0; kernel[7] = 0.0; kernel[8] = 2.0;

vec4 res = vec4(vec3(conv(kernel, imageData.avg, 1.0, 0.50)), 1.0);

imageStore(resultImage, ivec2(gl_GlobalInvocationID.xy), res);
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#version 450

layout (local_size_x = 16, local_size_y = 16) in;
layout (binding = 0, rgba8) uniform readonly image2D inputImage;
layout (binding = 1, rgba8) uniform image2D resultImage;

float conv(in float[9] kernel, in float[9] data, in float denom, in float offset)
{
float res = 0.0;
for (int i=0; i<9; ++i)
{
res += kernel[i] * data[i];
}
return clamp(res/denom + offset, 0.0, 1.0);
}

struct ImageData
{
float r[9];
float g[9];
float b[9];
} imageData;

void main()
{

// Fetch neighbouring texels
int n = -1;
for (int i=-1; i<2; ++i)
{
for(int j=-1; j<2; ++j)
{
n++;
vec3 rgb = imageLoad(inputImage, ivec2(gl_GlobalInvocationID.x + i, gl_GlobalInvocationID.y + j)).rgb;
imageData.r[n] = rgb.r;
imageData.g[n] = rgb.g;
imageData.b[n] = rgb.b;
}
}

float[9] kernel;
kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0;
kernel[3] = -1.0; kernel[4] = 9.0; kernel[5] = -1.0;
kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0;

vec4 res = vec4(
conv(kernel, imageData.r, 1.0, 0.0),
conv(kernel, imageData.g, 1.0, 0.0),
conv(kernel, imageData.b, 1.0, 0.0),
1.0);

imageStore(resultImage, ivec2(gl_GlobalInvocationID.xy), res);
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 450

layout (binding = 1) uniform sampler2D samplerColor;

layout (location = 0) in vec2 inUV;

layout (location = 0) out vec4 outFragColor;

void main()
{
outFragColor = texture(samplerColor, inUV);
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#version 450

layout (location = 0) in vec3 inPos;
layout (location = 1) in vec2 inUV;

layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
} ubo;

layout (location = 0) out vec2 outUV;

out gl_PerVertex
{
vec4 gl_Position;
};

void main()
{
outUV = inUV;
gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0);
}
Binary file not shown.
Binary file not shown.
40 changes: 25 additions & 15 deletions Vulkan/Vulkan_Sample/Vulkan/common/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void Application::init()
Tools::m_deviceEnabledFeatures = m_deviceEnabledFeatures;
Tools::m_deviceProperties = m_deviceProperties;
Tools::m_graphicsQueue = m_graphicsQueue;
Tools::m_computerQueue = m_computerQueue;
createCommandPool();
Tools::m_commandPool = m_commandPool;

Expand Down Expand Up @@ -155,7 +156,7 @@ void Application::render()
vkResetFences(m_device, 1, &m_inFlightFences[m_currentFrame]);

vkAcquireNextImageKHR(m_device, m_swapchainKHR, UINT64_MAX, m_imageAvailableSemaphores[m_currentFrame], VK_NULL_HANDLE, &m_imageIndex);

VkCommandBuffer commandBuffer = m_commandBuffers[m_imageIndex];
beginRenderCommandAndPass(commandBuffer, m_imageIndex);
recordRenderCommand(commandBuffer);
Expand Down Expand Up @@ -370,18 +371,21 @@ void Application::createLogicDeivce()
m_familyIndices = findQueueFamilyIndices();
uint32_t graphicsFamily = m_familyIndices.graphicsFamily.value();
uint32_t presentFamily = m_familyIndices.presentFamily.value();
uint32_t computerFamily = m_familyIndices.computerFamily.value();

std::vector<uint32_t> familyIndexs = {};
familyIndexs.push_back(graphicsFamily);

if ( graphicsFamily == presentFamily )
if(presentFamily != graphicsFamily)
{
familyIndexs.push_back(graphicsFamily);
}
else
{
familyIndexs.push_back(graphicsFamily);
familyIndexs.push_back(presentFamily);
}

if((computerFamily != graphicsFamily) && (computerFamily != presentFamily))
{
familyIndexs.push_back(computerFamily);
}

std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
for(uint32_t index : familyIndexs)
{
Expand Down Expand Up @@ -412,6 +416,7 @@ void Application::createLogicDeivce()

vkGetDeviceQueue(m_device, graphicsFamily, 0, &m_graphicsQueue);
vkGetDeviceQueue(m_device, presentFamily, 0, &m_presentQueue);
vkGetDeviceQueue(m_device, computerFamily, 0, &m_computerQueue);
}

void Application::createSwapchain()
Expand Down Expand Up @@ -679,13 +684,13 @@ void Application::createCommandBuffers()
{
m_commandBuffers.resize(m_framebuffers.size());

VkCommandBufferAllocateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
createInfo.commandPool = m_commandPool;
createInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
createInfo.commandBufferCount = static_cast<uint32_t>(m_commandBuffers.size());
VkCommandBufferAllocateInfo allocateInfo = {};
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.commandPool = m_commandPool;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandBufferCount = static_cast<uint32_t>(m_commandBuffers.size());

if( vkAllocateCommandBuffers(m_device, &createInfo, m_commandBuffers.data()) != VK_SUCCESS )
if( vkAllocateCommandBuffers(m_device, &allocateInfo, m_commandBuffers.data()) != VK_SUCCESS )
{
throw std::runtime_error("failed to create command buffer!");
}
Expand Down Expand Up @@ -786,10 +791,15 @@ QueueFamilyIndices Application::findQueueFamilyIndices()
uint32_t i = 0;
for(auto properties : queueFamilyProperties)
{
if( properties.queueFlags & VK_QUEUE_GRAPHICS_BIT )
if( properties.queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
indices.graphicsFamily = i;
}

if( properties.queueFlags & VK_QUEUE_COMPUTE_BIT)
{
indices.computerFamily = i;
}

VkBool32 supported;
vkGetPhysicalDeviceSurfaceSupportKHR(m_physicalDevice, i, m_surfaceKHR, &supported);
Expand All @@ -798,7 +808,7 @@ QueueFamilyIndices Application::findQueueFamilyIndices()
indices.presentFamily = i;
}

if(indices.graphicsFamily.has_value() && indices.presentFamily.has_value())
if(indices.isComplete())
{
break;
}
Expand Down
6 changes: 4 additions & 2 deletions Vulkan/Vulkan_Sample/Vulkan/common/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ struct QueueFamilyIndices
{
std::optional<uint32_t> graphicsFamily;
std::optional<uint32_t> presentFamily;
std::optional<uint32_t> computerFamily;

bool isComplete(){
return graphicsFamily.has_value() && presentFamily.has_value();
return graphicsFamily.has_value() && presentFamily.has_value() && computerFamily.has_value();
}
};

Expand All @@ -32,7 +33,7 @@ class Application
void run();
void loop();
void logic();
void render();
virtual void render();
virtual void betweenInitAndLoop();
virtual void updateRenderData();
void beginRenderCommandAndPass(const VkCommandBuffer commandBuffer, int frameBufferIndex);
Expand Down Expand Up @@ -97,6 +98,7 @@ class Application
VkPhysicalDevice m_physicalDevice;
VkDevice m_device;
QueueFamilyIndices m_familyIndices;
VkQueue m_computerQueue;
VkQueue m_graphicsQueue;
VkQueue m_presentQueue;

Expand Down
6 changes: 3 additions & 3 deletions Vulkan/Vulkan_Sample/Vulkan/common/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void Texture::fillTextrue(Texture* texture, void* buffer, VkDeviceSize bufferSiz
stagingBuffer, stagingMemory);
Tools::mapMemory(stagingMemory, bufferSize, buffer);
Tools::createImageAndMemoryThenBind(texture->m_fromat, texture->m_width, texture->m_height, texture->m_mipLevels, texture->m_layerCount,
VK_SAMPLE_COUNT_1_BIT, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_SAMPLE_COUNT_1_BIT, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT,
VK_IMAGE_TILING_OPTIMAL, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
texture->m_image, texture->m_imageMemory);

Expand Down Expand Up @@ -318,7 +318,7 @@ void Texture::fillTextrue(Texture* texture, ktxTexture* ktxTexture, VkQueue tran
}

Tools::createImageAndMemoryThenBind(texture->m_fromat, texture->m_width, texture->m_height, texture->m_mipLevels, layerCount,
VK_SAMPLE_COUNT_1_BIT, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_SAMPLE_COUNT_1_BIT, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT,
VK_IMAGE_TILING_OPTIMAL, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
texture->m_image, texture->m_imageMemory, flags);

Expand Down Expand Up @@ -540,7 +540,7 @@ Texture* Texture::loadTextrue2D(std::string fileName, VkQueue transferQueue, VkF
}

Tools::createImageAndMemoryThenBind(format, newTexture->m_width, newTexture->m_height, newTexture->m_mipLevels, layerCount,
VK_SAMPLE_COUNT_1_BIT, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_SAMPLE_COUNT_1_BIT, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT,
VK_IMAGE_TILING_OPTIMAL, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
newTexture->m_image, newTexture->m_imageMemory, flags);

Expand Down
Loading

0 comments on commit 287749a

Please sign in to comment.