1b8e80941Smrg/* 2b8e80941Smrg * Copyright 2011 Joakim Sindholt <opensource@zhasha.com> 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub 8b8e80941Smrg * license, and/or sell copies of the Software, and to permit persons to whom 9b8e80941Smrg * the Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 19b8e80941Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20b8e80941Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21b8e80941Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */ 22b8e80941Smrg 23b8e80941Smrg#include "stateblock9.h" 24b8e80941Smrg#include "device9.h" 25b8e80941Smrg#include "basetexture9.h" 26b8e80941Smrg#include "nine_helpers.h" 27b8e80941Smrg#include "vertexdeclaration9.h" 28b8e80941Smrg#include "vertexbuffer9.h" 29b8e80941Smrg#include "indexbuffer9.h" 30b8e80941Smrg 31b8e80941Smrg#define DBG_CHANNEL DBG_STATEBLOCK 32b8e80941Smrg 33b8e80941Smrg/* XXX TODO: handling of lights is broken */ 34b8e80941Smrg 35b8e80941SmrgHRESULT 36b8e80941SmrgNineStateBlock9_ctor( struct NineStateBlock9 *This, 37b8e80941Smrg struct NineUnknownParams *pParams, 38b8e80941Smrg enum nine_stateblock_type type ) 39b8e80941Smrg{ 40b8e80941Smrg HRESULT hr = NineUnknown_ctor(&This->base, pParams); 41b8e80941Smrg 42b8e80941Smrg DBG("This=%p pParams=%p type=%d\n", This, pParams, type); 43b8e80941Smrg 44b8e80941Smrg if (FAILED(hr)) 45b8e80941Smrg return hr; 46b8e80941Smrg 47b8e80941Smrg This->type = type; 48b8e80941Smrg 49b8e80941Smrg This->state.vs_const_f = MALLOC(VS_CONST_F_SIZE(This->base.device)); 50b8e80941Smrg This->state.ps_const_f = MALLOC(This->base.device->ps_const_size); 51b8e80941Smrg This->state.vs_const_i = MALLOC(VS_CONST_I_SIZE(This->base.device)); 52b8e80941Smrg This->state.vs_const_b = MALLOC(VS_CONST_B_SIZE(This->base.device)); 53b8e80941Smrg if (!This->state.vs_const_f || !This->state.ps_const_f || 54b8e80941Smrg !This->state.vs_const_i || !This->state.vs_const_b) 55b8e80941Smrg return E_OUTOFMEMORY; 56b8e80941Smrg 57b8e80941Smrg return D3D_OK; 58b8e80941Smrg} 59b8e80941Smrg 60b8e80941Smrgvoid 61b8e80941SmrgNineStateBlock9_dtor( struct NineStateBlock9 *This ) 62b8e80941Smrg{ 63b8e80941Smrg struct nine_state *state = &This->state; 64b8e80941Smrg struct nine_range *r; 65b8e80941Smrg struct nine_range_pool *pool = &This->base.device->range_pool; 66b8e80941Smrg unsigned i; 67b8e80941Smrg 68b8e80941Smrg for (i = 0; i < ARRAY_SIZE(state->rt); ++i) 69b8e80941Smrg nine_bind(&state->rt[i], NULL); 70b8e80941Smrg nine_bind(&state->ds, NULL); 71b8e80941Smrg nine_bind(&state->vs, NULL); 72b8e80941Smrg nine_bind(&state->ps, NULL); 73b8e80941Smrg nine_bind(&state->vdecl, NULL); 74b8e80941Smrg for (i = 0; i < PIPE_MAX_ATTRIBS; ++i) 75b8e80941Smrg nine_bind(&state->stream[i], NULL); 76b8e80941Smrg 77b8e80941Smrg nine_bind(&state->idxbuf, NULL); 78b8e80941Smrg for (i = 0; i < NINE_MAX_SAMPLERS; ++i) 79b8e80941Smrg nine_bind(&state->texture[i], NULL); 80b8e80941Smrg 81b8e80941Smrg FREE(state->vs_const_f); 82b8e80941Smrg FREE(state->ps_const_f); 83b8e80941Smrg FREE(state->vs_const_i); 84b8e80941Smrg FREE(state->vs_const_b); 85b8e80941Smrg 86b8e80941Smrg FREE(state->ff.light); 87b8e80941Smrg 88b8e80941Smrg FREE(state->ff.transform); 89b8e80941Smrg 90b8e80941Smrg if (This->state.changed.ps_const_f) { 91b8e80941Smrg for (r = This->state.changed.ps_const_f; r->next; r = r->next); 92b8e80941Smrg nine_range_pool_put_chain(pool, This->state.changed.ps_const_f, r); 93b8e80941Smrg } 94b8e80941Smrg if (This->state.changed.vs_const_f) { 95b8e80941Smrg for (r = This->state.changed.vs_const_f; r->next; r = r->next); 96b8e80941Smrg nine_range_pool_put_chain(pool, This->state.changed.vs_const_f, r); 97b8e80941Smrg } 98b8e80941Smrg if (This->state.changed.vs_const_i) { 99b8e80941Smrg for (r = This->state.changed.vs_const_i; r->next; r = r->next); 100b8e80941Smrg nine_range_pool_put_chain(pool, This->state.changed.vs_const_i, r); 101b8e80941Smrg } 102b8e80941Smrg if (This->state.changed.vs_const_b) { 103b8e80941Smrg for (r = This->state.changed.vs_const_b; r->next; r = r->next); 104b8e80941Smrg nine_range_pool_put_chain(pool, This->state.changed.vs_const_b, r); 105b8e80941Smrg } 106b8e80941Smrg 107b8e80941Smrg NineUnknown_dtor(&This->base); 108b8e80941Smrg} 109b8e80941Smrg 110b8e80941Smrgstatic void 111b8e80941SmrgNineStateBlock9_BindBuffer( struct NineDevice9 *device, 112b8e80941Smrg boolean applyToDevice, 113b8e80941Smrg struct NineBuffer9 **slot, 114b8e80941Smrg struct NineBuffer9 *buf ) 115b8e80941Smrg{ 116b8e80941Smrg if (applyToDevice) 117b8e80941Smrg NineBindBufferToDevice(device, slot, buf); 118b8e80941Smrg else 119b8e80941Smrg nine_bind(slot, buf); 120b8e80941Smrg} 121b8e80941Smrg 122b8e80941Smrgstatic void 123b8e80941SmrgNineStateBlock9_BindTexture( struct NineDevice9 *device, 124b8e80941Smrg boolean applyToDevice, 125b8e80941Smrg struct NineBaseTexture9 **slot, 126b8e80941Smrg struct NineBaseTexture9 *tex ) 127b8e80941Smrg{ 128b8e80941Smrg if (applyToDevice) 129b8e80941Smrg NineBindTextureToDevice(device, slot, tex); 130b8e80941Smrg else 131b8e80941Smrg nine_bind(slot, tex); 132b8e80941Smrg} 133b8e80941Smrg 134b8e80941Smrg/* Copy state marked changed in @mask from @src to @dst. 135b8e80941Smrg * If @apply is false, updating dst->changed can be omitted. 136b8e80941Smrg * TODO: compare ? 137b8e80941Smrg */ 138b8e80941Smrgstatic void 139b8e80941Smrgnine_state_copy_common(struct NineDevice9 *device, 140b8e80941Smrg struct nine_state *dst, 141b8e80941Smrg struct nine_state *src, 142b8e80941Smrg struct nine_state *mask, /* aliases either src or dst */ 143b8e80941Smrg const boolean apply, 144b8e80941Smrg struct nine_range_pool *pool) 145b8e80941Smrg{ 146b8e80941Smrg unsigned i, s; 147b8e80941Smrg 148b8e80941Smrg DBG("apply:%d changed.group: %x\n", (int)apply, (int)mask->changed.group ); 149b8e80941Smrg 150b8e80941Smrg /* device changed.* are unused. 151b8e80941Smrg * Instead nine_context_apply_stateblock is used and will 152b8e80941Smrg * internally set the right context->changed fields. 153b8e80941Smrg * Uncomment these only if we want to apply a stateblock onto a stateblock. 154b8e80941Smrg * 155b8e80941Smrg * if (apply) 156b8e80941Smrg * dst->changed.group |= mask->changed.group; 157b8e80941Smrg */ 158b8e80941Smrg 159b8e80941Smrg if (mask->changed.group & NINE_STATE_VIEWPORT) 160b8e80941Smrg dst->viewport = src->viewport; 161b8e80941Smrg if (mask->changed.group & NINE_STATE_SCISSOR) 162b8e80941Smrg dst->scissor = src->scissor; 163b8e80941Smrg 164b8e80941Smrg if (mask->changed.group & NINE_STATE_VS) 165b8e80941Smrg nine_bind(&dst->vs, src->vs); 166b8e80941Smrg if (mask->changed.group & NINE_STATE_PS) 167b8e80941Smrg nine_bind(&dst->ps, src->ps); 168b8e80941Smrg 169b8e80941Smrg /* Vertex constants. 170b8e80941Smrg * 171b8e80941Smrg * Various possibilities for optimization here, like creating a per-SB 172b8e80941Smrg * constant buffer, or memcmp'ing for changes. 173b8e80941Smrg * Will do that later depending on what works best for specific apps. 174b8e80941Smrg * 175b8e80941Smrg * Note: Currently when we apply stateblocks, it's always on the device state. 176b8e80941Smrg * Should it affect recording stateblocks ? Since it's on device state, there 177b8e80941Smrg * is no need to copy which ranges are dirty. If it turns out we should affect 178b8e80941Smrg * recording stateblocks, the info should be copied. 179b8e80941Smrg */ 180b8e80941Smrg if (mask->changed.group & NINE_STATE_VS_CONST) { 181b8e80941Smrg struct nine_range *r; 182b8e80941Smrg for (r = mask->changed.vs_const_f; r; r = r->next) { 183b8e80941Smrg memcpy(&dst->vs_const_f[r->bgn * 4], 184b8e80941Smrg &src->vs_const_f[r->bgn * 4], 185b8e80941Smrg (r->end - r->bgn) * 4 * sizeof(float)); 186b8e80941Smrg } 187b8e80941Smrg for (r = mask->changed.vs_const_i; r; r = r->next) { 188b8e80941Smrg memcpy(&dst->vs_const_i[r->bgn * 4], 189b8e80941Smrg &src->vs_const_i[r->bgn * 4], 190b8e80941Smrg (r->end - r->bgn) * 4 * sizeof(int)); 191b8e80941Smrg } 192b8e80941Smrg for (r = mask->changed.vs_const_b; r; r = r->next) { 193b8e80941Smrg memcpy(&dst->vs_const_b[r->bgn], 194b8e80941Smrg &src->vs_const_b[r->bgn], 195b8e80941Smrg (r->end - r->bgn) * sizeof(int)); 196b8e80941Smrg } 197b8e80941Smrg } 198b8e80941Smrg 199b8e80941Smrg /* Pixel constants. */ 200b8e80941Smrg if (mask->changed.group & NINE_STATE_PS_CONST) { 201b8e80941Smrg struct nine_range *r; 202b8e80941Smrg for (r = mask->changed.ps_const_f; r; r = r->next) { 203b8e80941Smrg memcpy(&dst->ps_const_f[r->bgn * 4], 204b8e80941Smrg &src->ps_const_f[r->bgn * 4], 205b8e80941Smrg (r->end - r->bgn) * 4 * sizeof(float)); 206b8e80941Smrg } 207b8e80941Smrg if (mask->changed.ps_const_i) { 208b8e80941Smrg uint16_t m = mask->changed.ps_const_i; 209b8e80941Smrg for (i = ffs(m) - 1, m >>= i; m; ++i, m >>= 1) 210b8e80941Smrg if (m & 1) 211b8e80941Smrg memcpy(dst->ps_const_i[i], src->ps_const_i[i], 4 * sizeof(int)); 212b8e80941Smrg } 213b8e80941Smrg if (mask->changed.ps_const_b) { 214b8e80941Smrg uint16_t m = mask->changed.ps_const_b; 215b8e80941Smrg for (i = ffs(m) - 1, m >>= i; m; ++i, m >>= 1) 216b8e80941Smrg if (m & 1) 217b8e80941Smrg dst->ps_const_b[i] = src->ps_const_b[i]; 218b8e80941Smrg } 219b8e80941Smrg } 220b8e80941Smrg 221b8e80941Smrg /* Render states. 222b8e80941Smrg * TODO: Maybe build a list ? 223b8e80941Smrg */ 224b8e80941Smrg for (i = 0; i < ARRAY_SIZE(mask->changed.rs); ++i) { 225b8e80941Smrg uint32_t m = mask->changed.rs[i]; 226b8e80941Smrg /* if (apply) 227b8e80941Smrg * dst->changed.rs[i] |= m; */ 228b8e80941Smrg while (m) { 229b8e80941Smrg const int r = ffs(m) - 1; 230b8e80941Smrg m &= ~(1 << r); 231b8e80941Smrg DBG("State %d %s = %d\n", i * 32 + r, nine_d3drs_to_string(i * 32 + r), (int)src->rs_advertised[i * 32 + r]); 232b8e80941Smrg dst->rs_advertised[i * 32 + r] = src->rs_advertised[i * 32 + r]; 233b8e80941Smrg } 234b8e80941Smrg } 235b8e80941Smrg 236b8e80941Smrg 237b8e80941Smrg /* Clip planes. */ 238b8e80941Smrg if (mask->changed.ucp) { 239b8e80941Smrg DBG("ucp: %x\n", mask->changed.ucp); 240b8e80941Smrg for (i = 0; i < PIPE_MAX_CLIP_PLANES; ++i) 241b8e80941Smrg if (mask->changed.ucp & (1 << i)) 242b8e80941Smrg memcpy(dst->clip.ucp[i], 243b8e80941Smrg src->clip.ucp[i], sizeof(src->clip.ucp[0])); 244b8e80941Smrg /* if (apply) 245b8e80941Smrg * dst->changed.ucp |= mask->changed.ucp;*/ 246b8e80941Smrg } 247b8e80941Smrg 248b8e80941Smrg /* Sampler state. */ 249b8e80941Smrg if (mask->changed.group & NINE_STATE_SAMPLER) { 250b8e80941Smrg for (s = 0; s < NINE_MAX_SAMPLERS; ++s) { 251b8e80941Smrg if (mask->changed.sampler[s] == 0x3ffe) { 252b8e80941Smrg memcpy(&dst->samp_advertised[s], &src->samp_advertised[s], sizeof(dst->samp_advertised[s])); 253b8e80941Smrg } else { 254b8e80941Smrg uint32_t m = mask->changed.sampler[s]; 255b8e80941Smrg DBG("samp %d: changed = %x\n", i, (int)m); 256b8e80941Smrg while (m) { 257b8e80941Smrg const int i = ffs(m) - 1; 258b8e80941Smrg m &= ~(1 << i); 259b8e80941Smrg dst->samp_advertised[s][i] = src->samp_advertised[s][i]; 260b8e80941Smrg } 261b8e80941Smrg } 262b8e80941Smrg /* if (apply) 263b8e80941Smrg * dst->changed.sampler[s] |= mask->changed.sampler[s];*/ 264b8e80941Smrg } 265b8e80941Smrg } 266b8e80941Smrg 267b8e80941Smrg /* Index buffer. */ 268b8e80941Smrg if (mask->changed.group & NINE_STATE_IDXBUF) 269b8e80941Smrg NineStateBlock9_BindBuffer(device, 270b8e80941Smrg apply, 271b8e80941Smrg (struct NineBuffer9 **)&dst->idxbuf, 272b8e80941Smrg (struct NineBuffer9 *)src->idxbuf); 273b8e80941Smrg 274b8e80941Smrg /* Vertex streams. */ 275b8e80941Smrg if (mask->changed.vtxbuf | mask->changed.stream_freq) { 276b8e80941Smrg DBG("vtxbuf/stream_freq: %x/%x\n", mask->changed.vtxbuf, mask->changed.stream_freq); 277b8e80941Smrg uint32_t m = mask->changed.vtxbuf | mask->changed.stream_freq; 278b8e80941Smrg for (i = 0; m; ++i, m >>= 1) { 279b8e80941Smrg if (mask->changed.vtxbuf & (1 << i)) { 280b8e80941Smrg NineStateBlock9_BindBuffer(device, 281b8e80941Smrg apply, 282b8e80941Smrg (struct NineBuffer9 **)&dst->stream[i], 283b8e80941Smrg (struct NineBuffer9 *)src->stream[i]); 284b8e80941Smrg if (src->stream[i]) { 285b8e80941Smrg dst->vtxbuf[i].buffer_offset = src->vtxbuf[i].buffer_offset; 286b8e80941Smrg dst->vtxbuf[i].stride = src->vtxbuf[i].stride; 287b8e80941Smrg } 288b8e80941Smrg } 289b8e80941Smrg if (mask->changed.stream_freq & (1 << i)) 290b8e80941Smrg dst->stream_freq[i] = src->stream_freq[i]; 291b8e80941Smrg } 292b8e80941Smrg /* 293b8e80941Smrg * if (apply) { 294b8e80941Smrg * dst->changed.vtxbuf |= mask->changed.vtxbuf; 295b8e80941Smrg * dst->changed.stream_freq |= mask->changed.stream_freq; 296b8e80941Smrg * }*/ 297b8e80941Smrg } 298b8e80941Smrg 299b8e80941Smrg /* Textures */ 300b8e80941Smrg if (mask->changed.texture) { 301b8e80941Smrg uint32_t m = mask->changed.texture; 302b8e80941Smrg for (s = 0; m; ++s, m >>= 1) 303b8e80941Smrg if (m & 1) 304b8e80941Smrg NineStateBlock9_BindTexture(device, apply, &dst->texture[s], src->texture[s]); 305b8e80941Smrg } 306b8e80941Smrg 307b8e80941Smrg if (!(mask->changed.group & NINE_STATE_FF)) 308b8e80941Smrg return; 309b8e80941Smrg WARN_ONCE("Fixed function state not handled properly by StateBlocks.\n"); 310b8e80941Smrg 311b8e80941Smrg /* Fixed function state. */ 312b8e80941Smrg 313b8e80941Smrg if (mask->changed.group & NINE_STATE_FF_MATERIAL) 314b8e80941Smrg dst->ff.material = src->ff.material; 315b8e80941Smrg 316b8e80941Smrg if (mask->changed.group & NINE_STATE_FF_PS_CONSTS) { 317b8e80941Smrg for (s = 0; s < NINE_MAX_TEXTURE_STAGES; ++s) { 318b8e80941Smrg for (i = 0; i < NINED3DTSS_COUNT; ++i) 319b8e80941Smrg if (mask->ff.changed.tex_stage[s][i / 32] & (1 << (i % 32))) 320b8e80941Smrg dst->ff.tex_stage[s][i] = src->ff.tex_stage[s][i]; 321b8e80941Smrg /* 322b8e80941Smrg * if (apply) { 323b8e80941Smrg * TODO: it's 32 exactly, just offset by 1 as 0 is unused 324b8e80941Smrg * dst->ff.changed.tex_stage[s][0] |= 325b8e80941Smrg * mask->ff.changed.tex_stage[s][0]; 326b8e80941Smrg * dst->ff.changed.tex_stage[s][1] |= 327b8e80941Smrg * mask->ff.changed.tex_stage[s][1]; 328b8e80941Smrg * }*/ 329b8e80941Smrg } 330b8e80941Smrg } 331b8e80941Smrg if (mask->changed.group & NINE_STATE_FF_LIGHTING) { 332b8e80941Smrg unsigned num_lights = MAX2(dst->ff.num_lights, src->ff.num_lights); 333b8e80941Smrg /* Can happen in Capture() if device state has created new lights after 334b8e80941Smrg * the stateblock was created. 335b8e80941Smrg * Can happen in Apply() if the stateblock had recorded the creation of 336b8e80941Smrg * new lights. */ 337b8e80941Smrg if (dst->ff.num_lights < num_lights) { 338b8e80941Smrg dst->ff.light = REALLOC(dst->ff.light, 339b8e80941Smrg dst->ff.num_lights * sizeof(D3DLIGHT9), 340b8e80941Smrg num_lights * sizeof(D3DLIGHT9)); 341b8e80941Smrg memset(&dst->ff.light[dst->ff.num_lights], 0, (num_lights - dst->ff.num_lights) * sizeof(D3DLIGHT9)); 342b8e80941Smrg /* if mask == dst, a Type of 0 will trigger 343b8e80941Smrg * "dst->ff.light[i] = src->ff.light[i];" later, 344b8e80941Smrg * which is what we want in that case. */ 345b8e80941Smrg if (mask != dst) { 346b8e80941Smrg for (i = dst->ff.num_lights; i < num_lights; ++i) 347b8e80941Smrg dst->ff.light[i].Type = (D3DLIGHTTYPE)NINED3DLIGHT_INVALID; 348b8e80941Smrg } 349b8e80941Smrg dst->ff.num_lights = num_lights; 350b8e80941Smrg } 351b8e80941Smrg /* Can happen in Capture() if the stateblock had recorded the creation of 352b8e80941Smrg * new lights. 353b8e80941Smrg * Can happen in Apply() if device state has created new lights after 354b8e80941Smrg * the stateblock was created. */ 355b8e80941Smrg if (src->ff.num_lights < num_lights) { 356b8e80941Smrg src->ff.light = REALLOC(src->ff.light, 357b8e80941Smrg src->ff.num_lights * sizeof(D3DLIGHT9), 358b8e80941Smrg num_lights * sizeof(D3DLIGHT9)); 359b8e80941Smrg memset(&src->ff.light[src->ff.num_lights], 0, (num_lights - src->ff.num_lights) * sizeof(D3DLIGHT9)); 360b8e80941Smrg for (i = src->ff.num_lights; i < num_lights; ++i) 361b8e80941Smrg src->ff.light[i].Type = (D3DLIGHTTYPE)NINED3DLIGHT_INVALID; 362b8e80941Smrg src->ff.num_lights = num_lights; 363b8e80941Smrg } 364b8e80941Smrg /* Note: mask is either src or dst, so at this point src, dst and mask 365b8e80941Smrg * have num_lights lights. */ 366b8e80941Smrg for (i = 0; i < num_lights; ++i) 367b8e80941Smrg if (mask->ff.light[i].Type != NINED3DLIGHT_INVALID) 368b8e80941Smrg dst->ff.light[i] = src->ff.light[i]; 369b8e80941Smrg 370b8e80941Smrg memcpy(dst->ff.active_light, src->ff.active_light, sizeof(src->ff.active_light) ); 371b8e80941Smrg dst->ff.num_lights_active = src->ff.num_lights_active; 372b8e80941Smrg } 373b8e80941Smrg if (mask->changed.group & NINE_STATE_FF_VSTRANSF) { 374b8e80941Smrg for (i = 0; i < ARRAY_SIZE(mask->ff.changed.transform); ++i) { 375b8e80941Smrg if (!mask->ff.changed.transform[i]) 376b8e80941Smrg continue; 377b8e80941Smrg for (s = i * 32; s < (i * 32 + 32); ++s) { 378b8e80941Smrg if (!(mask->ff.changed.transform[i] & (1 << (s % 32)))) 379b8e80941Smrg continue; 380b8e80941Smrg *nine_state_access_transform(&dst->ff, s, TRUE) = 381b8e80941Smrg *nine_state_access_transform(&src->ff, s, FALSE); 382b8e80941Smrg } 383b8e80941Smrg /* if (apply) 384b8e80941Smrg * dst->ff.changed.transform[i] |= mask->ff.changed.transform[i];*/ 385b8e80941Smrg } 386b8e80941Smrg } 387b8e80941Smrg} 388b8e80941Smrg 389b8e80941Smrgstatic void 390b8e80941Smrgnine_state_copy_common_all(struct NineDevice9 *device, 391b8e80941Smrg struct nine_state *dst, 392b8e80941Smrg struct nine_state *src, 393b8e80941Smrg struct nine_state *help, 394b8e80941Smrg const boolean apply, 395b8e80941Smrg struct nine_range_pool *pool, 396b8e80941Smrg const int MaxStreams) 397b8e80941Smrg{ 398b8e80941Smrg unsigned i; 399b8e80941Smrg 400b8e80941Smrg /* if (apply) 401b8e80941Smrg * dst->changed.group |= src->changed.group; 402b8e80941Smrg */ 403b8e80941Smrg 404b8e80941Smrg dst->viewport = src->viewport; 405b8e80941Smrg dst->scissor = src->scissor; 406b8e80941Smrg 407b8e80941Smrg nine_bind(&dst->vs, src->vs); 408b8e80941Smrg nine_bind(&dst->ps, src->ps); 409b8e80941Smrg 410b8e80941Smrg /* Vertex constants. 411b8e80941Smrg * 412b8e80941Smrg * Various possibilities for optimization here, like creating a per-SB 413b8e80941Smrg * constant buffer, or memcmp'ing for changes. 414b8e80941Smrg * Will do that later depending on what works best for specific apps. 415b8e80941Smrg */ 416b8e80941Smrg if (1) { 417b8e80941Smrg memcpy(&dst->vs_const_f[0], 418b8e80941Smrg &src->vs_const_f[0], VS_CONST_F_SIZE(device)); 419b8e80941Smrg 420b8e80941Smrg memcpy(dst->vs_const_i, src->vs_const_i, VS_CONST_I_SIZE(device)); 421b8e80941Smrg memcpy(dst->vs_const_b, src->vs_const_b, VS_CONST_B_SIZE(device)); 422b8e80941Smrg } 423b8e80941Smrg 424b8e80941Smrg /* Pixel constants. */ 425b8e80941Smrg if (1) { 426b8e80941Smrg struct nine_range *r = help->changed.ps_const_f; 427b8e80941Smrg memcpy(&dst->ps_const_f[0], 428b8e80941Smrg &src->ps_const_f[0], (r->end - r->bgn) * 4 * sizeof(float)); 429b8e80941Smrg 430b8e80941Smrg memcpy(dst->ps_const_i, src->ps_const_i, sizeof(dst->ps_const_i)); 431b8e80941Smrg memcpy(dst->ps_const_b, src->ps_const_b, sizeof(dst->ps_const_b)); 432b8e80941Smrg } 433b8e80941Smrg 434b8e80941Smrg /* Render states. */ 435b8e80941Smrg memcpy(dst->rs_advertised, src->rs_advertised, sizeof(dst->rs_advertised)); 436b8e80941Smrg /* if (apply) 437b8e80941Smrg * memcpy(dst->changed.rs, src->changed.rs, sizeof(dst->changed.rs));*/ 438b8e80941Smrg 439b8e80941Smrg 440b8e80941Smrg /* Clip planes. */ 441b8e80941Smrg memcpy(&dst->clip, &src->clip, sizeof(dst->clip)); 442b8e80941Smrg /* if (apply) 443b8e80941Smrg * dst->changed.ucp = src->changed.ucp;*/ 444b8e80941Smrg 445b8e80941Smrg /* Sampler state. */ 446b8e80941Smrg memcpy(dst->samp_advertised, src->samp_advertised, sizeof(dst->samp_advertised)); 447b8e80941Smrg /* if (apply) 448b8e80941Smrg * memcpy(dst->changed.sampler, 449b8e80941Smrg * src->changed.sampler, sizeof(dst->changed.sampler));*/ 450b8e80941Smrg 451b8e80941Smrg /* Index buffer. */ 452b8e80941Smrg NineStateBlock9_BindBuffer(device, 453b8e80941Smrg apply, 454b8e80941Smrg (struct NineBuffer9 **)&dst->idxbuf, 455b8e80941Smrg (struct NineBuffer9 *)src->idxbuf); 456b8e80941Smrg 457b8e80941Smrg /* Vertex streams. */ 458b8e80941Smrg if (1) { 459b8e80941Smrg for (i = 0; i < ARRAY_SIZE(dst->stream); ++i) { 460b8e80941Smrg NineStateBlock9_BindBuffer(device, 461b8e80941Smrg apply, 462b8e80941Smrg (struct NineBuffer9 **)&dst->stream[i], 463b8e80941Smrg (struct NineBuffer9 *)src->stream[i]); 464b8e80941Smrg if (src->stream[i]) { 465b8e80941Smrg dst->vtxbuf[i].buffer_offset = src->vtxbuf[i].buffer_offset; 466b8e80941Smrg dst->vtxbuf[i].stride = src->vtxbuf[i].stride; 467b8e80941Smrg } 468b8e80941Smrg dst->stream_freq[i] = src->stream_freq[i]; 469b8e80941Smrg } 470b8e80941Smrg /* if (apply) { 471b8e80941Smrg * dst->changed.vtxbuf = (1ULL << MaxStreams) - 1; 472b8e80941Smrg * dst->changed.stream_freq = (1ULL << MaxStreams) - 1; 473b8e80941Smrg * }*/ 474b8e80941Smrg } 475b8e80941Smrg 476b8e80941Smrg /* Textures */ 477b8e80941Smrg if (1) { 478b8e80941Smrg for (i = 0; i < NINE_MAX_SAMPLERS; i++) 479b8e80941Smrg NineStateBlock9_BindTexture(device, apply, &dst->texture[i], src->texture[i]); 480b8e80941Smrg } 481b8e80941Smrg 482b8e80941Smrg /* keep this check in case we want to disable FF */ 483b8e80941Smrg if (!(help->changed.group & NINE_STATE_FF)) 484b8e80941Smrg return; 485b8e80941Smrg WARN_ONCE("Fixed function state not handled properly by StateBlocks.\n"); 486b8e80941Smrg 487b8e80941Smrg /* Fixed function state. */ 488b8e80941Smrg dst->ff.material = src->ff.material; 489b8e80941Smrg 490b8e80941Smrg memcpy(dst->ff.tex_stage, src->ff.tex_stage, sizeof(dst->ff.tex_stage)); 491b8e80941Smrg /* if (apply) TODO: memset 492b8e80941Smrg * memcpy(dst->ff.changed.tex_stage, 493b8e80941Smrg * src->ff.changed.tex_stage, sizeof(dst->ff.changed.tex_stage));*/ 494b8e80941Smrg 495b8e80941Smrg /* Lights. */ 496b8e80941Smrg if (1) { 497b8e80941Smrg if (dst->ff.num_lights < src->ff.num_lights) { 498b8e80941Smrg dst->ff.light = REALLOC(dst->ff.light, 499b8e80941Smrg dst->ff.num_lights * sizeof(D3DLIGHT9), 500b8e80941Smrg src->ff.num_lights * sizeof(D3DLIGHT9)); 501b8e80941Smrg dst->ff.num_lights = src->ff.num_lights; 502b8e80941Smrg } 503b8e80941Smrg memcpy(dst->ff.light, 504b8e80941Smrg src->ff.light, src->ff.num_lights * sizeof(dst->ff.light[0])); 505b8e80941Smrg 506b8e80941Smrg memcpy(dst->ff.active_light, src->ff.active_light, sizeof(src->ff.active_light) ); 507b8e80941Smrg dst->ff.num_lights_active = src->ff.num_lights_active; 508b8e80941Smrg } 509b8e80941Smrg 510b8e80941Smrg /* Transforms. */ 511b8e80941Smrg if (1) { 512b8e80941Smrg /* Increase dst size if required (to copy the new states). 513b8e80941Smrg * Increase src size if required (to initialize missing transforms). 514b8e80941Smrg */ 515b8e80941Smrg if (dst->ff.num_transforms != src->ff.num_transforms) { 516b8e80941Smrg int num_transforms = MAX2(src->ff.num_transforms, dst->ff.num_transforms); 517b8e80941Smrg nine_state_resize_transform(&src->ff, num_transforms); 518b8e80941Smrg nine_state_resize_transform(&dst->ff, num_transforms); 519b8e80941Smrg } 520b8e80941Smrg memcpy(dst->ff.transform, 521b8e80941Smrg src->ff.transform, dst->ff.num_transforms * sizeof(D3DMATRIX)); 522b8e80941Smrg /* Apply is always used on device state. 523b8e80941Smrg * src is then the D3DSBT_ALL stateblock which 524b8e80941Smrg * ff.changed.transform indicates all matrices are dirty. 525b8e80941Smrg * 526b8e80941Smrg * if (apply) 527b8e80941Smrg * memcpy(dst->ff.changed.transform, 528b8e80941Smrg * src->ff.changed.transform, sizeof(dst->ff.changed.transform));*/ 529b8e80941Smrg } 530b8e80941Smrg} 531b8e80941Smrg 532b8e80941Smrg/* Capture those bits of current device state that have been changed between 533b8e80941Smrg * BeginStateBlock and EndStateBlock. 534b8e80941Smrg */ 535b8e80941SmrgHRESULT NINE_WINAPI 536b8e80941SmrgNineStateBlock9_Capture( struct NineStateBlock9 *This ) 537b8e80941Smrg{ 538b8e80941Smrg struct NineDevice9 *device = This->base.device; 539b8e80941Smrg struct nine_state *dst = &This->state; 540b8e80941Smrg struct nine_state *src = &device->state; 541b8e80941Smrg const int MaxStreams = device->caps.MaxStreams; 542b8e80941Smrg 543b8e80941Smrg DBG("This=%p\n", This); 544b8e80941Smrg 545b8e80941Smrg if (This->type == NINESBT_ALL) 546b8e80941Smrg nine_state_copy_common_all(device, dst, src, dst, FALSE, NULL, MaxStreams); 547b8e80941Smrg else 548b8e80941Smrg nine_state_copy_common(device, dst, src, dst, FALSE, NULL); 549b8e80941Smrg 550b8e80941Smrg if (dst->changed.group & NINE_STATE_VDECL) 551b8e80941Smrg nine_bind(&dst->vdecl, src->vdecl); 552b8e80941Smrg 553b8e80941Smrg return D3D_OK; 554b8e80941Smrg} 555b8e80941Smrg 556b8e80941Smrg/* Set state managed by this StateBlock as current device state. */ 557b8e80941SmrgHRESULT NINE_WINAPI 558b8e80941SmrgNineStateBlock9_Apply( struct NineStateBlock9 *This ) 559b8e80941Smrg{ 560b8e80941Smrg struct NineDevice9 *device = This->base.device; 561b8e80941Smrg struct nine_state *dst = &device->state; 562b8e80941Smrg struct nine_state *src = &This->state; 563b8e80941Smrg struct nine_range_pool *pool = &device->range_pool; 564b8e80941Smrg const int MaxStreams = device->caps.MaxStreams; 565b8e80941Smrg 566b8e80941Smrg DBG("This=%p\n", This); 567b8e80941Smrg 568b8e80941Smrg if (This->type == NINESBT_ALL) 569b8e80941Smrg nine_state_copy_common_all(device, dst, src, src, TRUE, pool, MaxStreams); 570b8e80941Smrg else 571b8e80941Smrg nine_state_copy_common(device, dst, src, src, TRUE, pool); 572b8e80941Smrg 573b8e80941Smrg nine_context_apply_stateblock(device, src); 574b8e80941Smrg 575b8e80941Smrg if ((src->changed.group & NINE_STATE_VDECL) && src->vdecl) 576b8e80941Smrg nine_bind(&dst->vdecl, src->vdecl); 577b8e80941Smrg 578b8e80941Smrg return D3D_OK; 579b8e80941Smrg} 580b8e80941Smrg 581b8e80941SmrgIDirect3DStateBlock9Vtbl NineStateBlock9_vtable = { 582b8e80941Smrg (void *)NineUnknown_QueryInterface, 583b8e80941Smrg (void *)NineUnknown_AddRef, 584b8e80941Smrg (void *)NineUnknown_Release, 585b8e80941Smrg (void *)NineUnknown_GetDevice, /* actually part of StateBlock9 iface */ 586b8e80941Smrg (void *)NineStateBlock9_Capture, 587b8e80941Smrg (void *)NineStateBlock9_Apply 588b8e80941Smrg}; 589b8e80941Smrg 590b8e80941Smrgstatic const GUID *NineStateBlock9_IIDs[] = { 591b8e80941Smrg &IID_IDirect3DStateBlock9, 592b8e80941Smrg &IID_IUnknown, 593b8e80941Smrg NULL 594b8e80941Smrg}; 595b8e80941Smrg 596b8e80941SmrgHRESULT 597b8e80941SmrgNineStateBlock9_new( struct NineDevice9 *pDevice, 598b8e80941Smrg struct NineStateBlock9 **ppOut, 599b8e80941Smrg enum nine_stateblock_type type) 600b8e80941Smrg{ 601b8e80941Smrg NINE_DEVICE_CHILD_NEW(StateBlock9, ppOut, pDevice, type); 602b8e80941Smrg} 603