Skip to content

Commit 378a1c2

Browse files
committed
增加点击截屏功能
1 parent 20231da commit 378a1c2

10 files changed

+369
-256
lines changed

CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ include_directories("C:\\VulkanSDK\\1.2.189.2\\Include")
1111
LINK_DIRECTORIES("C:\\VulkanSDK\\1.2.189.2\\Lib")
1212
LINK_DIRECTORIES("D:\\glfw-3.3.5.bin.WIN64\\lib-mingw-w64")
1313

14-
SET(CMAKE_BUILD_TYPE "Release")
14+
set(CMAKE_CXX_STANDARD 17)
15+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
16+
17+
set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/module")
18+
19+
#SET(CMAKE_BUILD_TYPE "Release")
1520
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb -pg")
1621
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
1722

@@ -25,6 +30,7 @@ INCLUDE_DIRECTORIES(include)
2530
INCLUDE_DIRECTORIES(thirdparty)
2631
INCLUDE_DIRECTORIES(source)
2732
INCLUDE_DIRECTORIES(./)
33+
link_directories(./lib)
2834

2935
add_library(
3036
vulkan-framwork
@@ -88,6 +94,7 @@ add_library(
8894
source/VK_SecondaryCommandBuffer.cpp
8995
thirdparty/tiny_obj_loader.cc
9096
thirdparty/stb_image.cpp
97+
thirdparty/tiff.cpp
9198
)
9299

93100
add_subdirectory(demo)

demo/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(DEMOS
1111
sampler
1212
texture
1313
texture-clear-image
14+
texture-save-image
1415
texture-push-descriptor
1516
model
1617
model-model
@@ -29,9 +30,9 @@ foreach(var IN LISTS DEMOS)
2930
message("build ${var}")
3031
add_executable(${var} main-${var}.cpp)
3132
if(WIN32)
32-
target_link_libraries(${var} vulkan-framwork glfw3 vulkan-1)
33+
target_link_libraries(${var} vulkan-framwork glfw3 vulkan-1 tiff)
3334
else()
34-
target_link_libraries(${var} vulkan-framwork glfw vulkan)
35+
target_link_libraries(${var} vulkan-framwork glfw vulkan tiff)
3536
endif()
3637
endforeach()
3738

demo/main-texture-save-image.cpp

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include <glm/mat4x4.hpp>
4+
#include <glm/gtx/transform.hpp>
5+
#include "VK_UniformBuffer.h"
6+
#include "VK_Context.h"
7+
#include "VK_Image.h"
8+
#include "VK_Texture.h"
9+
#include "VK_Pipeline.h"
10+
#include "VK_DynamicState.h"
11+
#include "VK_CommandPool.h"
12+
//#include "VK_Util.h"
13+
#include "tiff.h"
14+
#include <fstream>
15+
16+
using namespace std;
17+
18+
const std::vector<float> vertices = {
19+
-0.5f, -0.5, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.05f, 0.0f,
20+
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
21+
0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.5f,
22+
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.5f, 1.5f
23+
};
24+
25+
const std::vector<uint32_t> indices = {
26+
0, 1, 2, 2, 3, 0
27+
};
28+
29+
VK_Context *context = nullptr;
30+
VK_Pipeline *pipeline = nullptr;
31+
32+
uint32_t updateUniformBufferData(char *&data, uint32_t size)
33+
{
34+
glm::mat4 model = glm::identity<glm::mat4>();
35+
memcpy(data, &model[0][0], size);
36+
return sizeof(model);
37+
}
38+
39+
void onFrameSizeChanged(int width, int height)
40+
{
41+
pipeline->getDynamicState()->applyDynamicViewport({0, 0, (float)width, (float)height, 0, 1});
42+
}
43+
44+
//void writeFile(VK_Context *context, VkImage image, uint32_t width, uint32_t height);
45+
46+
int main()
47+
{
48+
VK_ContextConfig config;
49+
config.debug = true;
50+
config.name = "Save Image";
51+
52+
context = createVkContext(config);
53+
context->createWindow(480, 480, true);
54+
context->setOnFrameSizeChanged(onFrameSizeChanged);
55+
56+
VK_Context::VK_Config vkConfig;
57+
context->initVulkanDevice(vkConfig);
58+
59+
auto shaderSet = context->createShaderSet();
60+
shaderSet->addShader("../shader/texture/vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
61+
shaderSet->addShader("../shader/texture/frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
62+
63+
shaderSet->appendVertexAttributeDescription(0, sizeof (float) * 3, VK_FORMAT_R32G32B32_SFLOAT, 0);
64+
shaderSet->appendVertexAttributeDescription(1, sizeof (float) * 4, VK_FORMAT_R32G32B32A32_SFLOAT,
65+
sizeof(float) * 3);
66+
shaderSet->appendVertexAttributeDescription(2, sizeof (float) * 2, VK_FORMAT_R32G32_SFLOAT,
67+
sizeof(float) * 7);
68+
69+
shaderSet->appendVertexInputBindingDescription(9 * sizeof(float), 0, VK_VERTEX_INPUT_RATE_VERTEX);
70+
71+
VkDescriptorSetLayoutBinding uniformBinding = VK_ShaderSet::createDescriptorSetLayoutBinding(0,
72+
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT);
73+
shaderSet->addDescriptorSetLayoutBinding(uniformBinding);
74+
75+
auto samplerBinding = VK_ShaderSet::createDescriptorSetLayoutBinding(1,
76+
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
77+
VK_SHADER_STAGE_FRAGMENT_BIT);
78+
auto samplerCreateInfo = VK_Sampler::createSamplerCreateInfo();
79+
auto samplerPtr = context->createSampler(samplerCreateInfo);
80+
VkSampler sampler = samplerPtr->getSampler();
81+
samplerBinding.pImmutableSamplers = &sampler;
82+
83+
shaderSet->addDescriptorSetLayoutBinding(samplerBinding);
84+
85+
if (!shaderSet->isValid()) {
86+
std::cerr << "invalid shaderSet" << std::endl;
87+
shaderSet->release();
88+
context->release();
89+
return -1;
90+
}
91+
92+
auto ubo = shaderSet->addUniformBuffer(0, sizeof(float) * 16);
93+
ubo->setWriteDataCallback(updateUniformBufferData);
94+
95+
auto image = context->createImage("../images/cat.png");
96+
97+
auto cmd = context->getCommandPool()->beginSingleTimeCommands();
98+
adjustImageLayout(cmd, image->getImage(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
99+
context->getCommandPool()->endSingleTimeCommands(cmd, context->getGraphicQueue());
100+
writeFile(context, "cat.ppm", image->getImage(), image->getWidth(),
101+
image->getHeight());
102+
103+
cmd = context->getCommandPool()->beginSingleTimeCommands();
104+
adjustImageLayout(cmd, image->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
105+
context->getCommandPool()->endSingleTimeCommands(cmd, context->getGraphicQueue());
106+
107+
auto imageViewCreateInfo = VK_ImageView::createImageViewCreateInfo(image->getImage(),
108+
VK_FORMAT_R8G8B8A8_SRGB);
109+
auto imageView = context->createImageView(imageViewCreateInfo);
110+
shaderSet->addImageView(imageView, 1);
111+
112+
context->initVulkanContext();
113+
pipeline = context->createPipeline(shaderSet);
114+
pipeline->getDynamicState()->addDynamicState(VK_DYNAMIC_STATE_VIEWPORT);
115+
pipeline->create();
116+
pipeline->getDynamicState()->applyDynamicViewport({0, 0, 480, 480, 0, 1});
117+
118+
auto buffer = context->createVertexBuffer(vertices, 9, indices);
119+
pipeline->addRenderBuffer(buffer);
120+
121+
context->createCommandBuffers();
122+
123+
context->run();
124+
context->release();
125+
126+
return 0;
127+
}
128+

demo/main-uniform-buffer.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@ void onFrameSizeChanged(int width, int height)
4040
pipeline->getDynamicState()->applyDynamicViewport({0, 0, (float)width, (float)height, 0, 1});
4141
}
4242

43+
void onMouseButtonCallback(int button, int action, int mods)
44+
{
45+
(void)button;
46+
(void)mods;
47+
48+
if(action)
49+
context->captureScreenShot();
50+
}
51+
4352
int main()
4453
{
4554
VK_ContextConfig config;
4655
config.debug = true;
4756
config.name = "Uniform Demo";
57+
config.mouseCallback = onMouseButtonCallback;
4858

4959
context = createVkContext(config);
5060
context->createWindow(480, 480, true);

include/VK_Context.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ class VK_Context : public VK_Deleter
5656
virtual void setClearDepthStencil(float depth, uint32_t stencil) = 0;
5757

5858
virtual void setLogicalDeviceFeatures(const VkPhysicalDeviceFeatures &features) = 0;
59+
60+
virtual void captureScreenShot() = 0;
5961
public:
6062
virtual VK_ShaderSet *createShaderSet() = 0;
6163

6264
virtual VK_Buffer *createVertexBuffer(const std::vector<float> &vertices, uint32_t count,
6365
const std::vector<uint32_t> &indices = std::vector<uint32_t>(), bool indirectDraw = false) = 0;
6466
virtual VK_Buffer *createVertexBuffer(const std::vector<VK_Vertex> &vertices,
6567
const std::vector<uint32_t> &indices = std::vector<uint32_t>(), bool indirectDraw = false) = 0;
66-
virtual VK_Buffer *createVertexBuffer(const std::string &filename, bool zero = true, bool indirectDraw = false) = 0;
68+
virtual VK_Buffer *createVertexBuffer(const std::string &filename, bool zero = true,
69+
bool indirectDraw = false) = 0;
6770

6871
virtual VK_Buffer *createIndirectBuffer(uint32_t instanceCount, uint32_t oneInstanceSize,
6972
uint32_t vertexCount) = 0;
@@ -83,7 +86,7 @@ class VK_Context : public VK_Deleter
8386
VkBuffer &buffer, VkDeviceMemory &bufferMemory) = 0;
8487
virtual void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) = 0;
8588

86-
virtual VK_CommandPool* getCommandPool()const = 0;
89+
virtual VK_CommandPool *getCommandPool()const = 0;
8790
};
8891

8992
struct VK_ContextConfig {

include/VK_Util.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,14 @@ uint32_t findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter,
6464

6565
void printUUID(uint8_t *pipelineCacheUUID);
6666

67-
void adjustImageLayout(VkCommandBuffer command, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout, uint32_t levelCount = VK_REMAINING_MIP_LEVELS);
67+
void adjustImageLayout(VkCommandBuffer command, VkImage image, VkImageLayout oldLayout,
68+
VkImageLayout newLayout, uint32_t levelCount = VK_REMAINING_MIP_LEVELS);
69+
70+
void writePpm(const std::string &filename, uint32_t width, uint32_t height, VkFormat format,
71+
uint32_t rowPitch, char *ptr);
72+
73+
class VK_Context;
74+
class VK_Image;
75+
void writeFile(VK_Context *context, const std::string &file, VkImage image, uint32_t width,
76+
uint32_t height);
6877
#endif // VK_UTIL_H

model/Panda.mtl

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
2+
# ´´½¨µÄÎļþ:28.08.2014 09:55:34
3+
4+
newmtl Material__7
5+
Ns 64.0000
6+
Ni 1.5000
7+
d 1.0000
8+
Tr 0.0000
9+
Tf 1.0000 1.0000 1.0000
10+
illum 2
11+
Ka 0.0641 0.1582 0.1817
12+
Kd 1.0000 1.0000 1.0000
13+
Ks 0.9000 0.9000 0.9000
14+
Ke 0.0000 0.0000 0.0000
15+
16+
newmtl Material__13
17+
Ns 100.0000
18+
Ni 1.5000
19+
d 1.0000
20+
Tr 0.0000
21+
Tf 1.0000 1.0000 1.0000
22+
illum 2
23+
Ka 0.0641 0.1294 0.2261
24+
Kd 0.1725 0.1725 0.1725
25+
Ks 0.9000 0.9000 0.9000
26+
Ke 0.0000 0.0000 0.0000

0 commit comments

Comments
 (0)