13#ifndef _CS237_WINDOW_HPP_
14#define _CS237_WINDOW_HPP_
17#error "cs237/window.hpp should not be included directly"
24#ifdef CS237_MAX_FRAMES_IN_FLIGHT
25# define MAX_FRAMES CS237_MAX_FRAMES_IN_FLIGHT
107 glfwHideWindow (this->
_win);
114 glfwShowWindow (this->
_win);
137 return glfwWindowShouldClose (this->
_win);
143 virtual void key (
int key,
int scancode,
int action,
int mods);
147 virtual void scroll (
double xoffset,
double yoffset);
198 std::optional<DepthStencilBuffer>
dsBuf;
206 int size ()
const {
return this->images.size(); }
215 return this->dsBuf.has_value() && this->dsBuf->depth;
221 return this->dsBuf.has_value() && this->dsBuf->stencil;
228 if (this->dsBuf.has_value()) {
232 return std::optional<vk::ImageView>();
279 auto sts = this->win->
device().waitForFences(this->inFlight, VK_TRUE, UINT64_MAX);
280 if (sts != vk::Result::eSuccess) {
281 ERROR(
"Synchronization error");
288 this->win->
device().resetFences(this->inFlight);
298 vk::PresentInfoKHR presentInfo(
360 if (this->_curFrameIdx == 0) {
363 return this->_frames[this->_curFrameIdx - 1];
370 return this->_frames[(this->_curFrameIdx + 1) %
kMaxFrames];
387 std::vector<vk::AttachmentDescription> &descs,
388 std::vector<vk::AttachmentReference> &refs);
417 float(this->_swap.
extent.width),
418 float(this->_swap.extent.width),
419 -
float(this->_swap.extent.height),
425 float(this->_swap.
extent.width),
426 float(this->_swap.extent.height),
460 0, this->_swap.
extent.height,
461 this->_swap.extent.width, -(int32_t)this->_swap.extent.height);
465 this->_swap.
extent.width, this->_swap.extent.height);
480 int32_t x, int32_t y,
481 int32_t wid, int32_t ht);
499 vk::Image img, vk::Format fmt, vk::ImageAspectFlags aspectFlags)
512 vk::ImageLayout oldLayout,
513 vk::ImageLayout newLayout)
the base class for applications
Definition application.hpp:25
vk::Device _device
the logical device that we are using to render
Definition application.hpp:443
Queues< uint32_t > _qIdxs
the queue family indices
Definition application.hpp:444
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::ImageView _createImageView(vk::Image img, vk::Format fmt, vk::ImageAspectFlags aspectFlags)
A helper function for creating a Vulkan image view object for an image.
Queues< vk::Queue > _queues
the device queues that we are using
Definition application.hpp:445
vk::DeviceMemory _allocImageMemory(vk::Image img, vk::MemoryPropertyFlags props)
A helper function for allocating and binding device memory for an image.
abstract base class for simple GLFW windows used to view buffers, etc.
Definition window.hpp:69
SwapChain _swap
buffer-swapping information
Definition window.hpp:320
virtual void cursorPos(double xpos, double ypos)
void enableKeyEvent(bool enable)
void _transitionImageLayout(vk::Image img, vk::Format fmt, vk::ImageLayout oldLayout, vk::ImageLayout newLayout)
A helper function for changing the layout of an image.
Definition window.hpp:509
vk::Queue presentationQ() const
the presentation queue
Definition window.hpp:90
void enableScrollEvent(bool enable)
virtual FrameData * _allocFrameData(Window *w)
virtual function for allocating a FrameData object. Subclasses of the Window class can define a subcl...
vk::Device device() const
return the logical device for this window
Definition window.hpp:84
FrameData * _currentFrame()
get a pointer to the current per-frame rendering state
Definition window.hpp:355
FrameData * _frames[kMaxFrames]
the per-frame rendering state
Definition window.hpp:321
bool _keyEnabled
true when the Key callback is enabled
Definition window.hpp:313
uint32_t _curFrameIdx
index into _frames array for current frame data
Definition window.hpp:322
vk::Rect2D _getScissorsRect()
get the scissors rectangle for this window
Definition window.hpp:434
virtual void key(int key, int scancode, int action, int mods)
virtual void mouseButton(int button, int action, int mods)
vk::SurfaceKHR _surf
the Vulkan surface to render to
Definition window.hpp:319
void _setViewportCmd(vk::CommandBuffer cmdBuf, bool oglView=false)
add a command to set the viewport and scissor to the whole window
Definition window.hpp:451
bool _isVis
true when the window is visible
Definition window.hpp:312
vk::DeviceMemory _allocImageMemory(vk::Image img, vk::MemoryPropertyFlags props)
A helper function for allocating and binding device memory for an image.
Definition window.hpp:487
void show()
Show the window (a no-op if it is already visible)
Definition window.hpp:112
vk::Queue graphicsQ() const
the graphics queue
Definition window.hpp:87
void enableCursorEnterEvent(bool enable)
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.
Definition window.hpp:498
void _recreateSwapChain()
Recreate the swap chain for this window; this redefines the _swap instance variable and is used when ...
Window(Application *app, CreateWindowInfo const &info)
the Window base-class constructor
FrameData * _nextFrame()
get a pointer to the next per-frame rendering state
Definition window.hpp:368
virtual void _init()
subclasses can override this method to handle any additional initialization that needs to be run afte...
void enableCursorPosEvent(bool enable)
uint32_t _computeQIdx() const
the compute queue index
Definition window.hpp:403
int _ht
window dimensions
Definition window.hpp:311
void _advanceFrame()
advance the current frame
Definition window.hpp:374
bool _scrollEnabled
true when the Scroll callback is enabled
Definition window.hpp:317
bool _cursorEnterEnabled
true when the CursorEnter callback is enabled
Definition window.hpp:315
void setCursorMode(int mode)
bool windowShouldClose()
get the value of the "close" flag for the window
Definition window.hpp:135
virtual void cursorEnter(bool entered)
GLFWwindow * _win
the underlying window
Definition window.hpp:310
SwapChainDetails _getSwapChainDetails()
Get the swap-chain details for a physical device.
bool _mouseButtonEnabled
true when the MouseButton callback is enabled
Definition window.hpp:316
int width() const
the width of the window
Definition window.hpp:521
virtual ~Window()
destructor: it destroys the underlying GLFW window
Application * _app
the owning application
Definition window.hpp:309
bool _cursorPosEnabled
true when the CursorPos callback is enabled
Definition window.hpp:314
vk::Viewport _getViewport(bool oglView=false)
get the natural viewport for the window
Definition window.hpp:410
uint32_t _presentationQIdx() const
the presentation queue
Definition window.hpp:398
Application * app()
return the application pointer
Definition window.hpp:81
void _createSwapChain(bool depth, bool stencil)
Create the swap chain for this window; this initializes the _swap instance variable.
virtual void iconify(bool iconified)
method invoked on Iconify events.
vk::Queue computeQ() const
the compute queue
Definition window.hpp:93
void enableMouseButtonEvent(bool enable)
uint32_t _graphicsQIdx() const
the graphics queue-family index
Definition window.hpp:393
FrameData * _prevFrame()
get a pointer to the previous per-frame rendering state
Definition window.hpp:358
void refresh()
Definition window.hpp:97
virtual void scroll(double xoffset, double yoffset)
void _initAttachments(std::vector< vk::AttachmentDescription > &descs, std::vector< vk::AttachmentReference > &refs)
initialize the attachment descriptors and references for the color and optional depth/stencil-buffer
vk::Result _acquireNextImage()
acquire the next image from the swap chain. This method has the side effect of setting the index of t...
void hide()
Hide the window.
Definition window.hpp:105
virtual void reshape(int wid, int ht)
int height() const
the height of the window
Definition window.hpp:524
void _setViewportCmd(vk::CommandBuffer cmdBuf, int32_t x, int32_t y, int32_t wid, int32_t ht)
add a viewport command to the command buffer; this also sets the scissor rectangle.
int _wid
Definition window.hpp:311
#define ERROR(msg)
Definition cs237.hpp:60
constexpr int kMaxFrames
Definition window.hpp:33
T compute
the queue family that supports compute
Definition application.hpp:419
T present
the queue family that supports presentation
Definition application.hpp:418
T graphics
the queue family that supports graphics
Definition application.hpp:417
structure containing parameters for creating windows
Definition window.hpp:37
std::string title
window title
Definition window.hpp:40
int ht
the window height
Definition window.hpp:39
bool depth
do we need depth-buffer support?
Definition window.hpp:42
bool resizable
should the window support resizing
Definition window.hpp:41
bool needsDepthBuf() const
do we need a depth/stencil buffer for the window?
Definition window.hpp:64
CreateWindowInfo(int w, int h)
Definition window.hpp:59
CreateWindowInfo(int w, int h, std::string const &t, bool r, bool d, bool s)
Definition window.hpp:52
int wid
the window width
Definition window.hpp:38
bool stencil
do we need stencil-buffer support?
Definition window.hpp:43
information about the optional depth/stencil buffers for the window
Definition window.hpp:178
vk::ImageView view
image view for depth/image-buffer
Definition window.hpp:184
vk::Image image
depth/image-buffer image
Definition window.hpp:182
bool stencil
true if stencil-buffer is supported
Definition window.hpp:180
vk::Format format
the depth/image-buffer format
Definition window.hpp:181
vk::DeviceMemory imageMem
device memory for depth/image-buffer
Definition window.hpp:183
bool depth
true if depth-buffer is supported
Definition window.hpp:179
A container for the per-frame rendering state.
Definition window.hpp:246
vk::Semaphore imageAvail
Definition window.hpp:250
uint32_t index
Definition window.hpp:256
FrameData(FrameData &)=delete
void waitForFence()
wait for this frame's ‘inFlight’ fence
Definition window.hpp:277
vk::Semaphore finished
Definition window.hpp:252
vk::Fence inFlight
Definition window.hpp:254
Window * win
the owning window
Definition window.hpp:247
vk::CommandBuffer cmdBuf
Definition window.hpp:248
vk::Result present()
present this frame
Definition window.hpp:296
void submitDrawingCommands()
submit drawing commands for this frame using the main command buffer
FrameData(FrameData const &)=delete
FrameData(FrameData &&)=delete
void resetFence()
reset this frame's inFlight fence
Definition window.hpp:286
virtual ~FrameData()
Destructor.
information about swap-chain support
Definition window.hpp:162
vk::SurfaceCapabilitiesKHR capabilities
Definition window.hpp:163
vk::PresentModeKHR choosePresentMode()
choose a presentation mode from the available modes; we prefer "mailbox" (aka triple buffering)
std::vector< vk::SurfaceFormatKHR > formats
Definition window.hpp:164
std::vector< vk::PresentModeKHR > presentModes
Definition window.hpp:165
vk::Extent2D chooseExtent(GLFWwindow *win)
get the extent of the window subject to the limits of the Vulkan device
vk::SurfaceFormatKHR chooseSurfaceFormat()
choose a surface format from the available formats
the collected information about the swap-chain for a window
Definition window.hpp:188
std::vector< vk::ImageView > views
image views for the swap buffers
Definition window.hpp:197
std::vector< vk::Image > images
images for the swap buffers
Definition window.hpp:196
std::optional< vk::ImageView > depthImageView()
get the image view for the depth buffer
Definition window.hpp:226
std::vector< vk::Framebuffer > fBufs
frame buffers
Definition window.hpp:199
int size() const
return the number of buffers in the swap chain
Definition window.hpp:206
bool hasStencilBuffer() const
does the swap-chain support a stencil buffer?
Definition window.hpp:219
void initFramebuffers(vk::RenderPass renderPass)
allocate the frame buffers for the swap chain
std::optional< DepthStencilBuffer > dsBuf
optional depth/stencil-buffer
Definition window.hpp:198
SwapChain(vk::Device dev)
Definition window.hpp:201
bool hasDepthBuffer() const
does the swap-chain support a depth buffer?
Definition window.hpp:213
vk::SwapchainKHR chain
the swap chain object
Definition window.hpp:190
void cleanup()
destroy the Vulkan state for the swap chain
vk::Extent2D extent
size of swap buffer images
Definition window.hpp:192
vk::Format imageFormat
pixel format of image buffers
Definition window.hpp:191
int numAttachments
the number of framebuffer attachments
Definition window.hpp:193
vk::Device device
the owning logical device
Definition window.hpp:189
#define MAX_FRAMES
Definition window.hpp:27