1 /* Copyright (c) 2017-2018 Hans-Kristian Arntzen
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #pragma once
24 
25 #include "vulkan.hpp"
26 #include "texture_format.hpp"
27 
28 namespace Vulkan
29 {
format_is_srgb(VkFormat format)30 static inline bool format_is_srgb(VkFormat format)
31 {
32 	switch (format)
33 	{
34 	case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
35 	case VK_FORMAT_R8G8B8A8_SRGB:
36 	case VK_FORMAT_B8G8R8A8_SRGB:
37 	case VK_FORMAT_R8_SRGB:
38 	case VK_FORMAT_R8G8_SRGB:
39 	case VK_FORMAT_R8G8B8_SRGB:
40 	case VK_FORMAT_B8G8R8_SRGB:
41 		return true;
42 
43 	default:
44 		return false;
45 	}
46 }
47 
format_has_depth_aspect(VkFormat format)48 static inline bool format_has_depth_aspect(VkFormat format)
49 {
50 	switch (format)
51 	{
52 	case VK_FORMAT_D16_UNORM:
53 	case VK_FORMAT_D16_UNORM_S8_UINT:
54 	case VK_FORMAT_D24_UNORM_S8_UINT:
55 	case VK_FORMAT_D32_SFLOAT:
56 	case VK_FORMAT_X8_D24_UNORM_PACK32:
57 	case VK_FORMAT_D32_SFLOAT_S8_UINT:
58 		return true;
59 
60 	default:
61 		return false;
62 	}
63 }
64 
format_has_stencil_aspect(VkFormat format)65 static inline bool format_has_stencil_aspect(VkFormat format)
66 {
67 	switch (format)
68 	{
69 	case VK_FORMAT_D16_UNORM_S8_UINT:
70 	case VK_FORMAT_D24_UNORM_S8_UINT:
71 	case VK_FORMAT_D32_SFLOAT_S8_UINT:
72 	case VK_FORMAT_S8_UINT:
73 		return true;
74 
75 	default:
76 		return false;
77 	}
78 }
79 
format_has_depth_or_stencil_aspect(VkFormat format)80 static inline bool format_has_depth_or_stencil_aspect(VkFormat format)
81 {
82 	return format_has_depth_aspect(format) || format_has_stencil_aspect(format);
83 }
84 
format_to_aspect_mask(VkFormat format)85 static inline VkImageAspectFlags format_to_aspect_mask(VkFormat format)
86 {
87 	switch (format)
88 	{
89 	case VK_FORMAT_UNDEFINED:
90 		return 0;
91 
92 	case VK_FORMAT_S8_UINT:
93 		return VK_IMAGE_ASPECT_STENCIL_BIT;
94 
95 	case VK_FORMAT_D16_UNORM_S8_UINT:
96 	case VK_FORMAT_D24_UNORM_S8_UINT:
97 	case VK_FORMAT_D32_SFLOAT_S8_UINT:
98 		return VK_IMAGE_ASPECT_STENCIL_BIT | VK_IMAGE_ASPECT_DEPTH_BIT;
99 
100 	case VK_FORMAT_D16_UNORM:
101 	case VK_FORMAT_D32_SFLOAT:
102 	case VK_FORMAT_X8_D24_UNORM_PACK32:
103 		return VK_IMAGE_ASPECT_DEPTH_BIT;
104 
105 	default:
106 		return VK_IMAGE_ASPECT_COLOR_BIT;
107 	}
108 }
109 
format_align_dim(VkFormat format,uint32_t & width,uint32_t & height)110 static inline void format_align_dim(VkFormat format, uint32_t &width, uint32_t &height)
111 {
112 	uint32_t align_width, align_height;
113 	TextureFormatLayout::format_block_dim(format, align_width, align_height);
114 	width = ((width + align_width - 1) / align_width) * align_width;
115 	height = ((height + align_height - 1) / align_height) * align_height;
116 }
117 
format_num_blocks(VkFormat format,uint32_t & width,uint32_t & height)118 static inline void format_num_blocks(VkFormat format, uint32_t &width, uint32_t &height)
119 {
120 	uint32_t align_width, align_height;
121 	TextureFormatLayout::format_block_dim(format, align_width, align_height);
122 	width = (width + align_width - 1) / align_width;
123 	height = (height + align_height - 1) / align_height;
124 }
125 
format_get_layer_size(VkFormat format,unsigned width,unsigned height,unsigned depth)126 static inline VkDeviceSize format_get_layer_size(VkFormat format, unsigned width, unsigned height, unsigned depth)
127 {
128 	uint32_t blocks_x = width;
129 	uint32_t blocks_y = height;
130 	format_num_blocks(format, blocks_x, blocks_y);
131 	format_align_dim(format, width, height);
132 
133 	VkDeviceSize size = TextureFormatLayout::format_block_size(format) * depth * blocks_x * blocks_y;
134 	return size;
135 }
136 }
137