CMSC23740 Common Code Library
Support code for CS23740 programming projects
Loading...
Searching...
No Matches
application.hpp
Go to the documentation of this file.
1
7
8/*
9 * COPYRIGHT (c) 2026 John Reppy (https://cs.uchicago.edu/~jhr)
10 * All rights reserved.
11 */
12
13#ifndef _CS237_APPLICATION_HPP_
14#define _CS237_APPLICATION_HPP_
15
16#ifndef _CS237_HPP_
17#error "cs237/application.hpp should not be included directly"
18#endif
19
20namespace cs237 {
21
22namespace __detail { class TextureBase; }
23
26
27friend class Window;
28friend class Buffer;
29friend class MemoryObj;
31friend class Texture1D;
32friend class Texture2D;
33friend class DepthBuffer;
34friend class Attachment;
35friend class DepthAttachment;
36
37public:
38
42 Application (std::vector<std::string> const &args, std::string const &name = "CS237 App");
43
44 virtual ~Application ();
45
47 virtual void run () = 0;
48
50 std::string name () const { return this->_name; }
51
53 bool debug () const { return this->_debug; }
55 bool verbose () const
56 {
57 return (this->_messages & vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose)
58 == vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose;
59 }
60
63 static std::vector<vk::ExtensionProperties> supportedExtensions ()
64 {
65 return vk::enumerateInstanceExtensionProperties(nullptr);
66 }
67
71 std::vector<vk::ExtensionProperties> supportedDeviceExtensions ()
72 {
73 return this->_gpu.enumerateDeviceExtensionProperties();
74 }
75
78 static std::vector<vk::LayerProperties> supportedLayers ()
79 {
80 return vk::enumerateInstanceLayerProperties();
81 }
82
84 vk::Device device () const { return this->_device; }
85
87 const vk::PhysicalDeviceProperties *props () const
88 {
89 if (this->_propsCache == nullptr) {
91 }
92 return this->_propsCache;
93 }
94
96 const vk::PhysicalDeviceLimits *limits () const { return &this->props()->limits; }
97
99 const vk::PhysicalDeviceFeatures *features () const
100 {
101 if (this->_featuresCache == nullptr) {
103 }
104 return this->_featuresCache;
105 }
106
110 struct SamplerInfo {
111 vk::Filter magFilter;
112 vk::Filter minFilter;
113 vk::SamplerMipmapMode mipmapMode;
114 vk::SamplerAddressMode addressModeU;
115 vk::SamplerAddressMode addressModeV;
116 vk::SamplerAddressMode addressModeW;
117 vk::BorderColor borderColor;
118
120 : magFilter(vk::Filter::eLinear), minFilter(vk::Filter::eLinear),
121 mipmapMode(vk::SamplerMipmapMode::eLinear),
122 addressModeU(vk::SamplerAddressMode::eRepeat),
123 addressModeV(vk::SamplerAddressMode::eRepeat),
124 addressModeW(vk::SamplerAddressMode::eRepeat),
125 borderColor(vk::BorderColor::eIntOpaqueBlack)
126 { }
127
130 vk::Filter magF, vk::Filter minF, vk::SamplerMipmapMode mm,
131 vk::SamplerAddressMode am, vk::BorderColor color)
132 : magFilter(magF), minFilter(minF), mipmapMode(mm),
133 addressModeU(am), addressModeV(vk::SamplerAddressMode::eRepeat),
134 addressModeW(vk::SamplerAddressMode::eRepeat), borderColor(color)
135 { }
136
139 vk::Filter magF, vk::Filter minF, vk::SamplerMipmapMode mm,
140 vk::SamplerAddressMode am1, vk::SamplerAddressMode am2,
141 vk::BorderColor color)
142 : magFilter(magF), minFilter(minF), mipmapMode(mm),
143 addressModeU(am1), addressModeV(am2),
144 addressModeW(vk::SamplerAddressMode::eRepeat), borderColor(color)
145 { }
146
147 };
148
152 vk::Sampler createSampler (SamplerInfo const &info);
153
157 vk::Sampler createDepthSampler (SamplerInfo const &info);
158
160 vk::FormatProperties formatProps (vk::Format fmt) const
161 {
162 return this->_gpu.getFormatProperties(fmt);
163 }
164
175 vk::PipelineLayout createPipelineLayout (
176 vk::ArrayProxy<vk::DescriptorSetLayout> descSets,
177 vk::ArrayProxy<vk::PushConstantRange> pcrs)
178 {
179 vk::PipelineLayoutCreateInfo layoutInfo(
180 {}, /* flags */
181 descSets, /* set layouts */
182 pcrs); /* push-constant ranges */
183
184 return this->_device.createPipelineLayout(layoutInfo);
185 }
186
190 vk::PipelineLayout createPipelineLayout (vk::DescriptorSetLayout descSet)
191 {
192 vk::PipelineLayoutCreateInfo layoutInfo(
193 {}, /* flags */
194 descSet, /* set layouts */
195 {}); /* push-constant ranges */
196
197 return this->_device.createPipelineLayout(layoutInfo);
198 }
199
228 vk::Pipeline createPipeline (
229 cs237::Shaders *shaders,
230 vk::PipelineVertexInputStateCreateInfo const &vertexInfo,
231 vk::PrimitiveTopology prim,
232 bool primRestart,
233 vk::ArrayProxy<vk::Viewport> const &viewports,
234 vk::ArrayProxy<vk::Rect2D> const &scissors,
235 bool depthClamp,
236 vk::PolygonMode polyMode,
237 vk::CullModeFlags cullMode,
238 vk::FrontFace front,
239 vk::PipelineLayout layout,
240 vk::PipelineRenderingCreateInfo const &createInfo,
241 vk::PipelineColorBlendStateCreateInfo const &blending,
242 vk::ArrayProxy<vk::DynamicState> const &dynamic);
243
273 vk::Pipeline createPipeline (
274 cs237::Shaders *shaders,
275 vk::PipelineVertexInputStateCreateInfo const &vertexInfo,
276 vk::PrimitiveTopology prim,
277 bool primRestart,
278 vk::ArrayProxy<vk::Viewport> const &viewports,
279 vk::ArrayProxy<vk::Rect2D> const &scissors,
280 bool depthClamp,
281 vk::PolygonMode polyMode,
282 vk::CullModeFlags cullMode,
283 vk::FrontFace front,
284 vk::PipelineLayout layout,
285 vk::PipelineRenderingCreateInfo const &createInfo,
286 vk::ArrayProxy<vk::DynamicState> const &dynamic);
287
317 vk::Pipeline createPipeline (
318 cs237::Shaders *shaders,
319 vk::PipelineVertexInputStateCreateInfo const &vertexInfo,
320 vk::PrimitiveTopology prim,
321 vk::ArrayProxy<vk::Viewport> const &viewports,
322 vk::ArrayProxy<vk::Rect2D> const &scissors,
323 vk::PolygonMode polyMode,
324 vk::CullModeFlags cullMode,
325 vk::FrontFace front,
326 vk::PipelineLayout layout,
327 vk::PipelineRenderingCreateInfo const &createInfo,
328 vk::ArrayProxy<vk::DynamicState> const &dynamic)
329 {
330 return this->createPipeline(
331 shaders,
332 vertexInfo,
333 prim,
334 false,
335 viewports,
336 scissors,
337 false,
338 polyMode,
339 cullMode,
340 front,
341 layout,
342 createInfo,
343 dynamic);
344 }
345
346/* TODO: define a ComputeShader class, since compute shaders
347 * only have one stage and get used in different contexts.
348 */
353 vk::Pipeline createComputePipeline (vk::PipelineLayout layout, cs237::Shaders *shaders);
354
357 vk::CommandBuffer newCommandBuf ()
358 {
359 vk::CommandBufferAllocateInfo allocInfo(
360 this->_cmdPool,
361 vk::CommandBufferLevel::ePrimary,
362 1); /* buffer count */
363
364 return (this->_device.allocateCommandBuffers(allocInfo))[0];
365 }
366
370 void beginCommands (vk::CommandBuffer cmdBuf, bool oneTime = false)
371 {
372 vk::CommandBufferBeginInfo beginInfo(
373 oneTime
374 ? vk::CommandBufferUsageFlagBits::eOneTimeSubmit
375 : vk::CommandBufferUsageFlags());
376 cmdBuf.begin(beginInfo);
377 }
378
381 void endCommands (vk::CommandBuffer cmdBuf) { cmdBuf.end(); }
382
385 void submitCommands (vk::CommandBuffer cmdBuf)
386 {
387 vk::CommandBuffer cmdBufs[1] = { cmdBuf };
388 vk::SubmitInfo submitInfo(
389 {},
390 {},
391 vk::ArrayProxyNoTemporaries<const vk::CommandBuffer>(1, cmdBufs),
392 {});
393 this->_queues.graphics.submit ({submitInfo});
394 this->_queues.graphics.waitIdle();
395 }
396
399 void freeCommandBuf (vk::CommandBuffer & cmdBuf)
400 {
401 this->_device.freeCommandBuffers (this->_cmdPool, cmdBuf);
402 }
403
405 template <typename T>
406 struct Queues {
410 };
411
413 Queues<uint32_t> getQIndices () const { return this->_qIdxs; }
414
415protected:
416
417 // information about swap-chain support
419 vk::SurfaceCapabilitiesKHR capabilities;
420 std::vector<vk::SurfaceFormatKHR> formats;
421 std::vector<vk::PresentModeKHR> presentModes;
422 };
423
424 std::string _name;
425 vk::DebugUtilsMessageSeverityFlagsEXT _messages;
427 bool _debug;
428 vk::Instance _instance;
429 vk::PhysicalDevice _gpu;
430 mutable vk::PhysicalDeviceProperties *_propsCache;
431 mutable vk::PhysicalDeviceFeatures *_featuresCache;
433 vk::Device _device;
436 vk::CommandPool _cmdPool;
437#ifdef CS237_USE_VMA
438 VmaAllocator _allocator;
439#endif
440
444
448
452
456 void _selectDevice (vk::PhysicalDeviceFeatures *reqFeatures = nullptr);
457
464 int32_t _findMemory (uint32_t reqTypeBits, vk::MemoryPropertyFlags reqProps) const;
465
474 vk::Format _findBestFormat (
475 std::vector<vk::Format> candidates,
476 vk::ImageTiling tiling,
477 vk::FormatFeatureFlags features);
478
481
489 vk::Format _depthStencilBufferFormat (bool depth, bool stencil);
490
498 bool _getQIndices (vk::PhysicalDevice dev);
499
505
516 vk::Image _createImage (
517 uint32_t wid,
518 uint32_t ht,
519 vk::Format format,
520 vk::ImageTiling tiling,
521 vk::ImageUsageFlags usage,
522 vk::ImageLayout layout,
523 uint32_t mipLvls = 1);
524
534 vk::Image _createImage (
535 uint32_t wid,
536 uint32_t ht,
537 vk::Format format,
538 vk::ImageTiling tiling,
539 vk::ImageUsageFlags usage,
540 uint32_t mipLvls = 1)
541 {
542 return this->_createImage (
543 wid, ht, format, tiling, usage,
544 vk::ImageLayout::eUndefined,
545 mipLvls);
546 }
547
552 vk::DeviceMemory _allocImageMemory (vk::Image img, vk::MemoryPropertyFlags props);
553
560 vk::ImageView _createImageView (
561 vk::Image img, vk::Format fmt, vk::ImageAspectFlags aspectFlags);
562
569 vk::Image img,
570 vk::Format fmt,
571 vk::ImageLayout oldLayout,
572 vk::ImageLayout newLayout);
573
578 vk::Buffer _createBuffer (size_t size, vk::BufferUsageFlags usage);
579
584 vk::DeviceMemory _allocBufferMemory (vk::Buffer buf, vk::MemoryPropertyFlags props);
585
591 void _copyBuffer (vk::Buffer dstBuf, vk::Buffer srcBuf, size_t offset, size_t size);
592
601 vk::Image dstImg, vk::Buffer srcBuf, size_t size,
602 uint32_t wid, uint32_t ht=1, uint32_t depth=1);
603
604 /* debug-message support */
605 VkDebugUtilsMessengerEXT _debugMessenger;
606 void _initDebug ();
608};
609
610} // namespace cs237
611
612#endif // !_CS237_APPLICATION_HPP_
Definition texture.hpp:25
friend class DepthAttachment
Definition application.hpp:35
void _createInstance()
A helper function to create and initialize the Vulkan instance used by the application.
friend class DepthBuffer
Definition application.hpp:33
std::vector< vk::ExtensionProperties > supportedDeviceExtensions()
Get the list of supported Vulkan device extensions for the selected physical device.
Definition application.hpp:71
vk::Device _device
the logical device that we are using to render
Definition application.hpp:433
vk::Format _depthStencilBufferFormat(bool depth, bool stencil)
A helper function to identify the best depth/stencil-buffer attachment format for the device.
void _createLogicalDevice()
A helper function to create the logical device during initialization.
void _getPhysicalDeviceFeatures() const
function that gets the physical-device features and caches the pointer in the _featuresCache field.
vk::PhysicalDeviceFeatures * _featuresCache
a cache of the physical device properties
Definition application.hpp:431
vk::DeviceMemory _allocBufferMemory(vk::Buffer buf, vk::MemoryPropertyFlags props)
A helper function for allocating and binding device memory for a buffer.
const vk::PhysicalDeviceLimits * limits() const
access function for the physical device limits
Definition application.hpp:96
void freeCommandBuf(vk::CommandBuffer &cmdBuf)
free the command buffer
Definition application.hpp:399
VkDebugUtilsMessengerEXT _debugMessenger
Definition application.hpp:605
void _getPhysicalDeviceProperties() const
function that gets the physical-device properties and caches the pointer in the _propsCache field.
static std::vector< vk::LayerProperties > supportedLayers()
Get the list of supported layers.
Definition application.hpp:78
Application(std::vector< std::string > const &args, std::string const &name="CS237 App")
constructor for application base class
vk::Buffer _createBuffer(size_t size, vk::BufferUsageFlags usage)
create a vk::Buffer object
void _copyBufferToImage(vk::Image dstImg, vk::Buffer srcBuf, size_t size, uint32_t wid, uint32_t ht=1, uint32_t depth=1)
copy data from a buffer to an image
std::string name() const
return the application name
Definition application.hpp:50
Queues< uint32_t > _qIdxs
the queue family indices
Definition application.hpp:434
bool verbose() const
is the program in verbose mode?
Definition application.hpp:55
friend class MemoryObj
Definition application.hpp:29
void _transitionImageLayout(vk::Image img, vk::Format fmt, vk::ImageLayout oldLayout, vk::ImageLayout newLayout)
A helper function for changing the layout of an image.
vk::Pipeline createComputePipeline(vk::PipelineLayout layout, cs237::Shaders *shaders)
Create a compute pipeline.
vk::PipelineLayout createPipelineLayout(vk::DescriptorSetLayout descSet)
Create a pipeline layout for a single descriptor set.
Definition application.hpp:190
vk::CommandBuffer newCommandBuf()
create and initialize a command buffer
Definition application.hpp:357
vk::Pipeline createPipeline(cs237::Shaders *shaders, vk::PipelineVertexInputStateCreateInfo const &vertexInfo, vk::PrimitiveTopology prim, vk::ArrayProxy< vk::Viewport > const &viewports, vk::ArrayProxy< vk::Rect2D > const &scissors, vk::PolygonMode polyMode, vk::CullModeFlags cullMode, vk::FrontFace front, vk::PipelineLayout layout, vk::PipelineRenderingCreateInfo const &createInfo, vk::ArrayProxy< vk::DynamicState > const &dynamic)
Allocate a graphics pipeline using common defaults.
Definition application.hpp:317
vk::PhysicalDeviceProperties * _propsCache
Definition application.hpp:430
void _copyBuffer(vk::Buffer dstBuf, vk::Buffer srcBuf, size_t offset, size_t size)
copy data from one buffer to another using the GPU
friend class Window
Definition application.hpp:27
friend class Buffer
Definition application.hpp:28
bool _debug
set when validation layers should be enabled
Definition application.hpp:427
const vk::PhysicalDeviceProperties * props() const
get the physical-device properties pointer
Definition application.hpp:87
void endCommands(vk::CommandBuffer cmdBuf)
end the recording of commands in the give command buffer
Definition application.hpp:381
vk::DebugUtilsMessageSeverityFlagsEXT _messages
set to the message severity level
Definition application.hpp:425
vk::PhysicalDevice _gpu
the graphics card (aka device) that we are using
Definition application.hpp:429
friend class Attachment
Definition application.hpp:34
vk::Instance _instance
the Vulkan instance used by the application
Definition application.hpp:428
virtual ~Application()
vk::Sampler createDepthSampler(SamplerInfo const &info)
Create a depth-texture sampler as specified.
vk::FormatProperties formatProps(vk::Format fmt) const
access function for the properties of an image format
Definition application.hpp:160
Queues< uint32_t > getQIndices() const
get the queue indices
Definition application.hpp:413
vk::Pipeline createPipeline(cs237::Shaders *shaders, vk::PipelineVertexInputStateCreateInfo const &vertexInfo, vk::PrimitiveTopology prim, bool primRestart, vk::ArrayProxy< vk::Viewport > const &viewports, vk::ArrayProxy< vk::Rect2D > const &scissors, bool depthClamp, vk::PolygonMode polyMode, vk::CullModeFlags cullMode, vk::FrontFace front, vk::PipelineLayout layout, vk::PipelineRenderingCreateInfo const &createInfo, vk::ArrayProxy< vk::DynamicState > const &dynamic)
Allocate a graphics pipeline.
vk::Device device() const
get the logical device
Definition application.hpp:84
vk::ImageView _createImageView(vk::Image img, vk::Format fmt, vk::ImageAspectFlags aspectFlags)
A helper function for creating a Vulkan image view object for an image.
int32_t _findMemory(uint32_t reqTypeBits, vk::MemoryPropertyFlags reqProps) const
A helper function to identify the index of a device memory type that has the required type and proper...
void submitCommands(vk::CommandBuffer cmdBuf)
end the commands and submit the buffer to the graphics queue.
Definition application.hpp:385
std::string _name
the application name
Definition application.hpp:424
friend class Texture2D
Definition application.hpp:32
void beginCommands(vk::CommandBuffer cmdBuf, bool oneTime=false)
begin recording commands in the given command buffer
Definition application.hpp:370
void _selectDevice(vk::PhysicalDeviceFeatures *reqFeatures=nullptr)
A helper function to select the GPU to use.
friend class Texture1D
Definition application.hpp:31
const vk::PhysicalDeviceFeatures * features() const
access function for the physical device features
Definition application.hpp:99
Queues< vk::Queue > _queues
the device queues that we are using
Definition application.hpp:435
vk::Pipeline createPipeline(cs237::Shaders *shaders, vk::PipelineVertexInputStateCreateInfo const &vertexInfo, vk::PrimitiveTopology prim, bool primRestart, vk::ArrayProxy< vk::Viewport > const &viewports, vk::ArrayProxy< vk::Rect2D > const &scissors, bool depthClamp, vk::PolygonMode polyMode, vk::CullModeFlags cullMode, vk::FrontFace front, vk::PipelineLayout layout, vk::PipelineRenderingCreateInfo const &createInfo, vk::PipelineColorBlendStateCreateInfo const &blending, vk::ArrayProxy< vk::DynamicState > const &dynamic)
Allocate a graphics pipeline with blending support.
virtual void run()=0
main function for running the application
bool debug() const
is the program in debug mode?
Definition application.hpp:53
vk::PipelineLayout createPipelineLayout(vk::ArrayProxy< vk::DescriptorSetLayout > descSets, vk::ArrayProxy< vk::PushConstantRange > pcrs)
Create a pipeline layout.
Definition application.hpp:175
vk::CommandPool _cmdPool
pool for allocating command buffers
Definition application.hpp:436
vk::Sampler createSampler(SamplerInfo const &info)
Create a texture sampler as specified.
vk::Image _createImage(uint32_t wid, uint32_t ht, vk::Format format, vk::ImageTiling tiling, vk::ImageUsageFlags usage, uint32_t mipLvls=1)
A helper function for creating a Vulkan image that can be used for textures or depth buffers.
Definition application.hpp:534
void _initCommandPool()
allocate the command pool for the application
vk::Image _createImage(uint32_t wid, uint32_t ht, vk::Format format, vk::ImageTiling tiling, vk::ImageUsageFlags usage, vk::ImageLayout layout, uint32_t mipLvls=1)
A helper function for creating a Vulkan image that can be used for textures or depth buffers.
static std::vector< vk::ExtensionProperties > supportedExtensions()
Get the list of supported Vulkan instance extensions.
Definition application.hpp:63
vk::Format _findBestFormat(std::vector< vk::Format > candidates, vk::ImageTiling tiling, vk::FormatFeatureFlags features)
A helper function to identify the best image format supported by the device from an ordered list of c...
bool _getQIndices(vk::PhysicalDevice dev)
A helper function to identify the queue-family indices for the physical device that we are using.
vk::DeviceMemory _allocImageMemory(vk::Image img, vk::MemoryPropertyFlags props)
A helper function for allocating and binding device memory for an image.
Definition shader.hpp:25
Definition aabb.hpp:24
Definition aabb.hpp:22
information about queue families
Definition application.hpp:406
T compute
the queue family that supports compute
Definition application.hpp:409
T present
the queue family that supports presentation
Definition application.hpp:408
T graphics
the queue family that supports graphics
Definition application.hpp:407
Definition application.hpp:110
vk::SamplerAddressMode addressModeU
Definition application.hpp:114
SamplerInfo(vk::Filter magF, vk::Filter minF, vk::SamplerMipmapMode mm, vk::SamplerAddressMode am1, vk::SamplerAddressMode am2, vk::BorderColor color)
sampler info for 2D texture
Definition application.hpp:138
vk::SamplerMipmapMode mipmapMode
Definition application.hpp:113
vk::BorderColor borderColor
Definition application.hpp:117
vk::SamplerAddressMode addressModeW
Definition application.hpp:116
SamplerInfo(vk::Filter magF, vk::Filter minF, vk::SamplerMipmapMode mm, vk::SamplerAddressMode am, vk::BorderColor color)
sampler info for 1D texture
Definition application.hpp:129
vk::Filter magFilter
Definition application.hpp:111
SamplerInfo()
Definition application.hpp:119
vk::SamplerAddressMode addressModeV
Definition application.hpp:115
vk::Filter minFilter
Definition application.hpp:112
Definition application.hpp:418
std::vector< vk::SurfaceFormatKHR > formats
Definition application.hpp:420
vk::SurfaceCapabilitiesKHR capabilities
Definition application.hpp:419
std::vector< vk::PresentModeKHR > presentModes
Definition application.hpp:421