diff --git a/README.md b/README.md index a744a2e..33d6d6d 100644 --- a/README.md +++ b/README.md @@ -1,297 +1,80 @@ -Instructions - Vulkan Grass Rendering +Vulkan Grass Rendering ======================== -This is due **Sunday 11/5, evening at midnight**. +**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 6** -**Summary:** -In this project, you will use Vulkan to implement a grass simulator and renderer. You will -use compute shaders to perform physics calculations on Bezier curves that represent individual -grass blades in your application. Since rendering every grass blade on every frame will is fairly -inefficient, you will also use compute shaders to cull grass blades that don't contribute to a given frame. -The remaining blades will be passed to a graphics pipeline, in which you will write several shaders. -You will write a vertex shader to transform Bezier control points, tessellation shaders to dynamically create -the grass geometry from the Bezier curves, and a fragment shader to shade the grass blades. +* Hanming Zhang +* Tested on: Windows 10 Education, i7-6700K @ 4.00GHz 16.0GB, GTX 980 4096MB (Personal Desktop) -The base code provided includes all of the basic Vulkan setup, including a compute pipeline that will run your compute -shaders and two graphics pipelines, one for rendering the geometry that grass will be placed on and the other for -rendering the grass itself. Your job will be to write the shaders for the grass graphics pipeline and the compute pipeline, -as well as binding any resources (descriptors) you may need to accomplish the tasks described in this assignment. +### Demo Video -![](img/grass.gif) +[![](img/screenshot.jpg)](https://www.youtube.com/watch?v=ftxLTgV6KpM&feature=youtu.be) -You are not required to use this base code if you don't want -to. You may also change any part of the base code as you please. -**This is YOUR project.** The above .gif is just a simple example that you -can use as a reference to compare to. -**Important:** -- If you are not in CGGT/DMD, you may replace this project with a GPU compute -project. You MUST get this pre-approved by Austin Eng before continuing! +Project Features +================ -### Contents +### **Summary:** +This project use Vulkan to implement a grass simulator and renderer. Basically, this project is an implementation of the paper, [Responsive Real-Time Grass Rendering for General 3D Scenes](https://www.cg.tuwien.ac.at/research/publications/2017/JAHRMANN-2017-RRTG/JAHRMANN-2017-RRTG-draft.pdf). Compute shaders are used to perform physics calculations on Bezier curves that represent individual +grass blades in the application. Since rendering every grass blade on every frame will is fairly +inefficient, compute shaders are also used to cull grass blades that don't contribute to a given frame. +The remaining blades will be passed to a graphics pipeline. -* `src/` C++/Vulkan source files. - * `shaders/` glsl shader source files - * `images/` images used as textures within graphics pipelines -* `external/` Includes and static libraries for 3rd party libraries. -* `img/` Screenshots and images to use in your READMEs -### Installing Vulkan +### Screenshoots: +![](img/screenshot2.jpg) | ![](img/screenshot3.jpg) +------------ | ------------- +broad view | detail view -In order to run a Vulkan project, you first need to download and install the [Vulkan SDK](https://vulkan.lunarg.com/). -Make sure to run the downloaded installed as administrator so that the installer can set the appropriate environment -variables for you. -Once you have done this, you need to make sure your GPU driver supports Vulkan. Download and install a -[Vulkan driver](https://developer.nvidia.com/vulkan-driver) from NVIDIA's website. -Finally, to check that Vulkan is ready for use, go to your Vulkan SDK directory (`C:/VulkanSDK/` unless otherwise specified) -and run the `cube.exe` example within the `Bin` directory. IF you see a rotating gray cube with the LunarG logo, then you -are all set! +### Compute shader + - Calculate forces applied to grass blades, which include gravity, recovery force and natural wind force. Also, blades translation caused by collidor(in our case, a invisible sphere) are also computed here. + - Grass blades culling, which includes orientation, view frustum, and distance culling. -### Running the code +### Grass pipeline stages + - Vertex shader + - Tessellation control shader( tessellate to varying levels of detail as a function of how far the grass blade is from the camera) + - Tessellation evaluation shader + - Fragment shader -While developing your grass renderer, you will want to keep validation layers enabled so that error checking is turned on. -The project is set up such that when you are in `debug` mode, validation layers are enabled, and when you are in `release` mode, -validation layers are disabled. After building the code, you should be able to run the project without any errors. You will see -a plane with a grass texture on it to begin with. +### Controls : + - Left mouse button to rotate Camera + - Right mouse button to zoom in/out + - W, A, S, D to control invisible sphere collidor and interact with the scene. -![](img/cube_demo.png) -## Requirements -**Ask on the mailing list for any clarifications.** +### Project Analysis +#### All analysis happens under 1280 x 960 resolution. The size of the plane our grass blades on is 50 x 50 and except for the grass blades number performance test, there are totally pow(2, 17) = 131072 grass blades in our scene. -In this project, you are given the following code: +- #### Grass blades number performance analysis -* The basic setup for a Vulkan project, including the swapchain, physical device, logical device, and the pipelines described above. -* Structs for some of the uniform buffers you will be using. -* Some buffer creation utility functions. -* A simple interactive camera using the mouse. + ![](img/graph1.jpg) -You need to implement the following features/pipeline stages: + ##### Analysis: + As we can see, basically, the result is the same as we image, when the grass blades number increase, the render time per frame increases. Since in our case, excepting for culling some grass blades, no other optimizations are implemented, when there is really a large number of grass blades, FPS will drop below 60, which is not so good in real-time rendering. -* Compute shader (`shaders/compute.comp`) -* Grass pipeline stages - * Vertex shader (`shaders/grass.vert') - * Tessellation control shader (`shaders/grass.tesc`) - * Tessellation evaluation shader (`shaders/grass.tese`) - * Fragment shader (`shaders/grass.frag`) -* Binding of any extra descriptors you may need -See below for more guidance. +- #### Grass blades culling performance analysis + ##### since the test results in this part depends the camera positon and camera view, I set a fixed camera position and view to do this test. The view is shown like below. -## Base Code Tour + ![](img/cullingTestView.jpg) -Areas that you need to complete are -marked with a `TODO` comment. Functions that are useful -for reference are marked with the comment `CHECKITOUT`. + Here is the test results I got: -* `src/main.cpp` is the entry point of our application. -* `src/Instance.cpp` sets up the application state, initializes the Vulkan library, and contains functions that will create our -physical and logical device handles. -* `src/Device.cpp` manages the logical device and sets up the queues that our command buffers will be submitted to. -* `src/Renderer.cpp` contains most of the rendering implementation, including Vulkan setup and resource creation. You will -likely have to make changes to this file in order to support changes to your pipelines. -* `src/Camera.cpp` manages the camera state. -* `src/Model.cpp` manages the state of the model that grass will be created on. Currently a plane is hardcoded, but feel free to -update this with arbitrary model loading! -* `src/Blades.cpp` creates the control points corresponding to the grass blades. There are many parameters that you can play with -here that will change the behavior of your rendered grass blades. -* `src/Scene.cpp` manages the scene state, including the model, blades, and simualtion time. -* `src/BufferUtils.cpp` provides helper functions for creating buffers to be used as descriptors. + ![](img/graph2.jpg) -We left out descriptions for a couple files that you likely won't have to modify. Feel free to investigate them to understand their -importance within the scope of the project. + ##### Analysis: + Besides our camera position and view, orientation culling actually also relies heavily the culling threshold value set, which in our case is 0.05. It's not a so large number and only the dot product smaller than that, will the blade be culled. View-frustum and distance culling work well in our case, which I think it's mainly because of I set the camera in a relatively broad view. After all culling processes are open, we save almost half of the rendering time per frame. -## Grass Rendering -This project is an implementation of the paper, [Responsive Real-Time Grass Rendering for General 3D Scenes](https://www.cg.tuwien.ac.at/research/publications/2017/JAHRMANN-2017-RRTG/JAHRMANN-2017-RRTG-draft.pdf). -Please make sure to use this paper as a primary resource while implementing your grass renderers. It does a great job of explaining -the key algorithms and math you will be using. Below is a brief description of the different components in chronological order of how your renderer will -execute, but feel free to develop the components in whatever order you prefer. +- #### Tessellate according to grass blade depth -### Representing Grass as Bezier Curves +![](img/tessel_high.jpg) | ![](img/tessel_low.jpg) +------------ | ------------- +high tessellation level | low tessellation level -In this project, grass blades will be represented as Bezier curves while performing physics calculations and culling operations. -Each Bezier curve has three control points. -* `v0`: the position of the grass blade on the geomtry -* `v1`: a Bezier curve guide that is always "above" `v0` with respect to the grass blade's up vector (explained soon) -* `v2`: a physical guide for which we simulate forces on -We also need to store per-blade characteristics that will help us simulate and tessellate our grass blades correctly. -* `up`: the blade's up vector, which corresponds to the normal of the geometry that the grass blade resides on at `v0` -* Orientation: the orientation of the grass blade's face -* Height: the height of the grass blade -* Width: the width of the grass blade's face -* Stiffness coefficient: the stiffness of our grass blade, which will affect the force computations on our blade - -We can pack all this data into four `vec4`s, such that `v0.w` holds orientation, `v1.w` holds height, `v2.w` holds width, and -`up.w` holds the stiffness coefficient. - -![](img/blade_model.jpg) - -### Simulating Forces - -In this project, you will be simulating forces on grass blades while they are still Bezier curves. This will be done in a compute -shader using the compute pipeline that has been created for you. Remember that `v2` is our physical guide, so we will be -applying transformations to `v2` initially, then correcting for potential errors. We will finally update `v1` to maintain the appropriate -length of our grass blade. - -#### Binding Resources - -In order to update the state of your grass blades on every frame, you will need to create a storage buffer to maintain the grass data. -You will also need to pass information about how much time has passed in the simulation and the time since the last frame. To do this, -you can extend or create descriptor sets that will be bound to the compute pipeline. - -#### Gravity - -Given a gravity direction, `D.xyz`, and the magnitude of acceleration, `D.w`, we can compute the environmental gravity in -our scene as `gE = normalize(D.xyz) * D.w`. - -We then determine the contribution of the gravity with respect to the front facing direction of the blade, `f`, -as a term called the "front gravity". Front gravity is computed as `gF = (1/4) * ||gE|| * f`. - -We can then determine the total gravity on the grass blade as `g = gE + gF`. - -#### Recovery - -Recovery corresponds to the counter-force that brings our grass blade back into equilibrium. This is derived in the paper using Hooke's law. -In order to determine the recovery force, we need to compare the current position of `v2` to its original position before -simulation started, `iv2`. At the beginning of our simulation, `v1` and `v2` are initialized to be a distance of the blade height along the `up` vector. - -Once we have `iv2`, we can compute the recovery forces as `r = (iv2 - v2) * stiffness`. - -#### Wind - -In order to simulate wind, you are at liberty to create any wind function you want! In order to have something interesting, -you can make the function depend on the position of `v0` and a function that changes with time. Consider using some combination -of sine or cosine functions. - -Your wind function will determine a wind direction that is affecting the blade, but it is also worth noting that wind has a larger impact on -grass blades whose forward directions are parallel to the wind direction. The paper describes this as a "wind alignment" term. We won't go -over the exact math here, but use the paper as a reference when implementing this. It does a great job of explaining this! - -Once you have a wind direction and a wind alignment term, your total wind force (`w`) will be `windDirection * windAlignment`. - -#### Total force - -We can then determine a translation for `v2` based on the forces as `tv2 = (gravity + recovery + wind) * deltaTime`. However, we can't simply -apply this translation and expect the simulation to be robust. Our forces might push `v2` under the ground! Similarly, moving `v2` but leaving -`v1` in the same position will cause our grass blade to change length, which doesn't make sense. - -Read section 5.2 of the paper in order to learn how to determine the corrected final positions for `v1` and `v2`. - -### Culling tests - -Although we need to simulate forces on every grass blade at every frame, there are many blades that we won't need to render -due to a variety of reasons. Here are some heuristics we can use to cull blades that won't contribute positively to a given frame. - -#### Orientation culling - -Consider the scenario in which the front face direction of the grass blade is perpendicular to the view vector. Since our grass blades -won't have width, we will end up trying to render parts of the grass that are actually smaller than the size of a pixel. This could -lead to aliasing artifacts. - -In order to remedy this, we can cull these blades! Simply do a dot product test to see if the view vector and front face direction of -the blade are perpendicular. The paper uses a threshold value of `0.9` to cull, but feel free to use what you think looks best. - -#### View-frustum culling - -We also want to cull blades that are outside of the view-frustum, considering they won't show up in the frame anyway. To determine if -a grass blade is in the view-frustum, we want to compare the visibility of three points: `v0, v2, and m`, where `m = (1/4)v0 * (1/2)v1 * (1/4)v2`. -Notice that we aren't using `v1` for the visibility test. This is because the `v1` is a Bezier guide that doesn't represent a position on the grass blade. -We instead use `m` to approximate the midpoint of our Bezier curve. - -If all three points are outside of the view-frustum, we will cull the grass blade. The paper uses a tolerance value for this test so that we are culling -blades a little more conservatively. This can help with cases in which the Bezier curve is technically not visible, but we might be able to see the blade -if we consider its width. - -#### Distance culling - -Similarly to orientation culling, we can end up with grass blades that at large distances are smaller than the size of a pixel. This could lead to additional -artifacts in our renders. In this case, we can cull grass blades as a function of their distance from the camera. - -You are free to define two parameters here. -* A max distance afterwhich all grass blades will be culled. -* A number of buckets to place grass blades between the camera and max distance into. - -Define a function such that the grass blades in the bucket closest to the camera are kept while an increasing number of grass blades -are culled with each farther bucket. - -#### Occlusion culling (extra credit) - -This type of culling only makes sense if our scene has additional objects aside from the plane and the grass blades. We want to cull grass blades that -are occluded by other geometry. Think about how you can use a depth map to accomplish this! - -### Tessellating Bezier curves into grass blades - -In this project, you should pass in each Bezier curve as a single patch to be processed by your grass graphics pipeline. You will tessellate this patch into -a quad with a shape of your choosing (as long as it looks sufficiently like grass of course). The paper has some examples of grass shapes you can use as inspiration. - -In the tessellation control shader, specify the amount of tessellation you want to occur. Remember that you need to provide enough detail to create the curvature of a grass blade. - -The generated vertices will be passed to the tessellation evaluation shader, where you will place the vertices in world space, respecting the width, height, and orientation information -of each blade. Once you have determined the world space position of each vector, make sure to set the output `gl_Position` in clip space! - -** Extra Credit**: Tessellate to varying levels of detail as a function of how far the grass blade is from the camera. For example, if the blade is very far, only generate four vertices in the tessellation control shader. - -To build more intuition on how tessellation works, I highly recommend playing with the [helloTessellation sample](https://github.com/CIS565-Fall-2017/Vulkan-Samples/tree/master/samples/5_helloTessellation) -and reading this [tutorial on tessellation](http://in2gpu.com/2014/07/12/tessellation-tutorial-opengl-4-3/). - -## Resources - -### Links - -The following resources may be useful for this project. - -* [Responsive Real-Time Grass Grass Rendering for General 3D Scenes](https://www.cg.tuwien.ac.at/research/publications/2017/JAHRMANN-2017-RRTG/JAHRMANN-2017-RRTG-draft.pdf) -* [CIS565 Vulkan samples](https://github.com/CIS565-Fall-2017/Vulkan-Samples) -* [Official Vulkan documentation](https://www.khronos.org/registry/vulkan/) -* [Vulkan tutorial](https://vulkan-tutorial.com/) -* [RenderDoc blog on Vulkan](https://renderdoc.org/vulkan-in-30-minutes.html) -* [Tessellation tutorial](http://in2gpu.com/2014/07/12/tessellation-tutorial-opengl-4-3/) - - -## Third-Party Code Policy - -* Use of any third-party code must be approved by asking on our Google Group. -* If it is approved, all students are welcome to use it. Generally, we approve - use of third-party code that is not a core part of the project. For example, - for the path tracer, we would approve using a third-party library for loading - models, but would not approve copying and pasting a CUDA function for doing - refraction. -* Third-party code **MUST** be credited in README.md. -* Using third-party code without its approval, including using another - student's code, is an academic integrity violation, and will, at minimum, - result in you receiving an F for the semester. - - -## README - -* A brief description of the project and the specific features you implemented. -* At least one screenshot of your project running. -* A performance analysis (described below). - -### Performance Analysis - -The performance analysis is where you will investigate how... -* Your renderer handles varying numbers of grass blades -* The improvement you get by culling using each of the three culling tests - -## Submit - -If you have modified any of the `CMakeLists.txt` files at all (aside from the -list of `SOURCE_FILES`), mentions it explicity. -Beware of any build issues discussed on the Google Group. - -Open a GitHub pull request so that we can see that you have finished. -The title should be "Project 6: YOUR NAME". -The template of the comment section of your pull request is attached below, you can do some copy and paste: - -* [Repo Link](https://link-to-your-repo) -* (Briefly) Mentions features that you've completed. Especially those bells and whistles you want to highlight - * Feature 0 - * Feature 1 - * ... -* Feedback on the project itself, if any. +- #### Collision +![](img/collision.jpg) diff --git a/img/collision.jpg b/img/collision.jpg new file mode 100644 index 0000000..de76b91 Binary files /dev/null and b/img/collision.jpg differ diff --git a/img/cullingTestView.jpg b/img/cullingTestView.jpg new file mode 100644 index 0000000..1d5101e Binary files /dev/null and b/img/cullingTestView.jpg differ diff --git a/img/graph1.jpg b/img/graph1.jpg new file mode 100644 index 0000000..13eca33 Binary files /dev/null and b/img/graph1.jpg differ diff --git a/img/graph2.jpg b/img/graph2.jpg new file mode 100644 index 0000000..15d9460 Binary files /dev/null and b/img/graph2.jpg differ diff --git a/img/screenshot.jpg b/img/screenshot.jpg new file mode 100644 index 0000000..0b7df91 Binary files /dev/null and b/img/screenshot.jpg differ diff --git a/img/screenshot2.jpg b/img/screenshot2.jpg new file mode 100644 index 0000000..aff3fff Binary files /dev/null and b/img/screenshot2.jpg differ diff --git a/img/screenshot3.jpg b/img/screenshot3.jpg new file mode 100644 index 0000000..4479176 Binary files /dev/null and b/img/screenshot3.jpg differ diff --git a/img/tessel_high.jpg b/img/tessel_high.jpg new file mode 100644 index 0000000..375bc1a Binary files /dev/null and b/img/tessel_high.jpg differ diff --git a/img/tessel_low.jpg b/img/tessel_low.jpg new file mode 100644 index 0000000..ce5e722 Binary files /dev/null and b/img/tessel_low.jpg differ diff --git a/src/Blades.h b/src/Blades.h index 9bd1eed..ee27a98 100644 --- a/src/Blades.h +++ b/src/Blades.h @@ -4,7 +4,8 @@ #include #include "Model.h" -constexpr static unsigned int NUM_BLADES = 1 << 13; +//constexpr static unsigned int NUM_BLADES = 1 << 13; +constexpr static unsigned int NUM_BLADES = 1 << 17; // 1 << 20; constexpr static float MIN_HEIGHT = 1.3f; constexpr static float MAX_HEIGHT = 2.5f; constexpr static float MIN_WIDTH = 0.1f; diff --git a/src/CollidorSphere.cpp b/src/CollidorSphere.cpp new file mode 100644 index 0000000..863f2d1 --- /dev/null +++ b/src/CollidorSphere.cpp @@ -0,0 +1,44 @@ +#include + +#define GLM_FORCE_RADIANS +// Use Vulkan depth range of 0.0 to 1.0 instead of OpenGL +#define GLM_FORCE_DEPTH_ZERO_TO_ONE + +#include "CollidorSphere.h" +#include "BufferUtils.h" + +CollidorSphere::CollidorSphere(Device* device, glm::vec3 centroid_pos, float radius) : device(device) { + + collidorSphereObject.collidorSphereInfo = glm::vec4(centroid_pos, radius); + + + BufferUtils::CreateBuffer(device, sizeof(CollidorSphereObject), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, buffer, bufferMemory); + vkMapMemory(device->GetVkDevice(), bufferMemory, 0, sizeof(CollidorSphereObject), 0, &mappedData); + memcpy(mappedData, &collidorSphereObject, sizeof(CollidorSphereObject)); +} + +VkBuffer CollidorSphere::GetBuffer() const { + return buffer; +} + +void CollidorSphere::UpdatePosition(glm::vec3 moveDir) { + + glm::vec4 originInfo = collidorSphereObject.collidorSphereInfo; + glm::vec3 originPos = glm::vec3(originInfo); + + float moveSpeed = 0.2f; + + glm::vec3 newPos = originPos + moveSpeed * moveDir; + newPos.x = glm::clamp(newPos.x, -25.0f, 25.0f); + newPos.z = glm::clamp(newPos.z, -25.0f, 25.0f); + + collidorSphereObject.collidorSphereInfo = glm::vec4(newPos, originInfo.w); + + memcpy(mappedData, &collidorSphereObject, sizeof(CollidorSphereObject)); +} + +CollidorSphere::~CollidorSphere() { + vkUnmapMemory(device->GetVkDevice(), bufferMemory); + vkDestroyBuffer(device->GetVkDevice(), buffer, nullptr); + vkFreeMemory(device->GetVkDevice(), bufferMemory, nullptr); +} diff --git a/src/CollidorSphere.h b/src/CollidorSphere.h new file mode 100644 index 0000000..c75cde8 --- /dev/null +++ b/src/CollidorSphere.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include "Device.h" + +struct CollidorSphereObject { + glm::vec4 collidorSphereInfo; +}; + +class CollidorSphere { +private: + Device* device; + + CollidorSphereObject collidorSphereObject; + + VkBuffer buffer; + VkDeviceMemory bufferMemory; + + void* mappedData; + + +public: + CollidorSphere(Device* device, glm::vec3 centroid_pos, float radius); + ~CollidorSphere(); + + VkBuffer GetBuffer() const; + + void UpdatePosition(glm::vec3 moveDir); +}; diff --git a/src/Renderer.cpp b/src/Renderer.cpp index b445d04..35d68d6 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -8,29 +8,36 @@ static constexpr unsigned int WORKGROUP_SIZE = 32; -Renderer::Renderer(Device* device, SwapChain* swapChain, Scene* scene, Camera* camera) +Renderer::Renderer(Device* device, SwapChain* swapChain, Scene* scene, Camera* camera, CollidorSphere* collidorSphere) : device(device), logicalDevice(device->GetVkDevice()), swapChain(swapChain), scene(scene), - camera(camera) { + camera(camera), + collidorSphere(collidorSphere){ CreateCommandPools(); CreateRenderPass(); + CreateCameraDescriptorSetLayout(); CreateModelDescriptorSetLayout(); CreateTimeDescriptorSetLayout(); CreateComputeDescriptorSetLayout(); + CreateCollidorSphereDescriptorSetLayout(); + CreateDescriptorPool(); CreateCameraDescriptorSet(); CreateModelDescriptorSets(); CreateGrassDescriptorSets(); CreateTimeDescriptorSet(); CreateComputeDescriptorSets(); + CreateCollidorSphereDescriptorSet(); + CreateFrameResources(); CreateGraphicsPipeline(); CreateGrassPipeline(); CreateComputePipeline(); + RecordCommandBuffers(); RecordComputeCommandBuffer(); } @@ -196,10 +203,70 @@ void Renderer::CreateTimeDescriptorSetLayout() { void Renderer::CreateComputeDescriptorSetLayout() { // TODO: Create the descriptor set layout for the compute pipeline - // Remember this is like a class definition stating why types of information + // Remember this is like a class definition stating what types of information // will be stored at each binding + + // Describe the binding of the descriptor set layout + VkDescriptorSetLayoutBinding bladesLayoutBinding = {}; + bladesLayoutBinding.binding = 0; + bladesLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + bladesLayoutBinding.descriptorCount = 1; + bladesLayoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; + bladesLayoutBinding.pImmutableSamplers = nullptr; + + // Describe the binding of the descriptor set layout + VkDescriptorSetLayoutBinding culledBladesLayoutBinding = {}; + culledBladesLayoutBinding.binding = 1; + culledBladesLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + culledBladesLayoutBinding.descriptorCount = 1; + culledBladesLayoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; + culledBladesLayoutBinding.pImmutableSamplers = nullptr; + + // Describe the binding of the descriptor set layout + VkDescriptorSetLayoutBinding numBladesLayoutBinding = {}; + numBladesLayoutBinding.binding = 2; + numBladesLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + numBladesLayoutBinding.descriptorCount = 1; + numBladesLayoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; + numBladesLayoutBinding.pImmutableSamplers = nullptr; + + std::vector bindings = { bladesLayoutBinding, culledBladesLayoutBinding, numBladesLayoutBinding }; + + // Create the descriptor set layout + VkDescriptorSetLayoutCreateInfo layoutInfo = {}; + layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + layoutInfo.pNext = nullptr; + layoutInfo.bindingCount = static_cast(bindings.size()); + layoutInfo.pBindings = bindings.data(); + + if (vkCreateDescriptorSetLayout(logicalDevice, &layoutInfo, nullptr, &computeDescriptorSetLayout) != VK_SUCCESS) { + throw std::runtime_error("Failed to create descriptor set layout"); + } } +void Renderer::CreateCollidorSphereDescriptorSetLayout() { + // Describe the binding of the descriptor set layout + VkDescriptorSetLayoutBinding uboLayoutBinding = {}; + uboLayoutBinding.binding = 0; + uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + uboLayoutBinding.descriptorCount = 1; + uboLayoutBinding.stageFlags = VK_SHADER_STAGE_ALL; + uboLayoutBinding.pImmutableSamplers = nullptr; + + std::vector bindings = { uboLayoutBinding }; + + // Create the descriptor set layout + VkDescriptorSetLayoutCreateInfo layoutInfo = {}; + layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + layoutInfo.bindingCount = static_cast(bindings.size()); + layoutInfo.pBindings = bindings.data(); + + if (vkCreateDescriptorSetLayout(logicalDevice, &layoutInfo, nullptr, &collidorSphereDescriptorSetLayout) != VK_SUCCESS) { + throw std::runtime_error("Failed to create descriptor set layout"); + } +} + + void Renderer::CreateDescriptorPool() { // Describe which descriptor types that the descriptor sets will contain std::vector poolSizes = { @@ -216,13 +283,18 @@ void Renderer::CreateDescriptorPool() { { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER , 1 }, // TODO: Add any additional types and counts of descriptors you will need to allocate + // Blades (compute) + { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER , static_cast(scene->GetBlades().size() * 3) }, + + // Collidor Sphere + { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER , 1 }, }; VkDescriptorPoolCreateInfo poolInfo = {}; poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; poolInfo.poolSizeCount = static_cast(poolSizes.size()); poolInfo.pPoolSizes = poolSizes.data(); - poolInfo.maxSets = 5; + poolInfo.maxSets = 6; if (vkCreateDescriptorPool(logicalDevice, &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) { throw std::runtime_error("Failed to create descriptor pool"); @@ -320,6 +392,62 @@ void Renderer::CreateModelDescriptorSets() { void Renderer::CreateGrassDescriptorSets() { // TODO: Create Descriptor sets for the grass. // This should involve creating descriptor sets which point to the model matrix of each group of grass blades + grassDescriptorSets.resize(scene->GetBlades().size()); + + // Describe the desciptor set + VkDescriptorSetLayout layouts[] = { modelDescriptorSetLayout }; // still use modelDescriptorSetLayout, grass can also be treated as model here + VkDescriptorSetAllocateInfo allocInfo = {}; + allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + allocInfo.descriptorPool = descriptorPool; + allocInfo.descriptorSetCount = static_cast(grassDescriptorSets.size()); + allocInfo.pSetLayouts = layouts; + + // Allocate descriptor sets + if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, grassDescriptorSets.data()) != VK_SUCCESS) { + throw std::runtime_error("Failed to allocate descriptor set"); + } + + //std::vector descriptorWrites(2 * grassDescriptorSets.size()); + std::vector descriptorWrites(grassDescriptorSets.size()); + + for (uint32_t i = 0; i < scene->GetBlades().size(); ++i) { + VkDescriptorBufferInfo grassModelBufferInfo = {}; + grassModelBufferInfo.buffer = scene->GetBlades()[i]->GetModelBuffer(); // + grassModelBufferInfo.offset = 0; + grassModelBufferInfo.range = sizeof(ModelBufferObject); + + // Bind image and sampler resources to the descriptor + // ********************************************** + //VkDescriptorImageInfo imageInfo = {}; + //imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + //imageInfo.imageView = scene->GetBlades()[i]->GetTextureView(); + //imageInfo.sampler = scene->GetBlades()[i]->GetTextureSampler(); + // ******************************************************** + + descriptorWrites[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrites[i].dstSet = grassDescriptorSets[i]; + descriptorWrites[i].dstBinding = 0; + descriptorWrites[i].dstArrayElement = 0; + descriptorWrites[i].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorWrites[i].descriptorCount = 1; + descriptorWrites[i].pBufferInfo = &grassModelBufferInfo; + descriptorWrites[i].pImageInfo = nullptr; + descriptorWrites[i].pTexelBufferView = nullptr; + + // ********************************************** + //descriptorWrites[2 * i + 1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + //descriptorWrites[2 * i + 1].dstSet = grassDescriptorSets[i]; + //descriptorWrites[2 * i + 1].dstBinding = 1; + //descriptorWrites[2 * i + 1].dstArrayElement = 0; + //descriptorWrites[2 * i + 1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + //descriptorWrites[2 * i + 1].descriptorCount = 1; + //descriptorWrites[2 * i + 1].pImageInfo = &imageInfo; + // ******************************************************** + + } + + // Update descriptor sets + vkUpdateDescriptorSets(logicalDevice, static_cast(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); } void Renderer::CreateTimeDescriptorSet() { @@ -360,6 +488,116 @@ void Renderer::CreateTimeDescriptorSet() { void Renderer::CreateComputeDescriptorSets() { // TODO: Create Descriptor sets for the compute pipeline // The descriptors should point to Storage buffers which will hold the grass blades, the culled grass blades, and the output number of grass blades + + computeDescriptorSets.resize(scene->GetBlades().size()); + + // Describe the desciptor set + VkDescriptorSetLayout layouts[] = { computeDescriptorSetLayout }; + VkDescriptorSetAllocateInfo allocInfo = {}; + allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + allocInfo.descriptorPool = descriptorPool; + allocInfo.descriptorSetCount = static_cast(computeDescriptorSets.size()); + allocInfo.pSetLayouts = layouts; + + // Allocate descriptor sets + if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, computeDescriptorSets.data()) != VK_SUCCESS) { + throw std::runtime_error("Failed to allocate descriptor set"); + } + + // blades, culledblades, numBlades + std::vector descriptorWrites(3 * computeDescriptorSets.size()); + + for (uint32_t i = 0; i < scene->GetBlades().size(); ++i) { + + // Bind blades buffer + VkDescriptorBufferInfo bladesBufferInfo = {}; + bladesBufferInfo.buffer = scene->GetBlades()[i]->GetBladesBuffer(); + bladesBufferInfo.offset = 0; + bladesBufferInfo.range = NUM_BLADES * sizeof(Blade); + + // Bind culled blades buffer + VkDescriptorBufferInfo culledBladesBufferInfo = {}; + culledBladesBufferInfo.buffer = scene->GetBlades()[i]->GetCulledBladesBuffer(); + culledBladesBufferInfo.offset = 0; + culledBladesBufferInfo.range = NUM_BLADES * sizeof(Blade); + + // Bind numBladesBufferMemory + VkDescriptorBufferInfo numBladeBufferInfo = {}; + numBladeBufferInfo.buffer = scene->GetBlades()[i]->GetNumBladesBuffer(); + numBladeBufferInfo.offset = 0; + numBladeBufferInfo.range = sizeof(BladeDrawIndirect); + + descriptorWrites[3 * i + 0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrites[3 * i + 0].dstSet = computeDescriptorSets[i]; + descriptorWrites[3 * i + 0].dstBinding = 0; + descriptorWrites[3 * i + 0].dstArrayElement = 0; + descriptorWrites[3 * i + 0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + descriptorWrites[3 * i + 0].descriptorCount = 1; + descriptorWrites[3 * i + 0].pBufferInfo = &bladesBufferInfo; + descriptorWrites[3 * i + 0].pImageInfo = nullptr; + descriptorWrites[3 * i + 0].pTexelBufferView = nullptr; + + descriptorWrites[3 * i + 1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrites[3 * i + 1].dstSet = computeDescriptorSets[i]; + descriptorWrites[3 * i + 1].dstBinding = 1; + descriptorWrites[3 * i + 1].dstArrayElement = 0; + descriptorWrites[3 * i + 1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + descriptorWrites[3 * i + 1].descriptorCount = 1; + descriptorWrites[3 * i + 1].pBufferInfo = &culledBladesBufferInfo; + descriptorWrites[3 * i + 1].pImageInfo = nullptr; + descriptorWrites[3 * i + 1].pTexelBufferView = nullptr; + + descriptorWrites[3 * i + 2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrites[3 * i + 2].dstSet = computeDescriptorSets[i]; + descriptorWrites[3 * i + 2].dstBinding = 2; + descriptorWrites[3 * i + 2].dstArrayElement = 0; + descriptorWrites[3 * i + 2].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + descriptorWrites[3 * i + 2].descriptorCount = 1; + descriptorWrites[3 * i + 2].pBufferInfo = &numBladeBufferInfo; + descriptorWrites[3 * i + 2].pImageInfo = nullptr; + descriptorWrites[3 * i + 2].pTexelBufferView = nullptr; + } + + // Update descriptor sets + vkUpdateDescriptorSets(logicalDevice, static_cast(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); + +} + + +void Renderer::CreateCollidorSphereDescriptorSet() { + // Describe the desciptor set + VkDescriptorSetLayout layouts[] = { collidorSphereDescriptorSetLayout }; + VkDescriptorSetAllocateInfo allocInfo = {}; + allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + allocInfo.descriptorPool = descriptorPool; + allocInfo.descriptorSetCount = 1; + allocInfo.pSetLayouts = layouts; + + // Allocate descriptor sets + if (vkAllocateDescriptorSets(logicalDevice, &allocInfo, &collidorSphereDescriptorSet) != VK_SUCCESS) { + throw std::runtime_error("Failed to allocate descriptor set"); + } + + // Configure the descriptors to refer to buffers + VkDescriptorBufferInfo collidorSphereBufferInfo = {}; + collidorSphereBufferInfo.buffer = collidorSphere->GetBuffer(); + collidorSphereBufferInfo.offset = 0; + collidorSphereBufferInfo.range = sizeof(CollidorSphereObject); + + std::array descriptorWrites = {}; + descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrites[0].dstSet = collidorSphereDescriptorSet; + descriptorWrites[0].dstBinding = 0; + descriptorWrites[0].dstArrayElement = 0; + descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorWrites[0].descriptorCount = 1; + descriptorWrites[0].pBufferInfo = &collidorSphereBufferInfo; + descriptorWrites[0].pImageInfo = nullptr; + descriptorWrites[0].pTexelBufferView = nullptr; + + // Update descriptor sets + vkUpdateDescriptorSets(logicalDevice, static_cast(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); + } void Renderer::CreateGraphicsPipeline() { @@ -717,7 +955,7 @@ void Renderer::CreateComputePipeline() { computeShaderStageInfo.pName = "main"; // TODO: Add the compute dsecriptor set layout you create to this list - std::vector descriptorSetLayouts = { cameraDescriptorSetLayout, timeDescriptorSetLayout }; + std::vector descriptorSetLayouts = { cameraDescriptorSetLayout, timeDescriptorSetLayout, collidorSphereDescriptorSetLayout, computeDescriptorSetLayout }; // Create pipeline layout VkPipelineLayoutCreateInfo pipelineLayoutInfo = {}; @@ -883,7 +1121,19 @@ void Renderer::RecordComputeCommandBuffer() { // Bind descriptor set for time uniforms vkCmdBindDescriptorSets(computeCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipelineLayout, 1, 1, &timeDescriptorSet, 0, nullptr); + // Bind descriptor set for collidor sphere uniforms + vkCmdBindDescriptorSets(computeCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipelineLayout, 2, 1, &collidorSphereDescriptorSet, 0, nullptr); + + // TODO: For each group of blades bind its descriptor set and dispatch + for (uint32_t i = 0; i < scene->GetBlades().size(); ++i) { + // Bind descriptor sets for compute + vkCmdBindDescriptorSets(computeCommandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipelineLayout, 3, 1, &computeDescriptorSets[i], 0, nullptr); + + // Dispatch the compute kernel, with one thread for each vertex + vkCmdDispatch(computeCommandBuffer, (int)ceil(NUM_BLADES / WORKGROUP_SIZE), 1, 1); + } + // ~ End recording ~ if (vkEndCommandBuffer(computeCommandBuffer) != VK_SUCCESS) { @@ -976,13 +1226,14 @@ void Renderer::RecordCommandBuffers() { VkBuffer vertexBuffers[] = { scene->GetBlades()[j]->GetCulledBladesBuffer() }; VkDeviceSize offsets[] = { 0 }; // TODO: Uncomment this when the buffers are populated - // vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets); + vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets); // TODO: Bind the descriptor set for each grass blades model + vkCmdBindDescriptorSets(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, grassPipelineLayout, 1, 1, &grassDescriptorSets[j], 0, nullptr); // Draw // TODO: Uncomment this when the buffers are populated - // vkCmdDrawIndirect(commandBuffers[i], scene->GetBlades()[j]->GetNumBladesBuffer(), 0, 1, sizeof(BladeDrawIndirect)); + vkCmdDrawIndirect(commandBuffers[i], scene->GetBlades()[j]->GetNumBladesBuffer(), 0, 1, sizeof(BladeDrawIndirect)); } // End render pass @@ -1057,6 +1308,8 @@ Renderer::~Renderer() { vkDestroyDescriptorSetLayout(logicalDevice, cameraDescriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(logicalDevice, modelDescriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(logicalDevice, timeDescriptorSetLayout, nullptr); + vkDestroyDescriptorSetLayout(logicalDevice, computeDescriptorSetLayout, nullptr); + vkDestroyDescriptorSetLayout(logicalDevice, collidorSphereDescriptorSetLayout, nullptr); vkDestroyDescriptorPool(logicalDevice, descriptorPool, nullptr); diff --git a/src/Renderer.h b/src/Renderer.h index 95e025f..104603a 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -4,11 +4,12 @@ #include "SwapChain.h" #include "Scene.h" #include "Camera.h" +#include "CollidorSphere.h" class Renderer { public: Renderer() = delete; - Renderer(Device* device, SwapChain* swapChain, Scene* scene, Camera* camera); + Renderer(Device* device, SwapChain* swapChain, Scene* scene, Camera* camera, CollidorSphere* collidorSphere); ~Renderer(); void CreateCommandPools(); @@ -19,6 +20,8 @@ class Renderer { void CreateModelDescriptorSetLayout(); void CreateTimeDescriptorSetLayout(); void CreateComputeDescriptorSetLayout(); + void CreateCollidorSphereDescriptorSetLayout(); + void CreateDescriptorPool(); @@ -27,6 +30,7 @@ class Renderer { void CreateGrassDescriptorSets(); void CreateTimeDescriptorSet(); void CreateComputeDescriptorSets(); + void CreateCollidorSphereDescriptorSet(); void CreateGraphicsPipeline(); void CreateGrassPipeline(); @@ -48,6 +52,9 @@ class Renderer { Scene* scene; Camera* camera; + CollidorSphere* collidorSphere; + + VkCommandPool graphicsCommandPool; VkCommandPool computeCommandPool; @@ -56,12 +63,18 @@ class Renderer { VkDescriptorSetLayout cameraDescriptorSetLayout; VkDescriptorSetLayout modelDescriptorSetLayout; VkDescriptorSetLayout timeDescriptorSetLayout; - + VkDescriptorSetLayout computeDescriptorSetLayout; + VkDescriptorSetLayout collidorSphereDescriptorSetLayout; + VkDescriptorPool descriptorPool; VkDescriptorSet cameraDescriptorSet; std::vector modelDescriptorSets; VkDescriptorSet timeDescriptorSet; + std::vector computeDescriptorSets; + std::vector grassDescriptorSets; + VkDescriptorSet collidorSphereDescriptorSet; + VkPipelineLayout graphicsPipelineLayout; VkPipelineLayout grassPipelineLayout; diff --git a/src/Scene.cpp b/src/Scene.cpp index 86894f2..da63944 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -38,6 +38,10 @@ VkBuffer Scene::GetTimeBuffer() const { return timeBuffer; } +float Scene::getTotalTime() { + return time.totalTime; +} + Scene::~Scene() { vkUnmapMemory(device->GetVkDevice(), timeBufferMemory); vkDestroyBuffer(device->GetVkDevice(), timeBuffer, nullptr); diff --git a/src/Scene.h b/src/Scene.h index 7699d78..b7b162b 100644 --- a/src/Scene.h +++ b/src/Scene.h @@ -42,4 +42,7 @@ high_resolution_clock::time_point startTime = high_resolution_clock::now(); VkBuffer GetTimeBuffer() const; void UpdateTime(); + + + float getTotalTime(); }; diff --git a/src/SwapChain.cpp b/src/SwapChain.cpp index 711fec0..5309f7b 100644 --- a/src/SwapChain.cpp +++ b/src/SwapChain.cpp @@ -190,6 +190,9 @@ VkSemaphore SwapChain::GetRenderFinishedVkSemaphore() const { void SwapChain::Recreate() { Destroy(); + + //glfwSetWindowSize(GetGLFWWindow(), width, height); + Create(); } @@ -233,6 +236,7 @@ bool SwapChain::Present() { } if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { + Recreate(); return false; } diff --git a/src/main.cpp b/src/main.cpp index 8bf822b..9e3faec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,13 +3,18 @@ #include "Window.h" #include "Renderer.h" #include "Camera.h" +#include "CollidorSphere.h" #include "Scene.h" #include "Image.h" +#include // print fps + Device* device; SwapChain* swapChain; Renderer* renderer; Camera* camera; +CollidorSphere* collidorSphere; + namespace { void resizeCallback(GLFWwindow* window, int width, int height) { @@ -63,11 +68,32 @@ namespace { previousY = yPosition; } } + + void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { + if (action == GLFW_PRESS || action == GLFW_REPEAT) { + switch (key) { + case GLFW_KEY_W: + collidorSphere->UpdatePosition(glm::vec3(0.0f, 0.0f, -1.0f)); + break; + case GLFW_KEY_A: + collidorSphere->UpdatePosition(glm::vec3(-1.0f, 0.0f, 0.0f)); + break; + case GLFW_KEY_S: + collidorSphere->UpdatePosition(glm::vec3(0.0f, 0.0f, 1.0f)); + break; + case GLFW_KEY_D: + collidorSphere->UpdatePosition(glm::vec3(1.0f, 0.0f, 0.0f)); + break; + } + } + } } int main() { static constexpr char* applicationName = "Vulkan Grass Rendering"; - InitializeWindow(640, 480, applicationName); + //InitializeWindow(640, 480, applicationName); + + InitializeWindow(1280, 960, applicationName); unsigned int glfwExtensionCount = 0; const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); @@ -90,7 +116,10 @@ int main() { swapChain = device->CreateSwapChain(surface, 5); - camera = new Camera(device, 640.f / 480.f); + //camera = new Camera(device, 640.f / 480.f); + camera = new Camera(device, 1280.f / 960.f); + + collidorSphere = new CollidorSphere(device, glm::vec3(0.0f, 6.0f, 0.0f), 5.0f); VkCommandPoolCreateInfo transferPoolInfo = {}; transferPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; @@ -116,7 +145,8 @@ int main() { grassImageMemory ); - float planeDim = 15.f; + //float planeDim = 15.f; + float planeDim = 50.f; float halfWidth = planeDim * 0.5f; Model* plane = new Model(device, transferCommandPool, { @@ -137,13 +167,25 @@ int main() { scene->AddModel(plane); scene->AddBlades(blades); - renderer = new Renderer(device, swapChain, scene, camera); + renderer = new Renderer(device, swapChain, scene, camera, collidorSphere); + + glfwSetKeyCallback(GetGLFWWindow(), keyCallback); glfwSetWindowSizeCallback(GetGLFWWindow(), resizeCallback); glfwSetMouseButtonCallback(GetGLFWWindow(), mouseDownCallback); glfwSetCursorPosCallback(GetGLFWWindow(), mouseMoveCallback); + float lastTime = scene->getTotalTime(); + float nFrames = 0.0f; + while (!ShouldQuit()) { + nFrames += 1.0f; + if (scene->getTotalTime() - lastTime >= 1.0) { + std::cout << "FPS " << nFrames << " , "<< (1000.0f / nFrames) << " ms/frame" <UpdateTime(); renderer->Frame(); diff --git a/src/shaders/compute.comp b/src/shaders/compute.comp index 0fd0224..5a4f310 100644 --- a/src/shaders/compute.comp +++ b/src/shaders/compute.comp @@ -21,6 +21,11 @@ struct Blade { vec4 up; }; +layout(set = 2, binding = 0) uniform CollidorSphereObject { + vec4 collidorSphereInfo; +} collidorSphere; + + // TODO: Add bindings to: // 1. Store the input blades // 2. Write out the culled blades @@ -28,13 +33,24 @@ struct Blade { // The project is using vkCmdDrawIndirect to use a buffer as the arguments for a draw call // This is sort of an advanced feature so we've showed you what this buffer should look like -// -// layout(set = ???, binding = ???) buffer NumBlades { -// uint vertexCount; // Write the number of blades remaining here -// uint instanceCount; // = 1 -// uint firstVertex; // = 0 -// uint firstInstance; // = 0 -// } numBlades; + + +layout(set = 3, binding = 0) buffer Blades { + Blade blades[]; +}; + +layout(set = 3, binding = 1) buffer CulledBlades { + Blade culledBlades[]; +}; + +layout(set = 3, binding = 2) buffer NumBlades { + uint vertexCount; // Write the number of blades remaining here + uint instanceCount; // = 1 + uint firstVertex; // = 0 + uint firstInstance; // = 0 +} numBlades; + + bool inBounds(float value, float bounds) { return (value >= -bounds) && (value <= bounds); @@ -43,14 +59,199 @@ bool inBounds(float value, float bounds) { void main() { // Reset the number of blades to 0 if (gl_GlobalInvocationID.x == 0) { - // numBlades.vertexCount = 0; + numBlades.vertexCount = 0; } + + uint index = gl_GlobalInvocationID.x; + barrier(); // Wait till all threads reach this point + Blade thisBlade = blades[index]; + + // --------------------------------------------------------------------------------------- + // TODO: Apply forces on every blade and update the vertices in the buffer + + // Extra information we need from blade + vec3 v0_vec3 = thisBlade.v0.xyz; + float direction = thisBlade.v0.w; + + float height = thisBlade.v1.w; + + vec3 v2_vec3 = thisBlade.v2.xyz; + + vec3 up_vec3 = thisBlade.up.xyz; + float stiffnessCo = thisBlade.up.w; + + + // Recovery force + vec3 iv2 = v0_vec3 + up_vec3 * height; //initial position of v2 + + vec3 recovery = stiffnessCo * (iv2 - v2_vec3); + + + // Gravity + float gravityAccel = 9.8; + vec3 gravityDir = -normalize(up_vec3); // assume gravity has the opposite direction as up + + vec3 gE = gravityAccel * gravityDir; + + vec3 direction_vec = -normalize(vec3(sin(direction), 0.0, cos(direction))); + + vec3 gF = 0.25 * length(gE) * direction_vec; + + vec3 g = gE + gF; + + // Wind force + float frequency = 3.0; + float windIntensity = 5.0; + vec3 windDirection = normalize(vec3(1.0, 0, 0)); + + // simple sin wind function + vec3 wi_v0 = windIntensity * (0.5 * sin(0.8 * 3.1415926) + sin(0.2 * v0_vec3.x + totalTime/frequency) + 1.5) * windDirection; + //vec3 wi_v0 = windIntensity * (sin(0.8 * 3.1415926)) * windDirection; + + vec3 v0_to_v2 = v2_vec3 - v0_vec3; + + float f_d = 1.0 - abs(dot(wi_v0 / length(wi_v0), (v0_to_v2) / length(v0_to_v2))); + float f_r = dot(v0_to_v2, up_vec3) / height; + + vec3 windForce = f_d * f_r * wi_v0; + + // Collision Translation + vec3 collidorSphereCentroid = collidorSphere.collidorSphereInfo.xyz; + + //collidorSphereCentroid.x += 5.0 * sin(totalTime / frequency); + //collidorSphereCentroid.z += 5.0 * cos(totalTime / frequency); + + float collidorShpereR = collidorSphere.collidorSphereInfo.w; + + vec3 collision_m = 0.25 * v0_vec3 + 0.5 * thisBlade.v1.xyz + 0.25 * v2_vec3; + + // a collision of reaction of m has to be translated to a reaction of v2 + vec3 d = 4.0 * min(length(collidorSphereCentroid - collision_m) - collidorShpereR, 0) * normalize(collidorSphereCentroid - collision_m); + + // a collision of reaction of v2 + d += min(length(collidorSphereCentroid - v2_vec3) - collidorShpereR, 0) * normalize(collidorSphereCentroid - v2_vec3); + + // move V2 + v2_vec3 += deltaTime * (recovery + g + windForce); + v2_vec3 += d; + + + + // State Validation + // Ensure v2 is above the local plane + float tmp = min(dot(up_vec3, v2_vec3 - v0_vec3), 0); + v2_vec3 = v2_vec3 - tmp * up_vec3; + + // Get v1 acoording to v0 and v2 + float l_proj = length(v2_vec3 - v0_vec3 - dot(v2_vec3 - v0_vec3, up_vec3) * up_vec3); + + float l_proj_over_h = l_proj / height; + + vec3 v1_vec3 = v0_vec3 + height * max(1.0 - l_proj_over_h, 0.05 * max(l_proj_over_h, 1.0)) * up_vec3; + + // Correction + float L0 = distance(v2_vec3, v0_vec3); + float L1 = distance(v2_vec3, v1_vec3) + distance(v1_vec3, v0_vec3); + float n = 3.0; // This is Bezier curve degree + + float L = (2.0 * L0 + (n - 1.0) * L1) / (n + 1.0); + float r = height / L; + + vec3 v1_vec3_corr = v0_vec3 + r * (v1_vec3 - v0_vec3); + vec3 v2_vec3_corr = v1_vec3_corr + r * (v2_vec3 - v1_vec3); + + thisBlade.v1.xyz = v1_vec3_corr; + thisBlade.v2.xyz = v2_vec3_corr; + + blades[index] = thisBlade; // write back + + // --------------------------------------------------------------------------------------- // TODO: Cull blades that are too far away or not in the camera frustum and write them // to the culled blades buffer // Note: to do this, you will need to use an atomic operation to read and update numBlades.vertexCount // You want to write the visible blades to the buffer without write conflicts between threads + + + // Orientation culling + bool isOrientationCulled = false; + vec3 viewDir; + float oriCullThreshold = 0.05f; + + // mat4 should be column major + viewDir.x = camera.view[0][0] + camera.view[0][1] + camera.view[0][2]; // view - Right + viewDir.y = 0; + viewDir.z = camera.view[2][0] + camera.view[2][1] + camera.view[2][2]; // view - Forward + viewDir = normalize(viewDir); + + if(abs(dot(viewDir, direction_vec)) < oriCullThreshold){ + isOrientationCulled = true; + } + + + + // Viewfrustum culling + mat4 VP = camera.proj * camera.view; + vec4 viewfrustumTest_v0 = vec4(thisBlade.v0.xyz, 1.0); + vec4 viewfrustumTest_m = vec4(0.25 * thisBlade.v0.xyz + 0.5 * thisBlade.v1.xyz + 0.25 * thisBlade.v2.xyz, 1.0); + vec4 viewfrustumTest_v2 = vec4(thisBlade.v2.xyz, 1.0); + + viewfrustumTest_v0 = VP * viewfrustumTest_v0; + viewfrustumTest_v0 /= viewfrustumTest_v0.w; + + float t = 0.3; // threshold value + + float h = viewfrustumTest_v0.w + t; + bool is_v0_in_frustem = (viewfrustumTest_v0.x >= -h) && (viewfrustumTest_v0.x <= h) && (viewfrustumTest_v0.y >= -h) && (viewfrustumTest_v0.y <= h) && (viewfrustumTest_v0.z >= -h) && (viewfrustumTest_v0.z <= h); + + h = viewfrustumTest_m.w + t; + bool is_m_in_frustem = (viewfrustumTest_m.x >= -h) && (viewfrustumTest_m.x <= h) && (viewfrustumTest_m.y >= -h) && (viewfrustumTest_m.y <= h) && (viewfrustumTest_m.z >= -h) && (viewfrustumTest_m.z <= h); + + h = viewfrustumTest_v2.w + t; + bool is_v2_in_frustem = (viewfrustumTest_v2.x >= -h) && (viewfrustumTest_v2.x <= h) && (viewfrustumTest_v2.y >= -h) && (viewfrustumTest_v2.y <= h) && (viewfrustumTest_v2.z >= -h) && (viewfrustumTest_v2.z <= h); + + bool isViewfrustumCulled = ((!is_v0_in_frustem) && (!is_m_in_frustem) && (!is_v2_in_frustem)); + + + + + // Distance culling + float maxCullingDistance = 100.0; + uint numOfDistanceCullBuckets = 5; + + vec3 cameraPos; + cameraPos.x = -camera.view[3][0]; + cameraPos.y = -camera.view[3][1]; + cameraPos.z = -camera.view[3][2]; + + float d_proj = length(v0_vec3 - cameraPos - dot(v0_vec3 - cameraPos , up_vec3) * up_vec3); + + uint tmp1 = (index % numOfDistanceCullBuckets); + float tmp2 = floor(numOfDistanceCullBuckets * (1.0 - d_proj / maxCullingDistance)); + + bool isDistanceCulled = !( tmp1 <= uint(tmp2) ); + + + // orientation culling debug +// if(isOrientationCulled){ +// culledBlades[atomicAdd(numBlades.vertexCount , 1)] = thisBlade; +// } + + // view frustum culling debug +// if(isViewfrustumCulled){ +// culledBlades[atomicAdd(numBlades.vertexCount , 1)] = thisBlade; +// } + + // distance culling debug +// if(isDistanceCulled){ +// culledBlades[atomicAdd(numBlades.vertexCount , 1)] = thisBlade; +// } + + if(!isOrientationCulled && !isViewfrustumCulled && !isDistanceCulled){ + culledBlades[atomicAdd(numBlades.vertexCount , 1)] = thisBlade; + } + } diff --git a/src/shaders/grass.frag b/src/shaders/grass.frag index c7df157..acbcc28 100644 --- a/src/shaders/grass.frag +++ b/src/shaders/grass.frag @@ -8,10 +8,28 @@ layout(set = 0, binding = 0) uniform CameraBufferObject { // TODO: Declare fragment shader inputs +// input from tessellation evaluation + +layout(location = 0) in vec4 pos; +layout(location = 1) in vec3 nor; +layout(location = 2) in vec3 forward; +layout(location = 3) in vec2 uv; + layout(location = 0) out vec4 outColor; void main() { // TODO: Compute fragment color - outColor = vec4(1.0); + vec3 upperColor = vec3(0.2,1.0,0.2); + + vec3 lowerColor = vec3(0.0,0.3,0.1); + + // a blend effect grass color + vec3 mixedColor = mix(lowerColor, upperColor, uv.y); + + vec3 PointLightPos = normalize(vec3(-5.0, 8.0, 3.0)); + + float LambertTerm = clamp(dot(nor, PointLightPos), 0, 1.0) + 0.15; //simple Lambert shading + ambient light + + outColor = vec4(mixedColor * LambertTerm, 1.0); } diff --git a/src/shaders/grass.tesc b/src/shaders/grass.tesc index f9ffd07..132125f 100644 --- a/src/shaders/grass.tesc +++ b/src/shaders/grass.tesc @@ -10,17 +10,61 @@ layout(set = 0, binding = 0) uniform CameraBufferObject { // TODO: Declare tessellation control shader inputs and outputs +layout (location = 0) in vec4 tessellation_control_v1[]; +layout (location = 1) in vec4 tessellation_control_v2[]; +layout (location = 2) in vec4 tessellation_control_up[]; +layout (location = 3) in vec4 tessellation_control_forward[]; + +layout(location = 0) patch out vec4 tessellation_eval_v1; +layout(location = 1) patch out vec4 tessellation_eval_v2; +layout(location = 2) patch out vec4 tessellation_eval_up; +layout(location = 3) patch out vec4 tessellation_eval_forward; + + void main() { // Don't move the origin location of the patch - gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; + gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; // TODO: Write any shader outputs + // So far, just pass through + tessellation_eval_v1 = tessellation_control_v1[0]; + tessellation_eval_v2 = tessellation_control_v2[0]; + tessellation_eval_up = tessellation_control_up[0]; + tessellation_eval_forward = tessellation_control_forward[0]; // TODO: Set level of tesselation - // gl_TessLevelInner[0] = ??? - // gl_TessLevelInner[1] = ??? - // gl_TessLevelOuter[0] = ??? - // gl_TessLevelOuter[1] = ??? - // gl_TessLevelOuter[2] = ??? - // gl_TessLevelOuter[3] = ??? +// gl_TessLevelInner[0] = 1.0; +// gl_TessLevelInner[1] = 5.0; + +// gl_TessLevelOuter[0] = 5.0; +// gl_TessLevelOuter[1] = 1.0; +// gl_TessLevelOuter[2] = 5.0; +// gl_TessLevelOuter[3] = 1.0; + + + // Tess depend on the depth of origin location + vec4 originLocation = gl_in[gl_InvocationID].gl_Position; + originLocation.w = 1.0; + + // To clip space + originLocation = camera.proj * camera.view * originLocation; + + // To NDC + originLocation /= originLocation.w; + + float depth = clamp(-originLocation.z, 0.0, 1.0); + + float minTessLevel = 2.0; + float maxTessLevel = 8.0; + + // nearer, higher tessl level + float mixLevel = mix(maxTessLevel, minTessLevel, 0.25 * floor(depth / 0.2)); + + gl_TessLevelInner[0] = 1.0; + gl_TessLevelInner[1] = mixLevel; + + gl_TessLevelOuter[0] = mixLevel; + gl_TessLevelOuter[1] = 1.0; + gl_TessLevelOuter[2] = mixLevel; + gl_TessLevelOuter[3] = 1.0; } diff --git a/src/shaders/grass.tese b/src/shaders/grass.tese index 751fff6..f7c274c 100644 --- a/src/shaders/grass.tese +++ b/src/shaders/grass.tese @@ -10,9 +10,53 @@ layout(set = 0, binding = 0) uniform CameraBufferObject { // TODO: Declare tessellation evaluation shader inputs and outputs +// input from control shader +layout(location = 0) patch in vec4 tessellation_eval_v1; +layout(location = 1) patch in vec4 tessellation_eval_v2; +layout(location = 2) patch in vec4 tessellation_eval_up; +layout(location = 3) patch in vec4 tessellation_eval_forward; + +layout(location = 0) out vec4 pos; +layout(location = 1) out vec3 nor; +layout(location = 2) out vec3 forward; +layout(location = 3) out vec2 uv; + + + void main() { float u = gl_TessCoord.x; float v = gl_TessCoord.y; // TODO: Use u and v to parameterize along the grass blade and output positions for each vertex of the grass blade + + // refer to the paper + // Blade geometry + vec3 a = gl_in[0].gl_Position.xyz + v * (tessellation_eval_v1.xyz - gl_in[0].gl_Position.xyz); + vec3 b = tessellation_eval_v1.xyz + v * (tessellation_eval_v2.xyz - tessellation_eval_v1.xyz); + vec3 c = a + v * (b - a); + + vec3 t1 = cross(tessellation_eval_up.xyz, tessellation_eval_forward.xyz); + vec3 wt1 = t1 * tessellation_eval_v2.w * 0.5; + + vec3 c0 = c - wt1; + vec3 c1 = c + wt1; + + vec3 t0 = normalize(b - a); + nor = normalize(cross(t1, t0)); + + uv = vec2(u, v); + + // triagnle + float t = u + 0.5 * v - u * v; + pos.xyz = (1.0 - t) * c0 + t * c1; + pos = camera.proj * camera.view * vec4(pos.xyz, 1.0); + + forward = tessellation_eval_forward.xyz; + + // set gl_Position in clip space! + gl_Position = pos; + + + //gl_Position = camera.proj * camera.view * (gl_in[0].gl_Position + vec4(1.0 - u, v, 0.0, 0.0)); + } diff --git a/src/shaders/grass.vert b/src/shaders/grass.vert index db9dfe9..339d0e5 100644 --- a/src/shaders/grass.vert +++ b/src/shaders/grass.vert @@ -7,6 +7,16 @@ layout(set = 1, binding = 0) uniform ModelBufferObject { }; // TODO: Declare vertex shader inputs and outputs +layout (location = 0) in vec4 v0; +layout (location = 1) in vec4 v1; +layout (location = 2) in vec4 v2; +layout (location = 3) in vec4 up; + +layout (location = 0) out vec4 tessellation_control_v1; +layout (location = 1) out vec4 tessellation_control_v2; +layout (location = 2) out vec4 tessellation_control_up; +layout (location = 3) out vec4 tessellation_control_forward; + out gl_PerVertex { vec4 gl_Position; @@ -14,4 +24,24 @@ out gl_PerVertex { void main() { // TODO: Write gl_Position and any other shader outputs + + tessellation_control_v1 = model * vec4(v1.xyz, 1.0); + tessellation_control_v1.w = v1.w; // keep original height + + tessellation_control_v2 = model * vec4(v2.xyz, 1.0); + tessellation_control_v2.w = v2.w; // keep original width + + tessellation_control_up = vec4(normalize(up.xyz), 0.0); + + float direction = v0.w; // direction + + vec3 direction_vec = -normalize(vec3(sin(direction), 0.0, cos(direction))); + + //vec3 forward = normalize(cross(normalize(up.xyz), direction_vec)); + + tessellation_control_forward = vec4(direction_vec, 0.0); + + // write gl_Position as usual + gl_Position = model * vec4(v0.xyz, 1.0); + }