Skip to content

Commit

Permalink
feat(): use pushConstant
Browse files Browse the repository at this point in the history
  • Loading branch information
ylz-at authored and Jinesi Yelizati committed Jun 10, 2022
1 parent d0a9a24 commit 3057106
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 16 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: CMake

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release

jobs:
build:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ${{matrix.os}}

strategy:
matrix:
os: [windows-latest, windows-2019]
cxx_standard: [17, 20]

steps:
- uses: actions/checkout@v3

- name: Install Vulkan SDK
uses: humbletim/[email protected]
with:
version: latest
cache: true

- name: Install Ninja
uses: ashutoshvarma/setup-ninja@master
with:
version: 1.11.0

- if: runner.os == 'Windows'
uses: TheMrMilchmann/setup-msvc-dev@v1
with:
arch: x64

- name: Configure CMake
if: ${{matrix.os}}
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_CXX_STANDARD=${{matrix.cxx_standard}} -G "Ninja"

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Test
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{env.BUILD_TYPE}}

- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: stupid-vulkan
path: |
${{github.workspace}}\binaries\*
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@
out
gen

cmake-build-debug
cmake-build-*
binaries
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ set_property(TARGET ${Recipe_Name} PROPERTY RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBI
set_property(TARGET ${Recipe_Name} PROPERTY CXX_STANDARD 20)
set_property(TARGET ${Recipe_Name} PROPERTY CXX_STANDARD_REQUIRED ON)

set_property(TARGET ${Recipe_Name} PROPERTY C_STANDARD 99)
set_property(TARGET ${Recipe_Name} PROPERTY C_STANDARD 20)
set_property(TARGET ${Recipe_Name} PROPERTY C_STANDARD_REQUIRED ON)

set(resources Draw.vert.spv Draw.frag.spv)
foreach(resource IN LISTS resources)
add_custom_command(
TARGET ${Recipe_Name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/${resource}
${CMAKE_CURRENT_SOURCE_DIR}/binaries/${resource}
)
endforeach()
Binary file removed Draw-frag.spv
Binary file not shown.
23 changes: 20 additions & 3 deletions Draw.frag
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
#version 450

layout (location = 0) in vec4 color;
layout(location = 0) out vec4 outColor;
layout (location = 0) out vec4 outColor;

void main(){
outColor = color;
layout(push_constant) uniform colorBlock {
int constColor;
float mixerValue;
} pushConstantsColorBlock;

vec4 red = vec4(1.0, 0.0, 0.0, 1.0);
vec4 green = vec4(0.0, 1.0, 0.0, 1.0);
vec4 blue = vec4(0.0, 0.0, 1.0, 1.0);

void main()
{
if (pushConstantsColorBlock.constColor == 1)
outColor = red;
else if (pushConstantsColorBlock.constColor == 2)
outColor = green;
else if (pushConstantsColorBlock.constColor == 3)
outColor = blue;
else
outColor = color*pushConstantsColorBlock.mixerValue;
}
Binary file added Draw.frag.spv
Binary file not shown.
File renamed without changes.
2 changes: 2 additions & 0 deletions include/VulkanDrawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class VulkanDrawable : public VulkanDescriptor {

void initScissors(VkCommandBuffer *cmd);

void initPushConstant(VkCommandBuffer *cmd);

void setPipeline(VkPipeline *vulkanPipeline) { pipeline = vulkanPipeline; }

VkPipeline *getPipeline() { return pipeline; }
Expand Down
6 changes: 5 additions & 1 deletion include/VulkanRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class VulkanRenderer {

void createDescriptors();

void createPushConstants();

void destroyCommandBuffer();

void destroyCommandPool();
Expand Down Expand Up @@ -108,9 +110,10 @@ class VulkanRenderer {
VkImageView view;
} Depth;

VkCommandBuffer cmdDepthImage;
VkCommandPool cmdPool;
VkCommandBuffer cmdDepthImage;
VkCommandBuffer cmdVertexBuffer;
VkCommandBuffer cmdPushConstant;

VkRenderPass renderPass;
std::vector<VkFramebuffer> frameBuffers; // Number of frame Buffers corresponding to each swap chain
Expand All @@ -123,4 +126,5 @@ class VulkanRenderer {
std::vector<VulkanDrawable *> drawableList;
VulkanShader shaderObj;
VulkanPipeline pipelineObj;
const bool includeDepth = true;
};
2 changes: 1 addition & 1 deletion source/VulkanApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ VulkanApplication::VulkanApplication() {
instanceObj.layerExtension.getInstanceLayerProperties();

deviceObj = nullptr;
debugFlag = false;
debugFlag = true;
rendererObj = nullptr;
isPrepared = false;
isResizing = false;
Expand Down
40 changes: 38 additions & 2 deletions source/VulkanDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,34 @@ void VulkanDrawable::initScissors(VkCommandBuffer *cmd) {
vkCmdSetScissor(*cmd, 0, NUMBER_OF_SCISSORS, &scissor);
}

void VulkanDrawable::initPushConstant(VkCommandBuffer *cmd){
enum ColorFlag {
RED = 1,
GREEN = 2,
BLUE = 3,
MIXED_COLOR = 4,
};

float mixerValue = 0.3f;
unsigned constColorRGBFlag = MIXED_COLOR;

// Create push constant data, this contain a constant
// color flag and mixer value for non-const color
unsigned pushConstants[2] = {};
pushConstants[0] = constColorRGBFlag;
memcpy(&pushConstants[1], &mixerValue, sizeof(float));

// Check if number of push constants does not exceed the allowed size
uint32_t maxPushConstantSize = deviceObj->gpuProps.limits.maxPushConstantsSize;
if (sizeof(pushConstants) > maxPushConstantSize) {
assert(0);
printf("Push constant size is greater than expected, max allow size is %d", maxPushConstantSize);
}

vkCmdPushConstants(*cmd, pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0,
sizeof(pushConstants), pushConstants);
}

void VulkanDrawable::destroyVertexBuffer() {
vkDestroyBuffer(rendererObj->getDevice()->device, VertexBuffer.buf, nullptr);
vkFreeMemory(rendererObj->getDevice()->device, VertexBuffer.mem, nullptr);
Expand Down Expand Up @@ -343,6 +371,7 @@ void VulkanDrawable::recordCommandBuffer(int currentBuffer, VkCommandBuffer *cmd

initViewports(cmdDraw);
initScissors(cmdDraw);
initPushConstant(cmdDraw);

vkCmdDraw(*cmdDraw, 3 * 2 * 6, 1, 0, 0);
// End of render pass instance recording
Expand Down Expand Up @@ -555,12 +584,19 @@ void VulkanDrawable::createDescriptorSetLayout(bool useTexture)
// Creates the pipeline layout to inject into the pipeline
void VulkanDrawable::createPipelineLayout()
{
// Set up the push constant range
const unsigned pushConstantRangeCount = 1;
VkPushConstantRange pushConstantRanges[pushConstantRangeCount] = {};
pushConstantRanges[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRanges[0].offset = 0;
pushConstantRanges[0].size = 8;

// Create the pipeline layout with the help of descriptor layout.
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pPipelineLayoutCreateInfo.pNext = nullptr;
pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;
pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
pPipelineLayoutCreateInfo.pushConstantRangeCount = pushConstantRangeCount;
pPipelineLayoutCreateInfo.pPushConstantRanges = pushConstantRanges;
pPipelineLayoutCreateInfo.setLayoutCount = (uint32_t)descLayout.size();
pPipelineLayoutCreateInfo.pSetLayouts = descLayout.data();

Expand Down
52 changes: 45 additions & 7 deletions source/VulkanRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ void VulkanRenderer::initialize() {
// Build the vertex buffer
createVertexBuffer();

const bool includeDepth = true;

// Create the render pass now..
createRenderPass(includeDepth);

Expand All @@ -54,6 +52,9 @@ void VulkanRenderer::initialize() {

// Manage the pipeline state objects
createPipelineStateManagement();

// Build the push constants
createPushConstants();
}

void VulkanRenderer::prepare() {
Expand Down Expand Up @@ -358,8 +359,8 @@ void VulkanRenderer::createShaders() {

shaderObj.buildShader((const char*)vertShaderCode, (const char*)fragShaderCode);
#else
vertShaderCode = readFile("./../Draw-vert.spv", &sizeVert);
fragShaderCode = readFile("./../Draw-frag.spv", &sizeFrag);
vertShaderCode = readFile("Draw.vert.spv", &sizeVert);
fragShaderCode = readFile("Draw.frag.spv", &sizeFrag);

shaderObj.buildShaderModuleWithSPV((uint32_t *) vertShaderCode, sizeVert, (uint32_t *) fragShaderCode, sizeFrag);
#endif
Expand All @@ -380,6 +381,44 @@ void VulkanRenderer::createDescriptors() {

}

// this is not used
void VulkanRenderer::createPushConstants() {
CommandBufferMgr::allocCommandBuffer(&deviceObj->device, cmdPool, &cmdPushConstant);
CommandBufferMgr::beginCommandBuffer(cmdPushConstant);

enum ColorFlag {
RED = 1,
GREEN = 2,
BLUE = 3,
MIXED_COLOR = 4,
};

float mixerValue = 0.3f;
unsigned constColorRGBFlag = MIXED_COLOR;

// Create push constant data, this contain a constant
// color flag and mixer value for non-const color
unsigned pushConstants[2] = {};
pushConstants[0] = constColorRGBFlag;
memcpy(&pushConstants[1], &mixerValue, sizeof(float));

// Check if number of push constants does not exceed the allowed size
int maxPushConstantSize = getDevice()->gpuProps.limits.maxPushConstantsSize;
if (sizeof(pushConstants) > maxPushConstantSize) {
assert(0);
printf("Push constant size is greater than expected, max allow size is %d", maxPushConstantSize);
}

for (auto drawableObj : drawableList) {
vkCmdPushConstants(cmdPushConstant, drawableObj->pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0,
sizeof(pushConstants), pushConstants);
}

CommandBufferMgr::endCommandBuffer(cmdPushConstant);
CommandBufferMgr::submitCommandBuffer(deviceObj->queue, &cmdPushConstant);
}


void VulkanRenderer::destroyFramebuffers() {
for (uint32_t i = 0; i < swapChainObj->scPublicVars.swapchainImageCount; i++) {
vkDestroyFramebuffer(deviceObj->device, frameBuffers.at(i), NULL);
Expand Down Expand Up @@ -411,7 +450,7 @@ void VulkanRenderer::destroyDrawableUniformBuffer() {
}

void VulkanRenderer::destroyCommandBuffer() {
VkCommandBuffer cmdBufs[] = {cmdDepthImage};
VkCommandBuffer cmdBufs[] = {cmdDepthImage, cmdVertexBuffer, cmdPushConstant};
vkFreeCommandBuffers(deviceObj->device, cmdPool, sizeof(cmdBufs) / sizeof(VkCommandBuffer), cmdBufs);
}

Expand Down Expand Up @@ -576,10 +615,9 @@ void VulkanRenderer::createPipelineStateManagement() {
}
pipelineObj.createPipelineCache();

const bool depthPresent = VkBool32(true);
for (VulkanDrawable *drawable : drawableList) {
auto *pipeline = (VkPipeline *) malloc(sizeof(VkPipeline));
if (pipelineObj.createPipeline(drawable, pipeline, &shaderObj, depthPresent)) {
if (pipelineObj.createPipeline(drawable, pipeline, &shaderObj, includeDepth)) {
pipelineList.push_back(pipeline);
drawable->setPipeline(pipeline);
} else {
Expand Down

0 comments on commit 3057106

Please sign in to comment.