forked from KhronosGroup/Vulkan-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecialization_constants.h
More file actions
124 lines (98 loc) · 4.13 KB
/
specialization_constants.h
File metadata and controls
124 lines (98 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* Copyright (c) 2019-2020, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "buffer_pool.h"
#include "rendering/render_pipeline.h"
#include "rendering/subpasses/forward_subpass.h"
#include "scene_graph/components/light.h"
#include "scene_graph/components/mesh.h"
#include "scene_graph/components/perspective_camera.h"
#include "vulkan_sample.h"
#define LIGHT_COUNT 1
struct alignas(16) CustomForwardLights
{
uint32_t count;
vkb::Light lights[LIGHT_COUNT];
};
/**
* @brief Using specialization constants
*/
class SpecializationConstants : public vkb::VulkanSample
{
public:
SpecializationConstants();
virtual ~SpecializationConstants() = default;
virtual bool prepare(vkb::Platform &platform) override;
/**
* @brief This subpass is responsible for rendering a Scene
* It implements a custom draw function which passes a custom light count
*/
class ForwardSubpassCustomLights : public vkb::ForwardSubpass
{
public:
ForwardSubpassCustomLights(vkb::RenderContext &render_context,
vkb::ShaderSource &&vertex_source, vkb::ShaderSource &&fragment_source,
vkb::sg::Scene &scene, vkb::sg::Camera &camera);
virtual void prepare() override;
virtual void draw(vkb::CommandBuffer &command_buffer) override;
/**
* @brief Create a buffer allocation from scene graph lights for the specialization constants sample
* Provides the specified number of lights, regardless of how many are within the scene
*
* @tparam T ForwardLights / DeferredLights
* @param command_buffer The command buffer that the returned light buffer allocation will be bound to
* @param scene_lights Lights from the scene graph
* @param light_count Number of lights to render
* @return BufferAllocation A buffer allocation created for use in shaders
*/
template <typename T>
vkb::BufferAllocation allocate_custom_lights(vkb::CommandBuffer &command_buffer, const std::vector<vkb::sg::Light *> &scene_lights, size_t light_count)
{
T light_info;
light_info.count = vkb::to_u32(light_count);
std::vector<vkb::Light> lights;
for (auto &scene_light : scene_lights)
{
if (lights.size() < light_count)
{
const auto &properties = scene_light->get_properties();
auto & transform = scene_light->get_node()->get_transform();
vkb::Light light{{transform.get_translation(), static_cast<float>(scene_light->get_light_type())},
{properties.color, properties.intensity},
{transform.get_rotation() * properties.direction, properties.range},
{properties.inner_cone_angle, properties.outer_cone_angle}};
lights.push_back(light);
}
}
std::copy(lights.begin(), lights.end(), light_info.lights);
auto & render_frame = get_render_context().get_active_frame();
vkb::BufferAllocation light_buffer = render_frame.allocate_buffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, sizeof(T));
light_buffer.update(light_info);
return light_buffer;
};
};
private:
vkb::sg::PerspectiveCamera *camera{nullptr};
virtual void draw_gui() override;
virtual void render(vkb::CommandBuffer &command_buffer) override;
std::unique_ptr<vkb::RenderPipeline> create_specialization_renderpass();
std::unique_ptr<vkb::RenderPipeline> create_standard_renderpass();
std::unique_ptr<vkb::RenderPipeline> specialization_constants_pipeline{};
std::unique_ptr<vkb::RenderPipeline> standard_pipeline{};
int specialization_constants_enabled{0};
};
std::unique_ptr<vkb::VulkanSample> create_specialization_constants();