Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan Tests |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2014 LunarG, Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included |
| 14 | * in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: |
| 25 | * Courtney Goeltzenleuchter <courtney@lunarg.com> |
| 26 | */ |
| 27 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 28 | #include "vkrenderframework.h" |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 29 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 30 | XglRenderFramework::XglRenderFramework() : |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 31 | m_cmdBuffer( VK_NULL_HANDLE ), |
| 32 | m_stateRaster( VK_NULL_HANDLE ), |
| 33 | m_colorBlend( VK_NULL_HANDLE ), |
| 34 | m_stateViewport( VK_NULL_HANDLE ), |
| 35 | m_stateDepthStencil( VK_NULL_HANDLE ), |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 36 | m_width( 256.0 ), // default window width |
Courtney Goeltzenleuchter | 679bbfa | 2015-03-05 17:26:38 -0700 | [diff] [blame] | 37 | m_height( 256.0 ), // default window height |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 38 | m_render_target_fmt( VK_FMT_R8G8B8A8_UNORM ), |
| 39 | m_depth_stencil_fmt( VK_FMT_UNDEFINED ), |
Courtney Goeltzenleuchter | 679bbfa | 2015-03-05 17:26:38 -0700 | [diff] [blame] | 40 | m_depth_clear_color( 1.0 ), |
| 41 | m_stencil_clear_color( 0 ) |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 42 | { |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 43 | |
Courtney Goeltzenleuchter | 679bbfa | 2015-03-05 17:26:38 -0700 | [diff] [blame] | 44 | // clear the back buffer to dark grey |
| 45 | m_clear_color.color.rawColor[0] = 64; |
| 46 | m_clear_color.color.rawColor[1] = 64; |
| 47 | m_clear_color.color.rawColor[2] = 64; |
| 48 | m_clear_color.color.rawColor[3] = 0; |
| 49 | m_clear_color.useRawValue = true; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | XglRenderFramework::~XglRenderFramework() |
| 53 | { |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 54 | |
| 55 | } |
| 56 | |
| 57 | void XglRenderFramework::InitFramework() |
| 58 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 59 | VK_RESULT err; |
| 60 | VK_INSTANCE_CREATE_INFO instInfo = {}; |
| 61 | instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; |
Jon Ashburn | b317fad | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 62 | instInfo.pNext = NULL; |
| 63 | instInfo.pAppInfo = &app_info; |
| 64 | instInfo.pAllocCb = NULL; |
| 65 | instInfo.extensionCount = 0; |
| 66 | instInfo.ppEnabledExtensionNames = NULL; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 67 | err = vkCreateInstance(&instInfo, &this->inst); |
| 68 | ASSERT_VK_SUCCESS(err); |
| 69 | err = vkEnumerateGpus(inst, VK_MAX_PHYSICAL_GPUS, &this->gpu_count, |
Jon Ashburn | 1e46489 | 2015-01-29 15:48:00 -0700 | [diff] [blame] | 70 | objs); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 71 | ASSERT_VK_SUCCESS(err); |
Jon Ashburn | bf843b2 | 2014-11-26 11:06:49 -0700 | [diff] [blame] | 72 | ASSERT_GE(this->gpu_count, 1) << "No GPU available"; |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 73 | |
| 74 | m_device = new XglDevice(0, objs[0]); |
| 75 | m_device->get_device_queue(); |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 76 | |
| 77 | m_depthStencil = new XglDepthStencilObj(); |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void XglRenderFramework::ShutdownFramework() |
| 81 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 82 | if (m_colorBlend) vkDestroyObject(m_colorBlend); |
| 83 | if (m_stateDepthStencil) vkDestroyObject(m_stateDepthStencil); |
| 84 | if (m_stateRaster) vkDestroyObject(m_stateRaster); |
| 85 | if (m_cmdBuffer) vkDestroyObject(m_cmdBuffer); |
| 86 | if (m_framebuffer) vkDestroyObject(m_framebuffer); |
| 87 | if (m_renderPass) vkDestroyObject(m_renderPass); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 88 | |
| 89 | if (m_stateViewport) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 90 | vkDestroyObject(m_stateViewport); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 91 | } |
Tobin Ehlis | 976fc16 | 2015-03-26 08:23:25 -0600 | [diff] [blame] | 92 | while (!m_renderTargets.empty()) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 93 | vkDestroyObject(m_renderTargets.back()->targetView()); |
| 94 | vkBindObjectMemory(m_renderTargets.back()->image(), 0, VK_NULL_HANDLE, 0); |
| 95 | vkDestroyObject(m_renderTargets.back()->image()); |
| 96 | vkFreeMemory(m_renderTargets.back()->memory()); |
Tobin Ehlis | 976fc16 | 2015-03-26 08:23:25 -0600 | [diff] [blame] | 97 | m_renderTargets.pop_back(); |
| 98 | } |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 99 | |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 100 | delete m_depthStencil; |
| 101 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 102 | // reset the driver |
Chia-I Wu | b76e0fa | 2014-12-28 14:27:28 +0800 | [diff] [blame] | 103 | delete m_device; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 104 | vkDestroyInstance(this->inst); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 105 | } |
| 106 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 107 | void XglRenderFramework::InitState() |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 108 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 109 | VK_RESULT err; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 110 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 111 | m_render_target_fmt = VK_FMT_B8G8R8A8_UNORM; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 112 | |
| 113 | // create a raster state (solid, back-face culling) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 114 | VK_DYNAMIC_RS_STATE_CREATE_INFO raster = {}; |
| 115 | raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 116 | raster.pointSize = 1.0; |
| 117 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 118 | err = vkCreateDynamicRasterState( device(), &raster, &m_stateRaster ); |
| 119 | ASSERT_VK_SUCCESS(err); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 120 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 121 | VK_DYNAMIC_CB_STATE_CREATE_INFO blend = {}; |
| 122 | blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO; |
| 123 | err = vkCreateDynamicColorBlendState(device(), &blend, &m_colorBlend); |
| 124 | ASSERT_VK_SUCCESS( err ); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 125 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 126 | VK_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {}; |
| 127 | depthStencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 128 | depthStencil.minDepth = 0.f; |
| 129 | depthStencil.maxDepth = 1.f; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 130 | depthStencil.stencilFrontRef = 0; |
| 131 | depthStencil.stencilBackRef = 0; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 132 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 133 | err = vkCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil ); |
| 134 | ASSERT_VK_SUCCESS( err ); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 135 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 136 | VK_CMD_BUFFER_CREATE_INFO cmdInfo = {}; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 137 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 138 | cmdInfo.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO; |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 139 | cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_; |
| 140 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 141 | err = vkCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer); |
| 142 | ASSERT_VK_SUCCESS(err) << "vkCreateCommandBuffer failed"; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 143 | } |
| 144 | |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 145 | void XglRenderFramework::InitViewport(float width, float height) |
| 146 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 147 | VK_RESULT err; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 148 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 149 | VK_VIEWPORT viewport; |
| 150 | VK_RECT scissor; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 151 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 152 | VK_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {}; |
| 153 | viewportCreate.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | bbe3cc5 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 154 | viewportCreate.viewportAndScissorCount = 1; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 155 | viewport.originX = 0; |
| 156 | viewport.originY = 0; |
| 157 | viewport.width = 1.f * width; |
| 158 | viewport.height = 1.f * height; |
| 159 | viewport.minDepth = 0.f; |
| 160 | viewport.maxDepth = 1.f; |
Courtney Goeltzenleuchter | bbe3cc5 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 161 | scissor.extent.width = width; |
| 162 | scissor.extent.height = height; |
| 163 | scissor.offset.x = 0; |
| 164 | scissor.offset.y = 0; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 165 | viewportCreate.pViewports = &viewport; |
Courtney Goeltzenleuchter | bbe3cc5 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 166 | viewportCreate.pScissors = &scissor; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 167 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 168 | err = vkCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport ); |
| 169 | ASSERT_VK_SUCCESS( err ); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 170 | m_width = width; |
| 171 | m_height = height; |
| 172 | } |
| 173 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 174 | void XglRenderFramework::InitViewport() |
| 175 | { |
| 176 | InitViewport(m_width, m_height); |
| 177 | } |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 178 | void XglRenderFramework::InitRenderTarget() |
| 179 | { |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 180 | InitRenderTarget(1); |
| 181 | } |
| 182 | |
| 183 | void XglRenderFramework::InitRenderTarget(uint32_t targets) |
| 184 | { |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 185 | InitRenderTarget(targets, NULL); |
| 186 | } |
| 187 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 188 | void XglRenderFramework::InitRenderTarget(VK_DEPTH_STENCIL_BIND_INFO *dsBinding) |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 189 | { |
| 190 | InitRenderTarget(1, dsBinding); |
| 191 | } |
| 192 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 193 | void XglRenderFramework::InitRenderTarget(uint32_t targets, VK_DEPTH_STENCIL_BIND_INFO *dsBinding) |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 194 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 195 | std::vector<VK_ATTACHMENT_LOAD_OP> load_ops; |
| 196 | std::vector<VK_ATTACHMENT_STORE_OP> store_ops; |
| 197 | std::vector<VK_CLEAR_COLOR> clear_colors; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 198 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 199 | uint32_t i; |
Chia-I Wu | ecebf75 | 2014-12-05 10:45:15 +0800 | [diff] [blame] | 200 | |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 201 | for (i = 0; i < targets; i++) { |
Chia-I Wu | f50ee21 | 2014-12-29 14:31:52 +0800 | [diff] [blame] | 202 | XglImage *img = new XglImage(m_device); |
| 203 | img->init(m_width, m_height, m_render_target_fmt, |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 204 | VK_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | |
| 205 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 206 | m_colorBindings[i].view = img->targetView(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 207 | m_colorBindings[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 208 | m_renderTargets.push_back(img); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 209 | load_ops.push_back(VK_ATTACHMENT_LOAD_OP_LOAD); |
| 210 | store_ops.push_back(VK_ATTACHMENT_STORE_OP_STORE); |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 211 | clear_colors.push_back(m_clear_color); |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 212 | // m_mem_ref_mgr.AddMemoryRefs(*img); |
Chia-I Wu | ecebf75 | 2014-12-05 10:45:15 +0800 | [diff] [blame] | 213 | } |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 214 | // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 215 | VK_FRAMEBUFFER_CREATE_INFO fb_info = {}; |
| 216 | fb_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
Tony Barbour | bdf0a31 | 2015-04-01 17:10:07 -0600 | [diff] [blame] | 217 | fb_info.pNext = NULL; |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 218 | fb_info.colorAttachmentCount = m_renderTargets.size(); |
Tony Barbour | bdf0a31 | 2015-04-01 17:10:07 -0600 | [diff] [blame] | 219 | fb_info.pColorAttachments = m_colorBindings; |
| 220 | fb_info.pDepthStencilAttachment = dsBinding; |
| 221 | fb_info.sampleCount = 1; |
| 222 | fb_info.width = (uint32_t)m_width; |
| 223 | fb_info.height = (uint32_t)m_height; |
| 224 | fb_info.layers = 1; |
| 225 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 226 | vkCreateFramebuffer(device(), &fb_info, &m_framebuffer); |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 227 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 228 | VK_RENDER_PASS_CREATE_INFO rp_info = {}; |
| 229 | rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 230 | rp_info.renderArea.extent.width = m_width; |
| 231 | rp_info.renderArea.extent.height = m_height; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 232 | |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 233 | rp_info.colorAttachmentCount = m_renderTargets.size(); |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 234 | rp_info.pColorFormats = &m_render_target_fmt; |
| 235 | rp_info.pColorLayouts = &m_colorBindings[0].layout; |
| 236 | rp_info.pColorLoadOps = &load_ops[0]; |
| 237 | rp_info.pColorStoreOps = &store_ops[0]; |
| 238 | rp_info.pColorLoadClearValues = &clear_colors[0]; |
| 239 | rp_info.depthStencilFormat = m_depth_stencil_fmt; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 240 | if (dsBinding) { |
| 241 | rp_info.depthStencilLayout = dsBinding->layout; |
| 242 | } |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 243 | rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD; |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 244 | rp_info.depthLoadClearValue = m_depth_clear_color; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 245 | rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_STORE; |
| 246 | rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD; |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 247 | rp_info.stencilLoadClearValue = m_stencil_clear_color; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 248 | rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE; |
| 249 | vkCreateRenderPass(device(), &rp_info, &m_renderPass); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 250 | } |
| 251 | |
Mark Lobodzinski | c52b775 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 252 | |
| 253 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 254 | XglDevice::XglDevice(uint32_t id, VK_PHYSICAL_GPU obj) : |
| 255 | vk_testing::Device(obj), id(id) |
Chia-I Wu | fb1459b | 2014-12-29 15:23:20 +0800 | [diff] [blame] | 256 | { |
| 257 | init(); |
| 258 | |
| 259 | props = gpu().properties(); |
| 260 | queue_props = &gpu().queue_properties()[0]; |
| 261 | } |
| 262 | |
| 263 | void XglDevice::get_device_queue() |
| 264 | { |
| 265 | ASSERT_NE(true, graphics_queues().empty()); |
| 266 | m_queue = graphics_queues()[0]->obj(); |
| 267 | } |
| 268 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 269 | XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device) |
| 270 | { |
| 271 | m_device = device; |
| 272 | m_nextSlot = 0; |
| 273 | |
| 274 | } |
| 275 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 276 | XglDescriptorSetObj::~XglDescriptorSetObj() |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 277 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 278 | delete m_set; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 279 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 280 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 281 | int XglDescriptorSetObj::AppendDummy() |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 282 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 283 | /* request a descriptor but do not update it */ |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 284 | VK_DESCRIPTOR_TYPE_COUNT tc = {}; |
| 285 | tc.type = VK_DESCRIPTOR_TYPE_SHADER_STORAGE_BUFFER; |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 286 | tc.count = 1; |
| 287 | m_type_counts.push_back(tc); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 288 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 289 | return m_nextSlot++; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 290 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 291 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 292 | int XglDescriptorSetObj::AppendBuffer(VK_DESCRIPTOR_TYPE type, XglConstantBufferObj &constantBuffer) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 293 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 294 | VK_DESCRIPTOR_TYPE_COUNT tc = {}; |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 295 | tc.type = type; |
| 296 | tc.count = 1; |
| 297 | m_type_counts.push_back(tc); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 298 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 299 | m_updateBuffers.push_back(vk_testing::DescriptorSet::update(type, |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 300 | m_nextSlot, 0, 1, &constantBuffer.m_bufferViewInfo)); |
| 301 | |
| 302 | // Track mem references for this descriptor set object |
| 303 | mem_ref_mgr.AddMemoryRefs(constantBuffer); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 304 | |
| 305 | return m_nextSlot++; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 306 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 307 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 308 | int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 309 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 310 | VK_DESCRIPTOR_TYPE_COUNT tc = {}; |
| 311 | tc.type = VK_DESCRIPTOR_TYPE_SAMPLER_TEXTURE; |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 312 | tc.count = 1; |
| 313 | m_type_counts.push_back(tc); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 314 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 315 | VK_SAMPLER_IMAGE_VIEW_INFO tmp = {}; |
Courtney Goeltzenleuchter | f39b3ec | 2015-04-09 11:43:10 -0600 | [diff] [blame^] | 316 | tmp.sampler = sampler->obj(); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 317 | tmp.pImageView = &texture->m_textureViewInfo; |
| 318 | m_samplerTextureInfo.push_back(tmp); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 319 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 320 | m_updateSamplerTextures.push_back(vk_testing::DescriptorSet::update(m_nextSlot, 0, 1, |
| 321 | (const VK_SAMPLER_IMAGE_VIEW_INFO *) NULL)); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 322 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 323 | // Track mem references for the texture referenced here |
| 324 | mem_ref_mgr.AddMemoryRefs(*texture); |
| 325 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 326 | return m_nextSlot++; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 327 | } |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 328 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 329 | VK_DESCRIPTOR_SET_LAYOUT_CHAIN XglDescriptorSetObj::GetLayoutChain() const |
Tony Barbour | b5f4d08 | 2014-12-17 10:54:03 -0700 | [diff] [blame] | 330 | { |
Chia-I Wu | 41126e5 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 331 | return m_layout_chain.obj(); |
Tony Barbour | b5f4d08 | 2014-12-17 10:54:03 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 334 | VK_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle() const |
Tony Barbour | b5f4d08 | 2014-12-17 10:54:03 -0700 | [diff] [blame] | 335 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 336 | return m_set->obj(); |
Tony Barbour | b5f4d08 | 2014-12-17 10:54:03 -0700 | [diff] [blame] | 337 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 338 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 339 | void XglDescriptorSetObj::CreateVKDescriptorSet(XglCommandBufferObj *cmdBuffer) |
Tony Barbour | 824b771 | 2014-12-18 17:06:21 -0700 | [diff] [blame] | 340 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 341 | // create VK_DESCRIPTOR_POOL |
| 342 | VK_DESCRIPTOR_POOL_CREATE_INFO pool = {}; |
| 343 | pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; |
Chia-I Wu | 985ba16 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 344 | pool.count = m_type_counts.size(); |
| 345 | pool.pTypeCount = &m_type_counts[0]; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 346 | init(*m_device, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool); |
Tony Barbour | 824b771 | 2014-12-18 17:06:21 -0700 | [diff] [blame] | 347 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 348 | // create VK_DESCRIPTOR_SET_LAYOUT |
| 349 | vector<VK_DESCRIPTOR_SET_LAYOUT_BINDING> bindings; |
Chia-I Wu | b41393e | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 350 | bindings.resize(m_type_counts.size()); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 351 | for (int i = 0; i < m_type_counts.size(); i++) { |
Chia-I Wu | b41393e | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 352 | bindings[i].descriptorType = m_type_counts[i].type; |
| 353 | bindings[i].count = m_type_counts[i].count; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 354 | bindings[i].stageFlags = VK_SHADER_STAGE_FLAGS_ALL; |
Chia-I Wu | 0743e83 | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 355 | bindings[i].pImmutableSamplers = NULL; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 356 | } |
Chia-I Wu | ac0f1e7 | 2014-12-28 22:32:36 +0800 | [diff] [blame] | 357 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 358 | // create VK_DESCRIPTOR_SET_LAYOUT |
| 359 | VK_DESCRIPTOR_SET_LAYOUT_CREATE_INFO layout = {}; |
| 360 | layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
Chia-I Wu | b41393e | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 361 | layout.count = bindings.size(); |
| 362 | layout.pBinding = &bindings[0]; |
| 363 | |
Chia-I Wu | 41126e5 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 364 | m_layout.init(*m_device, layout); |
| 365 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 366 | vector<const vk_testing::DescriptorSetLayout *> layouts; |
Chia-I Wu | 41126e5 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 367 | layouts.push_back(&m_layout); |
| 368 | m_layout_chain.init(*m_device, layouts); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 369 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 370 | // create VK_DESCRIPTOR_SET |
| 371 | m_set = alloc_sets(VK_DESCRIPTOR_SET_USAGE_STATIC, m_layout); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 372 | |
Chia-I Wu | 41126e5 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 373 | // build the update array |
| 374 | vector<const void *> update_array; |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 375 | |
Chia-I Wu | 41126e5 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 376 | for (int i = 0; i < m_updateBuffers.size(); i++) { |
| 377 | update_array.push_back(&m_updateBuffers[i]); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 378 | } |
| 379 | for (int i = 0; i < m_updateSamplerTextures.size(); i++) { |
| 380 | m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i]; |
Chia-I Wu | 41126e5 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 381 | update_array.push_back(&m_updateSamplerTextures[i]); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 382 | } |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 383 | |
| 384 | // do the updates |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 385 | m_device->begin_descriptor_pool_update(VK_DESCRIPTOR_UPDATE_MODE_FASTEST); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 386 | clear_sets(*m_set); |
Chia-I Wu | 41126e5 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 387 | m_set->update(update_array); |
Chia-I Wu | 985ba16 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 388 | m_device->end_descriptor_pool_update(*cmdBuffer); |
Tony Barbour | 25ef8a6 | 2014-12-03 13:59:18 -0700 | [diff] [blame] | 389 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 390 | |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 391 | XglImage::XglImage(XglDevice *dev) |
| 392 | { |
| 393 | m_device = dev; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 394 | m_imageInfo.view = VK_NULL_HANDLE; |
| 395 | m_imageInfo.layout = VK_IMAGE_LAYOUT_GENERAL; |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 396 | } |
| 397 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 398 | void XglImage::ImageMemoryBarrier( |
| 399 | XglCommandBufferObj *cmd_buf, |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 400 | VK_IMAGE_ASPECT aspect, |
| 401 | VK_FLAGS output_mask /*= |
| 402 | VK_MEMORY_OUTPUT_CPU_WRITE_BIT | |
| 403 | VK_MEMORY_OUTPUT_SHADER_WRITE_BIT | |
| 404 | VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT | |
| 405 | VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 406 | VK_MEMORY_OUTPUT_COPY_BIT*/, |
| 407 | VK_FLAGS input_mask /*= |
| 408 | VK_MEMORY_INPUT_CPU_READ_BIT | |
| 409 | VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT | |
| 410 | VK_MEMORY_INPUT_INDEX_FETCH_BIT | |
| 411 | VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT | |
| 412 | VK_MEMORY_INPUT_UNIFORM_READ_BIT | |
| 413 | VK_MEMORY_INPUT_SHADER_READ_BIT | |
| 414 | VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT | |
| 415 | VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 416 | VK_MEMORY_INPUT_COPY_BIT*/, |
| 417 | VK_IMAGE_LAYOUT image_layout) |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 418 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 419 | const VK_IMAGE_SUBRESOURCE_RANGE subresourceRange = subresource_range(aspect, 0, 1, 0, 1); |
| 420 | VK_IMAGE_MEMORY_BARRIER barrier; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 421 | barrier = image_memory_barrier(output_mask, input_mask, layout(), image_layout, |
| 422 | subresourceRange); |
| 423 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 424 | VK_IMAGE_MEMORY_BARRIER *pmemory_barrier = &barrier; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 425 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 426 | VK_PIPE_EVENT pipe_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE }; |
| 427 | VK_PIPELINE_BARRIER pipeline_barrier = {}; |
| 428 | pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 429 | pipeline_barrier.pNext = NULL; |
| 430 | pipeline_barrier.eventCount = 1; |
| 431 | pipeline_barrier.pEvents = pipe_events; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 432 | pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 433 | pipeline_barrier.memBarrierCount = 1; |
| 434 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
| 435 | |
| 436 | // write barrier to the command buffer |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 437 | vkCmdPipelineBarrier(cmd_buf->obj(), &pipeline_barrier); |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void XglImage::SetLayout(XglCommandBufferObj *cmd_buf, |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 441 | VK_IMAGE_ASPECT aspect, |
| 442 | VK_IMAGE_LAYOUT image_layout) |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 443 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 444 | VK_FLAGS output_mask, input_mask; |
| 445 | const VK_FLAGS all_cache_outputs = |
| 446 | VK_MEMORY_OUTPUT_CPU_WRITE_BIT | |
| 447 | VK_MEMORY_OUTPUT_SHADER_WRITE_BIT | |
| 448 | VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT | |
| 449 | VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 450 | VK_MEMORY_OUTPUT_COPY_BIT; |
| 451 | const VK_FLAGS all_cache_inputs = |
| 452 | VK_MEMORY_INPUT_CPU_READ_BIT | |
| 453 | VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT | |
| 454 | VK_MEMORY_INPUT_INDEX_FETCH_BIT | |
| 455 | VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT | |
| 456 | VK_MEMORY_INPUT_UNIFORM_READ_BIT | |
| 457 | VK_MEMORY_INPUT_SHADER_READ_BIT | |
| 458 | VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT | |
| 459 | VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 460 | VK_MEMORY_INPUT_COPY_BIT; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 461 | |
| 462 | if (image_layout == m_imageInfo.layout) { |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | switch (image_layout) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 467 | case VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL: |
| 468 | output_mask = VK_MEMORY_OUTPUT_COPY_BIT; |
| 469 | input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_COPY_BIT; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 470 | break; |
| 471 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 472 | case VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL: |
| 473 | output_mask = VK_MEMORY_OUTPUT_COPY_BIT; |
| 474 | input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_COPY_BIT; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 475 | break; |
| 476 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 477 | case VK_IMAGE_LAYOUT_CLEAR_OPTIMAL: |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 478 | break; |
| 479 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 480 | case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: |
| 481 | output_mask = VK_MEMORY_OUTPUT_COPY_BIT; |
| 482 | input_mask = VK_MEMORY_INPUT_SHADER_READ_BIT | VK_MEMORY_INPUT_COPY_BIT; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 483 | break; |
| 484 | |
| 485 | default: |
| 486 | output_mask = all_cache_outputs; |
| 487 | input_mask = all_cache_inputs; |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | ImageMemoryBarrier(cmd_buf, aspect, output_mask, input_mask, image_layout); |
| 492 | m_imageInfo.layout = image_layout; |
| 493 | } |
| 494 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 495 | void XglImage::SetLayout(VK_IMAGE_ASPECT aspect, |
| 496 | VK_IMAGE_LAYOUT image_layout) |
Courtney Goeltzenleuchter | be4f0cd | 2015-04-07 08:57:30 -0600 | [diff] [blame] | 497 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 498 | VK_RESULT err; |
Courtney Goeltzenleuchter | be4f0cd | 2015-04-07 08:57:30 -0600 | [diff] [blame] | 499 | XglCommandBufferObj cmd_buf(m_device); |
| 500 | |
| 501 | /* Build command buffer to set image layout in the driver */ |
| 502 | err = cmd_buf.BeginCommandBuffer(); |
| 503 | assert(!err); |
| 504 | |
| 505 | SetLayout(&cmd_buf, aspect, image_layout); |
| 506 | |
| 507 | err = cmd_buf.EndCommandBuffer(); |
| 508 | assert(!err); |
| 509 | |
| 510 | cmd_buf.QueueCommandBuffer(); |
| 511 | } |
| 512 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 513 | bool XglImage::IsCompatible(VK_FLAGS usage, VK_FLAGS features) |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 514 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 515 | if ((usage & VK_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) && |
| 516 | !(features & VK_FORMAT_IMAGE_SHADER_READ_BIT)) |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 517 | return false; |
Courtney Goeltzenleuchter | be4f0cd | 2015-04-07 08:57:30 -0600 | [diff] [blame] | 518 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 519 | if ((usage & VK_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) && |
| 520 | !(features & VK_FORMAT_IMAGE_SHADER_WRITE_BIT)) |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 521 | return false; |
Courtney Goeltzenleuchter | be4f0cd | 2015-04-07 08:57:30 -0600 | [diff] [blame] | 522 | |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 523 | return true; |
| 524 | } |
| 525 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 526 | void XglImage::init(uint32_t w, uint32_t h, |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 527 | VK_FORMAT fmt, VK_FLAGS usage, |
| 528 | VK_IMAGE_TILING requested_tiling) |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 529 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 530 | uint32_t mipCount; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 531 | VK_FORMAT_PROPERTIES image_fmt; |
| 532 | VK_IMAGE_TILING tiling; |
| 533 | VK_RESULT err; |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 534 | size_t size; |
| 535 | |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 536 | mipCount = 0; |
| 537 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 538 | uint32_t _w = w; |
| 539 | uint32_t _h = h; |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 540 | while( ( _w > 0 ) || ( _h > 0 ) ) |
| 541 | { |
| 542 | _w >>= 1; |
| 543 | _h >>= 1; |
| 544 | mipCount++; |
| 545 | } |
| 546 | |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 547 | size = sizeof(image_fmt); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 548 | err = vkGetFormatInfo(m_device->obj(), fmt, |
| 549 | VK_INFO_TYPE_FORMAT_PROPERTIES, |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 550 | &size, &image_fmt); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 551 | ASSERT_VK_SUCCESS(err); |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 552 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 553 | if (requested_tiling == VK_LINEAR_TILING) { |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 554 | if (IsCompatible(usage, image_fmt.linearTilingFeatures)) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 555 | tiling = VK_LINEAR_TILING; |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 556 | } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 557 | tiling = VK_OPTIMAL_TILING; |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 558 | } else { |
| 559 | ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration"; |
| 560 | } |
| 561 | } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 562 | tiling = VK_OPTIMAL_TILING; |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 563 | } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 564 | tiling = VK_LINEAR_TILING; |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 565 | } else { |
| 566 | ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration"; |
| 567 | } |
| 568 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 569 | VK_IMAGE_CREATE_INFO imageCreateInfo = vk_testing::Image::create_info(); |
| 570 | imageCreateInfo.imageType = VK_IMAGE_2D; |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 571 | imageCreateInfo.format = fmt; |
| 572 | imageCreateInfo.extent.width = w; |
| 573 | imageCreateInfo.extent.height = h; |
| 574 | imageCreateInfo.mipLevels = mipCount; |
| 575 | imageCreateInfo.tiling = tiling; |
| 576 | |
| 577 | imageCreateInfo.usage = usage; |
| 578 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 579 | vk_testing::Image::init(*m_device, imageCreateInfo); |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 580 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 581 | if (usage & VK_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) { |
| 582 | SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
Courtney Goeltzenleuchter | be4f0cd | 2015-04-07 08:57:30 -0600 | [diff] [blame] | 583 | } else { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 584 | SetLayout(VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_GENERAL); |
Courtney Goeltzenleuchter | be4f0cd | 2015-04-07 08:57:30 -0600 | [diff] [blame] | 585 | } |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 586 | } |
| 587 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 588 | VK_RESULT XglImage::MapMemory(void** ptr) |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 589 | { |
| 590 | *ptr = map(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 591 | return (*ptr) ? VK_SUCCESS : VK_ERROR_UNKNOWN; |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 592 | } |
| 593 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 594 | VK_RESULT XglImage::UnmapMemory() |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 595 | { |
| 596 | unmap(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 597 | return VK_SUCCESS; |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 598 | } |
| 599 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 600 | VK_RESULT XglImage::CopyImage(XglImage &src_image) |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 601 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 602 | VK_RESULT err; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 603 | XglCommandBufferObj cmd_buf(m_device); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 604 | VK_IMAGE_LAYOUT src_image_layout, dest_image_layout; |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 605 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 606 | /* Build command buffer to copy staging texture to usable texture */ |
| 607 | err = cmd_buf.BeginCommandBuffer(); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 608 | assert(!err); |
| 609 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 610 | /* TODO: Can we determine image aspect from image object? */ |
| 611 | src_image_layout = src_image.layout(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 612 | src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 613 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 614 | dest_image_layout = this->layout(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 615 | this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL); |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 616 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 617 | VK_IMAGE_COPY copy_region = {}; |
| 618 | copy_region.srcSubresource.aspect = VK_IMAGE_ASPECT_COLOR; |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 619 | copy_region.srcSubresource.arraySlice = 0; |
| 620 | copy_region.srcSubresource.mipLevel = 0; |
| 621 | copy_region.srcOffset.x = 0; |
| 622 | copy_region.srcOffset.y = 0; |
| 623 | copy_region.srcOffset.z = 0; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 624 | copy_region.destSubresource.aspect = VK_IMAGE_ASPECT_COLOR; |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 625 | copy_region.destSubresource.arraySlice = 0; |
| 626 | copy_region.destSubresource.mipLevel = 0; |
| 627 | copy_region.destOffset.x = 0; |
| 628 | copy_region.destOffset.y = 0; |
| 629 | copy_region.destOffset.z = 0; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 630 | copy_region.extent = src_image.extent(); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 631 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 632 | vkCmdCopyImage(cmd_buf.obj(), |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 633 | src_image.obj(), src_image.layout(), |
| 634 | obj(), layout(), |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 635 | 1, ©_region); |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 636 | cmd_buf.mem_ref_mgr.AddMemoryRefs(src_image); |
| 637 | cmd_buf.mem_ref_mgr.AddMemoryRefs(*this); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 638 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 639 | src_image.SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, src_image_layout); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 640 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 641 | this->SetLayout(&cmd_buf, VK_IMAGE_ASPECT_COLOR, dest_image_layout); |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 642 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 643 | err = cmd_buf.EndCommandBuffer(); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 644 | assert(!err); |
| 645 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 646 | cmd_buf.QueueCommandBuffer(); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 647 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 648 | return VK_SUCCESS; |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 649 | } |
| 650 | |
Tony Barbour | ebc093f | 2015-04-01 16:38:10 -0600 | [diff] [blame] | 651 | XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors) |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 652 | :XglImage(device) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 653 | { |
| 654 | m_device = device; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 655 | const VK_FORMAT tex_format = VK_FMT_B8G8R8A8_UNORM; |
Tony Barbour | ebc093f | 2015-04-01 16:38:10 -0600 | [diff] [blame] | 656 | uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 }; |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 657 | void *data; |
| 658 | int32_t x, y; |
| 659 | XglImage stagingImage(device); |
| 660 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 661 | stagingImage.init(16, 16, tex_format, 0, VK_LINEAR_TILING); |
| 662 | VK_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(VK_IMAGE_ASPECT_COLOR, 0, 0)); |
Tony Barbour | ebc093f | 2015-04-01 16:38:10 -0600 | [diff] [blame] | 663 | |
| 664 | if (colors == NULL) |
| 665 | colors = tex_colors; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 666 | |
| 667 | memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo)); |
| 668 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 669 | m_textureViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 670 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 671 | VK_IMAGE_VIEW_CREATE_INFO view = {}; |
| 672 | view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
Tony Barbour | bdf0a31 | 2015-04-01 17:10:07 -0600 | [diff] [blame] | 673 | view.pNext = NULL; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 674 | view.image = VK_NULL_HANDLE; |
| 675 | view.viewType = VK_IMAGE_VIEW_2D; |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 676 | view.format = tex_format; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 677 | view.channels.r = VK_CHANNEL_SWIZZLE_R; |
| 678 | view.channels.g = VK_CHANNEL_SWIZZLE_G; |
| 679 | view.channels.b = VK_CHANNEL_SWIZZLE_B; |
| 680 | view.channels.a = VK_CHANNEL_SWIZZLE_A; |
| 681 | view.subresourceRange.aspect = VK_IMAGE_ASPECT_COLOR; |
Tony Barbour | bdf0a31 | 2015-04-01 17:10:07 -0600 | [diff] [blame] | 682 | view.subresourceRange.baseMipLevel = 0; |
| 683 | view.subresourceRange.mipLevels = 1; |
| 684 | view.subresourceRange.baseArraySlice = 0; |
| 685 | view.subresourceRange.arraySize = 1; |
| 686 | view.minLod = 0.0f; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 687 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 688 | /* create image */ |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 689 | init(16, 16, tex_format, VK_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, VK_OPTIMAL_TILING); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 690 | |
| 691 | /* create image view */ |
Chia-I Wu | 13a3aa8 | 2014-12-28 15:55:09 +0800 | [diff] [blame] | 692 | view.image = obj(); |
| 693 | m_textureView.init(*m_device, view); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 694 | m_textureViewInfo.view = m_textureView.obj(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 695 | |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 696 | data = stagingImage.map(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 697 | |
Chia-I Wu | 13a3aa8 | 2014-12-28 15:55:09 +0800 | [diff] [blame] | 698 | for (y = 0; y < extent().height; y++) { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 699 | uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y); |
Chia-I Wu | 13a3aa8 | 2014-12-28 15:55:09 +0800 | [diff] [blame] | 700 | for (x = 0; x < extent().width; x++) |
Tony Barbour | ebc093f | 2015-04-01 16:38:10 -0600 | [diff] [blame] | 701 | row[x] = colors[(x & 1) ^ (y & 1)]; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 702 | } |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 703 | stagingImage.unmap(); |
| 704 | XglImage::CopyImage(stagingImage); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 705 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 706 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 707 | XglSamplerObj::XglSamplerObj(XglDevice *device) |
| 708 | { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 709 | m_device = device; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 710 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 711 | VK_SAMPLER_CREATE_INFO samplerCreateInfo; |
Chia-I Wu | e9864b5 | 2014-12-28 16:32:24 +0800 | [diff] [blame] | 712 | memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo)); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 713 | samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; |
| 714 | samplerCreateInfo.magFilter = VK_TEX_FILTER_NEAREST; |
| 715 | samplerCreateInfo.minFilter = VK_TEX_FILTER_NEAREST; |
| 716 | samplerCreateInfo.mipMode = VK_TEX_MIPMAP_BASE; |
| 717 | samplerCreateInfo.addressU = VK_TEX_ADDRESS_WRAP; |
| 718 | samplerCreateInfo.addressV = VK_TEX_ADDRESS_WRAP; |
| 719 | samplerCreateInfo.addressW = VK_TEX_ADDRESS_WRAP; |
Chia-I Wu | e9864b5 | 2014-12-28 16:32:24 +0800 | [diff] [blame] | 720 | samplerCreateInfo.mipLodBias = 0.0; |
| 721 | samplerCreateInfo.maxAnisotropy = 0.0; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 722 | samplerCreateInfo.compareFunc = VK_COMPARE_NEVER; |
Chia-I Wu | e9864b5 | 2014-12-28 16:32:24 +0800 | [diff] [blame] | 723 | samplerCreateInfo.minLod = 0.0; |
| 724 | samplerCreateInfo.maxLod = 0.0; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 725 | samplerCreateInfo.borderColorType = VK_BORDER_COLOR_OPAQUE_WHITE; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 726 | |
Chia-I Wu | e9864b5 | 2014-12-28 16:32:24 +0800 | [diff] [blame] | 727 | init(*m_device, samplerCreateInfo); |
Tony Barbour | f325bf1 | 2014-12-03 15:59:38 -0700 | [diff] [blame] | 728 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 729 | |
Courtney Goeltzenleuchter | 3b0a815 | 2014-12-04 15:18:47 -0700 | [diff] [blame] | 730 | /* |
| 731 | * Basic ConstantBuffer constructor. Then use create methods to fill in the details. |
| 732 | */ |
| 733 | XglConstantBufferObj::XglConstantBufferObj(XglDevice *device) |
| 734 | { |
| 735 | m_device = device; |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 736 | m_commandBuffer = 0; |
Courtney Goeltzenleuchter | 3b0a815 | 2014-12-04 15:18:47 -0700 | [diff] [blame] | 737 | |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 738 | memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo)); |
Courtney Goeltzenleuchter | 3b0a815 | 2014-12-04 15:18:47 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Tony Barbour | 044703b | 2015-03-26 12:37:52 -0600 | [diff] [blame] | 741 | XglConstantBufferObj::~XglConstantBufferObj() |
| 742 | { |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 743 | // TODO: Should we call QueueRemoveMemReference for the constant buffer memory here? |
Tony Barbour | 044703b | 2015-03-26 12:37:52 -0600 | [diff] [blame] | 744 | if (m_commandBuffer) { |
| 745 | delete m_commandBuffer; |
| 746 | } |
| 747 | } |
| 748 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 749 | XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data) |
| 750 | { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 751 | m_device = device; |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 752 | m_commandBuffer = 0; |
| 753 | |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 754 | memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo)); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 755 | m_numVertices = constantCount; |
| 756 | m_stride = constantSize; |
| 757 | |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 758 | const size_t allocationSize = constantCount * constantSize; |
| 759 | init(*m_device, allocationSize); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 760 | |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 761 | void *pData = map(); |
| 762 | memcpy(pData, data, allocationSize); |
| 763 | unmap(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 764 | |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 765 | // set up the buffer view for the constant buffer |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 766 | VK_BUFFER_VIEW_CREATE_INFO view_info = {}; |
| 767 | view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO; |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 768 | view_info.buffer = obj(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 769 | view_info.viewType = VK_BUFFER_VIEW_RAW; |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 770 | view_info.offset = 0; |
| 771 | view_info.range = allocationSize; |
| 772 | m_bufferView.init(*m_device, view_info); |
| 773 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 774 | this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO; |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 775 | this->m_bufferViewInfo.view = m_bufferView.obj(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 776 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 777 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 778 | void XglConstantBufferObj::Bind(VK_CMD_BUFFER cmdBuffer, VK_GPU_SIZE offset, uint32_t binding) |
Courtney Goeltzenleuchter | 3764030 | 2014-12-04 15:26:56 -0700 | [diff] [blame] | 779 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 780 | vkCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding); |
Courtney Goeltzenleuchter | 3764030 | 2014-12-04 15:26:56 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 784 | void XglConstantBufferObj::BufferMemoryBarrier( |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 785 | VK_FLAGS outputMask /*= |
| 786 | VK_MEMORY_OUTPUT_CPU_WRITE_BIT | |
| 787 | VK_MEMORY_OUTPUT_SHADER_WRITE_BIT | |
| 788 | VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT | |
| 789 | VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 790 | VK_MEMORY_OUTPUT_COPY_BIT*/, |
| 791 | VK_FLAGS inputMask /*= |
| 792 | VK_MEMORY_INPUT_CPU_READ_BIT | |
| 793 | VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT | |
| 794 | VK_MEMORY_INPUT_INDEX_FETCH_BIT | |
| 795 | VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT | |
| 796 | VK_MEMORY_INPUT_UNIFORM_READ_BIT | |
| 797 | VK_MEMORY_INPUT_SHADER_READ_BIT | |
| 798 | VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT | |
| 799 | VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 800 | VK_MEMORY_INPUT_COPY_BIT*/) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 801 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 802 | VK_RESULT err = VK_SUCCESS; |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 803 | |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 804 | if (!m_commandBuffer) |
| 805 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 806 | m_fence.init(*m_device, vk_testing::Fence::create_info()); |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 807 | |
| 808 | m_commandBuffer = new XglCommandBufferObj(m_device); |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 809 | } |
| 810 | else |
| 811 | { |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 812 | m_device->wait(m_fence); |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 813 | m_commandBuffer->mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue); |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 816 | // open the command buffer |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 817 | VK_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {}; |
| 818 | cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO; |
Tony Barbour | bdf0a31 | 2015-04-01 17:10:07 -0600 | [diff] [blame] | 819 | cmd_buf_info.pNext = NULL; |
| 820 | cmd_buf_info.flags = 0; |
| 821 | |
Jon Ashburn | c4164b1 | 2014-12-31 17:10:47 -0700 | [diff] [blame] | 822 | err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 823 | ASSERT_VK_SUCCESS(err); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 824 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 825 | VK_BUFFER_MEMORY_BARRIER memory_barrier = |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 826 | buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 827 | VK_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 828 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 829 | VK_PIPE_EVENT set_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE }; |
| 830 | VK_PIPELINE_BARRIER pipeline_barrier = {}; |
| 831 | pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 832 | pipeline_barrier.eventCount = 1; |
| 833 | pipeline_barrier.pEvents = set_events; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 834 | pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 835 | pipeline_barrier.memBarrierCount = 1; |
Mark Lobodzinski | d5d83ed | 2015-02-02 11:55:52 -0600 | [diff] [blame] | 836 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 837 | |
| 838 | // write barrier to the command buffer |
| 839 | m_commandBuffer->PipelineBarrier(&pipeline_barrier); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 840 | |
| 841 | // finish recording the command buffer |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 842 | err = m_commandBuffer->EndCommandBuffer(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 843 | ASSERT_VK_SUCCESS(err); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 844 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 845 | /* |
| 846 | * Tell driver about memory references made in this command buffer |
| 847 | * Note: Since this command buffer only has a PipelineBarrier |
| 848 | * command there really aren't any memory refs to worry about. |
| 849 | */ |
| 850 | m_commandBuffer->mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 851 | |
| 852 | // submit the command buffer to the universal queue |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 853 | VK_CMD_BUFFER bufferArray[1]; |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 854 | bufferArray[0] = m_commandBuffer->GetBufferHandle(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 855 | err = vkQueueSubmit( m_device->m_queue, 1, bufferArray, m_fence.obj() ); |
| 856 | ASSERT_VK_SUCCESS(err); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 859 | XglIndexBufferObj::XglIndexBufferObj(XglDevice *device) |
| 860 | : XglConstantBufferObj(device) |
| 861 | { |
| 862 | |
| 863 | } |
| 864 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 865 | void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, VK_INDEX_TYPE indexType, const void* data) |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 866 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 867 | VK_FORMAT viewFormat; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 868 | |
| 869 | m_numVertices = numIndexes; |
| 870 | m_indexType = indexType; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 871 | switch (indexType) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 872 | case VK_INDEX_8: |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 873 | m_stride = 1; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 874 | viewFormat = VK_FMT_R8_UINT; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 875 | break; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 876 | case VK_INDEX_16: |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 877 | m_stride = 2; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 878 | viewFormat = VK_FMT_R16_UINT; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 879 | break; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 880 | case VK_INDEX_32: |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 881 | m_stride = 4; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 882 | viewFormat = VK_FMT_R32_UINT; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 883 | break; |
Chia-I Wu | b4c2aa4 | 2014-12-15 23:50:11 +0800 | [diff] [blame] | 884 | default: |
| 885 | assert(!"unknown index type"); |
| 886 | break; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 889 | const size_t allocationSize = numIndexes * m_stride; |
| 890 | init(*m_device, allocationSize); |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 891 | |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 892 | void *pData = map(); |
| 893 | memcpy(pData, data, allocationSize); |
| 894 | unmap(); |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 895 | |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 896 | // set up the buffer view for the constant buffer |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 897 | VK_BUFFER_VIEW_CREATE_INFO view_info = {}; |
| 898 | view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO; |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 899 | view_info.buffer = obj(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 900 | view_info.viewType = VK_BUFFER_VIEW_TYPED; |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 901 | view_info.format = viewFormat; |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 902 | view_info.offset = 0; |
| 903 | view_info.range = allocationSize; |
| 904 | m_bufferView.init(*m_device, view_info); |
| 905 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 906 | this->m_bufferViewInfo.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO; |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 907 | this->m_bufferViewInfo.view = m_bufferView.obj(); |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 910 | void XglIndexBufferObj::Bind(VK_CMD_BUFFER cmdBuffer, VK_GPU_SIZE offset) |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 911 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 912 | vkCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType); |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 913 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 914 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 915 | VK_INDEX_TYPE XglIndexBufferObj::GetIndexType() |
Tony Barbour | af1f919 | 2014-12-17 10:57:58 -0700 | [diff] [blame] | 916 | { |
| 917 | return m_indexType; |
| 918 | } |
| 919 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 920 | VK_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo() |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 921 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 922 | VK_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (VK_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(VK_PIPELINE_SHADER_STAGE_CREATE_INFO) ); |
| 923 | stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 924 | stageInfo->shader.stage = m_stage; |
Chia-I Wu | babc0fd | 2014-12-29 14:14:03 +0800 | [diff] [blame] | 925 | stageInfo->shader.shader = obj(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 926 | stageInfo->shader.linkConstBufferCount = 0; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 927 | stageInfo->shader.pLinkConstBufferInfo = VK_NULL_HANDLE; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 928 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 929 | return stageInfo; |
| 930 | } |
| 931 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 932 | XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, VK_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 933 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 934 | VK_RESULT err = VK_SUCCESS; |
Cody Northrop | 3bfd27c | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 935 | std::vector<unsigned int> spv; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 936 | VK_SHADER_CREATE_INFO createInfo; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 937 | size_t shader_len; |
| 938 | |
| 939 | m_stage = stage; |
| 940 | m_device = device; |
| 941 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 942 | createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 943 | createInfo.pNext = NULL; |
| 944 | |
Cody Northrop | 3bfd27c | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 945 | if (!framework->m_use_spv) { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 946 | |
| 947 | shader_len = strlen(shader_code); |
| 948 | createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1; |
| 949 | createInfo.pCode = malloc(createInfo.codeSize); |
| 950 | createInfo.flags = 0; |
| 951 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 952 | /* try version 0 first: VK_PIPELINE_SHADER_STAGE followed by GLSL */ |
Cody Northrop | 3bfd27c | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 953 | ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 954 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 955 | ((uint32_t *) createInfo.pCode)[2] = stage; |
| 956 | memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1); |
| 957 | |
Chia-I Wu | babc0fd | 2014-12-29 14:14:03 +0800 | [diff] [blame] | 958 | err = init_try(*m_device, createInfo); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Cody Northrop | 3bfd27c | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 961 | if (framework->m_use_spv || err) { |
| 962 | std::vector<unsigned int> spv; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 963 | err = VK_SUCCESS; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 964 | |
Cody Northrop | 3bfd27c | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 965 | // Use Reference GLSL to SPV compiler |
| 966 | framework->GLSLtoSPV(stage, shader_code, spv); |
| 967 | createInfo.pCode = spv.data(); |
| 968 | createInfo.codeSize = spv.size() * sizeof(unsigned int); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 969 | createInfo.flags = 0; |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 970 | |
Chia-I Wu | babc0fd | 2014-12-29 14:14:03 +0800 | [diff] [blame] | 971 | init(*m_device, createInfo); |
| 972 | } |
Tony Barbour | f325bf1 | 2014-12-03 15:59:38 -0700 | [diff] [blame] | 973 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 974 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 975 | XglPipelineObj::XglPipelineObj(XglDevice *device) |
| 976 | { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 977 | m_device = device; |
| 978 | m_vi_state.attributeCount = m_vi_state.bindingCount = 0; |
| 979 | m_vertexBufferCount = 0; |
| 980 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 981 | m_ia_state.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO; |
| 982 | m_ia_state.pNext = VK_NULL_HANDLE; |
| 983 | m_ia_state.topology = VK_TOPOLOGY_TRIANGLE_LIST; |
| 984 | m_ia_state.disableVertexReuse = VK_FALSE; |
| 985 | m_ia_state.primitiveRestartEnable = VK_FALSE; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 986 | m_ia_state.primitiveRestartIndex = 0; |
| 987 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 988 | m_rs_state.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 989 | m_rs_state.pNext = &m_ia_state; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 990 | m_rs_state.depthClipEnable = VK_FALSE; |
| 991 | m_rs_state.rasterizerDiscardEnable = VK_FALSE; |
| 992 | m_rs_state.programPointSize = VK_FALSE; |
| 993 | m_rs_state.pointOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT; |
| 994 | m_rs_state.provokingVertex = VK_PROVOKING_VERTEX_LAST; |
| 995 | m_rs_state.fillMode = VK_FILL_SOLID; |
| 996 | m_rs_state.cullMode = VK_CULL_NONE; |
| 997 | m_rs_state.frontFace = VK_FRONT_FACE_CCW; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 998 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 999 | memset(&m_cb_state,0,sizeof(m_cb_state)); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1000 | m_cb_state.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1001 | m_cb_state.pNext = &m_rs_state; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1002 | m_cb_state.alphaToCoverageEnable = VK_FALSE; |
| 1003 | m_cb_state.logicOp = VK_LOGIC_OP_COPY; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1004 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1005 | m_ms_state.pNext = &m_cb_state; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1006 | m_ms_state.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO; |
| 1007 | m_ms_state.multisampleEnable = VK_FALSE; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1008 | m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it? |
| 1009 | m_ms_state.samples = 1; |
| 1010 | m_ms_state.minSampleShading = 0; |
| 1011 | m_ms_state.sampleShadingEnable = 0; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1012 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1013 | m_ds_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1014 | m_ds_state.pNext = &m_ms_state, |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1015 | m_ds_state.format = VK_FMT_D32_SFLOAT; |
| 1016 | m_ds_state.depthTestEnable = VK_FALSE; |
| 1017 | m_ds_state.depthWriteEnable = VK_FALSE; |
| 1018 | m_ds_state.depthBoundsEnable = VK_FALSE; |
| 1019 | m_ds_state.depthFunc = VK_COMPARE_LESS_EQUAL; |
| 1020 | m_ds_state.back.stencilDepthFailOp = VK_STENCIL_OP_KEEP; |
| 1021 | m_ds_state.back.stencilFailOp = VK_STENCIL_OP_KEEP; |
| 1022 | m_ds_state.back.stencilPassOp = VK_STENCIL_OP_KEEP; |
| 1023 | m_ds_state.back.stencilFunc = VK_COMPARE_ALWAYS; |
| 1024 | m_ds_state.stencilTestEnable = VK_FALSE; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1025 | m_ds_state.front = m_ds_state.back; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1026 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1027 | VK_PIPELINE_CB_ATTACHMENT_STATE att = {}; |
| 1028 | att.blendEnable = VK_FALSE; |
| 1029 | att.format = VK_FMT_B8G8R8A8_UNORM; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1030 | att.channelWriteMask = 0xf; |
| 1031 | AddColorAttachment(0, &att); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1032 | |
| 1033 | }; |
| 1034 | |
| 1035 | void XglPipelineObj::AddShader(XglShaderObj* shader) |
| 1036 | { |
| 1037 | m_shaderObjs.push_back(shader); |
| 1038 | } |
| 1039 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1040 | void XglPipelineObj::AddVertexInputAttribs(VK_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1041 | { |
| 1042 | m_vi_state.pVertexAttributeDescriptions = vi_attrib; |
| 1043 | m_vi_state.attributeCount = count; |
| 1044 | } |
| 1045 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1046 | void XglPipelineObj::AddVertexInputBindings(VK_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1047 | { |
| 1048 | m_vi_state.pVertexBindingDescriptions = vi_binding; |
| 1049 | m_vi_state.bindingCount = count; |
| 1050 | } |
| 1051 | |
| 1052 | void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding) |
| 1053 | { |
| 1054 | m_vertexBufferObjs.push_back(vertexDataBuffer); |
| 1055 | m_vertexBufferBindings.push_back(binding); |
| 1056 | m_vertexBufferCount++; |
| 1057 | } |
| 1058 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1059 | void XglPipelineObj::AddColorAttachment(uint32_t binding, const VK_PIPELINE_CB_ATTACHMENT_STATE *att) |
Chia-I Wu | ecebf75 | 2014-12-05 10:45:15 +0800 | [diff] [blame] | 1060 | { |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1061 | if (binding+1 > m_colorAttachments.size()) |
| 1062 | { |
| 1063 | m_colorAttachments.resize(binding+1); |
| 1064 | } |
| 1065 | m_colorAttachments[binding] = *att; |
| 1066 | } |
| 1067 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1068 | void XglPipelineObj::SetDepthStencil(VK_PIPELINE_DS_STATE_CREATE_INFO *ds_state) |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1069 | { |
| 1070 | m_ds_state.format = ds_state->format; |
| 1071 | m_ds_state.depthTestEnable = ds_state->depthTestEnable; |
| 1072 | m_ds_state.depthWriteEnable = ds_state->depthWriteEnable; |
| 1073 | m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable; |
| 1074 | m_ds_state.depthFunc = ds_state->depthFunc; |
| 1075 | m_ds_state.stencilTestEnable = ds_state->stencilTestEnable; |
| 1076 | m_ds_state.back = ds_state->back; |
| 1077 | m_ds_state.front = ds_state->front; |
Chia-I Wu | ecebf75 | 2014-12-05 10:45:15 +0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1080 | void XglPipelineObj::CreateVKPipeline(XglDescriptorSetObj &descriptorSet) |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1081 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1082 | void* head_ptr = &m_ds_state; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1083 | VK_GRAPHICS_PIPELINE_CREATE_INFO info = {}; |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1084 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1085 | VK_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo; |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1086 | |
| 1087 | for (int i=0; i<m_shaderObjs.size(); i++) |
| 1088 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1089 | shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(); |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1090 | shaderCreateInfo->pNext = head_ptr; |
| 1091 | head_ptr = shaderCreateInfo; |
| 1092 | } |
| 1093 | |
| 1094 | if (m_vi_state.attributeCount && m_vi_state.bindingCount) |
| 1095 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1096 | m_vi_state.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO; |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1097 | m_vi_state.pNext = head_ptr; |
| 1098 | head_ptr = &m_vi_state; |
| 1099 | } |
| 1100 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1101 | info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1102 | info.pNext = head_ptr; |
| 1103 | info.flags = 0; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1104 | info.pSetLayoutChain = descriptorSet.GetLayoutChain(); |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1105 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1106 | m_cb_state.attachmentCount = m_colorAttachments.size(); |
| 1107 | m_cb_state.pAttachments = &m_colorAttachments[0]; |
| 1108 | |
Chia-I Wu | 2648d09 | 2014-12-29 14:24:14 +0800 | [diff] [blame] | 1109 | init(*m_device, info); |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1110 | } |
Chia-I Wu | 2648d09 | 2014-12-29 14:24:14 +0800 | [diff] [blame] | 1111 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1112 | vector<VK_GPU_MEMORY> XglMemoryRefManager::mem_refs() const |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1113 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1114 | std::vector<VK_GPU_MEMORY> mems; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1115 | if (this->mem_refs_.size()) { |
| 1116 | mems.reserve(this->mem_refs_.size()); |
| 1117 | for (uint32_t i = 0; i < this->mem_refs_.size(); i++) |
| 1118 | mems.push_back(this->mem_refs_[i]); |
| 1119 | } |
| 1120 | |
| 1121 | return mems; |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1124 | void XglMemoryRefManager::AddMemoryRefs(vk_testing::Object &vkObject) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1125 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1126 | const std::vector<VK_GPU_MEMORY> mems = vkObject.memories(); |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1127 | AddMemoryRefs(mems); |
| 1128 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1129 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1130 | void XglMemoryRefManager::AddMemoryRefs(vector<VK_GPU_MEMORY> mem) |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1131 | { |
| 1132 | for (size_t i = 0; i < mem.size(); i++) { |
| 1133 | if (mem[i] != NULL) { |
| 1134 | this->mem_refs_.push_back(mem[i]); |
| 1135 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1136 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1137 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 1138 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1139 | void XglMemoryRefManager::EmitAddMemoryRefs(VK_QUEUE queue) |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1140 | { |
| 1141 | for (uint32_t i = 0; i < mem_refs_.size(); i++) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1142 | vkQueueAddMemReference(queue, mem_refs_[i]); |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1143 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1144 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 1145 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1146 | void XglMemoryRefManager::EmitRemoveMemoryRefs(VK_QUEUE queue) |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1147 | { |
| 1148 | for (uint32_t i = 0; i < mem_refs_.size(); i++) { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1149 | vkQueueRemoveMemReference(queue, mem_refs_[i]); |
Mark Lobodzinski | c52b775 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1153 | XglCommandBufferObj::XglCommandBufferObj(XglDevice *device) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1154 | : vk_testing::CmdBuffer(*device, vk_testing::CmdBuffer::create_info(device->graphics_queue_node_index_)) |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1155 | { |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1156 | m_device = device; |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1157 | } |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1158 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1159 | VK_CMD_BUFFER XglCommandBufferObj::GetBufferHandle() |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1160 | { |
Chia-I Wu | d28343c | 2014-12-28 15:12:48 +0800 | [diff] [blame] | 1161 | return obj(); |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1162 | } |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1163 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1164 | VK_RESULT XglCommandBufferObj::BeginCommandBuffer(VK_CMD_BUFFER_BEGIN_INFO *pInfo) |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1165 | { |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 1166 | begin(pInfo); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1167 | return VK_SUCCESS; |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1170 | VK_RESULT XglCommandBufferObj::BeginCommandBuffer(VK_RENDER_PASS renderpass_obj, VK_FRAMEBUFFER framebuffer_obj) |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 1171 | { |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 1172 | begin(renderpass_obj, framebuffer_obj); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1173 | return VK_SUCCESS; |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1176 | VK_RESULT XglCommandBufferObj::BeginCommandBuffer() |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 1177 | { |
| 1178 | begin(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1179 | return VK_SUCCESS; |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1182 | VK_RESULT XglCommandBufferObj::EndCommandBuffer() |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1183 | { |
Chia-I Wu | d28343c | 2014-12-28 15:12:48 +0800 | [diff] [blame] | 1184 | end(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1185 | return VK_SUCCESS; |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1188 | void XglCommandBufferObj::PipelineBarrier(VK_PIPELINE_BARRIER *barrierPtr) |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1189 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1190 | vkCmdPipelineBarrier(obj(), barrierPtr); |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1193 | void XglCommandBufferObj::ClearAllBuffers(VK_CLEAR_COLOR clear_color, float depth_clear_color, uint32_t stencil_clear_color, |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1194 | XglDepthStencilObj *depthStencilObj) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1195 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1196 | uint32_t i; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1197 | const VK_FLAGS output_mask = |
| 1198 | VK_MEMORY_OUTPUT_CPU_WRITE_BIT | |
| 1199 | VK_MEMORY_OUTPUT_SHADER_WRITE_BIT | |
| 1200 | VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT | |
| 1201 | VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 1202 | VK_MEMORY_OUTPUT_COPY_BIT; |
| 1203 | const VK_FLAGS input_mask = 0; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1204 | |
| 1205 | // whatever we want to do, we do it to the whole buffer |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1206 | VK_IMAGE_SUBRESOURCE_RANGE srRange = {}; |
| 1207 | srRange.aspect = VK_IMAGE_ASPECT_COLOR; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1208 | srRange.baseMipLevel = 0; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1209 | srRange.mipLevels = VK_LAST_MIP_OR_SLICE; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1210 | srRange.baseArraySlice = 0; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1211 | srRange.arraySize = VK_LAST_MIP_OR_SLICE; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1212 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1213 | VK_IMAGE_MEMORY_BARRIER memory_barrier = {}; |
| 1214 | memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1215 | memory_barrier.outputMask = output_mask; |
| 1216 | memory_barrier.inputMask = input_mask; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1217 | memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1218 | memory_barrier.subresourceRange = srRange; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1219 | VK_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1220 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1221 | VK_PIPE_EVENT set_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE }; |
| 1222 | VK_PIPELINE_BARRIER pipeline_barrier = {}; |
| 1223 | pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1224 | pipeline_barrier.eventCount = 1; |
| 1225 | pipeline_barrier.pEvents = set_events; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1226 | pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1227 | pipeline_barrier.memBarrierCount = 1; |
Mark Lobodzinski | d5d83ed | 2015-02-02 11:55:52 -0600 | [diff] [blame] | 1228 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1229 | |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 1230 | for (i = 0; i < m_renderTargets.size(); i++) { |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1231 | memory_barrier.image = m_renderTargets[i]->image(); |
| 1232 | memory_barrier.oldLayout = m_renderTargets[i]->layout(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1233 | vkCmdPipelineBarrier( obj(), &pipeline_barrier); |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1234 | m_renderTargets[i]->layout(memory_barrier.newLayout); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1235 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1236 | vkCmdClearColorImage(obj(), |
| 1237 | m_renderTargets[i]->image(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL, |
Courtney Goeltzenleuchter | b3efe9b | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 1238 | clear_color, 1, &srRange ); |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1239 | |
| 1240 | mem_ref_mgr.AddMemoryRefs(*m_renderTargets[i]); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1243 | if (depthStencilObj) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1244 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1245 | VK_IMAGE_SUBRESOURCE_RANGE dsRange = {}; |
| 1246 | dsRange.aspect = VK_IMAGE_ASPECT_DEPTH; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1247 | dsRange.baseMipLevel = 0; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1248 | dsRange.mipLevels = VK_LAST_MIP_OR_SLICE; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1249 | dsRange.baseArraySlice = 0; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1250 | dsRange.arraySize = VK_LAST_MIP_OR_SLICE; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1251 | |
| 1252 | // prepare the depth buffer for clear |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1253 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1254 | memory_barrier.oldLayout = depthStencilObj->BindInfo()->layout; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1255 | memory_barrier.newLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1256 | memory_barrier.image = depthStencilObj->obj(); |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1257 | memory_barrier.subresourceRange = dsRange; |
| 1258 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1259 | vkCmdPipelineBarrier( obj(), &pipeline_barrier); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1260 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1261 | vkCmdClearDepthStencil(obj(), |
| 1262 | depthStencilObj->obj(), VK_IMAGE_LAYOUT_CLEAR_OPTIMAL, |
Courtney Goeltzenleuchter | 679bbfa | 2015-03-05 17:26:38 -0700 | [diff] [blame] | 1263 | depth_clear_color, stencil_clear_color, |
| 1264 | 1, &dsRange); |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1265 | mem_ref_mgr.AddMemoryRefs(*depthStencilObj); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1266 | |
| 1267 | // prepare depth buffer for rendering |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1268 | memory_barrier.image = depthStencilObj->obj(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1269 | memory_barrier.oldLayout = VK_IMAGE_LAYOUT_CLEAR_OPTIMAL; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1270 | memory_barrier.newLayout = depthStencilObj->BindInfo()->layout; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1271 | memory_barrier.subresourceRange = dsRange; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1272 | vkCmdPipelineBarrier( obj(), &pipeline_barrier); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
Jon Ashburn | cdc40be | 2015-01-02 18:27:14 -0700 | [diff] [blame] | 1276 | void XglCommandBufferObj::PrepareAttachments() |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1277 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1278 | uint32_t i; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1279 | const VK_FLAGS output_mask = |
| 1280 | VK_MEMORY_OUTPUT_CPU_WRITE_BIT | |
| 1281 | VK_MEMORY_OUTPUT_SHADER_WRITE_BIT | |
| 1282 | VK_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT | |
| 1283 | VK_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 1284 | VK_MEMORY_OUTPUT_COPY_BIT; |
| 1285 | const VK_FLAGS input_mask = |
| 1286 | VK_MEMORY_INPUT_CPU_READ_BIT | |
| 1287 | VK_MEMORY_INPUT_INDIRECT_COMMAND_BIT | |
| 1288 | VK_MEMORY_INPUT_INDEX_FETCH_BIT | |
| 1289 | VK_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT | |
| 1290 | VK_MEMORY_INPUT_UNIFORM_READ_BIT | |
| 1291 | VK_MEMORY_INPUT_SHADER_READ_BIT | |
| 1292 | VK_MEMORY_INPUT_COLOR_ATTACHMENT_BIT | |
| 1293 | VK_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 1294 | VK_MEMORY_INPUT_COPY_BIT; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1295 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1296 | VK_IMAGE_SUBRESOURCE_RANGE srRange = {}; |
| 1297 | srRange.aspect = VK_IMAGE_ASPECT_COLOR; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1298 | srRange.baseMipLevel = 0; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1299 | srRange.mipLevels = VK_LAST_MIP_OR_SLICE; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1300 | srRange.baseArraySlice = 0; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1301 | srRange.arraySize = VK_LAST_MIP_OR_SLICE; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1302 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1303 | VK_IMAGE_MEMORY_BARRIER memory_barrier = {}; |
| 1304 | memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1305 | memory_barrier.outputMask = output_mask; |
| 1306 | memory_barrier.inputMask = input_mask; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1307 | memory_barrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1308 | memory_barrier.subresourceRange = srRange; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1309 | VK_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1310 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1311 | VK_PIPE_EVENT set_events[] = { VK_PIPE_EVENT_GPU_COMMANDS_COMPLETE }; |
| 1312 | VK_PIPELINE_BARRIER pipeline_barrier = {}; |
| 1313 | pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1314 | pipeline_barrier.eventCount = 1; |
| 1315 | pipeline_barrier.pEvents = set_events; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1316 | pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1317 | pipeline_barrier.memBarrierCount = 1; |
Mark Lobodzinski | d5d83ed | 2015-02-02 11:55:52 -0600 | [diff] [blame] | 1318 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1319 | |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 1320 | for(i=0; i<m_renderTargets.size(); i++) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1321 | { |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1322 | memory_barrier.image = m_renderTargets[i]->image(); |
| 1323 | memory_barrier.oldLayout = m_renderTargets[i]->layout(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1324 | vkCmdPipelineBarrier( obj(), &pipeline_barrier); |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1325 | m_renderTargets[i]->layout(memory_barrier.newLayout); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1326 | } |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1329 | void XglCommandBufferObj::BeginRenderPass(VK_RENDER_PASS renderpass, VK_FRAMEBUFFER framebuffer) |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 1330 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1331 | VK_RENDER_PASS_BEGIN rp_begin = { |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 1332 | renderpass, |
| 1333 | framebuffer, |
| 1334 | }; |
| 1335 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1336 | vkCmdBeginRenderPass( obj(), &rp_begin); |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 1337 | } |
| 1338 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1339 | void XglCommandBufferObj::EndRenderPass(VK_RENDER_PASS renderpass) |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 1340 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1341 | vkCmdEndRenderPass( obj(), renderpass); |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 1342 | } |
| 1343 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1344 | void XglCommandBufferObj::BindStateObject(VK_STATE_BIND_POINT stateBindPoint, VK_DYNAMIC_STATE_OBJECT stateObject) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1345 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1346 | vkCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget) |
| 1350 | { |
| 1351 | m_renderTargets.push_back(renderTarget); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1354 | void XglCommandBufferObj::DrawIndexed(uint32_t firstIndex, uint32_t indexCount, int32_t vertexOffset, uint32_t firstInstance, uint32_t instanceCount) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1355 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1356 | vkCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1359 | void XglCommandBufferObj::Draw(uint32_t firstVertex, uint32_t vertexCount, uint32_t firstInstance, uint32_t instanceCount) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1360 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1361 | vkCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1364 | void XglCommandBufferObj::QueueCommandBuffer() |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1365 | { |
Tony Barbour | 09b7ac3 | 2015-04-08 10:11:29 -0600 | [diff] [blame] | 1366 | QueueCommandBuffer(NULL); |
| 1367 | } |
| 1368 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1369 | void XglCommandBufferObj::QueueCommandBuffer(VK_FENCE fence) |
Tony Barbour | 09b7ac3 | 2015-04-08 10:11:29 -0600 | [diff] [blame] | 1370 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1371 | VK_RESULT err = VK_SUCCESS; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1372 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1373 | mem_ref_mgr.EmitAddMemoryRefs(m_device->m_queue); |
| 1374 | |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1375 | // submit the command buffer to the universal queue |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1376 | err = vkQueueSubmit( m_device->m_queue, 1, &obj(), fence ); |
| 1377 | ASSERT_VK_SUCCESS( err ); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1378 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1379 | err = vkQueueWaitIdle( m_device->m_queue ); |
| 1380 | ASSERT_VK_SUCCESS( err ); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1381 | |
| 1382 | // Wait for work to finish before cleaning up. |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1383 | vkDeviceWaitIdle(m_device->device()); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1384 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1385 | /* |
| 1386 | * Now that processing on this command buffer is complete |
| 1387 | * we can remove the memory references. |
| 1388 | */ |
| 1389 | mem_ref_mgr.EmitRemoveMemoryRefs(m_device->m_queue); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1392 | void XglCommandBufferObj::BindPipeline(XglPipelineObj &pipeline) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1393 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1394 | vkCmdBindPipeline( obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.obj() ); |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1395 | mem_ref_mgr.AddMemoryRefs(pipeline); |
| 1396 | } |
| 1397 | |
| 1398 | void XglCommandBufferObj::BindDescriptorSet(XglDescriptorSetObj &descriptorSet) |
| 1399 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1400 | VK_DESCRIPTOR_SET set_obj = descriptorSet.GetDescriptorSetHandle(); |
Chia-I Wu | 53f07d7 | 2015-03-28 15:23:55 +0800 | [diff] [blame] | 1401 | |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1402 | // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1403 | vkCmdBindDescriptorSets(obj(), VK_PIPELINE_BIND_POINT_GRAPHICS, |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1404 | descriptorSet.GetLayoutChain(), 0, 1, &set_obj, NULL ); |
| 1405 | |
| 1406 | // Add descriptor set mem refs to command buffer's list |
| 1407 | mem_ref_mgr.AddMemoryRefs(descriptorSet.memories()); |
| 1408 | mem_ref_mgr.AddMemoryRefs(descriptorSet.mem_ref_mgr.mem_refs()); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1409 | } |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1410 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1411 | void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1412 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1413 | vkCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType()); |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1414 | mem_ref_mgr.AddMemoryRefs(*indexBuffer); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1415 | } |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1416 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1417 | void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1418 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1419 | vkCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding); |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1420 | mem_ref_mgr.AddMemoryRefs(*vertexBuffer); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1421 | } |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1422 | |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1423 | XglDepthStencilObj::XglDepthStencilObj() |
| 1424 | { |
| 1425 | m_initialized = false; |
| 1426 | } |
| 1427 | bool XglDepthStencilObj::Initialized() |
| 1428 | { |
| 1429 | return m_initialized; |
| 1430 | } |
| 1431 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1432 | VK_DEPTH_STENCIL_BIND_INFO* XglDepthStencilObj::BindInfo() |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1433 | { |
| 1434 | return &m_depthStencilBindInfo; |
| 1435 | } |
| 1436 | |
| 1437 | void XglDepthStencilObj::Init(XglDevice *device, int32_t width, int32_t height) |
| 1438 | { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1439 | VK_IMAGE_CREATE_INFO image_info; |
| 1440 | VK_DEPTH_STENCIL_VIEW_CREATE_INFO view_info; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1441 | |
| 1442 | m_device = device; |
| 1443 | m_initialized = true; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1444 | m_depth_stencil_fmt = VK_FMT_D16_UNORM; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1445 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1446 | image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1447 | image_info.pNext = NULL; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1448 | image_info.imageType = VK_IMAGE_2D; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1449 | image_info.format = m_depth_stencil_fmt; |
| 1450 | image_info.extent.width = width; |
| 1451 | image_info.extent.height = height; |
| 1452 | image_info.extent.depth = 1; |
| 1453 | image_info.mipLevels = 1; |
| 1454 | image_info.arraySize = 1; |
| 1455 | image_info.samples = 1; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1456 | image_info.tiling = VK_OPTIMAL_TILING; |
| 1457 | image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1458 | image_info.flags = 0; |
| 1459 | init(*m_device, image_info); |
| 1460 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1461 | view_info.sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1462 | view_info.pNext = NULL; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1463 | view_info.image = VK_NULL_HANDLE; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1464 | view_info.mipLevel = 0; |
| 1465 | view_info.baseArraySlice = 0; |
| 1466 | view_info.arraySize = 1; |
| 1467 | view_info.flags = 0; |
| 1468 | view_info.image = obj(); |
| 1469 | m_depthStencilView.init(*m_device, view_info); |
| 1470 | |
| 1471 | m_depthStencilBindInfo.view = m_depthStencilView.obj(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1472 | m_depthStencilBindInfo.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1473 | } |