Skip to content

Commit

Permalink
Initial base code
Browse files Browse the repository at this point in the history
  • Loading branch information
austinEng committed Oct 27, 2017
0 parents commit e5f19e1
Show file tree
Hide file tree
Showing 49 changed files with 3,856 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
build
9 changes: 9 additions & 0 deletions .gitmodules
@@ -0,0 +1,9 @@
[submodule "external/GLFW"]
path = external/GLFW
url = https://github.com/glfw/glfw.git
[submodule "external/glm"]
path = external/glm
url = https://github.com/g-truc/glm.git
[submodule "external/stb"]
path = external/stb
url = https://github.com/nothings/stb.git
66 changes: 66 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,66 @@
cmake_minimum_required(VERSION 2.8.12)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")

project(cis565_project6_vulkan_grass_rendering)

OPTION(USE_D2D_WSI "Build the project using Direct to Display swapchain" OFF)

find_package(Vulkan REQUIRED)

IF(WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DVK_USE_PLATFORM_WIN32_KHR")
ELSE(WIN32)
find_package(Threads REQUIRED)
IF(USE_D2D_WSI)
MESSAGE("Using direct to display extension...")
add_definitions(-D_DIRECT2DISPLAY)
ELSE(USE_D2D_WSI)
find_package(XCB REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DVK_USE_PLATFORM_XCB_KHR")
ENDIF(USE_D2D_WSI)
# Todo : android?
ENDIF(WIN32)

# Set preprocessor defines
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNOMINMAX -D_USE_MATH_DEFINES")

add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-std=c++11)

# Enable the creation of folders for Visual Studio projects
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

function(ExternalTarget folder target)
set_property(TARGET ${target} PROPERTY FOLDER ${folder})
endfunction(ExternalTarget)

function(InternalTarget folder target)
ExternalTarget("${folder}" ${target})
if (MSVC)
get_target_property(targetSources ${target} SOURCES)
foreach(sourceFile IN ITEMS ${targetSources})
if (IS_ABSOLUTE "${sourceFile}")
file(RELATIVE_PATH sourceFile "${CMAKE_CURRENT_SOURCE_DIR}" "${sourceFile}")
endif()
get_filename_component(sourceDir "${sourceFile}" PATH)
string(REPLACE "/" "\\" sourceDir "${sourceDir}")
source_group("${sourceDir}" FILES "${sourceFile}")
endforeach()
endif()
endfunction(InternalTarget)

# Compiler specific stuff
IF(MSVC)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
ENDIF(MSVC)

IF(WIN32)
# Nothing here (yet)
ELSE(WIN32)
link_libraries(${XCB_LIBRARIES} ${VULKAN_LIB})
ENDIF(WIN32)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/")

add_subdirectory(external)
add_subdirectory(src)
297 changes: 297 additions & 0 deletions README.md

Large diffs are not rendered by default.

80 changes: 80 additions & 0 deletions cmake/FindVulkan.cmake
@@ -0,0 +1,80 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

#.rst:
# FindVulkan
# ----------
#
# Try to find Vulkan
#
# IMPORTED Targets
# ^^^^^^^^^^^^^^^^
#
# This module defines :prop_tgt:`IMPORTED` target ``Vulkan::Vulkan``, if
# Vulkan has been found.
#
# Result Variables
# ^^^^^^^^^^^^^^^^
#
# This module defines the following variables::
#
# Vulkan_FOUND - True if Vulkan was found
# Vulkan_INCLUDE_DIRS - include directories for Vulkan
# Vulkan_LIBRARIES - link against this library to use Vulkan
#
# The module will also define two cache variables::
#
# Vulkan_INCLUDE_DIR - the Vulkan include directory
# Vulkan_LIBRARY - the path to the Vulkan library
#

if(WIN32)
find_path(Vulkan_INCLUDE_DIR
NAMES vulkan/vulkan.h
PATHS
"$ENV{VULKAN_SDK}/Include"
)

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
find_library(Vulkan_LIBRARY
NAMES vulkan-1
PATHS
"$ENV{VULKAN_SDK}/Lib"
"$ENV{VULKAN_SDK}/Bin"
)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
find_library(Vulkan_LIBRARY
NAMES vulkan-1
PATHS
"$ENV{VULKAN_SDK}/Lib32"
"$ENV{VULKAN_SDK}/Bin32"
NO_SYSTEM_ENVIRONMENT_PATH
)
endif()
else()
find_path(Vulkan_INCLUDE_DIR
NAMES vulkan/vulkan.h
PATHS
"$ENV{VULKAN_SDK}/include")
find_library(Vulkan_LIBRARY
NAMES vulkan
PATHS
"$ENV{VULKAN_SDK}/lib")
endif()

set(Vulkan_LIBRARIES ${Vulkan_LIBRARY})
set(Vulkan_INCLUDE_DIRS ${Vulkan_INCLUDE_DIR})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Vulkan
DEFAULT_MSG
Vulkan_LIBRARY Vulkan_INCLUDE_DIR)

mark_as_advanced(Vulkan_INCLUDE_DIR Vulkan_LIBRARY)

if(Vulkan_FOUND AND NOT TARGET Vulkan::Vulkan)
add_library(Vulkan::Vulkan UNKNOWN IMPORTED)
set_target_properties(Vulkan::Vulkan PROPERTIES
IMPORTED_LOCATION "${Vulkan_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${Vulkan_INCLUDE_DIRS}")
endif()
51 changes: 51 additions & 0 deletions cmake/FindXCB.cmake
@@ -0,0 +1,51 @@
# - FindXCB
#
# Copyright 2015 Valve Coporation

find_package(PkgConfig)

if(NOT XCB_FIND_COMPONENTS)
set(XCB_FIND_COMPONENTS xcb)
endif()

include(FindPackageHandleStandardArgs)
set(XCB_FOUND true)
set(XCB_INCLUDE_DIRS "")
set(XCB_LIBRARIES "")
foreach(comp ${XCB_FIND_COMPONENTS})
# component name
string(TOUPPER ${comp} compname)
string(REPLACE "-" "_" compname ${compname})
# header name
string(REPLACE "xcb-" "" headername xcb/${comp}.h)
# library name
set(libname ${comp})

pkg_check_modules(PC_${comp} QUIET ${comp})

find_path(${compname}_INCLUDE_DIR NAMES ${headername}
HINTS
${PC_${comp}_INCLUDEDIR}
${PC_${comp}_INCLUDE_DIRS}
)

find_library(${compname}_LIBRARY NAMES ${libname}
HINTS
${PC_${comp}_LIBDIR}
${PC_${comp}_LIBRARY_DIRS}
)

find_package_handle_standard_args(${comp}
FOUND_VAR ${comp}_FOUND
REQUIRED_VARS ${compname}_INCLUDE_DIR ${compname}_LIBRARY)
mark_as_advanced(${compname}_INCLUDE_DIR ${compname}_LIBRARY)

list(APPEND XCB_INCLUDE_DIRS ${${compname}_INCLUDE_DIR})
list(APPEND XCB_LIBRARIES ${${compname}_LIBRARY})

if(NOT ${comp}_FOUND)
set(XCB_FOUND false)
endif()
endforeach()

list(REMOVE_DUPLICATES XCB_INCLUDE_DIRS)
9 changes: 9 additions & 0 deletions external/CMakeLists.txt
@@ -0,0 +1,9 @@

set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "Build the GLFW example programs")
set(GLFW_BUILD_TESTS OFF CACHE BOOL "Build the GLFW test programs")
set(GLFW_BUILD_DOCS OFF CACHE BOOL "Build the GLFW documentation")
set(GLFW_INSTALL OFF CACHE BOOL "Generate installation target")
add_subdirectory(GLFW)

set(GLM_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/glm PARENT_SCOPE)
set(STB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/stb PARENT_SCOPE)
1 change: 1 addition & 0 deletions external/GLFW
Submodule GLFW added at 1be81a
1 change: 1 addition & 0 deletions external/glm
Submodule glm added at 9e0c71
1 change: 1 addition & 0 deletions external/stb
Submodule stb added at 9d9f75
Binary file added img/blade_model.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/cube_demo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/grass.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions src/Blades.cpp
@@ -0,0 +1,71 @@
#include <vector>
#include "Blades.h"
#include "BufferUtils.h"

float generateRandomFloat() {
return rand() / (float)RAND_MAX;
}

Blades::Blades(Device* device, VkCommandPool commandPool, float planeDim) : Model(device, commandPool, {}, {}) {
std::vector<Blade> blades;
blades.reserve(NUM_BLADES);

for (int i = 0; i < NUM_BLADES; i++) {
Blade currentBlade = Blade();

glm::vec3 bladeUp(0.0f, 1.0f, 0.0f);

// Generate positions and direction (v0)
float x = (generateRandomFloat() - 0.5f) * planeDim;
float y = 0.0f;
float z = (generateRandomFloat() - 0.5f) * planeDim;
float direction = generateRandomFloat() * 2.f * 3.14159265f;
glm::vec3 bladePosition(x, y, z);
currentBlade.v0 = glm::vec4(bladePosition, direction);

// Bezier point and height (v1)
float height = MIN_HEIGHT + (generateRandomFloat() * (MAX_HEIGHT - MIN_HEIGHT));
currentBlade.v1 = glm::vec4(bladePosition + bladeUp * height, height);

// Physical model guide and width (v2)
float width = MIN_WIDTH + (generateRandomFloat() * (MAX_WIDTH - MIN_WIDTH));
currentBlade.v2 = glm::vec4(bladePosition + bladeUp * height, width);

// Up vector and stiffness coefficient (up)
float stiffness = MIN_BEND + (generateRandomFloat() * (MAX_BEND - MIN_BEND));
currentBlade.up = glm::vec4(bladeUp, stiffness);

blades.push_back(currentBlade);
}

BladeDrawIndirect indirectDraw;
indirectDraw.vertexCount = NUM_BLADES;
indirectDraw.instanceCount = 1;
indirectDraw.firstVertex = 0;
indirectDraw.firstInstance = 0;

BufferUtils::CreateBufferFromData(device, commandPool, blades.data(), NUM_BLADES * sizeof(Blade), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, bladesBuffer, bladesBufferMemory);
BufferUtils::CreateBuffer(device, NUM_BLADES * sizeof(Blade), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, culledBladesBuffer, culledBladesBufferMemory);
BufferUtils::CreateBufferFromData(device, commandPool, &indirectDraw, sizeof(BladeDrawIndirect), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT, numBladesBuffer, numBladesBufferMemory);
}

VkBuffer Blades::GetBladesBuffer() const {
return bladesBuffer;
}

VkBuffer Blades::GetCulledBladesBuffer() const {
return culledBladesBuffer;
}

VkBuffer Blades::GetNumBladesBuffer() const {
return numBladesBuffer;
}

Blades::~Blades() {
vkDestroyBuffer(device->GetVkDevice(), bladesBuffer, nullptr);
vkFreeMemory(device->GetVkDevice(), bladesBufferMemory, nullptr);
vkDestroyBuffer(device->GetVkDevice(), culledBladesBuffer, nullptr);
vkFreeMemory(device->GetVkDevice(), culledBladesBufferMemory, nullptr);
vkDestroyBuffer(device->GetVkDevice(), numBladesBuffer, nullptr);
vkFreeMemory(device->GetVkDevice(), numBladesBufferMemory, nullptr);
}
88 changes: 88 additions & 0 deletions src/Blades.h
@@ -0,0 +1,88 @@
#pragma once
#include <vulkan/vulkan.h>
#include <glm/glm.hpp>
#include <array>
#include "Model.h"

constexpr static unsigned int NUM_BLADES = 1 << 13;
constexpr static float MIN_HEIGHT = 1.3f;
constexpr static float MAX_HEIGHT = 2.5f;
constexpr static float MIN_WIDTH = 0.1f;
constexpr static float MAX_WIDTH = 0.14f;
constexpr static float MIN_BEND = 7.0f;
constexpr static float MAX_BEND = 13.0f;

struct Blade {
// Position and direction
glm::vec4 v0;
// Bezier point and height
glm::vec4 v1;
// Physical model guide and width
glm::vec4 v2;
// Up vector and stiffness coefficient
glm::vec4 up;

static VkVertexInputBindingDescription getBindingDescription() {
VkVertexInputBindingDescription bindingDescription = {};
bindingDescription.binding = 0;
bindingDescription.stride = sizeof(Blade);
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;

return bindingDescription;
}

static std::array<VkVertexInputAttributeDescription, 4> getAttributeDescriptions() {
std::array<VkVertexInputAttributeDescription, 4> attributeDescriptions = {};

// v0
attributeDescriptions[0].binding = 0;
attributeDescriptions[0].location = 0;
attributeDescriptions[0].format = VK_FORMAT_R32G32B32A32_SFLOAT;
attributeDescriptions[0].offset = offsetof(Blade, v0);

// v1
attributeDescriptions[1].binding = 0;
attributeDescriptions[1].location = 1;
attributeDescriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT;
attributeDescriptions[1].offset = offsetof(Blade, v1);

// v2
attributeDescriptions[2].binding = 0;
attributeDescriptions[2].location = 2;
attributeDescriptions[2].format = VK_FORMAT_R32G32B32A32_SFLOAT;
attributeDescriptions[2].offset = offsetof(Blade, v2);

// up
attributeDescriptions[3].binding = 0;
attributeDescriptions[3].location = 3;
attributeDescriptions[3].format = VK_FORMAT_R32G32B32A32_SFLOAT;
attributeDescriptions[3].offset = offsetof(Blade, up);

return attributeDescriptions;
}
};

struct BladeDrawIndirect {
uint32_t vertexCount;
uint32_t instanceCount;
uint32_t firstVertex;
uint32_t firstInstance;
};

class Blades : public Model {
private:
VkBuffer bladesBuffer;
VkBuffer culledBladesBuffer;
VkBuffer numBladesBuffer;

VkDeviceMemory bladesBufferMemory;
VkDeviceMemory culledBladesBufferMemory;
VkDeviceMemory numBladesBufferMemory;

public:
Blades(Device* device, VkCommandPool commandPool, float planeDim);
VkBuffer GetBladesBuffer() const;
VkBuffer GetCulledBladesBuffer() const;
VkBuffer GetNumBladesBuffer() const;
~Blades();
};

0 comments on commit e5f19e1

Please sign in to comment.