diff --git a/Makefile b/Makefile index 9c5deb6..39897a2 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,30 @@ -CFLAGS = -std=c++17 -O2 +CXX ?= g++ +CCACHE := $(shell command -v ccache 2>/dev/null) +ifneq ($(CCACHE),) +CXX := ccache $(CXX) +endif +CXXFLAGS = -std=c++17 -O2 -MMD -MP LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi SOURCES = $(wildcard *.cpp) HEADERS = $(wildcard *.hpp) +OBJECTS = $(SOURCES:%.cpp=build/%.o) +DEPS = $(OBJECTS:.o=.d) -VulkanTest: $(SOURCES) $(HEADERS) - ./compile.sh - g++ $(CFLAGS) -o VulkanTest $(SOURCES) $(LDFLAGS) +SHADERS = shaders/simple_shader.vert shaders/simple_shader.frag +SPV = $(SHADERS:=.spv) + +VulkanTest: $(SPV) $(OBJECTS) + $(CXX) $(CXXFLAGS) -o VulkanTest $(OBJECTS) $(LDFLAGS) + +build/%.o: %.cpp $(HEADERS) | build + $(CXX) $(CXXFLAGS) -c $< -o $@ + +shaders/%.spv: shaders/% + /usr/local/bin/glslc $< -o $@ + +build: + mkdir -p build .PHONY: test clean @@ -14,4 +32,8 @@ test: VulkanTest ./VulkanTest clean: - rm -f VulkanTest \ No newline at end of file + rm -f VulkanTest + rm -rf build + rm -f $(SPV) + +-include $(DEPS) \ No newline at end of file diff --git a/VulkanTest b/VulkanTest index a2d8df6..7fea3c0 100755 Binary files a/VulkanTest and b/VulkanTest differ diff --git a/build/cve_device.d b/build/cve_device.d new file mode 100644 index 0000000..48bd825 --- /dev/null +++ b/build/cve_device.d @@ -0,0 +1,3 @@ +build/cve_device.o: cve_device.cpp cve_device.hpp cve_window.hpp +cve_device.hpp: +cve_window.hpp: diff --git a/build/cve_device.o b/build/cve_device.o new file mode 100644 index 0000000..5c49850 Binary files /dev/null and b/build/cve_device.o differ diff --git a/build/cve_model.d b/build/cve_model.d new file mode 100644 index 0000000..e007a78 --- /dev/null +++ b/build/cve_model.d @@ -0,0 +1,5 @@ +build/cve_model.o: cve_model.cpp cve_model.hpp cve_device.hpp \ + cve_window.hpp +cve_model.hpp: +cve_device.hpp: +cve_window.hpp: diff --git a/build/cve_model.o b/build/cve_model.o new file mode 100644 index 0000000..040530c Binary files /dev/null and b/build/cve_model.o differ diff --git a/build/cve_pipeline.d b/build/cve_pipeline.d new file mode 100644 index 0000000..da4a20e --- /dev/null +++ b/build/cve_pipeline.d @@ -0,0 +1,6 @@ +build/cve_pipeline.o: cve_pipeline.cpp cve_pipeline.hpp cve_device.hpp \ + cve_window.hpp cve_model.hpp +cve_pipeline.hpp: +cve_device.hpp: +cve_window.hpp: +cve_model.hpp: diff --git a/build/cve_pipeline.o b/build/cve_pipeline.o new file mode 100644 index 0000000..2c4ecc2 Binary files /dev/null and b/build/cve_pipeline.o differ diff --git a/build/cve_swap_chain.d b/build/cve_swap_chain.d new file mode 100644 index 0000000..ec645b3 --- /dev/null +++ b/build/cve_swap_chain.d @@ -0,0 +1,5 @@ +build/cve_swap_chain.o: cve_swap_chain.cpp cve_swap_chain.hpp \ + cve_device.hpp cve_window.hpp +cve_swap_chain.hpp: +cve_device.hpp: +cve_window.hpp: diff --git a/build/cve_swap_chain.o b/build/cve_swap_chain.o new file mode 100644 index 0000000..f1f3b72 Binary files /dev/null and b/build/cve_swap_chain.o differ diff --git a/build/cve_window.d b/build/cve_window.d new file mode 100644 index 0000000..c5a4822 --- /dev/null +++ b/build/cve_window.d @@ -0,0 +1,2 @@ +build/cve_window.o: cve_window.cpp cve_window.hpp +cve_window.hpp: diff --git a/build/cve_window.o b/build/cve_window.o new file mode 100644 index 0000000..b36818a Binary files /dev/null and b/build/cve_window.o differ diff --git a/build/first_app.d b/build/first_app.d new file mode 100644 index 0000000..d6f1eb0 --- /dev/null +++ b/build/first_app.d @@ -0,0 +1,8 @@ +build/first_app.o: first_app.cpp first_app.hpp cve_window.hpp \ + cve_pipeline.hpp cve_device.hpp cve_swap_chain.hpp cve_model.hpp +first_app.hpp: +cve_window.hpp: +cve_pipeline.hpp: +cve_device.hpp: +cve_swap_chain.hpp: +cve_model.hpp: diff --git a/build/first_app.o b/build/first_app.o new file mode 100644 index 0000000..363cf58 Binary files /dev/null and b/build/first_app.o differ diff --git a/build/main.d b/build/main.d new file mode 100644 index 0000000..0bd2a62 --- /dev/null +++ b/build/main.d @@ -0,0 +1,8 @@ +build/main.o: main.cpp first_app.hpp cve_window.hpp cve_pipeline.hpp \ + cve_device.hpp cve_swap_chain.hpp cve_model.hpp +first_app.hpp: +cve_window.hpp: +cve_pipeline.hpp: +cve_device.hpp: +cve_swap_chain.hpp: +cve_model.hpp: diff --git a/build/main.o b/build/main.o new file mode 100644 index 0000000..27ab73b Binary files /dev/null and b/build/main.o differ diff --git a/first_app.cpp b/first_app.cpp index c540d68..885e034 100644 --- a/first_app.cpp +++ b/first_app.cpp @@ -3,7 +3,16 @@ #include #include +#define GLM_FORCE_RADIANS +#define GLM_FORCE_DEPTH_ZERO_TO_ONE +#include + namespace cve { + struct SimplePushConstantData{ + glm::vec2 offset; + alignas(16) glm::vec3 color; + }; + FirstApp::FirstApp() { loadModels(); createPipelineLayout(); @@ -35,12 +44,17 @@ namespace cve { } void FirstApp::createPipelineLayout() { + VkPushConstantRange pushConstantRange{}; + pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT; + pushConstantRange.offset = 0; + pushConstantRange.size = sizeof(SimplePushConstantData); + VkPipelineLayoutCreateInfo pipelineLayoutInfo{}; pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutInfo.setLayoutCount = 0; pipelineLayoutInfo.pSetLayouts = nullptr; - pipelineLayoutInfo.pushConstantRangeCount = 0; - pipelineLayoutInfo.pPushConstantRanges = nullptr; + pipelineLayoutInfo.pushConstantRangeCount = 1; + pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange; if (vkCreatePipelineLayout(cveDevice.device(), &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) { throw std::runtime_error("Failed to create pipeline layout"); @@ -108,6 +122,9 @@ namespace cve { } void FirstApp::recordCommandBuffer(int imageIndex) { + static int frame = 30; + frame = (frame+1) % 2000; + VkCommandBufferBeginInfo beginInfo{}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; @@ -124,7 +141,7 @@ namespace cve { renderPassInfo.renderArea.extent = cveSwapChain->getSwapChainExtent(); std::array clearValues{}; - clearValues[0].color = {0.1f, 0.1f, 0.1f, 1.0f}; + clearValues[0].color = {0.01f, 0.01f, 0.01f, 1.0f}; clearValues[1].depthStencil = {1.0f, 0}; renderPassInfo.clearValueCount = static_cast(clearValues.size()); renderPassInfo.pClearValues = clearValues.data(); @@ -144,7 +161,24 @@ namespace cve { cvePipeline->bind(commandBuffers[imageIndex]); cveModel->bind(commandBuffers[imageIndex]); - cveModel->draw(commandBuffers[imageIndex]); + + for (int j = 0; j < 4; j++) { + SimplePushConstantData push{}; + push.offset = {-0.5f + frame * 0.0005f, -0.4f + j * 0.25f}; + push.color = {0.0f, 0.0f, 0.2f + 0.2f * j}; + + vkCmdPushConstants( + commandBuffers[imageIndex], + pipelineLayout, + VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, + 0, + sizeof(SimplePushConstantData), + &push + ); + cveModel->draw(commandBuffers[imageIndex]); + } + + vkCmdEndRenderPass(commandBuffers[imageIndex]); if (vkEndCommandBuffer(commandBuffers[imageIndex]) != VK_SUCCESS) { diff --git a/shaders/simple_shader.frag b/shaders/simple_shader.frag index ba9894d..6092dcd 100644 --- a/shaders/simple_shader.frag +++ b/shaders/simple_shader.frag @@ -1,9 +1,12 @@ #version 450 -layout(location = 0) in vec3 fragColor; - layout(location = 0) out vec4 outColor; +layout(push_constant) uniform Push { + vec2 offset; + vec3 color; +} push; + void main() { - outColor = vec4(fragColor, 1.0); + outColor = vec4(push.color, 1.0); } \ No newline at end of file diff --git a/shaders/simple_shader.frag.spv b/shaders/simple_shader.frag.spv index da37f7e..6cc4cd4 100644 Binary files a/shaders/simple_shader.frag.spv and b/shaders/simple_shader.frag.spv differ diff --git a/shaders/simple_shader.vert b/shaders/simple_shader.vert index 662c096..cfa9bc6 100644 --- a/shaders/simple_shader.vert +++ b/shaders/simple_shader.vert @@ -3,9 +3,11 @@ layout(location = 0) in vec2 position; layout(location = 1) in vec3 color; -layout(location = 0) out vec3 fragColor; +layout(push_constant) uniform Push { + vec2 offset; + vec3 color; +} push; void main() { - gl_Position = vec4(position, 0.0, 1.0); - fragColor = color; + gl_Position = vec4(position + push.offset, 0.0, 1.0); } diff --git a/shaders/simple_shader.vert.spv b/shaders/simple_shader.vert.spv index 75cc466..d1f503f 100644 Binary files a/shaders/simple_shader.vert.spv and b/shaders/simple_shader.vert.spv differ