]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/pixelformat_backend.cpp
Minor tweaks to MemoryAllocator
[libs/gl.git] / source / backends / vulkan / pixelformat_backend.cpp
1 #include "pixelformat.h"
2 #include "pixelformat_backend.h"
3 #include "vulkan.h"
4
5 using namespace std;
6
7 namespace {
8
9 static unsigned swizzle_orders[] =
10 {
11         VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
12         VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_ONE,
13         VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
14         VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_A,
15         VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_ONE
16 };
17
18 }
19
20 namespace Msp {
21 namespace GL {
22
23 ComponentSwizzle get_required_swizzle(PixelComponents comp)
24 {
25         switch(comp)
26         {
27         case RGB: return RGBA_TO_RGB;
28         case LUMINANCE: return R_TO_LUMINANCE;
29         case LUMINANCE_ALPHA: return RG_TO_LUMINANCE_ALPHA;
30         default: return NO_SWIZZLE;
31         }
32 }
33
34 void require_pixelformat(PixelFormat)
35 { }
36
37 unsigned get_vulkan_pixelformat(PixelFormat pf)
38 {
39         switch(pf)
40         {
41         case R8: return VK_FORMAT_R8_UNORM;
42         case R16F: return VK_FORMAT_R16_SFLOAT;
43         case R32F: return VK_FORMAT_R32_SFLOAT;
44         case RG8: return VK_FORMAT_R8G8_UNORM;
45         case RG16F: return VK_FORMAT_R16G16_SFLOAT;
46         case RG32F: return VK_FORMAT_R32G32_SFLOAT;
47         case RGB8: return VK_FORMAT_R8G8B8_UNORM;
48         case RGB16F: return VK_FORMAT_R16G16B16_SFLOAT;
49         case RGB32F: return VK_FORMAT_R32G32B32_SFLOAT;
50         case RGBA8: return VK_FORMAT_R8G8B8A8_UNORM;
51         case RGBA16F: return VK_FORMAT_R16G16B16A16_SFLOAT;
52         case RGBA32F: return VK_FORMAT_R32G32B32A32_SFLOAT;
53         case SRGB8: return VK_FORMAT_R8G8B8_SRGB;
54         case SRGB8_ALPHA8: return VK_FORMAT_R8G8B8A8_SRGB;
55         case BGR8: return VK_FORMAT_B8G8R8_UNORM;
56         case BGRA8: return VK_FORMAT_B8G8R8A8_UNORM;
57         case SBGR8: return VK_FORMAT_B8G8R8_SRGB;
58         case SBGR8_ALPHA8: return VK_FORMAT_B8G8R8A8_SRGB;
59         case DEPTH_COMPONENT16: return VK_FORMAT_D16_UNORM;
60         case DEPTH_COMPONENT24: return VK_FORMAT_X8_D24_UNORM_PACK32;
61         case DEPTH_COMPONENT32F: return VK_FORMAT_D32_SFLOAT;
62         case STENCIL_INDEX8: return VK_FORMAT_S8_UINT;
63         default: throw invalid_argument("get_vulkan_pixelformat");
64         }
65 }
66
67 unsigned get_vulkan_aspect(PixelComponents comp)
68 {
69         switch(comp)
70         {
71         case DEPTH_COMPONENT: return VK_IMAGE_ASPECT_DEPTH_BIT;
72         case STENCIL_INDEX: return VK_IMAGE_ASPECT_STENCIL_BIT;
73         default: return VK_IMAGE_ASPECT_COLOR_BIT;
74         }
75 }
76
77 unsigned get_vulkan_attachment_layout(PixelComponents comp)
78 {
79         switch(comp)
80         {
81         case DEPTH_COMPONENT:
82         case STENCIL_INDEX: return VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
83         default: return VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
84         }
85 }
86
87 PixelFormat pixelformat_from_vulkan(unsigned vkf)
88 {
89         switch(vkf)
90         {
91         case VK_FORMAT_R8_UNORM: return R8;
92         case VK_FORMAT_R16_SFLOAT: return R16F;
93         case VK_FORMAT_R32_SFLOAT: return R32F;
94         case VK_FORMAT_R8G8_UNORM: return RG8;
95         case VK_FORMAT_R16G16_SFLOAT: return RG16F;
96         case VK_FORMAT_R32G32_SFLOAT: return RG32F;
97         case VK_FORMAT_R8G8B8_UNORM: return RGB8;
98         case VK_FORMAT_R16G16B16_SFLOAT: return RGB16F;
99         case VK_FORMAT_R32G32B32_SFLOAT: return RGB32F;
100         case VK_FORMAT_R8G8B8A8_UNORM: return RGBA8;
101         case VK_FORMAT_R16G16B16A16_SFLOAT: return RGBA16F;
102         case VK_FORMAT_R32G32B32A32_SFLOAT: return RGBA32F;
103         case VK_FORMAT_R8G8B8_SRGB: return SRGB8;
104         case VK_FORMAT_R8G8B8A8_SRGB: return SRGB8_ALPHA8;
105         case VK_FORMAT_B8G8R8_UNORM: return BGR8;
106         case VK_FORMAT_B8G8R8A8_UNORM: return BGRA8;
107         case VK_FORMAT_B8G8R8_SRGB: return SBGR8;
108         case VK_FORMAT_B8G8R8A8_SRGB: return SBGR8_ALPHA8;
109         case VK_FORMAT_D16_UNORM: return DEPTH_COMPONENT16;
110         case VK_FORMAT_X8_D24_UNORM_PACK32: return DEPTH_COMPONENT24;
111         case VK_FORMAT_D32_SFLOAT: return DEPTH_COMPONENT32F;
112         case VK_FORMAT_S8_UINT: return STENCIL_INDEX8;
113         default: throw invalid_argument("pixelformat_from_vulkan");
114         }
115 }
116
117 const unsigned *get_vulkan_swizzle(ComponentSwizzle swiz)
118 {
119         return swizzle_orders+4*swiz;
120 }
121
122 } // namespace GL
123 } // namespace Msp