|
1 | 1 | # Shader Program
|
2 | 2 |
|
3 |
| -SPIR-V modules are binary files with a stride/alignment of 4 bytes. The Vulkan API accepts a span of `std::uint32_t`s, so we need to load it into such a buffer (and _not_ `std::vector<std::byte>` or other 1-byte equivalents). We will encapsulate both vertex and fragment shaders into a single `ShaderProgram`, which will also bind the shaders before a draw, and expose/set various dynamic states. |
| 3 | +To use Shader Objects we need to enable the corresponding feature and extension during device creation: |
| 4 | + |
| 5 | +```cpp |
| 6 | +auto shader_object_feature = |
| 7 | + vk::PhysicalDeviceShaderObjectFeaturesEXT{vk::True}; |
| 8 | +dynamic_rendering_feature.setPNext(&shader_object_feature); |
| 9 | + |
| 10 | +// ... |
| 11 | +// we need two device extensions: Swapchain and Shader Object. |
| 12 | +static constexpr auto extensions_v = std::array{ |
| 13 | + VK_KHR_SWAPCHAIN_EXTENSION_NAME, |
| 14 | + "VK_EXT_shader_object", |
| 15 | +}; |
| 16 | +``` |
| 17 | + |
| 18 | +We will encapsulate both vertex and fragment shaders into a single `ShaderProgram`, which will also bind the shaders before a draw, and expose/set various dynamic states. |
4 | 19 |
|
5 | 20 | In `shader_program.hpp`, first add a `ShaderProgramCreateInfo` struct:
|
6 | 21 |
|
@@ -233,35 +248,3 @@ void ShaderProgram::bind_shaders(vk::CommandBuffer const command_buffer) const {
|
233 | 248 | command_buffer.bindShadersEXT(stages_v, shaders);
|
234 | 249 | }
|
235 | 250 | ```
|
236 |
| -
|
237 |
| -## TODO: MOVE |
238 |
| -
|
239 |
| -Add new members to `App`: |
240 |
| -
|
241 |
| -```cpp |
242 |
| -void create_pipelines(); |
243 |
| -
|
244 |
| -[[nodiscard]] auto asset_path(std::string_view uri) const -> fs::path; |
245 |
| -``` |
246 |
| - |
247 |
| -Add code to load shaders in `create_pipelines()` and call it before starting the main loop: |
248 |
| - |
249 |
| -```cpp |
250 |
| -void App::create_pipelines() { |
251 |
| - auto shader_loader = ShaderLoader{*m_device}; |
252 |
| - // we only need shader modules to create the pipelines, thus no need to |
253 |
| - // store them as members. |
254 |
| - auto const vertex = shader_loader.load(asset_path("shader.vert")); |
255 |
| - auto const fragment = shader_loader.load(asset_path("shader.frag")); |
256 |
| - if (!vertex || !fragment) { |
257 |
| - throw std::runtime_error{"Failed to load Shaders"}; |
258 |
| - } |
259 |
| - std::println("[lvk] Shaders loaded"); |
260 |
| - |
261 |
| - // TODO |
262 |
| -} |
263 |
| - |
264 |
| -auto App::asset_path(std::string_view const uri) const -> fs::path { |
265 |
| - return m_assets_dir / uri; |
266 |
| -} |
267 |
| -``` |
0 commit comments