1/* 2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com> 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * on the rights to use, copy, modify, merge, publish, distribute, sub 8 * license, and/or sell copies of the Software, and to permit persons to whom 9 * the Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */ 22 23#include "nine_helpers.h" 24#include "nine_shader.h" 25 26#include "vertexdeclaration9.h" 27#include "vertexshader9.h" 28 29#include "device9.h" 30#include "pipe/p_context.h" 31#include "cso_cache/cso_context.h" 32 33#define DBG_CHANNEL DBG_VERTEXSHADER 34 35HRESULT 36NineVertexShader9_ctor( struct NineVertexShader9 *This, 37 struct NineUnknownParams *pParams, 38 const DWORD *pFunction, void *cso ) 39{ 40 struct NineDevice9 *device; 41 struct nine_shader_info info; 42 struct pipe_context *pipe; 43 HRESULT hr; 44 unsigned i; 45 46 DBG("This=%p pParams=%p pFunction=%p cso=%p\n", 47 This, pParams, pFunction, cso); 48 49 hr = NineUnknown_ctor(&This->base, pParams); 50 if (FAILED(hr)) 51 return hr; 52 53 if (cso) { 54 This->ff_cso = cso; 55 return D3D_OK; 56 } 57 58 device = This->base.device; 59 60 info.type = PIPE_SHADER_VERTEX; 61 info.byte_code = pFunction; 62 info.const_i_base = NINE_CONST_I_BASE(device->max_vs_const_f) / 16; 63 info.const_b_base = NINE_CONST_B_BASE(device->max_vs_const_f) / 16; 64 info.sampler_mask_shadow = 0x0; 65 info.sampler_ps1xtypes = 0x0; 66 info.fog_enable = 0; 67 info.point_size_min = 0; 68 info.point_size_max = 0; 69 info.add_constants_defs.c_combination = NULL; 70 info.add_constants_defs.int_const_added = NULL; 71 info.add_constants_defs.bool_const_added = NULL; 72 info.swvp_on = !!(device->params.BehaviorFlags & D3DCREATE_SOFTWARE_VERTEXPROCESSING); 73 info.process_vertices = false; 74 75 pipe = nine_context_get_pipe_acquire(device); 76 hr = nine_translate_shader(device, &info, pipe); 77 if (hr == D3DERR_INVALIDCALL && 78 (device->params.BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING)) { 79 /* Retry with a swvp shader. It will require swvp to be on. */ 80 info.swvp_on = true; 81 hr = nine_translate_shader(device, &info, pipe); 82 } 83 nine_context_get_pipe_release(device); 84 if (hr == D3DERR_INVALIDCALL) 85 ERR("Encountered buggy shader\n"); 86 if (FAILED(hr)) 87 return hr; 88 This->byte_code.version = info.version; 89 This->swvp_only = info.swvp_on; 90 91 This->byte_code.tokens = mem_dup(pFunction, info.byte_size); 92 if (!This->byte_code.tokens) 93 return E_OUTOFMEMORY; 94 This->byte_code.size = info.byte_size; 95 96 This->variant.cso = info.cso; 97 This->variant.const_ranges = info.const_ranges; 98 This->variant.const_used_size = info.const_used_size; 99 This->last_cso = info.cso; 100 This->last_const_ranges = info.const_ranges; 101 This->last_const_used_size = info.const_used_size; 102 This->last_key = (uint32_t) (info.swvp_on << 9); 103 104 This->lconstf = info.lconstf; 105 This->sampler_mask = info.sampler_mask; 106 This->position_t = info.position_t; 107 This->point_size = info.point_size; 108 109 memcpy(This->int_slots_used, info.int_slots_used, sizeof(This->int_slots_used)); 110 memcpy(This->bool_slots_used, info.bool_slots_used, sizeof(This->bool_slots_used)); 111 112 This->const_int_slots = info.const_int_slots; 113 This->const_bool_slots = info.const_bool_slots; 114 115 This->c_combinations = NULL; 116 117 for (i = 0; i < info.num_inputs && i < ARRAY_SIZE(This->input_map); ++i) 118 This->input_map[i].ndecl = info.input_map[i]; 119 This->num_inputs = i; 120 121 return D3D_OK; 122} 123 124void 125NineVertexShader9_dtor( struct NineVertexShader9 *This ) 126{ 127 DBG("This=%p\n", This); 128 129 if (This->base.device) { 130 struct pipe_context *pipe = nine_context_get_pipe_multithread(This->base.device); 131 struct nine_shader_variant *var = &This->variant; 132 struct nine_shader_variant_so *var_so = &This->variant_so; 133 134 do { 135 if (var->cso) { 136 if (This->base.device->context.cso_shader.vs == var->cso) 137 pipe->bind_vs_state(pipe, NULL); 138 pipe->delete_vs_state(pipe, var->cso); 139 FREE(var->const_ranges); 140 } 141 var = var->next; 142 } while (var); 143 144 while (var_so && var_so->vdecl) { 145 if (var_so->cso) { 146 cso_delete_vertex_shader(This->base.device->cso_sw, var_so->cso ); 147 } 148 var_so = var_so->next; 149 } 150 151 if (This->ff_cso) { 152 if (This->ff_cso == This->base.device->context.cso_shader.vs) 153 pipe->bind_vs_state(pipe, NULL); 154 pipe->delete_vs_state(pipe, This->ff_cso); 155 } 156 } 157 nine_shader_variants_free(&This->variant); 158 nine_shader_variants_so_free(&This->variant_so); 159 160 nine_shader_constant_combination_free(This->c_combinations); 161 162 FREE((void *)This->byte_code.tokens); /* const_cast */ 163 164 FREE(This->lconstf.data); 165 FREE(This->lconstf.ranges); 166 167 NineUnknown_dtor(&This->base); 168} 169 170HRESULT NINE_WINAPI 171NineVertexShader9_GetFunction( struct NineVertexShader9 *This, 172 void *pData, 173 UINT *pSizeOfData ) 174{ 175 user_assert(pSizeOfData, D3DERR_INVALIDCALL); 176 177 if (!pData) { 178 *pSizeOfData = This->byte_code.size; 179 return D3D_OK; 180 } 181 user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL); 182 183 memcpy(pData, This->byte_code.tokens, This->byte_code.size); 184 185 return D3D_OK; 186} 187 188void * 189NineVertexShader9_GetVariant( struct NineVertexShader9 *This, 190 unsigned **const_ranges, 191 unsigned *const_used_size ) 192{ 193 /* GetVariant is called from nine_context, thus we can 194 * get pipe directly */ 195 struct pipe_context *pipe = This->base.device->context.pipe; 196 void *cso; 197 uint64_t key; 198 199 key = This->next_key; 200 if (key == This->last_key) { 201 *const_ranges = This->last_const_ranges; 202 *const_used_size = This->last_const_used_size; 203 return This->last_cso; 204 } 205 206 cso = nine_shader_variant_get(&This->variant, const_ranges, const_used_size, key); 207 if (!cso) { 208 struct NineDevice9 *device = This->base.device; 209 struct nine_shader_info info; 210 HRESULT hr; 211 212 info.type = PIPE_SHADER_VERTEX; 213 info.const_i_base = NINE_CONST_I_BASE(device->max_vs_const_f) / 16; 214 info.const_b_base = NINE_CONST_B_BASE(device->max_vs_const_f) / 16; 215 info.byte_code = This->byte_code.tokens; 216 info.sampler_mask_shadow = key & 0xf; 217 info.fog_enable = device->context.rs[D3DRS_FOGENABLE]; 218 info.point_size_min = asfloat(device->context.rs[D3DRS_POINTSIZE_MIN]); 219 info.point_size_max = asfloat(device->context.rs[D3DRS_POINTSIZE_MAX]); 220 info.add_constants_defs.c_combination = 221 nine_shader_constant_combination_get(This->c_combinations, (key >> 16) & 0xff); 222 info.add_constants_defs.int_const_added = &This->int_slots_used; 223 info.add_constants_defs.bool_const_added = &This->bool_slots_used; 224 info.swvp_on = device->context.swvp; 225 info.process_vertices = false; 226 227 hr = nine_translate_shader(This->base.device, &info, pipe); 228 if (FAILED(hr)) 229 return NULL; 230 nine_shader_variant_add(&This->variant, key, info.cso, 231 info.const_ranges, info.const_used_size); 232 cso = info.cso; 233 *const_ranges = info.const_ranges; 234 *const_used_size = info.const_used_size; 235 } 236 237 This->last_key = key; 238 This->last_cso = cso; 239 This->last_const_ranges = *const_ranges; 240 This->last_const_used_size = *const_used_size; 241 242 return cso; 243} 244 245void * 246NineVertexShader9_GetVariantProcessVertices( struct NineVertexShader9 *This, 247 struct NineVertexDeclaration9 *vdecl_out, 248 struct pipe_stream_output_info *so ) 249{ 250 struct nine_shader_info info; 251 HRESULT hr; 252 void *cso; 253 254 cso = nine_shader_variant_so_get(&This->variant_so, vdecl_out, so); 255 if (cso) 256 return cso; 257 258 info.type = PIPE_SHADER_VERTEX; 259 info.const_i_base = 0; 260 info.const_b_base = 0; 261 info.byte_code = This->byte_code.tokens; 262 info.sampler_mask_shadow = 0; 263 info.fog_enable = false; 264 info.point_size_min = 0; 265 info.point_size_max = 0; 266 info.add_constants_defs.c_combination = NULL; 267 info.add_constants_defs.int_const_added = NULL; 268 info.add_constants_defs.bool_const_added = NULL; 269 info.swvp_on = true; 270 info.vdecl_out = vdecl_out; 271 info.process_vertices = true; 272 hr = nine_translate_shader(This->base.device, &info, This->base.device->pipe_sw); 273 if (FAILED(hr)) 274 return NULL; 275 *so = info.so; 276 nine_shader_variant_so_add(&This->variant_so, vdecl_out, so, info.cso); 277 return info.cso; 278} 279 280IDirect3DVertexShader9Vtbl NineVertexShader9_vtable = { 281 (void *)NineUnknown_QueryInterface, 282 (void *)NineUnknown_AddRef, 283 (void *)NineUnknown_Release, 284 (void *)NineUnknown_GetDevice, 285 (void *)NineVertexShader9_GetFunction 286}; 287 288static const GUID *NineVertexShader9_IIDs[] = { 289 &IID_IDirect3DVertexShader9, 290 &IID_IUnknown, 291 NULL 292}; 293 294HRESULT 295NineVertexShader9_new( struct NineDevice9 *pDevice, 296 struct NineVertexShader9 **ppOut, 297 const DWORD *pFunction, void *cso ) 298{ 299 if (cso) { 300 NINE_DEVICE_CHILD_BIND_NEW(VertexShader9, ppOut, pDevice, pFunction, cso); 301 } else { 302 NINE_DEVICE_CHILD_NEW(VertexShader9, ppOut, pDevice, pFunction, cso); 303 } 304} 305