Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 1 | /* |
| 2 | * XGL Tests |
| 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 | |
| 28 | #include "xglrenderframework.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 | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 31 | m_cmdBuffer( XGL_NULL_HANDLE ), |
Chia-I Wu | 837f995 | 2014-12-15 23:29:34 +0800 | [diff] [blame] | 32 | m_stateRaster( XGL_NULL_HANDLE ), |
| 33 | m_colorBlend( XGL_NULL_HANDLE ), |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 34 | m_stateViewport( XGL_NULL_HANDLE ), |
Chia-I Wu | 837f995 | 2014-12-15 23:29:34 +0800 | [diff] [blame] | 35 | m_stateDepthStencil( XGL_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 |
| 38 | m_render_target_fmt( XGL_FMT_R8G8B8A8_UNORM ), |
| 39 | m_depth_stencil_fmt( XGL_FMT_UNDEFINED ), |
| 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 | { |
| 59 | XGL_RESULT err; |
| 60 | |
Jon Ashburn | 1e46489 | 2015-01-29 15:48:00 -0700 | [diff] [blame] | 61 | err = xglCreateInstance(&app_info, NULL, &this->inst); |
| 62 | ASSERT_XGL_SUCCESS(err); |
| 63 | err = xglEnumerateGpus(inst, XGL_MAX_PHYSICAL_GPUS, &this->gpu_count, |
| 64 | objs); |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 65 | ASSERT_XGL_SUCCESS(err); |
Jon Ashburn | bf843b2 | 2014-11-26 11:06:49 -0700 | [diff] [blame] | 66 | ASSERT_GE(this->gpu_count, 1) << "No GPU available"; |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 67 | |
| 68 | m_device = new XglDevice(0, objs[0]); |
| 69 | m_device->get_device_queue(); |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 70 | |
| 71 | m_depthStencil = new XglDepthStencilObj(); |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void XglRenderFramework::ShutdownFramework() |
| 75 | { |
| 76 | if (m_colorBlend) xglDestroyObject(m_colorBlend); |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 77 | if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil); |
| 78 | if (m_stateRaster) xglDestroyObject(m_stateRaster); |
| 79 | if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer); |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 80 | if (m_framebuffer) xglDestroyObject(m_framebuffer); |
Tobin Ehlis | 976fc16 | 2015-03-26 08:23:25 -0600 | [diff] [blame] | 81 | if (m_renderPass) xglDestroyObject(m_renderPass); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 82 | |
| 83 | if (m_stateViewport) { |
| 84 | xglDestroyObject(m_stateViewport); |
| 85 | } |
Tobin Ehlis | 976fc16 | 2015-03-26 08:23:25 -0600 | [diff] [blame] | 86 | while (!m_renderTargets.empty()) { |
| 87 | xglDestroyObject(m_renderTargets.back()->targetView()); |
| 88 | xglBindObjectMemory(m_renderTargets.back()->image(), 0, XGL_NULL_HANDLE, 0); |
| 89 | xglDestroyObject(m_renderTargets.back()->image()); |
| 90 | xglFreeMemory(m_renderTargets.back()->memory()); |
| 91 | m_renderTargets.pop_back(); |
| 92 | } |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 93 | |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 94 | delete m_depthStencil; |
| 95 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 96 | // reset the driver |
Chia-I Wu | b76e0fa | 2014-12-28 14:27:28 +0800 | [diff] [blame] | 97 | delete m_device; |
Jon Ashburn | 1e46489 | 2015-01-29 15:48:00 -0700 | [diff] [blame] | 98 | xglDestroyInstance(this->inst); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 99 | } |
| 100 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 101 | void XglRenderFramework::InitState() |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 102 | { |
| 103 | XGL_RESULT err; |
| 104 | |
Tony Barbour | a53a694 | 2015-02-25 11:25:11 -0700 | [diff] [blame] | 105 | m_render_target_fmt = XGL_FMT_B8G8R8A8_UNORM; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 106 | |
| 107 | // create a raster state (solid, back-face culling) |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 108 | XGL_DYNAMIC_RS_STATE_CREATE_INFO raster = {}; |
| 109 | raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO; |
| 110 | raster.pointSize = 1.0; |
| 111 | |
| 112 | err = xglCreateDynamicRasterState( device(), &raster, &m_stateRaster ); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 113 | ASSERT_XGL_SUCCESS(err); |
| 114 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 115 | XGL_DYNAMIC_CB_STATE_CREATE_INFO blend = {}; |
| 116 | blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO; |
| 117 | err = xglCreateDynamicColorBlendState(device(), &blend, &m_colorBlend); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 118 | ASSERT_XGL_SUCCESS( err ); |
| 119 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 120 | XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {}; |
| 121 | depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 122 | depthStencil.minDepth = 0.f; |
| 123 | depthStencil.maxDepth = 1.f; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 124 | depthStencil.stencilFrontRef = 0; |
| 125 | depthStencil.stencilBackRef = 0; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 126 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 127 | err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil ); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 128 | ASSERT_XGL_SUCCESS( err ); |
| 129 | |
| 130 | XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {}; |
| 131 | |
| 132 | cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO; |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 133 | cmdInfo.queueNodeIndex = m_device->graphics_queue_node_index_; |
| 134 | |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 135 | err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer); |
| 136 | ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed"; |
| 137 | } |
| 138 | |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 139 | void XglRenderFramework::InitViewport(float width, float height) |
| 140 | { |
| 141 | XGL_RESULT err; |
| 142 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 143 | XGL_VIEWPORT viewport; |
Courtney Goeltzenleuchter | bbe3cc5 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 144 | XGL_RECT scissor; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 145 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 146 | XGL_DYNAMIC_VP_STATE_CREATE_INFO viewportCreate = {}; |
| 147 | viewportCreate.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | bbe3cc5 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 148 | viewportCreate.viewportAndScissorCount = 1; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 149 | viewport.originX = 0; |
| 150 | viewport.originY = 0; |
| 151 | viewport.width = 1.f * width; |
| 152 | viewport.height = 1.f * height; |
| 153 | viewport.minDepth = 0.f; |
| 154 | viewport.maxDepth = 1.f; |
Courtney Goeltzenleuchter | bbe3cc5 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 155 | scissor.extent.width = width; |
| 156 | scissor.extent.height = height; |
| 157 | scissor.offset.x = 0; |
| 158 | scissor.offset.y = 0; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 159 | viewportCreate.pViewports = &viewport; |
Courtney Goeltzenleuchter | bbe3cc5 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 160 | viewportCreate.pScissors = &scissor; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 161 | |
| 162 | err = xglCreateDynamicViewportState( device(), &viewportCreate, &m_stateViewport ); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 163 | ASSERT_XGL_SUCCESS( err ); |
| 164 | m_width = width; |
| 165 | m_height = height; |
| 166 | } |
| 167 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 168 | void XglRenderFramework::InitViewport() |
| 169 | { |
| 170 | InitViewport(m_width, m_height); |
| 171 | } |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 172 | void XglRenderFramework::InitRenderTarget() |
| 173 | { |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 174 | |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 175 | InitRenderTarget(1); |
| 176 | } |
| 177 | |
| 178 | void XglRenderFramework::InitRenderTarget(uint32_t targets) |
| 179 | { |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 180 | InitRenderTarget(targets, NULL); |
| 181 | } |
| 182 | |
| 183 | void XglRenderFramework::InitRenderTarget(XGL_DEPTH_STENCIL_BIND_INFO *dsBinding) |
| 184 | { |
| 185 | InitRenderTarget(1, dsBinding); |
| 186 | } |
| 187 | |
| 188 | void XglRenderFramework::InitRenderTarget(uint32_t targets, XGL_DEPTH_STENCIL_BIND_INFO *dsBinding) |
| 189 | { |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 190 | std::vector<XGL_ATTACHMENT_LOAD_OP> load_ops; |
| 191 | std::vector<XGL_ATTACHMENT_STORE_OP> store_ops; |
| 192 | std::vector<XGL_CLEAR_COLOR> clear_colors; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 193 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 194 | uint32_t i; |
Chia-I Wu | ecebf75 | 2014-12-05 10:45:15 +0800 | [diff] [blame] | 195 | |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 196 | for (i = 0; i < targets; i++) { |
Chia-I Wu | f50ee21 | 2014-12-29 14:31:52 +0800 | [diff] [blame] | 197 | XglImage *img = new XglImage(m_device); |
| 198 | img->init(m_width, m_height, m_render_target_fmt, |
| 199 | XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | |
| 200 | XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 201 | m_colorBindings[i].view = img->targetView(); |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 202 | m_colorBindings[i].layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 203 | m_renderTargets.push_back(img); |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 204 | load_ops.push_back(XGL_ATTACHMENT_LOAD_OP_LOAD); |
| 205 | store_ops.push_back(XGL_ATTACHMENT_STORE_OP_STORE); |
| 206 | clear_colors.push_back(m_clear_color); |
Chia-I Wu | ecebf75 | 2014-12-05 10:45:15 +0800 | [diff] [blame] | 207 | } |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 208 | // Create Framebuffer and RenderPass with color attachments and any depth/stencil attachment |
Tony Barbour | bdf0a31 | 2015-04-01 17:10:07 -0600 | [diff] [blame] | 209 | XGL_FRAMEBUFFER_CREATE_INFO fb_info = {}; |
| 210 | fb_info.sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
| 211 | fb_info.pNext = NULL; |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 212 | fb_info.colorAttachmentCount = m_renderTargets.size(); |
Tony Barbour | bdf0a31 | 2015-04-01 17:10:07 -0600 | [diff] [blame] | 213 | fb_info.pColorAttachments = m_colorBindings; |
| 214 | fb_info.pDepthStencilAttachment = dsBinding; |
| 215 | fb_info.sampleCount = 1; |
| 216 | fb_info.width = (uint32_t)m_width; |
| 217 | fb_info.height = (uint32_t)m_height; |
| 218 | fb_info.layers = 1; |
| 219 | |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 220 | xglCreateFramebuffer(device(), &fb_info, &m_framebuffer); |
| 221 | |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 222 | XGL_RENDER_PASS_CREATE_INFO rp_info = {}; |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 223 | rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; |
| 224 | rp_info.renderArea.extent.width = m_width; |
| 225 | rp_info.renderArea.extent.height = m_height; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 226 | |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 227 | rp_info.colorAttachmentCount = m_renderTargets.size(); |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 228 | rp_info.pColorFormats = &m_render_target_fmt; |
| 229 | rp_info.pColorLayouts = &m_colorBindings[0].layout; |
| 230 | rp_info.pColorLoadOps = &load_ops[0]; |
| 231 | rp_info.pColorStoreOps = &store_ops[0]; |
| 232 | rp_info.pColorLoadClearValues = &clear_colors[0]; |
| 233 | rp_info.depthStencilFormat = m_depth_stencil_fmt; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 234 | if (dsBinding) { |
| 235 | rp_info.depthStencilLayout = dsBinding->layout; |
| 236 | } |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 237 | rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD; |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 238 | rp_info.depthLoadClearValue = m_depth_clear_color; |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 239 | rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_STORE; |
| 240 | rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_LOAD; |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 241 | rp_info.stencilLoadClearValue = m_stencil_clear_color; |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 242 | rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_STORE; |
| 243 | xglCreateRenderPass(device(), &rp_info, &m_renderPass); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 244 | } |
| 245 | |
Mark Lobodzinski | c52b775 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 246 | |
| 247 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 248 | XglDevice::XglDevice(uint32_t id, XGL_PHYSICAL_GPU obj) : |
Chia-I Wu | fb1459b | 2014-12-29 15:23:20 +0800 | [diff] [blame] | 249 | xgl_testing::Device(obj), id(id) |
| 250 | { |
| 251 | init(); |
| 252 | |
| 253 | props = gpu().properties(); |
| 254 | queue_props = &gpu().queue_properties()[0]; |
| 255 | } |
| 256 | |
| 257 | void XglDevice::get_device_queue() |
| 258 | { |
| 259 | ASSERT_NE(true, graphics_queues().empty()); |
| 260 | m_queue = graphics_queues()[0]->obj(); |
| 261 | } |
| 262 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 263 | XglDescriptorSetObj::XglDescriptorSetObj(XglDevice *device) |
| 264 | { |
| 265 | m_device = device; |
| 266 | m_nextSlot = 0; |
| 267 | |
| 268 | } |
| 269 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 270 | XglDescriptorSetObj::~XglDescriptorSetObj() |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 271 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 272 | delete m_set; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 273 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 274 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 275 | int XglDescriptorSetObj::AppendDummy() |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 276 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 277 | /* request a descriptor but do not update it */ |
| 278 | XGL_DESCRIPTOR_TYPE_COUNT tc = {}; |
Chia-I Wu | 47a5d6f | 2015-03-26 15:26:05 +0800 | [diff] [blame^] | 279 | tc.type = XGL_DESCRIPTOR_TYPE_SHADER_STORAGE_BUFFER; |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 280 | tc.count = 1; |
| 281 | m_type_counts.push_back(tc); |
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 | return m_nextSlot++; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 284 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 285 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 286 | int XglDescriptorSetObj::AppendBuffer(XGL_DESCRIPTOR_TYPE type, XglConstantBufferObj *constantBuffer) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 287 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 288 | XGL_DESCRIPTOR_TYPE_COUNT tc = {}; |
| 289 | tc.type = type; |
| 290 | tc.count = 1; |
| 291 | m_type_counts.push_back(tc); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 292 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 293 | m_bufferInfo.push_back(&constantBuffer->m_bufferViewInfo); |
| 294 | |
| 295 | m_updateBuffers.push_back(xgl_testing::DescriptorSet::update(type, m_nextSlot, 1, |
| 296 | (const XGL_BUFFER_VIEW_ATTACH_INFO **) NULL)); |
| 297 | |
| 298 | return m_nextSlot++; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 299 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 300 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 301 | int XglDescriptorSetObj::AppendSamplerTexture( XglSamplerObj* sampler, XglTextureObj* texture) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 302 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 303 | XGL_DESCRIPTOR_TYPE_COUNT tc = {}; |
| 304 | tc.type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE; |
| 305 | tc.count = 1; |
| 306 | m_type_counts.push_back(tc); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 307 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 308 | XGL_SAMPLER_IMAGE_VIEW_INFO tmp = {}; |
| 309 | tmp.pSampler = sampler->obj(); |
| 310 | tmp.pImageView = &texture->m_textureViewInfo; |
| 311 | m_samplerTextureInfo.push_back(tmp); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 312 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 313 | m_updateSamplerTextures.push_back(xgl_testing::DescriptorSet::update(m_nextSlot, 1, |
| 314 | (const XGL_SAMPLER_IMAGE_VIEW_INFO *) NULL)); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 315 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 316 | return m_nextSlot++; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 317 | } |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 318 | |
| 319 | XGL_DESCRIPTOR_SET_LAYOUT XglDescriptorSetObj::GetLayout() |
Tony Barbour | b5f4d08 | 2014-12-17 10:54:03 -0700 | [diff] [blame] | 320 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 321 | return m_layout.obj(); |
Tony Barbour | b5f4d08 | 2014-12-17 10:54:03 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | XGL_DESCRIPTOR_SET XglDescriptorSetObj::GetDescriptorSetHandle() |
| 325 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 326 | return m_set->obj(); |
Tony Barbour | b5f4d08 | 2014-12-17 10:54:03 -0700 | [diff] [blame] | 327 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 328 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 329 | void XglDescriptorSetObj::CreateXGLDescriptorSet(XglCommandBufferObj *cmdBuffer) |
Tony Barbour | 824b771 | 2014-12-18 17:06:21 -0700 | [diff] [blame] | 330 | { |
Chia-I Wu | 985ba16 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 331 | // create XGL_DESCRIPTOR_POOL |
| 332 | XGL_DESCRIPTOR_POOL_CREATE_INFO pool = {}; |
| 333 | pool.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; |
| 334 | pool.count = m_type_counts.size(); |
| 335 | pool.pTypeCount = &m_type_counts[0]; |
| 336 | init(*m_device, XGL_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, pool); |
Tony Barbour | 824b771 | 2014-12-18 17:06:21 -0700 | [diff] [blame] | 337 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 338 | // create XGL_DESCRIPTOR_SET_LAYOUT |
Chia-I Wu | b41393e | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 339 | vector<XGL_DESCRIPTOR_SET_LAYOUT_BINDING> bindings; |
| 340 | bindings.resize(m_type_counts.size()); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 341 | for (int i = 0; i < m_type_counts.size(); i++) { |
Chia-I Wu | b41393e | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 342 | bindings[i].descriptorType = m_type_counts[i].type; |
| 343 | bindings[i].count = m_type_counts[i].count; |
| 344 | bindings[i].stageFlags = XGL_SHADER_STAGE_FLAGS_ALL; |
| 345 | bindings[i].immutableSampler = XGL_NULL_HANDLE; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 346 | } |
Chia-I Wu | ac0f1e7 | 2014-12-28 22:32:36 +0800 | [diff] [blame] | 347 | |
Chia-I Wu | b41393e | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 348 | // create XGL_DESCRIPTOR_SET_LAYOUT |
| 349 | XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO layout = {}; |
| 350 | layout.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
| 351 | layout.count = bindings.size(); |
| 352 | layout.pBinding = &bindings[0]; |
| 353 | |
| 354 | m_layout.init(*m_device, 0, layout); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 355 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 356 | // create XGL_DESCRIPTOR_SET |
| 357 | m_set = alloc_sets(XGL_DESCRIPTOR_SET_USAGE_STATIC, m_layout); |
| 358 | |
| 359 | // build the update chain |
| 360 | for (int i = 0; i < m_updateBuffers.size(); i++) { |
| 361 | m_updateBuffers[i].pBufferViews = &m_bufferInfo[i]; |
| 362 | |
| 363 | if (i < m_updateBuffers.size() - 1) |
| 364 | m_updateBuffers[i].pNext = &m_updateBuffers[i + 1]; |
| 365 | else if (m_updateSamplerTextures.empty()) |
| 366 | m_updateBuffers[i].pNext = NULL; |
| 367 | else |
| 368 | m_updateBuffers[i].pNext = &m_updateSamplerTextures[0]; |
| 369 | } |
| 370 | for (int i = 0; i < m_updateSamplerTextures.size(); i++) { |
| 371 | m_updateSamplerTextures[i].pSamplerImageViews = &m_samplerTextureInfo[i]; |
| 372 | |
| 373 | if (i < m_updateSamplerTextures.size() - 1) |
| 374 | m_updateSamplerTextures[i].pNext = &m_updateSamplerTextures[i + 1]; |
| 375 | else |
| 376 | m_updateSamplerTextures[i].pNext = NULL; |
| 377 | } |
| 378 | const void *chain = (!m_updateBuffers.empty()) ? (const void *) &m_updateBuffers[0] : |
| 379 | (!m_updateSamplerTextures.empty()) ? (const void *) &m_updateSamplerTextures[0] : |
| 380 | NULL; |
| 381 | |
| 382 | // do the updates |
Chia-I Wu | 985ba16 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 383 | m_device->begin_descriptor_pool_update(XGL_DESCRIPTOR_UPDATE_MODE_FASTEST); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 384 | clear_sets(*m_set); |
| 385 | m_set->update(chain); |
Chia-I Wu | 985ba16 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 386 | m_device->end_descriptor_pool_update(*cmdBuffer); |
Tony Barbour | 25ef8a6 | 2014-12-03 13:59:18 -0700 | [diff] [blame] | 387 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 388 | |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 389 | XglImage::XglImage(XglDevice *dev) |
| 390 | { |
| 391 | m_device = dev; |
| 392 | m_imageInfo.view = XGL_NULL_HANDLE; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 393 | m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL; |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 394 | } |
| 395 | |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 396 | static bool IsCompatible(XGL_FLAGS usage, XGL_FLAGS features) |
| 397 | { |
| 398 | if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) && |
| 399 | !(features & XGL_FORMAT_IMAGE_SHADER_READ_BIT)) |
| 400 | return false; |
| 401 | if ((usage & XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT) && |
| 402 | !(features & XGL_FORMAT_IMAGE_SHADER_WRITE_BIT)) |
| 403 | return false; |
| 404 | return true; |
| 405 | } |
| 406 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 407 | void XglImage::init(uint32_t w, uint32_t h, |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 408 | XGL_FORMAT fmt, XGL_FLAGS usage, |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 409 | XGL_IMAGE_TILING requested_tiling) |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 410 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 411 | uint32_t mipCount; |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 412 | XGL_FORMAT_PROPERTIES image_fmt; |
| 413 | XGL_IMAGE_TILING tiling; |
| 414 | XGL_RESULT err; |
| 415 | size_t size; |
| 416 | |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 417 | mipCount = 0; |
| 418 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 419 | uint32_t _w = w; |
| 420 | uint32_t _h = h; |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 421 | while( ( _w > 0 ) || ( _h > 0 ) ) |
| 422 | { |
| 423 | _w >>= 1; |
| 424 | _h >>= 1; |
| 425 | mipCount++; |
| 426 | } |
| 427 | |
Tony Barbour | 579f780 | 2015-04-03 15:11:43 -0600 | [diff] [blame] | 428 | size = sizeof(image_fmt); |
| 429 | err = xglGetFormatInfo(m_device->obj(), fmt, |
| 430 | XGL_INFO_TYPE_FORMAT_PROPERTIES, |
| 431 | &size, &image_fmt); |
| 432 | ASSERT_XGL_SUCCESS(err); |
| 433 | |
| 434 | if (requested_tiling == XGL_LINEAR_TILING) { |
| 435 | if (IsCompatible(usage, image_fmt.linearTilingFeatures)) { |
| 436 | tiling = XGL_LINEAR_TILING; |
| 437 | } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) { |
| 438 | tiling = XGL_OPTIMAL_TILING; |
| 439 | } else { |
| 440 | ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration"; |
| 441 | } |
| 442 | } else if (IsCompatible(usage, image_fmt.optimalTilingFeatures)) { |
| 443 | tiling = XGL_OPTIMAL_TILING; |
| 444 | } else if (IsCompatible(usage, image_fmt.linearTilingFeatures)) { |
| 445 | tiling = XGL_LINEAR_TILING; |
| 446 | } else { |
| 447 | ASSERT_TRUE(false) << "Error: Cannot find requested tiling configuration"; |
| 448 | } |
| 449 | |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 450 | XGL_IMAGE_CREATE_INFO imageCreateInfo = xgl_testing::Image::create_info(); |
| 451 | imageCreateInfo.imageType = XGL_IMAGE_2D; |
| 452 | imageCreateInfo.format = fmt; |
| 453 | imageCreateInfo.extent.width = w; |
| 454 | imageCreateInfo.extent.height = h; |
| 455 | imageCreateInfo.mipLevels = mipCount; |
| 456 | imageCreateInfo.tiling = tiling; |
| 457 | |
| 458 | imageCreateInfo.usage = usage; |
| 459 | |
| 460 | xgl_testing::Image::init(*m_device, imageCreateInfo); |
| 461 | |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 462 | m_imageInfo.layout = XGL_IMAGE_LAYOUT_GENERAL; |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 463 | } |
| 464 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 465 | XGL_RESULT XglImage::MapMemory(void** ptr) |
Chia-I Wu | a6bc0ce | 2014-12-29 14:38:28 +0800 | [diff] [blame] | 466 | { |
| 467 | *ptr = map(); |
| 468 | return (*ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN; |
| 469 | } |
| 470 | |
| 471 | XGL_RESULT XglImage::UnmapMemory() |
| 472 | { |
| 473 | unmap(); |
| 474 | return XGL_SUCCESS; |
| 475 | } |
| 476 | |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 477 | XGL_RESULT XglImage::CopyImage(XglImage &fromImage) |
| 478 | { |
| 479 | XGL_RESULT err; |
| 480 | |
| 481 | XGL_CMD_BUFFER cmd_buf; |
| 482 | XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {}; |
| 483 | cmd_buf_create_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO; |
| 484 | cmd_buf_create_info.pNext = NULL; |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 485 | cmd_buf_create_info.queueNodeIndex = m_device->graphics_queue_node_index_; |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 486 | cmd_buf_create_info.flags = 0; |
| 487 | |
| 488 | err = xglCreateCommandBuffer(m_device->device(), &cmd_buf_create_info, &cmd_buf); |
| 489 | assert(!err); |
| 490 | |
| 491 | /* Copy staging texture to usable texture */ |
| 492 | XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {}; |
| 493 | cmd_buf_begin_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO; |
| 494 | cmd_buf_begin_info.pNext = NULL; |
| 495 | cmd_buf_begin_info.flags = 0; |
| 496 | |
| 497 | err = xglResetCommandBuffer(cmd_buf); |
| 498 | assert(!err); |
| 499 | |
| 500 | err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_begin_info); |
| 501 | assert(!err); |
| 502 | |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 503 | XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {}; |
| 504 | image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 505 | image_memory_barrier.pNext = NULL; |
| 506 | image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT; |
| 507 | image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT; |
| 508 | image_memory_barrier.oldLayout = fromImage.layout(); |
| 509 | image_memory_barrier.newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL; |
| 510 | image_memory_barrier.image = fromImage.obj(); |
| 511 | image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 512 | image_memory_barrier.subresourceRange.baseMipLevel = 0; |
| 513 | image_memory_barrier.subresourceRange.mipLevels = 1; |
| 514 | image_memory_barrier.subresourceRange.baseArraySlice = 0; |
| 515 | image_memory_barrier.subresourceRange.arraySize = 0; |
| 516 | |
| 517 | XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier; |
| 518 | |
| 519 | XGL_PIPE_EVENT pipe_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE }; |
| 520 | XGL_PIPELINE_BARRIER pipeline_barrier; |
| 521 | pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER; |
| 522 | pipeline_barrier.pNext = NULL; |
| 523 | pipeline_barrier.eventCount = 1; |
| 524 | pipeline_barrier.pEvents = pipe_events; |
| 525 | pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE; |
| 526 | pipeline_barrier.memBarrierCount = 1; |
| 527 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
| 528 | |
| 529 | // write barrier to the command buffer |
| 530 | xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier); |
| 531 | |
| 532 | image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 533 | image_memory_barrier.pNext = NULL; |
| 534 | image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT; |
| 535 | image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT; |
| 536 | image_memory_barrier.oldLayout = this->layout(); |
| 537 | image_memory_barrier.newLayout = XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL; |
| 538 | image_memory_barrier.image = this->obj(); |
| 539 | image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 540 | image_memory_barrier.subresourceRange.baseMipLevel = 0; |
| 541 | image_memory_barrier.subresourceRange.mipLevels = 1; |
| 542 | image_memory_barrier.subresourceRange.baseArraySlice = 0; |
| 543 | image_memory_barrier.subresourceRange.arraySize = 0; |
| 544 | |
| 545 | pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER; |
| 546 | pipeline_barrier.pNext = NULL; |
| 547 | pipeline_barrier.eventCount = 1; |
| 548 | pipeline_barrier.pEvents = pipe_events; |
| 549 | pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE; |
| 550 | pipeline_barrier.memBarrierCount = 1; |
| 551 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
| 552 | |
| 553 | // write barrier to the command buffer |
| 554 | xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier); |
| 555 | |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 556 | XGL_IMAGE_COPY copy_region = {}; |
| 557 | copy_region.srcSubresource.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 558 | copy_region.srcSubresource.arraySlice = 0; |
| 559 | copy_region.srcSubresource.mipLevel = 0; |
| 560 | copy_region.srcOffset.x = 0; |
| 561 | copy_region.srcOffset.y = 0; |
| 562 | copy_region.srcOffset.z = 0; |
| 563 | copy_region.destSubresource.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 564 | copy_region.destSubresource.arraySlice = 0; |
| 565 | copy_region.destSubresource.mipLevel = 0; |
| 566 | copy_region.destOffset.x = 0; |
| 567 | copy_region.destOffset.y = 0; |
| 568 | copy_region.destOffset.z = 0; |
| 569 | copy_region.extent = fromImage.extent(); |
| 570 | |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 571 | xglCmdCopyImage(cmd_buf, fromImage.obj(), fromImage.layout(), |
| 572 | obj(), XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
| 573 | 1, ©_region); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 574 | |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 575 | image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 576 | image_memory_barrier.pNext = NULL; |
| 577 | image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT; |
| 578 | image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 579 | image_memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL; |
| 580 | image_memory_barrier.newLayout = fromImage.layout(); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 581 | image_memory_barrier.image = fromImage.obj(); |
| 582 | image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 583 | image_memory_barrier.subresourceRange.baseMipLevel = 0; |
| 584 | image_memory_barrier.subresourceRange.mipLevels = 1; |
| 585 | image_memory_barrier.subresourceRange.baseArraySlice = 0; |
| 586 | image_memory_barrier.subresourceRange.arraySize = 0; |
| 587 | |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 588 | pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER; |
| 589 | pipeline_barrier.pNext = NULL; |
| 590 | pipeline_barrier.eventCount = 1; |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 591 | pipeline_barrier.pEvents = pipe_events; |
| 592 | pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE; |
| 593 | pipeline_barrier.memBarrierCount = 1; |
| 594 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
| 595 | |
| 596 | // write barrier to the command buffer |
| 597 | xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier); |
| 598 | |
| 599 | image_memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 600 | image_memory_barrier.pNext = NULL; |
| 601 | image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT; |
| 602 | image_memory_barrier.inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT; |
| 603 | image_memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL; |
| 604 | image_memory_barrier.newLayout = this->layout(); |
| 605 | image_memory_barrier.image = this->obj(); |
| 606 | image_memory_barrier.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 607 | image_memory_barrier.subresourceRange.baseMipLevel = 0; |
| 608 | image_memory_barrier.subresourceRange.mipLevels = 1; |
| 609 | image_memory_barrier.subresourceRange.baseArraySlice = 0; |
| 610 | image_memory_barrier.subresourceRange.arraySize = 0; |
| 611 | |
| 612 | pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER; |
| 613 | pipeline_barrier.pNext = NULL; |
| 614 | pipeline_barrier.eventCount = 1; |
| 615 | pipeline_barrier.pEvents = pipe_events; |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 616 | pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE; |
| 617 | pipeline_barrier.memBarrierCount = 1; |
| 618 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
| 619 | |
| 620 | // write barrier to the command buffer |
| 621 | xglCmdPipelineBarrier(cmd_buf, &pipeline_barrier); |
| 622 | |
| 623 | err = xglEndCommandBuffer(cmd_buf); |
| 624 | assert(!err); |
| 625 | |
| 626 | const XGL_CMD_BUFFER cmd_bufs[] = { cmd_buf }; |
| 627 | XGL_MEMORY_REF mem_refs[16]; |
| 628 | uint32_t num_refs = 0; |
| 629 | const std::vector<XGL_GPU_MEMORY> from_mems = fromImage.memories(); |
| 630 | const std::vector<XGL_GPU_MEMORY> to_mems = memories(); |
| 631 | |
| 632 | for (uint32_t j = 0; j < from_mems.size(); j++) { |
| 633 | mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT; |
| 634 | mem_refs[num_refs].mem = from_mems[j]; |
| 635 | num_refs++; |
| 636 | assert(num_refs < 16); |
| 637 | } |
| 638 | |
| 639 | for (uint32_t j = 0; j < to_mems.size(); j++) { |
| 640 | mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT; |
| 641 | mem_refs[num_refs].mem = to_mems[j]; |
| 642 | num_refs++; |
| 643 | assert(num_refs < 16); |
| 644 | } |
| 645 | |
| 646 | err = xglQueueSubmit(m_device->m_queue, 1, cmd_bufs, |
| 647 | num_refs, mem_refs, XGL_NULL_HANDLE); |
| 648 | assert(!err); |
| 649 | |
| 650 | err = xglQueueWaitIdle(m_device->m_queue); |
| 651 | assert(!err); |
| 652 | |
| 653 | xglDestroyObject(cmd_buf); |
| 654 | |
| 655 | return XGL_SUCCESS; |
| 656 | } |
| 657 | |
Tony Barbour | ebc093f | 2015-04-01 16:38:10 -0600 | [diff] [blame] | 658 | XglTextureObj::XglTextureObj(XglDevice *device, uint32_t *colors) |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 659 | :XglImage(device) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 660 | { |
| 661 | m_device = device; |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 662 | const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM; |
Tony Barbour | ebc093f | 2015-04-01 16:38:10 -0600 | [diff] [blame] | 663 | uint32_t tex_colors[2] = { 0xffff0000, 0xff00ff00 }; |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 664 | void *data; |
| 665 | int32_t x, y; |
| 666 | XglImage stagingImage(device); |
| 667 | |
| 668 | stagingImage.init(16, 16, tex_format, 0, XGL_LINEAR_TILING); |
| 669 | XGL_SUBRESOURCE_LAYOUT layout = stagingImage.subresource_layout(subresource(XGL_IMAGE_ASPECT_COLOR, 0, 0)); |
Tony Barbour | ebc093f | 2015-04-01 16:38:10 -0600 | [diff] [blame] | 670 | |
| 671 | if (colors == NULL) |
| 672 | colors = tex_colors; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 673 | |
| 674 | memset(&m_textureViewInfo,0,sizeof(m_textureViewInfo)); |
| 675 | |
| 676 | m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO; |
| 677 | |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 678 | XGL_IMAGE_VIEW_CREATE_INFO view = {}; |
Tony Barbour | bdf0a31 | 2015-04-01 17:10:07 -0600 | [diff] [blame] | 679 | view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 680 | view.pNext = NULL; |
| 681 | view.image = XGL_NULL_HANDLE; |
| 682 | view.viewType = XGL_IMAGE_VIEW_2D; |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 683 | view.format = tex_format; |
Tony Barbour | bdf0a31 | 2015-04-01 17:10:07 -0600 | [diff] [blame] | 684 | view.channels.r = XGL_CHANNEL_SWIZZLE_R; |
| 685 | view.channels.g = XGL_CHANNEL_SWIZZLE_G; |
| 686 | view.channels.b = XGL_CHANNEL_SWIZZLE_B; |
| 687 | view.channels.a = XGL_CHANNEL_SWIZZLE_A; |
| 688 | view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 689 | view.subresourceRange.baseMipLevel = 0; |
| 690 | view.subresourceRange.mipLevels = 1; |
| 691 | view.subresourceRange.baseArraySlice = 0; |
| 692 | view.subresourceRange.arraySize = 1; |
| 693 | view.minLod = 0.0f; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 694 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 695 | /* create image */ |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 696 | init(16, 16, tex_format, XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, XGL_OPTIMAL_TILING); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 697 | |
| 698 | /* create image view */ |
Chia-I Wu | 13a3aa8 | 2014-12-28 15:55:09 +0800 | [diff] [blame] | 699 | view.image = obj(); |
| 700 | m_textureView.init(*m_device, view); |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 701 | m_textureViewInfo.view = m_textureView.obj(); |
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 | data = stagingImage.map(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 704 | |
Chia-I Wu | 13a3aa8 | 2014-12-28 15:55:09 +0800 | [diff] [blame] | 705 | for (y = 0; y < extent().height; y++) { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 706 | uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y); |
Chia-I Wu | 13a3aa8 | 2014-12-28 15:55:09 +0800 | [diff] [blame] | 707 | for (x = 0; x < extent().width; x++) |
Tony Barbour | ebc093f | 2015-04-01 16:38:10 -0600 | [diff] [blame] | 708 | row[x] = colors[(x & 1) ^ (y & 1)]; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 709 | } |
Tony Barbour | 5dc515d | 2015-04-01 17:47:06 -0600 | [diff] [blame] | 710 | stagingImage.unmap(); |
| 711 | XglImage::CopyImage(stagingImage); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 712 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 713 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 714 | XglSamplerObj::XglSamplerObj(XglDevice *device) |
| 715 | { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 716 | m_device = device; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 717 | |
Chia-I Wu | e9864b5 | 2014-12-28 16:32:24 +0800 | [diff] [blame] | 718 | XGL_SAMPLER_CREATE_INFO samplerCreateInfo; |
| 719 | memset(&samplerCreateInfo,0,sizeof(samplerCreateInfo)); |
| 720 | samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; |
| 721 | samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST; |
| 722 | samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST; |
| 723 | samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE; |
| 724 | samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP; |
| 725 | samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP; |
| 726 | samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP; |
| 727 | samplerCreateInfo.mipLodBias = 0.0; |
| 728 | samplerCreateInfo.maxAnisotropy = 0.0; |
| 729 | samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER; |
| 730 | samplerCreateInfo.minLod = 0.0; |
| 731 | samplerCreateInfo.maxLod = 0.0; |
| 732 | samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 733 | |
Chia-I Wu | e9864b5 | 2014-12-28 16:32:24 +0800 | [diff] [blame] | 734 | init(*m_device, samplerCreateInfo); |
Tony Barbour | f325bf1 | 2014-12-03 15:59:38 -0700 | [diff] [blame] | 735 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 736 | |
Courtney Goeltzenleuchter | 3b0a815 | 2014-12-04 15:18:47 -0700 | [diff] [blame] | 737 | /* |
| 738 | * Basic ConstantBuffer constructor. Then use create methods to fill in the details. |
| 739 | */ |
| 740 | XglConstantBufferObj::XglConstantBufferObj(XglDevice *device) |
| 741 | { |
| 742 | m_device = device; |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 743 | m_commandBuffer = 0; |
Courtney Goeltzenleuchter | 3b0a815 | 2014-12-04 15:18:47 -0700 | [diff] [blame] | 744 | |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 745 | memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo)); |
Courtney Goeltzenleuchter | 3b0a815 | 2014-12-04 15:18:47 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Tony Barbour | 044703b | 2015-03-26 12:37:52 -0600 | [diff] [blame] | 748 | XglConstantBufferObj::~XglConstantBufferObj() |
| 749 | { |
| 750 | if (m_commandBuffer) { |
| 751 | delete m_commandBuffer; |
| 752 | } |
| 753 | } |
| 754 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 755 | XglConstantBufferObj::XglConstantBufferObj(XglDevice *device, int constantCount, int constantSize, const void* data) |
| 756 | { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 757 | m_device = device; |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 758 | m_commandBuffer = 0; |
| 759 | |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 760 | memset(&m_bufferViewInfo,0,sizeof(m_bufferViewInfo)); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 761 | m_numVertices = constantCount; |
| 762 | m_stride = constantSize; |
| 763 | |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 764 | const size_t allocationSize = constantCount * constantSize; |
| 765 | init(*m_device, allocationSize); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 766 | |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 767 | void *pData = map(); |
| 768 | memcpy(pData, data, allocationSize); |
| 769 | unmap(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 770 | |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 771 | // set up the buffer view for the constant buffer |
| 772 | XGL_BUFFER_VIEW_CREATE_INFO view_info = {}; |
| 773 | view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO; |
| 774 | view_info.buffer = obj(); |
Chia-I Wu | bb0c8d2 | 2015-01-16 22:31:25 +0800 | [diff] [blame] | 775 | view_info.viewType = XGL_BUFFER_VIEW_RAW; |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 776 | view_info.offset = 0; |
| 777 | view_info.range = allocationSize; |
| 778 | m_bufferView.init(*m_device, view_info); |
| 779 | |
| 780 | this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO; |
| 781 | this->m_bufferViewInfo.view = m_bufferView.obj(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 782 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 783 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 784 | void XglConstantBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset, uint32_t binding) |
Courtney Goeltzenleuchter | 3764030 | 2014-12-04 15:26:56 -0700 | [diff] [blame] | 785 | { |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 786 | xglCmdBindVertexBuffer(cmdBuffer, obj(), offset, binding); |
Courtney Goeltzenleuchter | 3764030 | 2014-12-04 15:26:56 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 790 | void XglConstantBufferObj::BufferMemoryBarrier( |
| 791 | XGL_FLAGS outputMask /*= |
| 792 | XGL_MEMORY_OUTPUT_CPU_WRITE_BIT | |
| 793 | XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT | |
| 794 | XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT | |
| 795 | XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 796 | XGL_MEMORY_OUTPUT_COPY_BIT*/, |
| 797 | XGL_FLAGS inputMask /*= |
| 798 | XGL_MEMORY_INPUT_CPU_READ_BIT | |
| 799 | XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT | |
| 800 | XGL_MEMORY_INPUT_INDEX_FETCH_BIT | |
| 801 | XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT | |
| 802 | XGL_MEMORY_INPUT_UNIFORM_READ_BIT | |
| 803 | XGL_MEMORY_INPUT_SHADER_READ_BIT | |
| 804 | XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT | |
| 805 | XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 806 | XGL_MEMORY_INPUT_COPY_BIT*/) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 807 | { |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 808 | XGL_RESULT err = XGL_SUCCESS; |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 809 | |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 810 | if (!m_commandBuffer) |
| 811 | { |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 812 | m_fence.init(*m_device, xgl_testing::Fence::create_info(0)); |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 813 | |
| 814 | m_commandBuffer = new XglCommandBufferObj(m_device); |
| 815 | |
| 816 | } |
| 817 | else |
| 818 | { |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 819 | m_device->wait(m_fence); |
Tony Barbour | 3842280 | 2014-12-10 14:36:31 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 822 | // open the command buffer |
Tony Barbour | bdf0a31 | 2015-04-01 17:10:07 -0600 | [diff] [blame] | 823 | XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {}; |
| 824 | cmd_buf_info.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO; |
| 825 | cmd_buf_info.pNext = NULL; |
| 826 | cmd_buf_info.flags = 0; |
| 827 | |
Jon Ashburn | c4164b1 | 2014-12-31 17:10:47 -0700 | [diff] [blame] | 828 | err = m_commandBuffer->BeginCommandBuffer(&cmd_buf_info); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 829 | ASSERT_XGL_SUCCESS(err); |
| 830 | |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 831 | XGL_BUFFER_MEMORY_BARRIER memory_barrier = |
| 832 | buffer_memory_barrier(outputMask, inputMask, 0, m_numVertices * m_stride); |
Mark Lobodzinski | 837ef92 | 2015-01-29 14:24:14 -0600 | [diff] [blame] | 833 | XGL_BUFFER_MEMORY_BARRIER *pmemory_barrier = &memory_barrier; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 834 | |
Courtney Goeltzenleuchter | 1e8f3be | 2015-03-24 18:02:34 -0600 | [diff] [blame] | 835 | XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE }; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 836 | XGL_PIPELINE_BARRIER pipeline_barrier = {}; |
| 837 | pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER; |
| 838 | pipeline_barrier.eventCount = 1; |
| 839 | pipeline_barrier.pEvents = set_events; |
| 840 | pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE; |
| 841 | pipeline_barrier.memBarrierCount = 1; |
Mark Lobodzinski | d5d83ed | 2015-02-02 11:55:52 -0600 | [diff] [blame] | 842 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 843 | |
| 844 | // write barrier to the command buffer |
| 845 | m_commandBuffer->PipelineBarrier(&pipeline_barrier); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 846 | |
| 847 | // finish recording the command buffer |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 848 | err = m_commandBuffer->EndCommandBuffer(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 849 | ASSERT_XGL_SUCCESS(err); |
| 850 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 851 | uint32_t numMemRefs=1; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 852 | XGL_MEMORY_REF memRefs; |
| 853 | // this command buffer only uses the vertex buffer memory |
| 854 | memRefs.flags = 0; |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 855 | memRefs.mem = memories()[0]; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 856 | |
| 857 | // submit the command buffer to the universal queue |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 858 | XGL_CMD_BUFFER bufferArray[1]; |
| 859 | bufferArray[0] = m_commandBuffer->GetBufferHandle(); |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 860 | err = xglQueueSubmit( m_device->m_queue, 1, bufferArray, numMemRefs, &memRefs, m_fence.obj() ); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 861 | ASSERT_XGL_SUCCESS(err); |
| 862 | } |
| 863 | |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 864 | XglIndexBufferObj::XglIndexBufferObj(XglDevice *device) |
| 865 | : XglConstantBufferObj(device) |
| 866 | { |
| 867 | |
| 868 | } |
| 869 | |
| 870 | void XglIndexBufferObj::CreateAndInitBuffer(int numIndexes, XGL_INDEX_TYPE indexType, const void* data) |
| 871 | { |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 872 | XGL_FORMAT viewFormat; |
| 873 | |
| 874 | m_numVertices = numIndexes; |
| 875 | m_indexType = indexType; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 876 | switch (indexType) { |
| 877 | case XGL_INDEX_8: |
| 878 | m_stride = 1; |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 879 | viewFormat = XGL_FMT_R8_UINT; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 880 | break; |
| 881 | case XGL_INDEX_16: |
| 882 | m_stride = 2; |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 883 | viewFormat = XGL_FMT_R16_UINT; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 884 | break; |
| 885 | case XGL_INDEX_32: |
| 886 | m_stride = 4; |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 887 | viewFormat = XGL_FMT_R32_UINT; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 888 | break; |
Chia-I Wu | b4c2aa4 | 2014-12-15 23:50:11 +0800 | [diff] [blame] | 889 | default: |
| 890 | assert(!"unknown index type"); |
| 891 | break; |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 894 | const size_t allocationSize = numIndexes * m_stride; |
| 895 | init(*m_device, allocationSize); |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 896 | |
Chia-I Wu | a07fee6 | 2014-12-28 15:26:08 +0800 | [diff] [blame] | 897 | void *pData = map(); |
| 898 | memcpy(pData, data, allocationSize); |
| 899 | unmap(); |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 900 | |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 901 | // set up the buffer view for the constant buffer |
| 902 | XGL_BUFFER_VIEW_CREATE_INFO view_info = {}; |
| 903 | view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO; |
| 904 | view_info.buffer = obj(); |
| 905 | view_info.viewType = XGL_BUFFER_VIEW_TYPED; |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 906 | view_info.format = viewFormat; |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 907 | view_info.offset = 0; |
| 908 | view_info.range = allocationSize; |
| 909 | m_bufferView.init(*m_device, view_info); |
| 910 | |
| 911 | this->m_bufferViewInfo.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO; |
| 912 | this->m_bufferViewInfo.view = m_bufferView.obj(); |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | void XglIndexBufferObj::Bind(XGL_CMD_BUFFER cmdBuffer, XGL_GPU_SIZE offset) |
| 916 | { |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 917 | xglCmdBindIndexBuffer(cmdBuffer, obj(), offset, m_indexType); |
Courtney Goeltzenleuchter | 8a78593 | 2014-12-04 15:24:05 -0700 | [diff] [blame] | 918 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 919 | |
Tony Barbour | af1f919 | 2014-12-17 10:57:58 -0700 | [diff] [blame] | 920 | XGL_INDEX_TYPE XglIndexBufferObj::GetIndexType() |
| 921 | { |
| 922 | return m_indexType; |
| 923 | } |
| 924 | |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 925 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* XglShaderObj::GetStageCreateInfo() |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 926 | { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 927 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO *stageInfo = (XGL_PIPELINE_SHADER_STAGE_CREATE_INFO*) calloc( 1,sizeof(XGL_PIPELINE_SHADER_STAGE_CREATE_INFO) ); |
| 928 | stageInfo->sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 929 | stageInfo->shader.stage = m_stage; |
Chia-I Wu | babc0fd | 2014-12-29 14:14:03 +0800 | [diff] [blame] | 930 | stageInfo->shader.shader = obj(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 931 | stageInfo->shader.linkConstBufferCount = 0; |
| 932 | stageInfo->shader.pLinkConstBufferInfo = XGL_NULL_HANDLE; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 933 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 934 | return stageInfo; |
| 935 | } |
| 936 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 937 | XglShaderObj::XglShaderObj(XglDevice *device, const char * shader_code, XGL_PIPELINE_SHADER_STAGE stage, XglRenderFramework *framework) |
| 938 | { |
| 939 | XGL_RESULT err = XGL_SUCCESS; |
Cody Northrop | 3bfd27c | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 940 | std::vector<unsigned int> spv; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 941 | XGL_SHADER_CREATE_INFO createInfo; |
| 942 | size_t shader_len; |
| 943 | |
| 944 | m_stage = stage; |
| 945 | m_device = device; |
| 946 | |
| 947 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 948 | createInfo.pNext = NULL; |
| 949 | |
Cody Northrop | 3bfd27c | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 950 | if (!framework->m_use_spv) { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 951 | |
| 952 | shader_len = strlen(shader_code); |
| 953 | createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1; |
| 954 | createInfo.pCode = malloc(createInfo.codeSize); |
| 955 | createInfo.flags = 0; |
| 956 | |
| 957 | /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */ |
Cody Northrop | 3bfd27c | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 958 | ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 959 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 960 | ((uint32_t *) createInfo.pCode)[2] = stage; |
| 961 | memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1); |
| 962 | |
Chia-I Wu | babc0fd | 2014-12-29 14:14:03 +0800 | [diff] [blame] | 963 | err = init_try(*m_device, createInfo); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Cody Northrop | 3bfd27c | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 966 | if (framework->m_use_spv || err) { |
| 967 | std::vector<unsigned int> spv; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 968 | err = XGL_SUCCESS; |
| 969 | |
Cody Northrop | 3bfd27c | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 970 | // Use Reference GLSL to SPV compiler |
| 971 | framework->GLSLtoSPV(stage, shader_code, spv); |
| 972 | createInfo.pCode = spv.data(); |
| 973 | createInfo.codeSize = spv.size() * sizeof(unsigned int); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 974 | createInfo.flags = 0; |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 975 | |
Chia-I Wu | babc0fd | 2014-12-29 14:14:03 +0800 | [diff] [blame] | 976 | init(*m_device, createInfo); |
| 977 | } |
Tony Barbour | f325bf1 | 2014-12-03 15:59:38 -0700 | [diff] [blame] | 978 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 979 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 980 | XglPipelineObj::XglPipelineObj(XglDevice *device) |
| 981 | { |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 982 | m_device = device; |
| 983 | m_vi_state.attributeCount = m_vi_state.bindingCount = 0; |
| 984 | m_vertexBufferCount = 0; |
| 985 | |
| 986 | m_ia_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO; |
| 987 | m_ia_state.pNext = XGL_NULL_HANDLE; |
| 988 | m_ia_state.topology = XGL_TOPOLOGY_TRIANGLE_LIST; |
| 989 | m_ia_state.disableVertexReuse = XGL_FALSE; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 990 | m_ia_state.primitiveRestartEnable = XGL_FALSE; |
| 991 | m_ia_state.primitiveRestartIndex = 0; |
| 992 | |
| 993 | m_rs_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO; |
| 994 | m_rs_state.pNext = &m_ia_state; |
| 995 | m_rs_state.depthClipEnable = XGL_FALSE; |
| 996 | m_rs_state.rasterizerDiscardEnable = XGL_FALSE; |
Chia-I Wu | c8d1ec5 | 2015-03-24 11:01:50 +0800 | [diff] [blame] | 997 | m_rs_state.programPointSize = XGL_FALSE; |
| 998 | m_rs_state.pointOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 999 | m_rs_state.provokingVertex = XGL_PROVOKING_VERTEX_LAST; |
| 1000 | m_rs_state.fillMode = XGL_FILL_SOLID; |
| 1001 | m_rs_state.cullMode = XGL_CULL_NONE; |
| 1002 | m_rs_state.frontFace = XGL_FRONT_FACE_CCW; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1003 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1004 | memset(&m_cb_state,0,sizeof(m_cb_state)); |
| 1005 | m_cb_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO; |
| 1006 | m_cb_state.pNext = &m_rs_state; |
| 1007 | m_cb_state.alphaToCoverageEnable = XGL_FALSE; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1008 | m_cb_state.logicOp = XGL_LOGIC_OP_COPY; |
| 1009 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1010 | m_ms_state.pNext = &m_cb_state; |
| 1011 | m_ms_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO; |
| 1012 | m_ms_state.multisampleEnable = XGL_FALSE; |
| 1013 | m_ms_state.sampleMask = 1; // Do we have to specify MSAA even just to disable it? |
| 1014 | m_ms_state.samples = 1; |
| 1015 | m_ms_state.minSampleShading = 0; |
| 1016 | m_ms_state.sampleShadingEnable = 0; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1017 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1018 | m_ds_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO; |
| 1019 | m_ds_state.pNext = &m_ms_state, |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 1020 | m_ds_state.format = XGL_FMT_D32_SFLOAT; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1021 | m_ds_state.depthTestEnable = XGL_FALSE; |
| 1022 | m_ds_state.depthWriteEnable = XGL_FALSE; |
| 1023 | m_ds_state.depthBoundsEnable = XGL_FALSE; |
| 1024 | m_ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL; |
| 1025 | m_ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP; |
| 1026 | m_ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP; |
| 1027 | m_ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP; |
| 1028 | m_ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS; |
| 1029 | m_ds_state.stencilTestEnable = XGL_FALSE; |
| 1030 | m_ds_state.front = m_ds_state.back; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1031 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1032 | XGL_PIPELINE_CB_ATTACHMENT_STATE att = {}; |
| 1033 | att.blendEnable = XGL_FALSE; |
Tony Barbour | a53a694 | 2015-02-25 11:25:11 -0700 | [diff] [blame] | 1034 | att.format = XGL_FMT_B8G8R8A8_UNORM; |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1035 | att.channelWriteMask = 0xf; |
| 1036 | AddColorAttachment(0, &att); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1037 | |
| 1038 | }; |
| 1039 | |
| 1040 | void XglPipelineObj::AddShader(XglShaderObj* shader) |
| 1041 | { |
| 1042 | m_shaderObjs.push_back(shader); |
| 1043 | } |
| 1044 | |
| 1045 | void XglPipelineObj::AddVertexInputAttribs(XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION* vi_attrib, int count) |
| 1046 | { |
| 1047 | m_vi_state.pVertexAttributeDescriptions = vi_attrib; |
| 1048 | m_vi_state.attributeCount = count; |
| 1049 | } |
| 1050 | |
| 1051 | void XglPipelineObj::AddVertexInputBindings(XGL_VERTEX_INPUT_BINDING_DESCRIPTION* vi_binding, int count) |
| 1052 | { |
| 1053 | m_vi_state.pVertexBindingDescriptions = vi_binding; |
| 1054 | m_vi_state.bindingCount = count; |
| 1055 | } |
| 1056 | |
| 1057 | void XglPipelineObj::AddVertexDataBuffer(XglConstantBufferObj* vertexDataBuffer, int binding) |
| 1058 | { |
| 1059 | m_vertexBufferObjs.push_back(vertexDataBuffer); |
| 1060 | m_vertexBufferBindings.push_back(binding); |
| 1061 | m_vertexBufferCount++; |
| 1062 | } |
| 1063 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1064 | void XglPipelineObj::AddColorAttachment(uint32_t binding, const XGL_PIPELINE_CB_ATTACHMENT_STATE *att) |
Chia-I Wu | ecebf75 | 2014-12-05 10:45:15 +0800 | [diff] [blame] | 1065 | { |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1066 | if (binding+1 > m_colorAttachments.size()) |
| 1067 | { |
| 1068 | m_colorAttachments.resize(binding+1); |
| 1069 | } |
| 1070 | m_colorAttachments[binding] = *att; |
| 1071 | } |
| 1072 | |
| 1073 | void XglPipelineObj::SetDepthStencil(XGL_PIPELINE_DS_STATE_CREATE_INFO *ds_state) |
| 1074 | { |
| 1075 | m_ds_state.format = ds_state->format; |
| 1076 | m_ds_state.depthTestEnable = ds_state->depthTestEnable; |
| 1077 | m_ds_state.depthWriteEnable = ds_state->depthWriteEnable; |
| 1078 | m_ds_state.depthBoundsEnable = ds_state->depthBoundsEnable; |
| 1079 | m_ds_state.depthFunc = ds_state->depthFunc; |
| 1080 | m_ds_state.stencilTestEnable = ds_state->stencilTestEnable; |
| 1081 | m_ds_state.back = ds_state->back; |
| 1082 | m_ds_state.front = ds_state->front; |
Chia-I Wu | ecebf75 | 2014-12-05 10:45:15 +0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1085 | void XglPipelineObj::CreateXGLPipeline(XglDescriptorSetObj *descriptorSet) |
| 1086 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1087 | void* head_ptr = &m_ds_state; |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1088 | XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {}; |
| 1089 | |
| 1090 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo; |
| 1091 | |
| 1092 | for (int i=0; i<m_shaderObjs.size(); i++) |
| 1093 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1094 | shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(); |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1095 | shaderCreateInfo->pNext = head_ptr; |
| 1096 | head_ptr = shaderCreateInfo; |
| 1097 | } |
| 1098 | |
| 1099 | if (m_vi_state.attributeCount && m_vi_state.bindingCount) |
| 1100 | { |
| 1101 | m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO; |
| 1102 | m_vi_state.pNext = head_ptr; |
| 1103 | head_ptr = &m_vi_state; |
| 1104 | } |
| 1105 | |
| 1106 | info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 1107 | info.pNext = head_ptr; |
| 1108 | info.flags = 0; |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1109 | info.lastSetLayout = descriptorSet->GetLayout(); |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1110 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1111 | m_cb_state.attachmentCount = m_colorAttachments.size(); |
| 1112 | m_cb_state.pAttachments = &m_colorAttachments[0]; |
| 1113 | |
Chia-I Wu | 2648d09 | 2014-12-29 14:24:14 +0800 | [diff] [blame] | 1114 | init(*m_device, info); |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1115 | } |
Chia-I Wu | 2648d09 | 2014-12-29 14:24:14 +0800 | [diff] [blame] | 1116 | |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1117 | XGL_PIPELINE XglPipelineObj::GetPipelineHandle() |
| 1118 | { |
Chia-I Wu | 2648d09 | 2014-12-29 14:24:14 +0800 | [diff] [blame] | 1119 | return obj(); |
Tony Barbour | 976e1cf | 2014-12-17 11:57:31 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Tony Barbour | 5420af0 | 2014-12-03 13:58:15 -0700 | [diff] [blame] | 1122 | void XglPipelineObj::BindPipelineCommandBuffer(XGL_CMD_BUFFER m_cmdBuffer, XglDescriptorSetObj *descriptorSet) |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1123 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1124 | void* head_ptr = &m_ds_state; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1125 | XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {}; |
| 1126 | |
| 1127 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO* shaderCreateInfo; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1128 | |
| 1129 | for (int i=0; i<m_shaderObjs.size(); i++) |
| 1130 | { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1131 | shaderCreateInfo = m_shaderObjs[i]->GetStageCreateInfo(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1132 | shaderCreateInfo->pNext = head_ptr; |
| 1133 | head_ptr = shaderCreateInfo; |
| 1134 | } |
| 1135 | |
| 1136 | if (m_vi_state.attributeCount && m_vi_state.bindingCount) |
| 1137 | { |
| 1138 | m_vi_state.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO; |
| 1139 | m_vi_state.pNext = head_ptr; |
| 1140 | head_ptr = &m_vi_state; |
| 1141 | } |
| 1142 | |
| 1143 | info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 1144 | info.pNext = head_ptr; |
| 1145 | info.flags = 0; |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1146 | info.lastSetLayout = descriptorSet->GetLayout(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1147 | |
Chia-I Wu | 2648d09 | 2014-12-29 14:24:14 +0800 | [diff] [blame] | 1148 | init(*m_device, info); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1149 | |
Chia-I Wu | 2648d09 | 2014-12-29 14:24:14 +0800 | [diff] [blame] | 1150 | xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, obj() ); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1151 | |
| 1152 | |
| 1153 | for (int i=0; i < m_vertexBufferCount; i++) |
| 1154 | { |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1155 | m_vertexBufferObjs[i]->Bind(m_cmdBuffer, 0, m_vertexBufferBindings[i]); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1156 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1157 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 1158 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1159 | XglMemoryRefManager::XglMemoryRefManager() { |
| 1160 | |
| 1161 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 1162 | |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1163 | void XglMemoryRefManager::AddMemoryRef(xgl_testing::Object *xglObject) { |
| 1164 | const std::vector<XGL_GPU_MEMORY> mems = xglObject->memories(); |
| 1165 | for (size_t i = 0; i < mems.size(); i++) { |
| 1166 | m_bufferObjs.push_back(mems[i]); |
| 1167 | } |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1168 | } |
Tony Barbour | 82c3952 | 2014-12-04 14:33:33 -0700 | [diff] [blame] | 1169 | |
Mark Lobodzinski | c52b775 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 1170 | void XglMemoryRefManager::AddMemoryRef(XGL_GPU_MEMORY *mem, uint32_t refCount) { |
| 1171 | for (size_t i = 0; i < refCount; i++) { |
| 1172 | m_bufferObjs.push_back(mem[i]); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | void XglMemoryRefManager::AddRTMemoryRefs(vector<XglImage*>images, uint32_t rtCount) { |
| 1177 | for (uint32_t i = 0; i < rtCount; i++) { |
| 1178 | const std::vector<XGL_GPU_MEMORY> mems = images[i]->memories(); |
| 1179 | if (!mems.empty()) |
| 1180 | m_bufferObjs.push_back(mems[0]); |
| 1181 | } |
| 1182 | } |
| 1183 | |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1184 | XGL_MEMORY_REF* XglMemoryRefManager::GetMemoryRefList() { |
| 1185 | |
| 1186 | XGL_MEMORY_REF *localRefs; |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1187 | uint32_t numRefs=m_bufferObjs.size(); |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1188 | |
| 1189 | if (numRefs <= 0) |
| 1190 | return NULL; |
| 1191 | |
| 1192 | localRefs = (XGL_MEMORY_REF*) malloc( numRefs * sizeof(XGL_MEMORY_REF) ); |
| 1193 | for (int i=0; i<numRefs; i++) |
| 1194 | { |
| 1195 | localRefs[i].flags = 0; |
Chia-I Wu | 283d7a6 | 2014-12-28 15:43:42 +0800 | [diff] [blame] | 1196 | localRefs[i].mem = m_bufferObjs[i]; |
Tony Barbour | e2c58df | 2014-11-25 13:18:32 -0700 | [diff] [blame] | 1197 | } |
| 1198 | return localRefs; |
| 1199 | } |
| 1200 | int XglMemoryRefManager::GetNumRefs() { |
| 1201 | return m_bufferObjs.size(); |
| 1202 | } |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1203 | |
| 1204 | XglCommandBufferObj::XglCommandBufferObj(XglDevice *device) |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1205 | : xgl_testing::CmdBuffer(*device, xgl_testing::CmdBuffer::create_info(device->graphics_queue_node_index_)) |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1206 | { |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1207 | m_device = device; |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1208 | } |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1209 | |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1210 | XGL_CMD_BUFFER XglCommandBufferObj::GetBufferHandle() |
| 1211 | { |
Chia-I Wu | d28343c | 2014-12-28 15:12:48 +0800 | [diff] [blame] | 1212 | return obj(); |
Tony Barbour | 6d047bf | 2014-12-10 14:34:45 -0700 | [diff] [blame] | 1213 | } |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1214 | |
Jon Ashburn | c4164b1 | 2014-12-31 17:10:47 -0700 | [diff] [blame] | 1215 | XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_CMD_BUFFER_BEGIN_INFO *pInfo) |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1216 | { |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 1217 | begin(pInfo); |
| 1218 | return XGL_SUCCESS; |
| 1219 | } |
| 1220 | |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 1221 | XGL_RESULT XglCommandBufferObj::BeginCommandBuffer(XGL_RENDER_PASS renderpass_obj, XGL_FRAMEBUFFER framebuffer_obj) |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 1222 | { |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 1223 | begin(renderpass_obj, framebuffer_obj); |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 1224 | return XGL_SUCCESS; |
| 1225 | } |
| 1226 | |
| 1227 | XGL_RESULT XglCommandBufferObj::BeginCommandBuffer() |
| 1228 | { |
| 1229 | begin(); |
Chia-I Wu | d28343c | 2014-12-28 15:12:48 +0800 | [diff] [blame] | 1230 | return XGL_SUCCESS; |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | XGL_RESULT XglCommandBufferObj::EndCommandBuffer() |
| 1234 | { |
Chia-I Wu | d28343c | 2014-12-28 15:12:48 +0800 | [diff] [blame] | 1235 | end(); |
| 1236 | return XGL_SUCCESS; |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1239 | void XglCommandBufferObj::PipelineBarrier(XGL_PIPELINE_BARRIER *barrierPtr) |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1240 | { |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1241 | xglCmdPipelineBarrier(obj(), barrierPtr); |
Tony Barbour | 471338d | 2014-12-10 17:28:39 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Courtney Goeltzenleuchter | 679bbfa | 2015-03-05 17:26:38 -0700 | [diff] [blame] | 1244 | void XglCommandBufferObj::ClearAllBuffers(XGL_CLEAR_COLOR clear_color, float depth_clear_color, uint32_t stencil_clear_color, |
| 1245 | XGL_DEPTH_STENCIL_BIND_INFO *depthStencilBinding, XGL_IMAGE depthStencilImage) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1246 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1247 | uint32_t i; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1248 | const XGL_FLAGS output_mask = |
| 1249 | XGL_MEMORY_OUTPUT_CPU_WRITE_BIT | |
| 1250 | XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT | |
| 1251 | XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT | |
| 1252 | XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 1253 | XGL_MEMORY_OUTPUT_COPY_BIT; |
| 1254 | const XGL_FLAGS input_mask = 0; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1255 | |
| 1256 | // whatever we want to do, we do it to the whole buffer |
| 1257 | XGL_IMAGE_SUBRESOURCE_RANGE srRange = {}; |
| 1258 | srRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 1259 | srRange.baseMipLevel = 0; |
| 1260 | srRange.mipLevels = XGL_LAST_MIP_OR_SLICE; |
| 1261 | srRange.baseArraySlice = 0; |
| 1262 | srRange.arraySize = XGL_LAST_MIP_OR_SLICE; |
| 1263 | |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1264 | XGL_IMAGE_MEMORY_BARRIER memory_barrier = {}; |
| 1265 | memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1266 | memory_barrier.outputMask = output_mask; |
| 1267 | memory_barrier.inputMask = input_mask; |
| 1268 | memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL; |
| 1269 | memory_barrier.subresourceRange = srRange; |
Mark Lobodzinski | 837ef92 | 2015-01-29 14:24:14 -0600 | [diff] [blame] | 1270 | XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1271 | |
Courtney Goeltzenleuchter | 1e8f3be | 2015-03-24 18:02:34 -0600 | [diff] [blame] | 1272 | XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE }; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1273 | XGL_PIPELINE_BARRIER pipeline_barrier = {}; |
| 1274 | pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER; |
| 1275 | pipeline_barrier.eventCount = 1; |
| 1276 | pipeline_barrier.pEvents = set_events; |
| 1277 | pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE; |
| 1278 | pipeline_barrier.memBarrierCount = 1; |
Mark Lobodzinski | d5d83ed | 2015-02-02 11:55:52 -0600 | [diff] [blame] | 1279 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1280 | |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 1281 | for (i = 0; i < m_renderTargets.size(); i++) { |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1282 | memory_barrier.image = m_renderTargets[i]->image(); |
| 1283 | memory_barrier.oldLayout = m_renderTargets[i]->layout(); |
| 1284 | xglCmdPipelineBarrier( obj(), &pipeline_barrier); |
| 1285 | m_renderTargets[i]->layout(memory_barrier.newLayout); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1286 | |
Courtney Goeltzenleuchter | b3efe9b | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 1287 | xglCmdClearColorImage(obj(), |
| 1288 | m_renderTargets[i]->image(), XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL, |
| 1289 | clear_color, 1, &srRange ); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | if (depthStencilImage) |
| 1293 | { |
| 1294 | XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {}; |
| 1295 | dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH; |
| 1296 | dsRange.baseMipLevel = 0; |
| 1297 | dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE; |
| 1298 | dsRange.baseArraySlice = 0; |
| 1299 | dsRange.arraySize = XGL_LAST_MIP_OR_SLICE; |
| 1300 | |
| 1301 | // prepare the depth buffer for clear |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1302 | |
| 1303 | memory_barrier.oldLayout = depthStencilBinding->layout; |
| 1304 | memory_barrier.newLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL; |
| 1305 | memory_barrier.image = depthStencilImage; |
| 1306 | memory_barrier.subresourceRange = dsRange; |
| 1307 | |
| 1308 | xglCmdPipelineBarrier( obj(), &pipeline_barrier); |
| 1309 | depthStencilBinding->layout = memory_barrier.newLayout; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1310 | |
Courtney Goeltzenleuchter | b3efe9b | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 1311 | xglCmdClearDepthStencil(obj(), |
| 1312 | depthStencilImage, XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL, |
Courtney Goeltzenleuchter | 679bbfa | 2015-03-05 17:26:38 -0700 | [diff] [blame] | 1313 | depth_clear_color, stencil_clear_color, |
| 1314 | 1, &dsRange); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1315 | |
| 1316 | // prepare depth buffer for rendering |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1317 | memory_barrier.image = depthStencilImage; |
| 1318 | memory_barrier.oldLayout = XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL; |
| 1319 | memory_barrier.newLayout = depthStencilBinding->layout; |
| 1320 | memory_barrier.subresourceRange = dsRange; |
| 1321 | xglCmdPipelineBarrier( obj(), &pipeline_barrier); |
| 1322 | depthStencilBinding->layout = memory_barrier.newLayout; |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
Jon Ashburn | cdc40be | 2015-01-02 18:27:14 -0700 | [diff] [blame] | 1326 | void XglCommandBufferObj::PrepareAttachments() |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1327 | { |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1328 | uint32_t i; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1329 | const XGL_FLAGS output_mask = |
| 1330 | XGL_MEMORY_OUTPUT_CPU_WRITE_BIT | |
| 1331 | XGL_MEMORY_OUTPUT_SHADER_WRITE_BIT | |
| 1332 | XGL_MEMORY_OUTPUT_COLOR_ATTACHMENT_BIT | |
| 1333 | XGL_MEMORY_OUTPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 1334 | XGL_MEMORY_OUTPUT_COPY_BIT; |
| 1335 | const XGL_FLAGS input_mask = |
| 1336 | XGL_MEMORY_INPUT_CPU_READ_BIT | |
| 1337 | XGL_MEMORY_INPUT_INDIRECT_COMMAND_BIT | |
| 1338 | XGL_MEMORY_INPUT_INDEX_FETCH_BIT | |
| 1339 | XGL_MEMORY_INPUT_VERTEX_ATTRIBUTE_FETCH_BIT | |
| 1340 | XGL_MEMORY_INPUT_UNIFORM_READ_BIT | |
| 1341 | XGL_MEMORY_INPUT_SHADER_READ_BIT | |
| 1342 | XGL_MEMORY_INPUT_COLOR_ATTACHMENT_BIT | |
| 1343 | XGL_MEMORY_INPUT_DEPTH_STENCIL_ATTACHMENT_BIT | |
| 1344 | XGL_MEMORY_INPUT_COPY_BIT; |
| 1345 | |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1346 | XGL_IMAGE_SUBRESOURCE_RANGE srRange = {}; |
| 1347 | srRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 1348 | srRange.baseMipLevel = 0; |
| 1349 | srRange.mipLevels = XGL_LAST_MIP_OR_SLICE; |
| 1350 | srRange.baseArraySlice = 0; |
| 1351 | srRange.arraySize = XGL_LAST_MIP_OR_SLICE; |
| 1352 | |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1353 | XGL_IMAGE_MEMORY_BARRIER memory_barrier = {}; |
| 1354 | memory_barrier.sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1355 | memory_barrier.outputMask = output_mask; |
| 1356 | memory_barrier.inputMask = input_mask; |
| 1357 | memory_barrier.newLayout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 1358 | memory_barrier.subresourceRange = srRange; |
Mark Lobodzinski | 837ef92 | 2015-01-29 14:24:14 -0600 | [diff] [blame] | 1359 | XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &memory_barrier; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1360 | |
Courtney Goeltzenleuchter | 1e8f3be | 2015-03-24 18:02:34 -0600 | [diff] [blame] | 1361 | XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE }; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1362 | XGL_PIPELINE_BARRIER pipeline_barrier = {}; |
| 1363 | pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER; |
| 1364 | pipeline_barrier.eventCount = 1; |
| 1365 | pipeline_barrier.pEvents = set_events; |
| 1366 | pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE; |
| 1367 | pipeline_barrier.memBarrierCount = 1; |
Mark Lobodzinski | d5d83ed | 2015-02-02 11:55:52 -0600 | [diff] [blame] | 1368 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1369 | |
Courtney Goeltzenleuchter | b4337c1 | 2015-03-05 16:47:18 -0700 | [diff] [blame] | 1370 | for(i=0; i<m_renderTargets.size(); i++) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1371 | { |
Mike Stroyan | fb80d5f | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 1372 | memory_barrier.image = m_renderTargets[i]->image(); |
| 1373 | memory_barrier.oldLayout = m_renderTargets[i]->layout(); |
| 1374 | xglCmdPipelineBarrier( obj(), &pipeline_barrier); |
| 1375 | m_renderTargets[i]->layout(memory_barrier.newLayout); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1376 | } |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Courtney Goeltzenleuchter | 69894b7 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 1379 | void XglCommandBufferObj::BeginRenderPass(XGL_RENDER_PASS renderpass, XGL_FRAMEBUFFER framebuffer) |
| 1380 | { |
| 1381 | XGL_RENDER_PASS_BEGIN rp_begin = { |
| 1382 | renderpass, |
| 1383 | framebuffer, |
| 1384 | }; |
| 1385 | |
| 1386 | xglCmdBeginRenderPass( obj(), &rp_begin); |
| 1387 | } |
| 1388 | |
| 1389 | void XglCommandBufferObj::EndRenderPass(XGL_RENDER_PASS renderpass) |
| 1390 | { |
| 1391 | xglCmdEndRenderPass( obj(), renderpass); |
| 1392 | } |
| 1393 | |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1394 | void XglCommandBufferObj::BindStateObject(XGL_STATE_BIND_POINT stateBindPoint, XGL_DYNAMIC_STATE_OBJECT stateObject) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1395 | { |
Tony Barbour | f52346d | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1396 | xglCmdBindDynamicStateObject( obj(), stateBindPoint, stateObject); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | void XglCommandBufferObj::AddRenderTarget(XglImage *renderTarget) |
| 1400 | { |
| 1401 | m_renderTargets.push_back(renderTarget); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1404 | 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] | 1405 | { |
Chia-I Wu | d28343c | 2014-12-28 15:12:48 +0800 | [diff] [blame] | 1406 | xglCmdDrawIndexed(obj(), firstIndex, indexCount, vertexOffset, firstInstance, instanceCount); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1409 | 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] | 1410 | { |
Chia-I Wu | d28343c | 2014-12-28 15:12:48 +0800 | [diff] [blame] | 1411 | xglCmdDraw(obj(), firstVertex, vertexCount, firstInstance, instanceCount); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1412 | } |
| 1413 | |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1414 | void XglCommandBufferObj::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, uint32_t numMemRefs) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1415 | { |
| 1416 | XGL_RESULT err = XGL_SUCCESS; |
| 1417 | |
| 1418 | // submit the command buffer to the universal queue |
Chia-I Wu | d28343c | 2014-12-28 15:12:48 +0800 | [diff] [blame] | 1419 | err = xglQueueSubmit( m_device->m_queue, 1, &obj(), numMemRefs, memRefs, NULL ); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1420 | ASSERT_XGL_SUCCESS( err ); |
| 1421 | |
| 1422 | err = xglQueueWaitIdle( m_device->m_queue ); |
| 1423 | ASSERT_XGL_SUCCESS( err ); |
| 1424 | |
| 1425 | // Wait for work to finish before cleaning up. |
| 1426 | xglDeviceWaitIdle(m_device->device()); |
| 1427 | |
| 1428 | } |
| 1429 | void XglCommandBufferObj::BindPipeline(XGL_PIPELINE pipeline) |
| 1430 | { |
Chia-I Wu | d28343c | 2014-12-28 15:12:48 +0800 | [diff] [blame] | 1431 | xglCmdBindPipeline( obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline ); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | void XglCommandBufferObj::BindDescriptorSet(XGL_DESCRIPTOR_SET descriptorSet) |
| 1435 | { |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1436 | // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic buffer view) |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1437 | xglCmdBindDescriptorSet(obj(), XGL_PIPELINE_BIND_POINT_GRAPHICS, descriptorSet, NULL ); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1438 | } |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1439 | void XglCommandBufferObj::BindIndexBuffer(XglIndexBufferObj *indexBuffer, uint32_t offset) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1440 | { |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1441 | xglCmdBindIndexBuffer(obj(), indexBuffer->obj(), offset, indexBuffer->GetIndexType()); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1442 | } |
Mark Lobodzinski | 17caf57 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1443 | void XglCommandBufferObj::BindVertexBuffer(XglConstantBufferObj *vertexBuffer, uint32_t offset, uint32_t binding) |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1444 | { |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1445 | xglCmdBindVertexBuffer(obj(), vertexBuffer->obj(), offset, binding); |
Tony Barbour | 30cc9e8 | 2014-12-17 11:53:55 -0700 | [diff] [blame] | 1446 | } |
Tony Barbour | 1c45ce0 | 2015-03-27 17:03:18 -0600 | [diff] [blame] | 1447 | XglDepthStencilObj::XglDepthStencilObj() |
| 1448 | { |
| 1449 | m_initialized = false; |
| 1450 | } |
| 1451 | bool XglDepthStencilObj::Initialized() |
| 1452 | { |
| 1453 | return m_initialized; |
| 1454 | } |
| 1455 | |
| 1456 | XGL_DEPTH_STENCIL_BIND_INFO* XglDepthStencilObj::BindInfo() |
| 1457 | { |
| 1458 | return &m_depthStencilBindInfo; |
| 1459 | } |
| 1460 | |
| 1461 | void XglDepthStencilObj::Init(XglDevice *device, int32_t width, int32_t height) |
| 1462 | { |
| 1463 | XGL_IMAGE_CREATE_INFO image_info; |
| 1464 | XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view_info; |
| 1465 | |
| 1466 | m_device = device; |
| 1467 | m_initialized = true; |
| 1468 | m_depth_stencil_fmt = XGL_FMT_D16_UNORM; |
| 1469 | |
| 1470 | image_info.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
| 1471 | image_info.pNext = NULL; |
| 1472 | image_info.imageType = XGL_IMAGE_2D; |
| 1473 | image_info.format = m_depth_stencil_fmt; |
| 1474 | image_info.extent.width = width; |
| 1475 | image_info.extent.height = height; |
| 1476 | image_info.extent.depth = 1; |
| 1477 | image_info.mipLevels = 1; |
| 1478 | image_info.arraySize = 1; |
| 1479 | image_info.samples = 1; |
| 1480 | image_info.tiling = XGL_OPTIMAL_TILING; |
| 1481 | image_info.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT; |
| 1482 | image_info.flags = 0; |
| 1483 | init(*m_device, image_info); |
| 1484 | |
| 1485 | view_info.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO; |
| 1486 | view_info.pNext = NULL; |
| 1487 | view_info.image = XGL_NULL_HANDLE; |
| 1488 | view_info.mipLevel = 0; |
| 1489 | view_info.baseArraySlice = 0; |
| 1490 | view_info.arraySize = 1; |
| 1491 | view_info.flags = 0; |
| 1492 | view_info.image = obj(); |
| 1493 | m_depthStencilView.init(*m_device, view_info); |
| 1494 | |
| 1495 | m_depthStencilBindInfo.view = m_depthStencilView.obj(); |
| 1496 | m_depthStencilBindInfo.layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 1497 | } |