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() : |
| 31 | m_colorBlend( XGL_NULL_HANDLE ), |
| 32 | m_stateMsaa( XGL_NULL_HANDLE ), |
| 33 | m_stateDepthStencil( XGL_NULL_HANDLE ), |
| 34 | m_stateRaster( XGL_NULL_HANDLE ), |
| 35 | m_cmdBuffer( XGL_NULL_HANDLE ), |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 36 | m_stateViewport( XGL_NULL_HANDLE ), |
| 37 | m_width( 256.0 ), // default window width |
| 38 | m_height( 256.0 ) // default window height |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 39 | { |
| 40 | m_render_target_fmt.channelFormat = XGL_CH_FMT_R8G8B8A8; |
| 41 | m_render_target_fmt.numericFormat = XGL_NUM_FMT_UNORM; |
Courtney Goeltzenleuchter | 32e486c | 2014-10-22 14:12:38 -0600 | [diff] [blame] | 42 | |
| 43 | m_colorBinding.view = XGL_NULL_HANDLE; |
| 44 | m_depthStencilBinding.view = XGL_NULL_HANDLE; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | XglRenderFramework::~XglRenderFramework() |
| 48 | { |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 49 | |
| 50 | } |
| 51 | |
| 52 | void XglRenderFramework::InitFramework() |
| 53 | { |
| 54 | XGL_RESULT err; |
| 55 | |
| 56 | memset(&m_vtxBufferView, 0, sizeof(m_vtxBufferView)); |
| 57 | m_vtxBufferView.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO; |
| 58 | |
| 59 | memset(&m_constantBufferView, 0, sizeof(m_constantBufferView)); |
| 60 | m_constantBufferView.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO; |
| 61 | |
| 62 | err = xglInitAndEnumerateGpus(&app_info, NULL, |
| 63 | MAX_GPUS, &this->gpu_count, objs); |
| 64 | ASSERT_XGL_SUCCESS(err); |
Jon Ashburn | bf843b2 | 2014-11-26 11:06:49 -0700 | [diff] [blame] | 65 | ASSERT_GE(this->gpu_count, 1) << "No GPU available"; |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 66 | |
| 67 | m_device = new XglDevice(0, objs[0]); |
| 68 | m_device->get_device_queue(); |
| 69 | } |
| 70 | |
| 71 | void XglRenderFramework::ShutdownFramework() |
| 72 | { |
| 73 | if (m_colorBlend) xglDestroyObject(m_colorBlend); |
| 74 | if (m_stateMsaa) xglDestroyObject(m_stateMsaa); |
| 75 | if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil); |
| 76 | if (m_stateRaster) xglDestroyObject(m_stateRaster); |
| 77 | if (m_cmdBuffer) xglDestroyObject(m_cmdBuffer); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 78 | |
| 79 | if (m_stateViewport) { |
| 80 | xglDestroyObject(m_stateViewport); |
| 81 | } |
| 82 | |
| 83 | if (m_renderTarget) { |
| 84 | // TODO: XglImage should be able to destroy itself |
| 85 | // m_renderTarget-> |
| 86 | // xglDestroyObject(*m_renderTarget); |
| 87 | } |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 88 | |
| 89 | // reset the driver |
Tobin Ehlis | a3fdbec | 2014-10-23 13:45:13 -0600 | [diff] [blame] | 90 | m_device->destroy_device(); |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 91 | xglInitAndEnumerateGpus(&this->app_info, XGL_NULL_HANDLE, 0, &gpu_count, XGL_NULL_HANDLE); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 92 | } |
| 93 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 94 | void XglRenderFramework::InitState() |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 95 | { |
| 96 | XGL_RESULT err; |
| 97 | |
| 98 | m_render_target_fmt.channelFormat = XGL_CH_FMT_R8G8B8A8; |
| 99 | m_render_target_fmt.numericFormat = XGL_NUM_FMT_UNORM; |
| 100 | |
| 101 | // create a raster state (solid, back-face culling) |
| 102 | XGL_RASTER_STATE_CREATE_INFO raster = {}; |
| 103 | raster.sType = XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO; |
| 104 | raster.fillMode = XGL_FILL_SOLID; |
| 105 | raster.cullMode = XGL_CULL_NONE; |
| 106 | raster.frontFace = XGL_FRONT_FACE_CCW; |
| 107 | err = xglCreateRasterState( device(), &raster, &m_stateRaster ); |
| 108 | ASSERT_XGL_SUCCESS(err); |
| 109 | |
| 110 | XGL_COLOR_BLEND_STATE_CREATE_INFO blend = {}; |
| 111 | blend.sType = XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO; |
| 112 | err = xglCreateColorBlendState(device(), &blend, &m_colorBlend); |
| 113 | ASSERT_XGL_SUCCESS( err ); |
| 114 | |
| 115 | XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {}; |
| 116 | depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO; |
| 117 | depthStencil.depthTestEnable = XGL_FALSE; |
| 118 | depthStencil.depthWriteEnable = XGL_FALSE; |
| 119 | depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL; |
| 120 | depthStencil.depthBoundsEnable = XGL_FALSE; |
| 121 | depthStencil.minDepth = 0.f; |
| 122 | depthStencil.maxDepth = 1.f; |
| 123 | depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP; |
| 124 | depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP; |
| 125 | depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP; |
| 126 | depthStencil.back.stencilRef = 0x00; |
| 127 | depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS; |
| 128 | depthStencil.front = depthStencil.back; |
| 129 | |
| 130 | err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil ); |
| 131 | ASSERT_XGL_SUCCESS( err ); |
| 132 | |
| 133 | XGL_MSAA_STATE_CREATE_INFO msaa = {}; |
| 134 | msaa.sType = XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO; |
| 135 | msaa.sampleMask = 1; |
| 136 | msaa.samples = 1; |
| 137 | |
| 138 | err = xglCreateMsaaState( device(), &msaa, &m_stateMsaa ); |
| 139 | ASSERT_XGL_SUCCESS( err ); |
| 140 | |
| 141 | XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {}; |
| 142 | |
| 143 | cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO; |
| 144 | cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS; |
| 145 | err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer); |
| 146 | ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed"; |
| 147 | } |
| 148 | |
| 149 | void XglRenderFramework::InitConstantBuffer(int constantCount, int constantSize, |
| 150 | const void* data) |
| 151 | { |
| 152 | XGL_RESULT err = XGL_SUCCESS; |
| 153 | |
| 154 | XGL_MEMORY_ALLOC_INFO alloc_info = {}; |
| 155 | XGL_UINT8 *pData; |
| 156 | |
| 157 | alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 158 | alloc_info.allocationSize = constantCount * constantSize; |
| 159 | alloc_info.alignment = 0; |
| 160 | alloc_info.heapCount = 1; |
| 161 | alloc_info.heaps[0] = 0; // TODO: Use known existing heap |
| 162 | |
| 163 | alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT; |
| 164 | alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 165 | |
| 166 | err = xglAllocMemory(device(), &alloc_info, &m_constantBufferMem); |
| 167 | ASSERT_XGL_SUCCESS(err); |
| 168 | |
| 169 | err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData); |
| 170 | ASSERT_XGL_SUCCESS(err); |
| 171 | |
| 172 | memcpy(pData, data, alloc_info.allocationSize); |
| 173 | |
| 174 | err = xglUnmapMemory(m_constantBufferMem); |
| 175 | ASSERT_XGL_SUCCESS(err); |
| 176 | |
| 177 | // set up the memory view for the constant buffer |
Cody Northrop | e1ab9bf | 2014-10-14 14:10:26 -0600 | [diff] [blame] | 178 | this->m_constantBufferView.stride = 16; |
| 179 | this->m_constantBufferView.range = alloc_info.allocationSize; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 180 | this->m_constantBufferView.offset = 0; |
| 181 | this->m_constantBufferView.mem = m_constantBufferMem; |
| 182 | this->m_constantBufferView.format.channelFormat = XGL_CH_FMT_R32G32B32A32; |
| 183 | this->m_constantBufferView.format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 184 | } |
| 185 | |
Courtney Goeltzenleuchter | fdcfb9f | 2014-10-10 18:04:39 -0600 | [diff] [blame] | 186 | /* |
| 187 | * Update existing constant value with new data of exactly |
| 188 | * the same size. |
| 189 | */ |
| 190 | void XglRenderFramework::UpdateConstantBuffer(const void* data) |
| 191 | { |
| 192 | XGL_RESULT err = XGL_SUCCESS; |
| 193 | XGL_UINT8 *pData; |
| 194 | |
| 195 | err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData); |
| 196 | ASSERT_XGL_SUCCESS(err); |
| 197 | |
| 198 | memcpy(pData + this->m_constantBufferView.offset, data, this->m_constantBufferView.range); |
| 199 | |
| 200 | err = xglUnmapMemory(m_constantBufferMem); |
| 201 | ASSERT_XGL_SUCCESS(err); |
| 202 | } |
| 203 | |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 204 | void XglRenderFramework::CreateQueryPool(XGL_QUERY_TYPE type, XGL_UINT slots, |
| 205 | XGL_QUERY_POOL *pPool, XGL_GPU_MEMORY *pMem) |
| 206 | { |
| 207 | XGL_RESULT err; |
| 208 | |
| 209 | XGL_QUERY_POOL_CREATE_INFO poolCreateInfo = {}; |
| 210 | poolCreateInfo.sType = XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; |
| 211 | poolCreateInfo.pNext = NULL; |
| 212 | poolCreateInfo.queryType = type; |
| 213 | poolCreateInfo.slots = slots; |
| 214 | |
| 215 | err = xglCreateQueryPool(device(), &poolCreateInfo, pPool); |
| 216 | ASSERT_XGL_SUCCESS(err); |
| 217 | |
| 218 | XGL_MEMORY_REQUIREMENTS mem_req; |
| 219 | XGL_UINT data_size = sizeof(mem_req); |
| 220 | err = xglGetObjectInfo(*pPool, XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 221 | &data_size, &mem_req); |
| 222 | ASSERT_XGL_SUCCESS(err); |
| 223 | ASSERT_EQ(data_size, sizeof(mem_req)); |
| 224 | |
| 225 | if (!mem_req.size) { |
| 226 | *pMem = XGL_NULL_HANDLE; |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | XGL_MEMORY_ALLOC_INFO mem_info; |
| 231 | |
| 232 | memset(&mem_info, 0, sizeof(mem_info)); |
| 233 | mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 234 | mem_info.allocationSize = mem_req.size; |
| 235 | mem_info.alignment = mem_req.alignment; |
| 236 | mem_info.heapCount = mem_req.heapCount; |
| 237 | memcpy(mem_info.heaps, mem_req.heaps, sizeof(XGL_UINT)*XGL_MAX_MEMORY_HEAPS); |
| 238 | mem_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 239 | mem_info.flags = XGL_MEMORY_ALLOC_SHAREABLE_BIT; |
| 240 | err = xglAllocMemory(device(), &mem_info, pMem); |
| 241 | ASSERT_XGL_SUCCESS(err); |
| 242 | |
| 243 | err = xglBindObjectMemory(*pPool, *pMem, 0); |
| 244 | ASSERT_XGL_SUCCESS(err); |
| 245 | } |
| 246 | |
| 247 | void XglRenderFramework::DestroyQueryPool(XGL_QUERY_POOL pool, XGL_GPU_MEMORY mem) |
| 248 | { |
| 249 | ASSERT_XGL_SUCCESS(xglBindObjectMemory(pool, XGL_NULL_HANDLE, 0)); |
| 250 | ASSERT_XGL_SUCCESS(xglFreeMemory(mem)); |
| 251 | ASSERT_XGL_SUCCESS(xglDestroyObject(pool)); |
| 252 | } |
| 253 | |
| 254 | void XglRenderFramework::CreateShader(XGL_PIPELINE_SHADER_STAGE stage, |
| 255 | const char *shader_code, |
| 256 | XGL_SHADER *pshader) |
| 257 | { |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame] | 258 | XGL_RESULT err = XGL_SUCCESS; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 259 | std::vector<unsigned int> bil; |
| 260 | XGL_SHADER_CREATE_INFO createInfo; |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame] | 261 | size_t shader_len; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 262 | XGL_SHADER shader; |
| 263 | |
| 264 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 265 | createInfo.pNext = NULL; |
| 266 | |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame] | 267 | if (!this->m_use_bil) { |
| 268 | shader_len = strlen(shader_code); |
| 269 | createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1; |
| 270 | createInfo.pCode = malloc(createInfo.codeSize); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 271 | createInfo.flags = 0; |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame] | 272 | |
| 273 | /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */ |
| 274 | ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC; |
| 275 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 276 | ((uint32_t *) createInfo.pCode)[2] = stage; |
| 277 | memcpy(((uint32_t *) createInfo.pCode + 3), shader_code, shader_len + 1); |
| 278 | |
| 279 | err = xglCreateShader(device(), &createInfo, &shader); |
| 280 | if (err) { |
| 281 | free((void *) createInfo.pCode); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Only use BIL if GLSL compile fails or it's requested via m_use_bil |
| 286 | if (this->m_use_bil || err) { |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 287 | // Use Reference GLSL to BIL compiler |
| 288 | GLSLtoBIL(stage, shader_code, bil); |
| 289 | createInfo.pCode = bil.data(); |
| 290 | createInfo.codeSize = bil.size() * sizeof(unsigned int); |
| 291 | createInfo.flags = 0; |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame] | 292 | err = xglCreateShader(device(), &createInfo, &shader); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 293 | } |
| 294 | |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 295 | ASSERT_XGL_SUCCESS(err); |
| 296 | |
| 297 | *pshader = shader; |
| 298 | } |
| 299 | |
| 300 | void XglRenderFramework::InitViewport(float width, float height) |
| 301 | { |
| 302 | XGL_RESULT err; |
| 303 | |
| 304 | XGL_VIEWPORT_STATE_CREATE_INFO viewport = {}; |
| 305 | viewport.viewportCount = 1; |
| 306 | viewport.scissorEnable = XGL_FALSE; |
| 307 | viewport.viewports[0].originX = 0; |
| 308 | viewport.viewports[0].originY = 0; |
| 309 | viewport.viewports[0].width = 1.f * width; |
| 310 | viewport.viewports[0].height = 1.f * height; |
| 311 | viewport.viewports[0].minDepth = 0.f; |
| 312 | viewport.viewports[0].maxDepth = 1.f; |
| 313 | |
| 314 | err = xglCreateViewportState( device(), &viewport, &m_stateViewport ); |
| 315 | ASSERT_XGL_SUCCESS( err ); |
| 316 | m_width = width; |
| 317 | m_height = height; |
| 318 | } |
| 319 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 320 | void XglRenderFramework::InitViewport() |
| 321 | { |
| 322 | InitViewport(m_width, m_height); |
| 323 | } |
| 324 | |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 325 | void XglRenderFramework::InitRenderTarget() |
| 326 | { |
| 327 | m_device->CreateImage(m_width, m_height, m_render_target_fmt, |
| 328 | XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | |
| 329 | XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
| 330 | &m_renderTarget); |
| 331 | } |
| 332 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 333 | void XglRenderFramework::CreateDefaultPipeline(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps) |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 334 | { |
| 335 | XGL_RESULT err; |
| 336 | XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {}; |
| 337 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage; |
| 338 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage; |
| 339 | |
| 340 | #if 0 |
| 341 | // Create descriptor set for our one resource |
| 342 | XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {}; |
| 343 | descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO; |
| 344 | descriptorInfo.slots = 1; // Vertex buffer only |
| 345 | |
| 346 | // create a descriptor set with a single slot |
| 347 | err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet ); |
| 348 | ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed"; |
| 349 | |
| 350 | // bind memory to the descriptor set |
| 351 | err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem); |
| 352 | |
| 353 | // set up the memory view for the vertex buffer |
| 354 | this->m_vtxBufferView.stride = vbStride; |
| 355 | this->m_vtxBufferView.range = numVertices * vbStride; |
| 356 | this->m_vtxBufferView.offset = 0; |
| 357 | this->m_vtxBufferView.mem = m_vtxBufferMem; |
| 358 | this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED; |
| 359 | this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED; |
| 360 | // write the vertex buffer view to the descriptor set |
| 361 | xglBeginDescriptorSetUpdate( m_rsrcDescSet ); |
| 362 | xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_vtxBufferView ); |
| 363 | xglEndDescriptorSetUpdate( m_rsrcDescSet ); |
| 364 | #endif |
Jon Ashburn | a15fa64 | 2014-11-19 09:23:30 -0700 | [diff] [blame] | 365 | memset(&vs_stage, 0, sizeof(vs_stage)); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 366 | vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 367 | vs_stage.pNext = XGL_NULL_HANDLE; |
| 368 | vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX; |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 369 | vs_stage.shader.shader = vs; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 370 | vs_stage.shader.descriptorSetMapping[0].descriptorCount = 0; |
| 371 | vs_stage.shader.linkConstBufferCount = 0; |
| 372 | vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE; |
| 373 | vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED; |
| 374 | vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0; |
| 375 | |
Jon Ashburn | a15fa64 | 2014-11-19 09:23:30 -0700 | [diff] [blame] | 376 | memset(&ps_stage, 0, sizeof(ps_stage)); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 377 | ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 378 | ps_stage.pNext = &vs_stage; |
| 379 | ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT; |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 380 | ps_stage.shader.shader = ps; |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 381 | |
| 382 | const int slots = 1; |
| 383 | XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( slots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) ); |
| 384 | slotInfo[0].shaderEntityIndex = 0; |
| 385 | slotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE; |
| 386 | |
| 387 | ps_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo; |
| 388 | ps_stage.shader.descriptorSetMapping[0].descriptorCount = 1; |
| 389 | |
| 390 | ps_stage.shader.linkConstBufferCount = 0; |
| 391 | ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE; |
| 392 | ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED; |
| 393 | ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0; |
| 394 | |
| 395 | XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = { |
| 396 | XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType |
| 397 | &ps_stage, // pNext |
| 398 | XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY |
| 399 | XGL_FALSE, // disableVertexReuse |
| 400 | XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION |
| 401 | XGL_FALSE, // primitiveRestartEnable |
| 402 | 0 // primitiveRestartIndex |
| 403 | }; |
| 404 | |
| 405 | XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = { |
| 406 | XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO, |
| 407 | &ia_state, |
| 408 | XGL_FALSE, // depthClipEnable |
| 409 | XGL_FALSE, // rasterizerDiscardEnable |
| 410 | 1.0 // pointSize |
| 411 | }; |
| 412 | |
| 413 | XGL_PIPELINE_CB_STATE cb_state = { |
| 414 | XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO, |
| 415 | &rs_state, |
| 416 | XGL_FALSE, // alphaToCoverageEnable |
| 417 | XGL_FALSE, // dualSourceBlendEnable |
| 418 | XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP |
| 419 | { // XGL_PIPELINE_CB_ATTACHMENT_STATE |
| 420 | { |
| 421 | XGL_FALSE, // blendEnable |
| 422 | m_render_target_fmt, // XGL_FORMAT |
| 423 | 0xF // channelWriteMask |
| 424 | } |
| 425 | } |
| 426 | }; |
| 427 | |
| 428 | // TODO: Should take depth buffer format from queried formats |
| 429 | XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = { |
| 430 | XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO, |
| 431 | &cb_state, |
| 432 | {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT |
| 433 | }; |
| 434 | |
| 435 | info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 436 | info.pNext = &db_state; |
| 437 | info.flags = 0; |
| 438 | err = xglCreateGraphicsPipeline(device(), &info, pipeline); |
| 439 | ASSERT_XGL_SUCCESS(err); |
| 440 | |
| 441 | err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem); |
| 442 | ASSERT_XGL_SUCCESS(err); |
| 443 | } |
| 444 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 445 | void XglRenderFramework::GenerateBindRenderTargetCmd() |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 446 | { |
| 447 | // bind render target |
Courtney Goeltzenleuchter | 32e486c | 2014-10-22 14:12:38 -0600 | [diff] [blame] | 448 | m_colorBinding.view = m_renderTarget->targetView(); |
| 449 | m_colorBinding.colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL; |
| 450 | if (m_depthStencilBinding.view) { |
| 451 | xglCmdBindAttachments(m_cmdBuffer, 1, &m_colorBinding, &m_depthStencilBinding ); |
| 452 | } else { |
| 453 | xglCmdBindAttachments(m_cmdBuffer, 1, &m_colorBinding, XGL_NULL_HANDLE ); |
| 454 | } |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void XglRenderFramework::GenerateBindStateAndPipelineCmds(XGL_PIPELINE* pipeline) |
| 458 | { |
| 459 | // set all states |
| 460 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_RASTER, m_stateRaster ); |
| 461 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_VIEWPORT, m_stateViewport ); |
| 462 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_COLOR_BLEND, m_colorBlend); |
| 463 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_DEPTH_STENCIL, m_stateDepthStencil ); |
| 464 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_MSAA, m_stateMsaa ); |
| 465 | |
Chia-I Wu | 7a42e12 | 2014-11-08 10:48:20 +0800 | [diff] [blame] | 466 | // bind pipeline and WVP (dynamic memory view) |
Tony Barbour | 9d951a0 | 2014-11-19 16:33:11 -0700 | [diff] [blame^] | 467 | xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, *pipeline ); |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 468 | |
Chia-I Wu | 7a42e12 | 2014-11-08 10:48:20 +0800 | [diff] [blame] | 469 | // bind pipeline and WVP (dynamic memory view) |
Tony Barbour | 9d951a0 | 2014-11-19 16:33:11 -0700 | [diff] [blame^] | 470 | xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 ); |
Chia-I Wu | 7a42e12 | 2014-11-08 10:48:20 +0800 | [diff] [blame] | 471 | |
Tony Barbour | 9d951a0 | 2014-11-19 16:33:11 -0700 | [diff] [blame^] | 472 | xglCmdBindVertexData(m_cmdBuffer, m_vtxBufferView.mem, m_vtxBufferView.offset, 0); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 473 | } |
| 474 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 475 | void XglRenderFramework::GenerateClearAndPrepareBufferCmds() |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 476 | { |
| 477 | // whatever we want to do, we do it to the whole buffer |
| 478 | XGL_IMAGE_SUBRESOURCE_RANGE srRange = {}; |
| 479 | srRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 480 | srRange.baseMipLevel = 0; |
| 481 | srRange.mipLevels = XGL_LAST_MIP_OR_SLICE; |
| 482 | srRange.baseArraySlice = 0; |
| 483 | srRange.arraySize = XGL_LAST_MIP_OR_SLICE; |
| 484 | |
| 485 | // prepare the whole back buffer for clear |
| 486 | XGL_IMAGE_STATE_TRANSITION transitionToClear = {}; |
| 487 | transitionToClear.image = m_renderTarget->image(); |
| 488 | transitionToClear.oldState = m_renderTarget->state(); |
| 489 | transitionToClear.newState = XGL_IMAGE_STATE_CLEAR; |
| 490 | transitionToClear.subresourceRange = srRange; |
| 491 | xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear ); |
| 492 | m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState); |
| 493 | |
| 494 | // clear the back buffer to dark grey |
| 495 | XGL_UINT clearColor[4] = {64, 64, 64, 0}; |
| 496 | xglCmdClearColorImageRaw( m_cmdBuffer, m_renderTarget->image(), clearColor, 1, &srRange ); |
| 497 | |
| 498 | // prepare back buffer for rendering |
| 499 | XGL_IMAGE_STATE_TRANSITION transitionToRender = {}; |
| 500 | transitionToRender.image = m_renderTarget->image(); |
| 501 | transitionToRender.oldState = m_renderTarget->state(); |
| 502 | transitionToRender.newState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL; |
| 503 | transitionToRender.subresourceRange = srRange; |
| 504 | xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender ); |
| 505 | m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState); |
Courtney Goeltzenleuchter | a4b278b | 2014-10-08 08:50:49 -0600 | [diff] [blame] | 506 | } |