Chia-I Wu | 1f7540b | 2014-08-22 13:56:18 +0800 | [diff] [blame] | 1 | /* |
| 2 | * XGL |
| 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. |
Chia-I Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 23 | * |
| 24 | * Authors: |
| 25 | * Chia-I Wu <olv@lunarg.com> |
Chia-I Wu | 1f7540b | 2014-08-22 13:56:18 +0800 | [diff] [blame] | 26 | */ |
| 27 | |
Chia-I Wu | 9882459 | 2014-09-02 09:42:46 +0800 | [diff] [blame] | 28 | #include "shader.h" |
Chia-I Wu | 1f7540b | 2014-08-22 13:56:18 +0800 | [diff] [blame] | 29 | #include "pipeline_priv.h" |
| 30 | |
| 31 | static struct intel_rmap_slot *rmap_get_slot(struct intel_rmap *rmap, |
| 32 | XGL_DESCRIPTOR_SET_SLOT_TYPE type, |
| 33 | XGL_UINT index) |
| 34 | { |
| 35 | const XGL_UINT resource_offset = rmap->rt_count; |
| 36 | const XGL_UINT uav_offset = resource_offset + rmap->resource_count; |
| 37 | const XGL_UINT sampler_offset = uav_offset + rmap->uav_count; |
| 38 | struct intel_rmap_slot *slot; |
| 39 | |
| 40 | switch (type) { |
| 41 | case XGL_SLOT_UNUSED: |
| 42 | slot = NULL; |
| 43 | break; |
| 44 | case XGL_SLOT_SHADER_RESOURCE: |
| 45 | slot = &rmap->slots[resource_offset + index]; |
| 46 | break; |
| 47 | case XGL_SLOT_SHADER_UAV: |
| 48 | slot = &rmap->slots[uav_offset + index]; |
| 49 | break; |
| 50 | case XGL_SLOT_SHADER_SAMPLER: |
| 51 | slot = &rmap->slots[sampler_offset + index]; |
| 52 | break; |
| 53 | default: |
| 54 | assert(!"unknown rmap slot type"); |
| 55 | slot = NULL; |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | return slot; |
| 60 | } |
| 61 | |
| 62 | static bool rmap_init_slots_with_path(struct intel_rmap *rmap, |
| 63 | const XGL_DESCRIPTOR_SET_MAPPING *mapping, |
| 64 | XGL_UINT *nest_path, |
| 65 | XGL_UINT nest_level) |
| 66 | { |
| 67 | XGL_UINT i; |
| 68 | |
| 69 | for (i = 0; i < mapping->descriptorCount; i++) { |
| 70 | const XGL_DESCRIPTOR_SLOT_INFO *info = &mapping->pDescriptorInfo[i]; |
| 71 | struct intel_rmap_slot *slot; |
| 72 | |
| 73 | if (info->slotObjectType == XGL_SLOT_NEXT_DESCRIPTOR_SET) { |
| 74 | nest_path[nest_level] = i; |
| 75 | if (!rmap_init_slots_with_path(rmap, info->pNextLevelSet, |
| 76 | nest_path, nest_level + 1)) |
| 77 | return false; |
| 78 | |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | slot = rmap_get_slot(rmap, info->slotObjectType, |
| 83 | info->shaderEntityIndex); |
| 84 | if (!slot) |
| 85 | continue; |
| 86 | |
| 87 | assert(!slot->path_len); |
| 88 | slot->path_len = nest_level + 1; |
| 89 | |
| 90 | if (nest_level) { |
| 91 | slot->u.path = icd_alloc(sizeof(slot->u.path[0]) * |
| 92 | slot->path_len, 0, XGL_SYSTEM_ALLOC_INTERNAL); |
| 93 | if (!slot->u.path) { |
| 94 | slot->path_len = 0; |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | memcpy(slot->u.path, nest_path, |
| 99 | sizeof(slot->u.path[0]) * nest_level); |
| 100 | slot->u.path[nest_level] = i; |
| 101 | } else { |
| 102 | slot->u.index = i; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | static bool rmap_init_slots(struct intel_rmap *rmap, |
| 110 | const XGL_DESCRIPTOR_SET_MAPPING *mapping, |
| 111 | XGL_UINT depth) |
| 112 | { |
| 113 | XGL_UINT *nest_path; |
| 114 | bool ok; |
| 115 | |
| 116 | if (depth) { |
| 117 | nest_path = icd_alloc(sizeof(nest_path[0]) * depth, |
| 118 | 0, XGL_SYSTEM_ALLOC_INTERNAL_TEMP); |
| 119 | if (!nest_path) |
| 120 | return false; |
| 121 | } else { |
| 122 | nest_path = NULL; |
| 123 | } |
| 124 | |
| 125 | ok = rmap_init_slots_with_path(rmap, mapping, nest_path, 0); |
| 126 | |
| 127 | if (nest_path) |
| 128 | icd_free(nest_path); |
| 129 | |
| 130 | return ok; |
| 131 | } |
| 132 | |
| 133 | static void rmap_update_count(struct intel_rmap *rmap, |
| 134 | XGL_DESCRIPTOR_SET_SLOT_TYPE type, |
| 135 | XGL_UINT index) |
| 136 | { |
| 137 | switch (type) { |
| 138 | case XGL_SLOT_UNUSED: |
| 139 | break; |
| 140 | case XGL_SLOT_SHADER_RESOURCE: |
| 141 | if (rmap->resource_count < index + 1) |
| 142 | rmap->resource_count = index + 1; |
| 143 | break; |
| 144 | case XGL_SLOT_SHADER_UAV: |
| 145 | if (rmap->uav_count < index + 1) |
| 146 | rmap->uav_count = index + 1; |
| 147 | break; |
| 148 | case XGL_SLOT_SHADER_SAMPLER: |
| 149 | if (rmap->sampler_count < index + 1) |
| 150 | rmap->sampler_count = index + 1; |
| 151 | break; |
| 152 | default: |
| 153 | assert(!"unknown rmap slot type"); |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static XGL_UINT rmap_init_counts(struct intel_rmap *rmap, |
| 159 | const XGL_DESCRIPTOR_SET_MAPPING *mapping) |
| 160 | { |
| 161 | XGL_UINT depth = 0; |
| 162 | XGL_UINT i; |
| 163 | |
| 164 | for (i = 0; i < mapping->descriptorCount; i++) { |
| 165 | const XGL_DESCRIPTOR_SLOT_INFO *info = &mapping->pDescriptorInfo[i]; |
| 166 | |
| 167 | if (info->slotObjectType == XGL_SLOT_NEXT_DESCRIPTOR_SET) { |
| 168 | const XGL_UINT d = rmap_init_counts(rmap, |
| 169 | info->pNextLevelSet); |
| 170 | if (depth < d + 1) |
| 171 | depth = d + 1; |
| 172 | |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | rmap_update_count(rmap, info->slotObjectType, |
| 177 | info->shaderEntityIndex); |
| 178 | } |
| 179 | |
| 180 | return depth; |
| 181 | } |
| 182 | |
Chia-I Wu | a6d50aa | 2014-09-02 10:21:34 +0800 | [diff] [blame^] | 183 | static void rmap_destroy(struct intel_rmap *rmap) |
| 184 | { |
| 185 | XGL_UINT i; |
| 186 | |
| 187 | for (i = 0; i < rmap->slot_count; i++) { |
| 188 | struct intel_rmap_slot *slot = &rmap->slots[i]; |
| 189 | |
| 190 | switch (slot->path_len) { |
| 191 | case 0: |
| 192 | case 1: |
| 193 | case INTEL_RMAP_SLOT_RT: |
| 194 | case INTEL_RMAP_SLOT_DYN: |
| 195 | break; |
| 196 | default: |
| 197 | icd_free(slot->u.path); |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | icd_free(rmap->slots); |
| 203 | icd_free(rmap); |
| 204 | } |
| 205 | |
| 206 | static struct intel_rmap *rmap_create(struct intel_dev *dev, |
| 207 | const XGL_DESCRIPTOR_SET_MAPPING *mapping, |
| 208 | const XGL_DYNAMIC_MEMORY_VIEW_SLOT_INFO *dyn, |
| 209 | XGL_UINT rt_count) |
Chia-I Wu | 1f7540b | 2014-08-22 13:56:18 +0800 | [diff] [blame] | 210 | { |
| 211 | struct intel_rmap *rmap; |
| 212 | struct intel_rmap_slot *slot; |
| 213 | XGL_UINT depth, rt; |
| 214 | |
| 215 | rmap = icd_alloc(sizeof(*rmap), 0, XGL_SYSTEM_ALLOC_INTERNAL); |
| 216 | if (!rmap) |
| 217 | return NULL; |
| 218 | |
| 219 | memset(rmap, 0, sizeof(*rmap)); |
| 220 | |
| 221 | depth = rmap_init_counts(rmap, mapping); |
| 222 | |
| 223 | /* add RTs and the dynamic memory view */ |
| 224 | rmap_update_count(rmap, dyn->slotObjectType, dyn->shaderEntityIndex); |
| 225 | rmap->rt_count = rt_count; |
| 226 | |
| 227 | rmap->slot_count = rmap->rt_count + rmap->resource_count + |
| 228 | rmap->uav_count + rmap->sampler_count; |
| 229 | |
| 230 | rmap->slots = icd_alloc(sizeof(rmap->slots[0]) * rmap->slot_count, |
| 231 | 0, XGL_SYSTEM_ALLOC_INTERNAL); |
| 232 | if (!rmap->slots) { |
| 233 | icd_free(rmap); |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | memset(rmap->slots, 0, sizeof(rmap->slots[0]) * rmap->slot_count); |
| 238 | |
| 239 | if (!rmap_init_slots(rmap, mapping, depth)) { |
Chia-I Wu | a6d50aa | 2014-09-02 10:21:34 +0800 | [diff] [blame^] | 240 | rmap_destroy(rmap); |
Chia-I Wu | 1f7540b | 2014-08-22 13:56:18 +0800 | [diff] [blame] | 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | /* add RTs and the dynamic memory view */ |
| 245 | slot = rmap_get_slot(rmap, dyn->slotObjectType, dyn->shaderEntityIndex); |
| 246 | if (slot) { |
| 247 | slot->path_len = INTEL_RMAP_SLOT_DYN; |
| 248 | slot->u.index = 0; |
| 249 | } |
| 250 | for (rt = 0; rt < rmap->rt_count; rt++) { |
| 251 | slot = &rmap->slots[rt]; |
| 252 | slot->path_len = INTEL_RMAP_SLOT_RT; |
| 253 | slot->u.index = rt; |
| 254 | } |
| 255 | |
| 256 | return rmap; |
| 257 | } |
| 258 | |
Chia-I Wu | 9882459 | 2014-09-02 09:42:46 +0800 | [diff] [blame] | 259 | static void intel_pipe_shader_init(struct intel_shader *sh, |
| 260 | struct intel_pipe_shader *pipe_sh) |
| 261 | { |
| 262 | pipe_sh->in_count = sh->in_count; |
| 263 | pipe_sh->out_count = sh->out_count; |
| 264 | pipe_sh->sampler_count = sh->sampler_count; |
| 265 | pipe_sh->surface_count = sh->surface_count; |
| 266 | pipe_sh->barycentric_interps = sh->barycentric_interps; |
| 267 | pipe_sh->urb_read_length = sh->urb_read_length; |
| 268 | pipe_sh->urb_grf_start = sh->urb_grf_start; |
| 269 | pipe_sh->uses = sh->uses; |
| 270 | } |
| 271 | |
| 272 | static XGL_RESULT pipeline_shader(struct intel_pipeline *pipeline, |
| 273 | const XGL_PIPELINE_SHADER *info) |
| 274 | { |
| 275 | struct intel_shader *sh = intel_shader(info->shader); |
| 276 | void *kernel; |
| 277 | |
| 278 | // TODO: process shader object and include in pipeline |
| 279 | // For now that processing is simply a copy so that the app |
| 280 | // can destroy the original shader object after pipeline creation. |
| 281 | kernel = icd_alloc(sh->ir->size, 0, XGL_SYSTEM_ALLOC_INTERNAL_SHADER); |
| 282 | if (!kernel) |
| 283 | return XGL_ERROR_OUT_OF_MEMORY; |
| 284 | |
| 285 | // TODO: This should be a compile step |
| 286 | memcpy(kernel, sh->ir->kernel, sh->ir->size); |
| 287 | |
| 288 | switch (info->stage) { |
| 289 | case XGL_SHADER_STAGE_VERTEX: |
| 290 | /* |
| 291 | * TODO: What should we do here? |
| 292 | * shader_state (XGL_PIPELINE_SHADER) contains links |
| 293 | * to application memory in the pLinkConstBufferInfo and |
| 294 | * it's pBufferData pointers. Do we need to bring all that |
| 295 | * into the driver or is it okay to rely on those references |
| 296 | * holding good data. In OpenGL we'd make a driver copy. Not |
| 297 | * as clear for XGL. |
| 298 | * For now, use the app pointers. |
| 299 | */ |
| 300 | pipeline->vs = *info; |
| 301 | |
| 302 | /* |
| 303 | * Grab what we need from the intel_shader object as that |
| 304 | * could go away after the pipeline is created. |
| 305 | */ |
| 306 | intel_pipe_shader_init(sh, &pipeline->intel_vs); |
| 307 | pipeline->intel_vs.pCode = kernel; |
| 308 | pipeline->intel_vs.codeSize = sh->ir->size; |
| 309 | pipeline->active_shaders |= SHADER_VERTEX_FLAG; |
Chia-I Wu | a6d50aa | 2014-09-02 10:21:34 +0800 | [diff] [blame^] | 310 | pipeline->vs_rmap = rmap_create(pipeline->dev, |
Chia-I Wu | 9882459 | 2014-09-02 09:42:46 +0800 | [diff] [blame] | 311 | &info->descriptorSetMapping[0], |
| 312 | &info->dynamicMemoryViewMapping, 0); |
| 313 | if (!pipeline->vs_rmap) { |
| 314 | icd_free(kernel); |
| 315 | return XGL_ERROR_OUT_OF_MEMORY; |
| 316 | } |
| 317 | break; |
| 318 | case XGL_SHADER_STAGE_GEOMETRY: |
| 319 | intel_pipe_shader_init(sh, &pipeline->gs); |
| 320 | pipeline->gs.pCode = kernel; |
| 321 | pipeline->gs.codeSize = sh->ir->size; |
| 322 | pipeline->active_shaders |= SHADER_GEOMETRY_FLAG; |
| 323 | break; |
| 324 | case XGL_SHADER_STAGE_FRAGMENT: |
| 325 | pipeline->fs = *info; |
| 326 | intel_pipe_shader_init(sh, &pipeline->intel_fs); |
| 327 | pipeline->intel_fs.pCode = kernel; |
| 328 | pipeline->intel_fs.codeSize = sh->ir->size; |
| 329 | pipeline->active_shaders |= SHADER_FRAGMENT_FLAG; |
| 330 | /* assuming one RT; need to parse the shader */ |
Chia-I Wu | a6d50aa | 2014-09-02 10:21:34 +0800 | [diff] [blame^] | 331 | pipeline->fs_rmap = rmap_create(pipeline->dev, |
Chia-I Wu | 9882459 | 2014-09-02 09:42:46 +0800 | [diff] [blame] | 332 | &info->descriptorSetMapping[0], |
| 333 | &info->dynamicMemoryViewMapping, 1); |
| 334 | if (!pipeline->fs_rmap) { |
| 335 | icd_free(kernel); |
| 336 | return XGL_ERROR_OUT_OF_MEMORY; |
| 337 | } |
| 338 | break; |
| 339 | case XGL_SHADER_STAGE_TESS_CONTROL: |
| 340 | intel_pipe_shader_init(sh, &pipeline->tess_control); |
| 341 | pipeline->tess_control.pCode = kernel; |
| 342 | pipeline->tess_control.codeSize = sh->ir->size; |
| 343 | pipeline->active_shaders |= SHADER_TESS_CONTROL_FLAG; |
| 344 | break; |
| 345 | case XGL_SHADER_STAGE_TESS_EVALUATION: |
| 346 | intel_pipe_shader_init(sh, &pipeline->tess_eval); |
| 347 | pipeline->tess_eval.pCode = kernel; |
| 348 | pipeline->tess_eval.codeSize = sh->ir->size; |
| 349 | pipeline->active_shaders |= SHADER_TESS_EVAL_FLAG; |
| 350 | break; |
| 351 | case XGL_SHADER_STAGE_COMPUTE: |
| 352 | intel_pipe_shader_init(sh, &pipeline->compute); |
| 353 | pipeline->compute.pCode = kernel; |
| 354 | pipeline->compute.codeSize = sh->ir->size; |
| 355 | pipeline->active_shaders |= SHADER_COMPUTE_FLAG; |
| 356 | break; |
| 357 | default: |
| 358 | assert(!"unknown shader stage"); |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | return XGL_SUCCESS; |
| 363 | } |
| 364 | |
| 365 | XGL_RESULT pipeline_build_shaders(struct intel_pipeline *pipeline, |
| 366 | const struct intel_pipeline_create_info *info) |
| 367 | { |
| 368 | XGL_RESULT ret = XGL_SUCCESS; |
| 369 | |
| 370 | if (ret == XGL_SUCCESS && info->vs.shader) |
| 371 | ret = pipeline_shader(pipeline, &info->vs); |
| 372 | if (ret == XGL_SUCCESS && info->tcs.shader) |
| 373 | ret = pipeline_shader(pipeline, &info->tcs); |
| 374 | if (ret == XGL_SUCCESS && info->tes.shader) |
| 375 | ret = pipeline_shader(pipeline, &info->tes); |
| 376 | if (ret == XGL_SUCCESS && info->gs.shader) |
| 377 | ret = pipeline_shader(pipeline, &info->gs); |
| 378 | if (ret == XGL_SUCCESS && info->fs.shader) |
| 379 | ret = pipeline_shader(pipeline, &info->fs); |
| 380 | |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | void pipeline_tear_shaders(struct intel_pipeline *pipeline) |
| 385 | { |
| 386 | if (pipeline->active_shaders & SHADER_VERTEX_FLAG) { |
| 387 | icd_free(pipeline->intel_vs.pCode); |
| 388 | } |
| 389 | if (pipeline->active_shaders & SHADER_GEOMETRY_FLAG) { |
| 390 | icd_free(pipeline->gs.pCode); |
| 391 | } |
| 392 | if (pipeline->active_shaders & SHADER_FRAGMENT_FLAG) { |
| 393 | icd_free(pipeline->intel_fs.pCode); |
| 394 | } |
| 395 | if (pipeline->active_shaders & SHADER_TESS_CONTROL_FLAG) { |
| 396 | icd_free(pipeline->tess_control.pCode); |
| 397 | } |
| 398 | if (pipeline->active_shaders & SHADER_TESS_EVAL_FLAG) { |
| 399 | icd_free(pipeline->tess_eval.pCode); |
| 400 | } |
| 401 | |
| 402 | if (pipeline->vs_rmap) |
Chia-I Wu | a6d50aa | 2014-09-02 10:21:34 +0800 | [diff] [blame^] | 403 | rmap_destroy(pipeline->vs_rmap); |
Chia-I Wu | 9882459 | 2014-09-02 09:42:46 +0800 | [diff] [blame] | 404 | if (pipeline->fs_rmap) |
Chia-I Wu | a6d50aa | 2014-09-02 10:21:34 +0800 | [diff] [blame^] | 405 | rmap_destroy(pipeline->fs_rmap); |
Chia-I Wu | 9882459 | 2014-09-02 09:42:46 +0800 | [diff] [blame] | 406 | } |