st/nine: Enable passthrough only if positiont is used
[mesa.git] / src / gallium / state_trackers / nine / device9.c
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 "device9.h"
24 #include "stateblock9.h"
25 #include "surface9.h"
26 #include "swapchain9.h"
27 #include "swapchain9ex.h"
28 #include "indexbuffer9.h"
29 #include "vertexbuffer9.h"
30 #include "vertexdeclaration9.h"
31 #include "vertexshader9.h"
32 #include "pixelshader9.h"
33 #include "query9.h"
34 #include "texture9.h"
35 #include "cubetexture9.h"
36 #include "volumetexture9.h"
37 #include "nine_helpers.h"
38 #include "nine_pipe.h"
39 #include "nine_ff.h"
40 #include "nine_dump.h"
41 #include "nine_limits.h"
42
43 #include "pipe/p_screen.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_config.h"
46 #include "util/u_math.h"
47 #include "util/u_inlines.h"
48 #include "util/u_hash_table.h"
49 #include "util/u_format.h"
50 #include "util/u_surface.h"
51 #include "util/u_upload_mgr.h"
52 #include "hud/hud_context.h"
53
54 #include "cso_cache/cso_context.h"
55
56 #define DBG_CHANNEL DBG_DEVICE
57
58 #if defined(PIPE_CC_GCC) && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))
59
60 static void nine_setup_fpu()
61 {
62 uint16_t c;
63
64 __asm__ __volatile__ ("fnstcw %0" : "=m" (*&c));
65
66 /* clear the control word */
67 c &= 0xF0C0;
68 /* d3d9 doc/wine tests: mask all exceptions, use single-precision
69 * and round to nearest */
70 c |= 0x003F;
71
72 __asm__ __volatile__ ("fldcw %0" : : "m" (*&c));
73 }
74
75 #else
76
77 static void nine_setup_fpu(void)
78 {
79 WARN_ONCE("FPU setup not supported on non-x86 platforms\n");
80 }
81
82 #endif
83
84 void
85 NineDevice9_SetDefaultState( struct NineDevice9 *This, boolean is_reset )
86 {
87 struct NineSurface9 *refSurf = NULL;
88
89 DBG("This=%p is_reset=%d\n", This, (int) is_reset);
90
91 assert(!This->is_recording);
92
93 nine_state_set_defaults(This, &This->caps, is_reset);
94
95 This->state.viewport.X = 0;
96 This->state.viewport.Y = 0;
97 This->state.viewport.Width = 0;
98 This->state.viewport.Height = 0;
99
100 This->state.scissor.minx = 0;
101 This->state.scissor.miny = 0;
102 This->state.scissor.maxx = 0xffff;
103 This->state.scissor.maxy = 0xffff;
104
105 if (This->nswapchains && This->swapchains[0]->params.BackBufferCount)
106 refSurf = This->swapchains[0]->buffers[0];
107
108 if (refSurf) {
109 This->state.viewport.Width = refSurf->desc.Width;
110 This->state.viewport.Height = refSurf->desc.Height;
111 This->state.scissor.maxx = refSurf->desc.Width;
112 This->state.scissor.maxy = refSurf->desc.Height;
113 }
114
115 if (This->nswapchains && This->swapchains[0]->params.EnableAutoDepthStencil) {
116 This->state.rs[D3DRS_ZENABLE] = TRUE;
117 This->state.rs_advertised[D3DRS_ZENABLE] = TRUE;
118 }
119 if (This->state.rs[D3DRS_ZENABLE])
120 NineDevice9_SetDepthStencilSurface(
121 This, (IDirect3DSurface9 *)This->swapchains[0]->zsbuf);
122 }
123
124 #define GET_PCAP(n) pScreen->get_param(pScreen, PIPE_CAP_##n)
125 HRESULT
126 NineDevice9_ctor( struct NineDevice9 *This,
127 struct NineUnknownParams *pParams,
128 struct pipe_screen *pScreen,
129 D3DDEVICE_CREATION_PARAMETERS *pCreationParameters,
130 D3DCAPS9 *pCaps,
131 D3DPRESENT_PARAMETERS *pPresentationParameters,
132 IDirect3D9 *pD3D9,
133 ID3DPresentGroup *pPresentationGroup,
134 struct d3dadapter9_context *pCTX,
135 boolean ex,
136 D3DDISPLAYMODEEX *pFullscreenDisplayMode,
137 int minorVersionNum )
138 {
139 unsigned i;
140 HRESULT hr = NineUnknown_ctor(&This->base, pParams);
141
142 DBG("This=%p pParams=%p pScreen=%p pCreationParameters=%p pCaps=%p pPresentationParameters=%p "
143 "pD3D9=%p pPresentationGroup=%p pCTX=%p ex=%d pFullscreenDisplayMode=%p\n",
144 This, pParams, pScreen, pCreationParameters, pCaps, pPresentationParameters, pD3D9,
145 pPresentationGroup, pCTX, (int) ex, pFullscreenDisplayMode);
146
147 if (FAILED(hr)) { return hr; }
148
149 list_inithead(&This->update_buffers);
150 list_inithead(&This->update_textures);
151 list_inithead(&This->managed_buffers);
152 list_inithead(&This->managed_textures);
153
154 This->screen = pScreen;
155 This->screen_sw = pCTX->ref;
156 This->caps = *pCaps;
157 This->d3d9 = pD3D9;
158 This->params = *pCreationParameters;
159 This->ex = ex;
160 This->present = pPresentationGroup;
161 This->minor_version_num = minorVersionNum;
162
163 IDirect3D9_AddRef(This->d3d9);
164 ID3DPresentGroup_AddRef(This->present);
165
166 if (!(This->params.BehaviorFlags & D3DCREATE_FPU_PRESERVE))
167 nine_setup_fpu();
168
169 if (This->params.BehaviorFlags & D3DCREATE_SOFTWARE_VERTEXPROCESSING) {
170 DBG("Application asked full Software Vertex Processing.\n");
171 This->swvp = true;
172 This->may_swvp = true;
173 } else
174 This->swvp = false;
175 if (This->params.BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING) {
176 DBG("Application asked mixed Software Vertex Processing.\n");
177 This->may_swvp = true;
178 }
179 /* TODO: check if swvp is resetted by device Resets */
180
181 if (This->may_swvp &&
182 (This->screen->get_shader_param(This->screen, PIPE_SHADER_VERTEX,
183 PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE)
184 < (NINE_MAX_CONST_F_SWVP/2) * sizeof(float[4]) ||
185 This->screen->get_shader_param(This->screen, PIPE_SHADER_VERTEX,
186 PIPE_SHADER_CAP_MAX_CONST_BUFFERS) < 5)) {
187 /* Note: We just go on, some apps never use the abilities of
188 * swvp, and just set more constants than allowed at init.
189 * Only cards we support that are affected are the r500 */
190 WARN("Card unable to handle Software Vertex Processing. Game may fail\n");
191 }
192
193 /* When may_swvp, SetConstant* limits are different */
194 if (This->may_swvp)
195 This->caps.MaxVertexShaderConst = NINE_MAX_CONST_F_SWVP;
196
197 This->pipe = This->screen->context_create(This->screen, NULL, 0);
198 if (!This->pipe) { return E_OUTOFMEMORY; } /* guess */
199 This->pipe_sw = This->screen_sw->context_create(This->screen_sw, NULL, 0);
200 if (!This->pipe_sw) { return E_OUTOFMEMORY; }
201
202 This->cso = cso_create_context(This->pipe);
203 if (!This->cso) { return E_OUTOFMEMORY; } /* also a guess */
204 This->cso_sw = cso_create_context(This->pipe_sw);
205 if (!This->cso_sw) { return E_OUTOFMEMORY; }
206
207 /* Create first, it messes up our state. */
208 This->hud = hud_create(This->pipe, This->cso); /* NULL result is fine */
209
210 /* Available memory counter. Updated only for allocations with this device
211 * instance. This is the Win 7 behavior.
212 * Win XP shares this counter across multiple devices. */
213 This->available_texture_mem = This->screen->get_param(This->screen, PIPE_CAP_VIDEO_MEMORY);
214 if (This->available_texture_mem < 4096)
215 This->available_texture_mem <<= 20;
216 else
217 This->available_texture_mem = UINT_MAX;
218 /* We cap texture memory usage to 80% of what is reported free initially
219 * This helps get closer Win behaviour. For example VertexBuffer allocation
220 * still succeeds when texture allocation fails. */
221 This->available_texture_limit = This->available_texture_mem * 20LL / 100LL;
222
223 /* create implicit swapchains */
224 This->nswapchains = ID3DPresentGroup_GetMultiheadCount(This->present);
225 This->swapchains = CALLOC(This->nswapchains,
226 sizeof(struct NineSwapChain9 *));
227 if (!This->swapchains) { return E_OUTOFMEMORY; }
228
229 for (i = 0; i < This->nswapchains; ++i) {
230 ID3DPresent *present;
231
232 hr = ID3DPresentGroup_GetPresent(This->present, i, &present);
233 if (FAILED(hr))
234 return hr;
235
236 if (ex) {
237 D3DDISPLAYMODEEX *mode = NULL;
238 struct NineSwapChain9Ex **ret =
239 (struct NineSwapChain9Ex **)&This->swapchains[i];
240
241 if (pFullscreenDisplayMode) mode = &(pFullscreenDisplayMode[i]);
242 /* when this is a Device9Ex, it should create SwapChain9Exs */
243 hr = NineSwapChain9Ex_new(This, TRUE, present,
244 &pPresentationParameters[i], pCTX,
245 This->params.hFocusWindow, mode, ret);
246 } else {
247 hr = NineSwapChain9_new(This, TRUE, present,
248 &pPresentationParameters[i], pCTX,
249 This->params.hFocusWindow,
250 &This->swapchains[i]);
251 }
252
253 ID3DPresent_Release(present);
254 if (FAILED(hr))
255 return hr;
256 NineUnknown_ConvertRefToBind(NineUnknown(This->swapchains[i]));
257
258 hr = NineSwapChain9_GetBackBuffer(This->swapchains[i], 0,
259 D3DBACKBUFFER_TYPE_MONO,
260 (IDirect3DSurface9 **)
261 &This->state.rt[i]);
262 if (FAILED(hr))
263 return hr;
264 NineUnknown_ConvertRefToBind(NineUnknown(This->state.rt[i]));
265 }
266
267 /* Initialize a dummy VBO to be used when a vertex declaration does not
268 * specify all the inputs needed by vertex shader, on win default behavior
269 * is to pass 0,0,0,0 to the shader */
270 {
271 struct pipe_transfer *transfer;
272 struct pipe_resource tmpl;
273 struct pipe_box box;
274 unsigned char *data;
275
276 memset(&tmpl, 0, sizeof(tmpl));
277 tmpl.target = PIPE_BUFFER;
278 tmpl.format = PIPE_FORMAT_R8_UNORM;
279 tmpl.width0 = 16; /* 4 floats */
280 tmpl.height0 = 1;
281 tmpl.depth0 = 1;
282 tmpl.array_size = 1;
283 tmpl.last_level = 0;
284 tmpl.nr_samples = 0;
285 tmpl.usage = PIPE_USAGE_DEFAULT;
286 tmpl.bind = PIPE_BIND_VERTEX_BUFFER;
287 tmpl.flags = 0;
288 This->dummy_vbo = pScreen->resource_create(pScreen, &tmpl);
289
290 if (!This->dummy_vbo)
291 return D3DERR_OUTOFVIDEOMEMORY;
292
293 u_box_1d(0, 16, &box);
294 data = This->pipe->transfer_map(This->pipe, This->dummy_vbo, 0,
295 PIPE_TRANSFER_WRITE |
296 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE,
297 &box, &transfer);
298 assert(data);
299 assert(transfer);
300 memset(data, 0, 16);
301 This->pipe->transfer_unmap(This->pipe, transfer);
302 }
303
304 This->cursor.software = FALSE;
305 This->cursor.hotspot.x = -1;
306 This->cursor.hotspot.y = -1;
307 {
308 struct pipe_resource tmpl;
309 memset(&tmpl, 0, sizeof(tmpl));
310 tmpl.target = PIPE_TEXTURE_2D;
311 tmpl.format = PIPE_FORMAT_R8G8B8A8_UNORM;
312 tmpl.width0 = 64;
313 tmpl.height0 = 64;
314 tmpl.depth0 = 1;
315 tmpl.array_size = 1;
316 tmpl.last_level = 0;
317 tmpl.nr_samples = 0;
318 tmpl.usage = PIPE_USAGE_DEFAULT;
319 tmpl.bind = PIPE_BIND_CURSOR | PIPE_BIND_SAMPLER_VIEW;
320 tmpl.flags = 0;
321
322 This->cursor.image = pScreen->resource_create(pScreen, &tmpl);
323 if (!This->cursor.image)
324 return D3DERR_OUTOFVIDEOMEMORY;
325 }
326
327 /* Create constant buffers. */
328 {
329 unsigned max_const_vs, max_const_ps;
330
331 /* vs 3.0: >= 256 float constants, but for cards with exactly 256 slots,
332 * we have to take in some more slots for int and bool*/
333 max_const_vs = _min(pScreen->get_shader_param(pScreen, PIPE_SHADER_VERTEX,
334 PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE) /
335 sizeof(float[4]),
336 NINE_MAX_CONST_ALL);
337 /* ps 3.0: 224 float constants. All cards supported support at least
338 * 256 constants for ps */
339 max_const_ps = NINE_MAX_CONST_F_PS3 + (NINE_MAX_CONST_I + NINE_MAX_CONST_B / 4);
340
341 This->max_vs_const_f = max_const_vs -
342 (NINE_MAX_CONST_I + NINE_MAX_CONST_B / 4);
343 This->max_ps_const_f = max_const_ps -
344 (NINE_MAX_CONST_I + NINE_MAX_CONST_B / 4);
345
346 This->vs_const_size = max_const_vs * sizeof(float[4]);
347 This->ps_const_size = max_const_ps * sizeof(float[4]);
348 /* Include space for I,B constants for user constbuf. */
349 if (This->may_swvp) {
350 This->state.vs_const_f_swvp = CALLOC(NINE_MAX_CONST_F_SWVP * sizeof(float[4]),1);
351 if (!This->state.vs_const_f_swvp)
352 return E_OUTOFMEMORY;
353 This->state.vs_lconstf_temp = CALLOC(NINE_MAX_CONST_F_SWVP * sizeof(float[4]),1);
354 This->state.vs_const_i = CALLOC(NINE_MAX_CONST_I_SWVP * sizeof(int[4]), 1);
355 This->state.vs_const_b = CALLOC(NINE_MAX_CONST_B_SWVP * sizeof(BOOL), 1);
356 } else {
357 This->state.vs_const_f_swvp = NULL;
358 This->state.vs_lconstf_temp = CALLOC(This->vs_const_size,1);
359 This->state.vs_const_i = CALLOC(NINE_MAX_CONST_I * sizeof(int[4]), 1);
360 This->state.vs_const_b = CALLOC(NINE_MAX_CONST_B * sizeof(BOOL), 1);
361 }
362 This->state.vs_const_f = CALLOC(This->vs_const_size, 1);
363 This->state.ps_const_f = CALLOC(This->ps_const_size, 1);
364 This->state.ps_lconstf_temp = CALLOC(This->ps_const_size,1);
365 if (!This->state.vs_const_f || !This->state.ps_const_f ||
366 !This->state.vs_lconstf_temp || !This->state.ps_lconstf_temp ||
367 !This->state.vs_const_i || !This->state.vs_const_b)
368 return E_OUTOFMEMORY;
369
370 if (strstr(pScreen->get_name(pScreen), "AMD") ||
371 strstr(pScreen->get_name(pScreen), "ATI")) {
372 This->driver_bugs.buggy_barycentrics = TRUE;
373 }
374 }
375
376 /* allocate dummy texture/sampler for when there are missing ones bound */
377 {
378 struct pipe_resource tmplt;
379 struct pipe_sampler_view templ;
380 struct pipe_sampler_state samp;
381 memset(&tmplt, 0, sizeof(tmplt));
382 memset(&samp, 0, sizeof(samp));
383
384 tmplt.target = PIPE_TEXTURE_2D;
385 tmplt.width0 = 1;
386 tmplt.height0 = 1;
387 tmplt.depth0 = 1;
388 tmplt.last_level = 0;
389 tmplt.array_size = 1;
390 tmplt.usage = PIPE_USAGE_DEFAULT;
391 tmplt.flags = 0;
392 tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM;
393 tmplt.bind = PIPE_BIND_SAMPLER_VIEW;
394 tmplt.nr_samples = 0;
395
396 This->dummy_texture = This->screen->resource_create(This->screen, &tmplt);
397 if (!This->dummy_texture)
398 return D3DERR_DRIVERINTERNALERROR;
399
400 templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
401 templ.u.tex.first_layer = 0;
402 templ.u.tex.last_layer = 0;
403 templ.u.tex.first_level = 0;
404 templ.u.tex.last_level = 0;
405 templ.swizzle_r = PIPE_SWIZZLE_0;
406 templ.swizzle_g = PIPE_SWIZZLE_0;
407 templ.swizzle_b = PIPE_SWIZZLE_0;
408 templ.swizzle_a = PIPE_SWIZZLE_1;
409 templ.target = This->dummy_texture->target;
410
411 This->dummy_sampler_view = This->pipe->create_sampler_view(This->pipe, This->dummy_texture, &templ);
412 if (!This->dummy_sampler_view)
413 return D3DERR_DRIVERINTERNALERROR;
414
415 samp.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
416 samp.max_lod = 15.0f;
417 samp.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
418 samp.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
419 samp.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
420 samp.min_img_filter = PIPE_TEX_FILTER_NEAREST;
421 samp.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
422 samp.compare_mode = PIPE_TEX_COMPARE_NONE;
423 samp.compare_func = PIPE_FUNC_LEQUAL;
424 samp.normalized_coords = 1;
425 samp.seamless_cube_map = 0;
426 This->dummy_sampler_state = samp;
427 }
428
429 /* Allocate upload helper for drivers that suck (from st pov ;). */
430
431 This->driver_caps.user_vbufs = GET_PCAP(USER_VERTEX_BUFFERS);
432 This->driver_caps.user_ibufs = GET_PCAP(USER_INDEX_BUFFERS);
433 This->driver_caps.user_cbufs = GET_PCAP(USER_CONSTANT_BUFFERS);
434 This->driver_caps.user_sw_vbufs = This->screen_sw->get_param(This->screen_sw, PIPE_CAP_USER_VERTEX_BUFFERS);
435 This->driver_caps.user_sw_cbufs = This->screen_sw->get_param(This->screen_sw, PIPE_CAP_USER_CONSTANT_BUFFERS);
436
437 if (!This->driver_caps.user_vbufs)
438 This->vertex_uploader = u_upload_create(This->pipe, 65536,
439 PIPE_BIND_VERTEX_BUFFER, PIPE_USAGE_STREAM);
440 This->vertex_sw_uploader = u_upload_create(This->pipe_sw, 65536,
441 PIPE_BIND_VERTEX_BUFFER, PIPE_USAGE_STREAM);
442 if (!This->driver_caps.user_ibufs)
443 This->index_uploader = u_upload_create(This->pipe, 128 * 1024,
444 PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_STREAM);
445 if (!This->driver_caps.user_cbufs) {
446 This->constbuf_alignment = GET_PCAP(CONSTANT_BUFFER_OFFSET_ALIGNMENT);
447 This->constbuf_uploader = u_upload_create(This->pipe, This->vs_const_size,
448 PIPE_BIND_CONSTANT_BUFFER, PIPE_USAGE_STREAM);
449 }
450
451 This->constbuf_sw_uploader = u_upload_create(This->pipe_sw, 128 * 1024,
452 PIPE_BIND_CONSTANT_BUFFER, PIPE_USAGE_STREAM);
453
454 This->driver_caps.window_space_position_support = GET_PCAP(TGSI_VS_WINDOW_SPACE_POSITION);
455 This->driver_caps.vs_integer = pScreen->get_shader_param(pScreen, PIPE_SHADER_VERTEX, PIPE_SHADER_CAP_INTEGERS);
456 This->driver_caps.ps_integer = pScreen->get_shader_param(pScreen, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_INTEGERS);
457 This->driver_caps.offset_units_unscaled = GET_PCAP(POLYGON_OFFSET_UNITS_UNSCALED);
458
459 nine_ff_init(This); /* initialize fixed function code */
460
461 NineDevice9_SetDefaultState(This, FALSE);
462
463 {
464 struct pipe_poly_stipple stipple;
465 memset(&stipple, ~0, sizeof(stipple));
466 This->pipe->set_polygon_stipple(This->pipe, &stipple);
467 }
468
469 This->update = &This->state;
470 nine_update_state(This);
471
472 nine_state_init_sw(This);
473
474 ID3DPresentGroup_Release(This->present);
475
476 return D3D_OK;
477 }
478 #undef GET_PCAP
479
480 void
481 NineDevice9_dtor( struct NineDevice9 *This )
482 {
483 unsigned i;
484
485 DBG("This=%p\n", This);
486
487 if (This->pipe && This->cso)
488 nine_pipe_context_clear(This);
489 nine_ff_fini(This);
490 nine_state_destroy_sw(This);
491 nine_state_clear(&This->state, TRUE);
492
493 if (This->vertex_uploader)
494 u_upload_destroy(This->vertex_uploader);
495 if (This->index_uploader)
496 u_upload_destroy(This->index_uploader);
497 if (This->constbuf_uploader)
498 u_upload_destroy(This->constbuf_uploader);
499 if (This->vertex_sw_uploader)
500 u_upload_destroy(This->vertex_sw_uploader);
501 if (This->constbuf_sw_uploader)
502 u_upload_destroy(This->constbuf_sw_uploader);
503
504 nine_bind(&This->record, NULL);
505
506 pipe_sampler_view_reference(&This->dummy_sampler_view, NULL);
507 pipe_resource_reference(&This->dummy_texture, NULL);
508 pipe_resource_reference(&This->dummy_vbo, NULL);
509 FREE(This->state.vs_const_f);
510 FREE(This->state.ps_const_f);
511 FREE(This->state.vs_lconstf_temp);
512 FREE(This->state.ps_lconstf_temp);
513 FREE(This->state.vs_const_i);
514 FREE(This->state.vs_const_b);
515 FREE(This->state.vs_const_f_swvp);
516
517 if (This->swapchains) {
518 for (i = 0; i < This->nswapchains; ++i)
519 if (This->swapchains[i])
520 NineUnknown_Unbind(NineUnknown(This->swapchains[i]));
521 FREE(This->swapchains);
522 }
523
524 /* Destroy cso first */
525 if (This->cso) { cso_destroy_context(This->cso); }
526 if (This->cso_sw) { cso_destroy_context(This->cso_sw); }
527 if (This->pipe && This->pipe->destroy) { This->pipe->destroy(This->pipe); }
528 if (This->pipe_sw && This->pipe_sw->destroy) { This->pipe_sw->destroy(This->pipe_sw); }
529
530 if (This->present) { ID3DPresentGroup_Release(This->present); }
531 if (This->d3d9) { IDirect3D9_Release(This->d3d9); }
532
533 NineUnknown_dtor(&This->base);
534 }
535
536 struct pipe_screen *
537 NineDevice9_GetScreen( struct NineDevice9 *This )
538 {
539 return This->screen;
540 }
541
542 struct pipe_context *
543 NineDevice9_GetPipe( struct NineDevice9 *This )
544 {
545 return This->pipe;
546 }
547
548 struct cso_context *
549 NineDevice9_GetCSO( struct NineDevice9 *This )
550 {
551 return This->cso;
552 }
553
554 const D3DCAPS9 *
555 NineDevice9_GetCaps( struct NineDevice9 *This )
556 {
557 return &This->caps;
558 }
559
560 static inline void
561 NineDevice9_PauseRecording( struct NineDevice9 *This )
562 {
563 if (This->record) {
564 This->update = &This->state;
565 This->is_recording = FALSE;
566 }
567 }
568
569 static inline void
570 NineDevice9_ResumeRecording( struct NineDevice9 *This )
571 {
572 if (This->record) {
573 This->update = &This->record->state;
574 This->is_recording = TRUE;
575 }
576 }
577
578 HRESULT NINE_WINAPI
579 NineDevice9_TestCooperativeLevel( struct NineDevice9 *This )
580 {
581 if (NineSwapChain9_GetOccluded(This->swapchains[0])) {
582 This->device_needs_reset = TRUE;
583 return D3DERR_DEVICELOST;
584 } else if (NineSwapChain9_ResolutionMismatch(This->swapchains[0])) {
585 This->device_needs_reset = TRUE;
586 return D3DERR_DEVICENOTRESET;
587 } else if (This->device_needs_reset) {
588 return D3DERR_DEVICENOTRESET;
589 }
590
591 return D3D_OK;
592 }
593
594 UINT NINE_WINAPI
595 NineDevice9_GetAvailableTextureMem( struct NineDevice9 *This )
596 {
597 return This->available_texture_mem;
598 }
599
600 HRESULT NINE_WINAPI
601 NineDevice9_EvictManagedResources( struct NineDevice9 *This )
602 {
603 struct NineBaseTexture9 *tex;
604 struct NineBuffer9 *buf;
605
606 DBG("This=%p\n", This);
607 LIST_FOR_EACH_ENTRY(tex, &This->managed_textures, list2) {
608 NineBaseTexture9_UnLoad(tex);
609 }
610 /* Vertex/index buffers don't take a lot of space and aren't accounted
611 * for d3d memory usage. Instead of actually freeing from memory,
612 * just mark the buffer dirty to trigger a re-upload later. We
613 * could just ignore, but some bad behaving apps could rely on it (if
614 * they write outside the locked regions typically). */
615 LIST_FOR_EACH_ENTRY(buf, &This->managed_buffers, managed.list2) {
616 NineBuffer9_SetDirty(buf);
617 }
618
619 return D3D_OK;
620 }
621
622 HRESULT NINE_WINAPI
623 NineDevice9_GetDirect3D( struct NineDevice9 *This,
624 IDirect3D9 **ppD3D9 )
625 {
626 user_assert(ppD3D9 != NULL, E_POINTER);
627 IDirect3D9_AddRef(This->d3d9);
628 *ppD3D9 = This->d3d9;
629 return D3D_OK;
630 }
631
632 HRESULT NINE_WINAPI
633 NineDevice9_GetDeviceCaps( struct NineDevice9 *This,
634 D3DCAPS9 *pCaps )
635 {
636 user_assert(pCaps != NULL, D3DERR_INVALIDCALL);
637 *pCaps = This->caps;
638 return D3D_OK;
639 }
640
641 HRESULT NINE_WINAPI
642 NineDevice9_GetDisplayMode( struct NineDevice9 *This,
643 UINT iSwapChain,
644 D3DDISPLAYMODE *pMode )
645 {
646 DBG("This=%p iSwapChain=%u pMode=%p\n", This, iSwapChain, pMode);
647
648 user_assert(iSwapChain < This->nswapchains, D3DERR_INVALIDCALL);
649
650 return NineSwapChain9_GetDisplayMode(This->swapchains[iSwapChain], pMode);
651 }
652
653 HRESULT NINE_WINAPI
654 NineDevice9_GetCreationParameters( struct NineDevice9 *This,
655 D3DDEVICE_CREATION_PARAMETERS *pParameters )
656 {
657 user_assert(pParameters != NULL, D3DERR_INVALIDCALL);
658 *pParameters = This->params;
659 return D3D_OK;
660 }
661
662 HRESULT NINE_WINAPI
663 NineDevice9_SetCursorProperties( struct NineDevice9 *This,
664 UINT XHotSpot,
665 UINT YHotSpot,
666 IDirect3DSurface9 *pCursorBitmap )
667 {
668 struct NineSurface9 *surf = NineSurface9(pCursorBitmap);
669 struct pipe_context *pipe = This->pipe;
670 struct pipe_box box;
671 struct pipe_transfer *transfer;
672 BOOL hw_cursor;
673 void *ptr;
674
675 DBG_FLAG(DBG_SWAPCHAIN, "This=%p XHotSpot=%u YHotSpot=%u "
676 "pCursorBitmap=%p\n", This, XHotSpot, YHotSpot, pCursorBitmap);
677
678 user_assert(pCursorBitmap, D3DERR_INVALIDCALL);
679 user_assert(surf->desc.Format == D3DFMT_A8R8G8B8, D3DERR_INVALIDCALL);
680
681 if (This->swapchains[0]->params.Windowed) {
682 This->cursor.w = MIN2(surf->desc.Width, 32);
683 This->cursor.h = MIN2(surf->desc.Height, 32);
684 hw_cursor = 1; /* always use hw cursor for windowed mode */
685 } else {
686 This->cursor.w = MIN2(surf->desc.Width, This->cursor.image->width0);
687 This->cursor.h = MIN2(surf->desc.Height, This->cursor.image->height0);
688 hw_cursor = This->cursor.w == 32 && This->cursor.h == 32;
689 }
690
691 u_box_origin_2d(This->cursor.w, This->cursor.h, &box);
692
693 ptr = pipe->transfer_map(pipe, This->cursor.image, 0,
694 PIPE_TRANSFER_WRITE |
695 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE,
696 &box, &transfer);
697 if (!ptr)
698 ret_err("Failed to update cursor image.\n", D3DERR_DRIVERINTERNALERROR);
699
700 This->cursor.hotspot.x = XHotSpot;
701 This->cursor.hotspot.y = YHotSpot;
702
703 /* Copy cursor image to internal storage. */
704 {
705 D3DLOCKED_RECT lock;
706 HRESULT hr;
707 const struct util_format_description *sfmt =
708 util_format_description(surf->base.info.format);
709 assert(sfmt);
710
711 hr = NineSurface9_LockRect(surf, &lock, NULL, D3DLOCK_READONLY);
712 if (FAILED(hr))
713 ret_err("Failed to map cursor source image.\n",
714 D3DERR_DRIVERINTERNALERROR);
715
716 sfmt->unpack_rgba_8unorm(ptr, transfer->stride,
717 lock.pBits, lock.Pitch,
718 This->cursor.w, This->cursor.h);
719
720 if (hw_cursor)
721 hw_cursor = ID3DPresent_SetCursor(This->swapchains[0]->present,
722 lock.pBits,
723 &This->cursor.hotspot,
724 This->cursor.visible) == D3D_OK;
725
726 NineSurface9_UnlockRect(surf);
727 }
728 pipe->transfer_unmap(pipe, transfer);
729
730 /* hide cursor if we emulate it */
731 if (!hw_cursor)
732 ID3DPresent_SetCursor(This->swapchains[0]->present, NULL, NULL, FALSE);
733 This->cursor.software = !hw_cursor;
734
735 return D3D_OK;
736 }
737
738 void NINE_WINAPI
739 NineDevice9_SetCursorPosition( struct NineDevice9 *This,
740 int X,
741 int Y,
742 DWORD Flags )
743 {
744 struct NineSwapChain9 *swap = This->swapchains[0];
745
746 DBG("This=%p X=%d Y=%d Flags=%d\n", This, X, Y, Flags);
747
748 This->cursor.pos.x = X;
749 This->cursor.pos.y = Y;
750
751 if (!This->cursor.software)
752 This->cursor.software = ID3DPresent_SetCursorPos(swap->present, &This->cursor.pos) != D3D_OK;
753 }
754
755 BOOL NINE_WINAPI
756 NineDevice9_ShowCursor( struct NineDevice9 *This,
757 BOOL bShow )
758 {
759 BOOL old = This->cursor.visible;
760
761 DBG("This=%p bShow=%d\n", This, (int) bShow);
762
763 This->cursor.visible = bShow && (This->cursor.hotspot.x != -1);
764 if (!This->cursor.software)
765 This->cursor.software = ID3DPresent_SetCursor(This->swapchains[0]->present, NULL, NULL, bShow) != D3D_OK;
766
767 return old;
768 }
769
770 HRESULT NINE_WINAPI
771 NineDevice9_CreateAdditionalSwapChain( struct NineDevice9 *This,
772 D3DPRESENT_PARAMETERS *pPresentationParameters,
773 IDirect3DSwapChain9 **pSwapChain )
774 {
775 struct NineSwapChain9 *swapchain, *tmplt = This->swapchains[0];
776 ID3DPresent *present;
777 HRESULT hr;
778
779 DBG("This=%p pPresentationParameters=%p pSwapChain=%p\n",
780 This, pPresentationParameters, pSwapChain);
781
782 user_assert(pPresentationParameters, D3DERR_INVALIDCALL);
783 user_assert(tmplt->params.Windowed && pPresentationParameters->Windowed, D3DERR_INVALIDCALL);
784
785 /* TODO: this deserves more tests */
786 if (!pPresentationParameters->hDeviceWindow)
787 pPresentationParameters->hDeviceWindow = This->params.hFocusWindow;
788
789 hr = ID3DPresentGroup_CreateAdditionalPresent(This->present, pPresentationParameters, &present);
790
791 if (FAILED(hr))
792 return hr;
793
794 hr = NineSwapChain9_new(This, FALSE, present, pPresentationParameters,
795 tmplt->actx,
796 tmplt->params.hDeviceWindow,
797 &swapchain);
798 if (FAILED(hr))
799 return hr;
800
801 *pSwapChain = (IDirect3DSwapChain9 *)swapchain;
802 return D3D_OK;
803 }
804
805 HRESULT NINE_WINAPI
806 NineDevice9_GetSwapChain( struct NineDevice9 *This,
807 UINT iSwapChain,
808 IDirect3DSwapChain9 **pSwapChain )
809 {
810 user_assert(pSwapChain != NULL, D3DERR_INVALIDCALL);
811
812 *pSwapChain = NULL;
813 user_assert(iSwapChain < This->nswapchains, D3DERR_INVALIDCALL);
814
815 NineUnknown_AddRef(NineUnknown(This->swapchains[iSwapChain]));
816 *pSwapChain = (IDirect3DSwapChain9 *)This->swapchains[iSwapChain];
817
818 return D3D_OK;
819 }
820
821 UINT NINE_WINAPI
822 NineDevice9_GetNumberOfSwapChains( struct NineDevice9 *This )
823 {
824 return This->nswapchains;
825 }
826
827 HRESULT NINE_WINAPI
828 NineDevice9_Reset( struct NineDevice9 *This,
829 D3DPRESENT_PARAMETERS *pPresentationParameters )
830 {
831 HRESULT hr = D3D_OK;
832 unsigned i;
833
834 DBG("This=%p pPresentationParameters=%p\n", This, pPresentationParameters);
835
836 if (NineSwapChain9_GetOccluded(This->swapchains[0])) {
837 This->device_needs_reset = TRUE;
838 return D3DERR_DEVICELOST;
839 }
840
841 for (i = 0; i < This->nswapchains; ++i) {
842 D3DPRESENT_PARAMETERS *params = &pPresentationParameters[i];
843 hr = NineSwapChain9_Resize(This->swapchains[i], params, NULL);
844 if (hr != D3D_OK)
845 break;
846 }
847
848 nine_pipe_context_clear(This);
849 nine_state_clear(&This->state, TRUE);
850
851 NineDevice9_SetDefaultState(This, TRUE);
852 NineDevice9_SetRenderTarget(
853 This, 0, (IDirect3DSurface9 *)This->swapchains[0]->buffers[0]);
854 /* XXX: better use GetBackBuffer here ? */
855
856 This->device_needs_reset = (hr != D3D_OK);
857 return hr;
858 }
859
860 HRESULT NINE_WINAPI
861 NineDevice9_Present( struct NineDevice9 *This,
862 const RECT *pSourceRect,
863 const RECT *pDestRect,
864 HWND hDestWindowOverride,
865 const RGNDATA *pDirtyRegion )
866 {
867 unsigned i;
868 HRESULT hr;
869
870 DBG("This=%p pSourceRect=%p pDestRect=%p hDestWindowOverride=%p pDirtyRegion=%p\n",
871 This, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
872
873 /* XXX is this right? */
874 for (i = 0; i < This->nswapchains; ++i) {
875 hr = NineSwapChain9_Present(This->swapchains[i], pSourceRect, pDestRect,
876 hDestWindowOverride, pDirtyRegion, 0);
877 if (FAILED(hr)) { return hr; }
878 }
879
880 return D3D_OK;
881 }
882
883 HRESULT NINE_WINAPI
884 NineDevice9_GetBackBuffer( struct NineDevice9 *This,
885 UINT iSwapChain,
886 UINT iBackBuffer,
887 D3DBACKBUFFER_TYPE Type,
888 IDirect3DSurface9 **ppBackBuffer )
889 {
890 user_assert(ppBackBuffer != NULL, D3DERR_INVALIDCALL);
891 /* return NULL on error */
892 *ppBackBuffer = NULL;
893 user_assert(iSwapChain < This->nswapchains, D3DERR_INVALIDCALL);
894
895 return NineSwapChain9_GetBackBuffer(This->swapchains[iSwapChain],
896 iBackBuffer, Type, ppBackBuffer);
897 }
898
899 HRESULT NINE_WINAPI
900 NineDevice9_GetRasterStatus( struct NineDevice9 *This,
901 UINT iSwapChain,
902 D3DRASTER_STATUS *pRasterStatus )
903 {
904 user_assert(pRasterStatus != NULL, D3DERR_INVALIDCALL);
905 user_assert(iSwapChain < This->nswapchains, D3DERR_INVALIDCALL);
906
907 return NineSwapChain9_GetRasterStatus(This->swapchains[iSwapChain],
908 pRasterStatus);
909 }
910
911 HRESULT NINE_WINAPI
912 NineDevice9_SetDialogBoxMode( struct NineDevice9 *This,
913 BOOL bEnableDialogs )
914 {
915 STUB(D3DERR_INVALIDCALL);
916 }
917
918 void NINE_WINAPI
919 NineDevice9_SetGammaRamp( struct NineDevice9 *This,
920 UINT iSwapChain,
921 DWORD Flags,
922 const D3DGAMMARAMP *pRamp )
923 {
924 DBG("This=%p iSwapChain=%u Flags=%x pRamp=%p\n", This,
925 iSwapChain, Flags, pRamp);
926
927 user_warn(iSwapChain >= This->nswapchains);
928 user_warn(!pRamp);
929
930 if (pRamp && (iSwapChain < This->nswapchains)) {
931 struct NineSwapChain9 *swap = This->swapchains[iSwapChain];
932 swap->gamma = *pRamp;
933 ID3DPresent_SetGammaRamp(swap->present, pRamp, swap->params.hDeviceWindow);
934 }
935 }
936
937 void NINE_WINAPI
938 NineDevice9_GetGammaRamp( struct NineDevice9 *This,
939 UINT iSwapChain,
940 D3DGAMMARAMP *pRamp )
941 {
942 DBG("This=%p iSwapChain=%u pRamp=%p\n", This, iSwapChain, pRamp);
943
944 user_warn(iSwapChain >= This->nswapchains);
945 user_warn(!pRamp);
946
947 if (pRamp && (iSwapChain < This->nswapchains))
948 *pRamp = This->swapchains[iSwapChain]->gamma;
949 }
950
951 HRESULT NINE_WINAPI
952 NineDevice9_CreateTexture( struct NineDevice9 *This,
953 UINT Width,
954 UINT Height,
955 UINT Levels,
956 DWORD Usage,
957 D3DFORMAT Format,
958 D3DPOOL Pool,
959 IDirect3DTexture9 **ppTexture,
960 HANDLE *pSharedHandle )
961 {
962 struct NineTexture9 *tex;
963 HRESULT hr;
964
965 DBG("This=%p Width=%u Height=%u Levels=%u Usage=%s Format=%s Pool=%s "
966 "ppOut=%p pSharedHandle=%p\n", This, Width, Height, Levels,
967 nine_D3DUSAGE_to_str(Usage), d3dformat_to_string(Format),
968 nine_D3DPOOL_to_str(Pool), ppTexture, pSharedHandle);
969
970 Usage &= D3DUSAGE_AUTOGENMIPMAP | D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_DMAP |
971 D3DUSAGE_DYNAMIC | D3DUSAGE_NONSECURE | D3DUSAGE_RENDERTARGET |
972 D3DUSAGE_SOFTWAREPROCESSING | D3DUSAGE_TEXTAPI;
973
974 *ppTexture = NULL;
975
976 hr = NineTexture9_new(This, Width, Height, Levels, Usage, Format, Pool,
977 &tex, pSharedHandle);
978 if (SUCCEEDED(hr))
979 *ppTexture = (IDirect3DTexture9 *)tex;
980
981 return hr;
982 }
983
984 HRESULT NINE_WINAPI
985 NineDevice9_CreateVolumeTexture( struct NineDevice9 *This,
986 UINT Width,
987 UINT Height,
988 UINT Depth,
989 UINT Levels,
990 DWORD Usage,
991 D3DFORMAT Format,
992 D3DPOOL Pool,
993 IDirect3DVolumeTexture9 **ppVolumeTexture,
994 HANDLE *pSharedHandle )
995 {
996 struct NineVolumeTexture9 *tex;
997 HRESULT hr;
998
999 DBG("This=%p Width=%u Height=%u Depth=%u Levels=%u Usage=%s Format=%s Pool=%s "
1000 "ppOut=%p pSharedHandle=%p\n", This, Width, Height, Depth, Levels,
1001 nine_D3DUSAGE_to_str(Usage), d3dformat_to_string(Format),
1002 nine_D3DPOOL_to_str(Pool), ppVolumeTexture, pSharedHandle);
1003
1004 Usage &= D3DUSAGE_DYNAMIC | D3DUSAGE_NONSECURE |
1005 D3DUSAGE_SOFTWAREPROCESSING;
1006
1007 *ppVolumeTexture = NULL;
1008
1009 hr = NineVolumeTexture9_new(This, Width, Height, Depth, Levels,
1010 Usage, Format, Pool, &tex, pSharedHandle);
1011 if (SUCCEEDED(hr))
1012 *ppVolumeTexture = (IDirect3DVolumeTexture9 *)tex;
1013
1014 return hr;
1015 }
1016
1017 HRESULT NINE_WINAPI
1018 NineDevice9_CreateCubeTexture( struct NineDevice9 *This,
1019 UINT EdgeLength,
1020 UINT Levels,
1021 DWORD Usage,
1022 D3DFORMAT Format,
1023 D3DPOOL Pool,
1024 IDirect3DCubeTexture9 **ppCubeTexture,
1025 HANDLE *pSharedHandle )
1026 {
1027 struct NineCubeTexture9 *tex;
1028 HRESULT hr;
1029
1030 DBG("This=%p EdgeLength=%u Levels=%u Usage=%s Format=%s Pool=%s ppOut=%p "
1031 "pSharedHandle=%p\n", This, EdgeLength, Levels,
1032 nine_D3DUSAGE_to_str(Usage), d3dformat_to_string(Format),
1033 nine_D3DPOOL_to_str(Pool), ppCubeTexture, pSharedHandle);
1034
1035 Usage &= D3DUSAGE_AUTOGENMIPMAP | D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_DYNAMIC |
1036 D3DUSAGE_NONSECURE | D3DUSAGE_RENDERTARGET |
1037 D3DUSAGE_SOFTWAREPROCESSING;
1038
1039 *ppCubeTexture = NULL;
1040
1041 hr = NineCubeTexture9_new(This, EdgeLength, Levels, Usage, Format, Pool,
1042 &tex, pSharedHandle);
1043 if (SUCCEEDED(hr))
1044 *ppCubeTexture = (IDirect3DCubeTexture9 *)tex;
1045
1046 return hr;
1047 }
1048
1049 HRESULT NINE_WINAPI
1050 NineDevice9_CreateVertexBuffer( struct NineDevice9 *This,
1051 UINT Length,
1052 DWORD Usage,
1053 DWORD FVF,
1054 D3DPOOL Pool,
1055 IDirect3DVertexBuffer9 **ppVertexBuffer,
1056 HANDLE *pSharedHandle )
1057 {
1058 struct NineVertexBuffer9 *buf;
1059 HRESULT hr;
1060 D3DVERTEXBUFFER_DESC desc;
1061
1062 DBG("This=%p Length=%u Usage=%x FVF=%x Pool=%u ppOut=%p pSharedHandle=%p\n",
1063 This, Length, Usage, FVF, Pool, ppVertexBuffer, pSharedHandle);
1064
1065 user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_NOTAVAILABLE);
1066
1067 desc.Format = D3DFMT_VERTEXDATA;
1068 desc.Type = D3DRTYPE_VERTEXBUFFER;
1069 desc.Usage = Usage &
1070 (D3DUSAGE_DONOTCLIP | D3DUSAGE_DYNAMIC | D3DUSAGE_NONSECURE |
1071 D3DUSAGE_NPATCHES | D3DUSAGE_POINTS | D3DUSAGE_RTPATCHES |
1072 D3DUSAGE_SOFTWAREPROCESSING | D3DUSAGE_TEXTAPI |
1073 D3DUSAGE_WRITEONLY);
1074 desc.Pool = Pool;
1075 desc.Size = Length;
1076 desc.FVF = FVF;
1077
1078 user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1079 user_assert(desc.Usage == Usage, D3DERR_INVALIDCALL);
1080
1081 hr = NineVertexBuffer9_new(This, &desc, &buf);
1082 if (SUCCEEDED(hr))
1083 *ppVertexBuffer = (IDirect3DVertexBuffer9 *)buf;
1084 return hr;
1085 }
1086
1087 HRESULT NINE_WINAPI
1088 NineDevice9_CreateIndexBuffer( struct NineDevice9 *This,
1089 UINT Length,
1090 DWORD Usage,
1091 D3DFORMAT Format,
1092 D3DPOOL Pool,
1093 IDirect3DIndexBuffer9 **ppIndexBuffer,
1094 HANDLE *pSharedHandle )
1095 {
1096 struct NineIndexBuffer9 *buf;
1097 HRESULT hr;
1098 D3DINDEXBUFFER_DESC desc;
1099
1100 DBG("This=%p Length=%u Usage=%x Format=%s Pool=%u ppOut=%p "
1101 "pSharedHandle=%p\n", This, Length, Usage,
1102 d3dformat_to_string(Format), Pool, ppIndexBuffer, pSharedHandle);
1103
1104 user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_NOTAVAILABLE);
1105
1106 desc.Format = Format;
1107 desc.Type = D3DRTYPE_INDEXBUFFER;
1108 desc.Usage = Usage &
1109 (D3DUSAGE_DONOTCLIP | D3DUSAGE_DYNAMIC | D3DUSAGE_NONSECURE |
1110 D3DUSAGE_NPATCHES | D3DUSAGE_POINTS | D3DUSAGE_RTPATCHES |
1111 D3DUSAGE_SOFTWAREPROCESSING | D3DUSAGE_WRITEONLY);
1112 desc.Pool = Pool;
1113 desc.Size = Length;
1114
1115 user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1116 user_assert(desc.Usage == Usage, D3DERR_INVALIDCALL);
1117
1118 hr = NineIndexBuffer9_new(This, &desc, &buf);
1119 if (SUCCEEDED(hr))
1120 *ppIndexBuffer = (IDirect3DIndexBuffer9 *)buf;
1121 return hr;
1122 }
1123
1124 static HRESULT
1125 create_zs_or_rt_surface(struct NineDevice9 *This,
1126 unsigned type, /* 0 = RT, 1 = ZS, 2 = plain */
1127 D3DPOOL Pool,
1128 UINT Width, UINT Height,
1129 D3DFORMAT Format,
1130 D3DMULTISAMPLE_TYPE MultiSample,
1131 DWORD MultisampleQuality,
1132 BOOL Discard_or_Lockable,
1133 IDirect3DSurface9 **ppSurface,
1134 HANDLE *pSharedHandle)
1135 {
1136 struct NineSurface9 *surface;
1137 struct pipe_screen *screen = This->screen;
1138 struct pipe_resource *resource = NULL;
1139 HRESULT hr;
1140 D3DSURFACE_DESC desc;
1141 struct pipe_resource templ;
1142
1143 DBG("This=%p type=%u Pool=%s Width=%u Height=%u Format=%s MS=%u Quality=%u "
1144 "Discard_or_Lockable=%i ppSurface=%p pSharedHandle=%p\n",
1145 This, type, nine_D3DPOOL_to_str(Pool), Width, Height,
1146 d3dformat_to_string(Format), MultiSample, MultisampleQuality,
1147 Discard_or_Lockable, ppSurface, pSharedHandle);
1148
1149 if (pSharedHandle)
1150 DBG("FIXME Used shared handle! This option isn't probably handled correctly!\n");
1151
1152 user_assert(Width && Height, D3DERR_INVALIDCALL);
1153 user_assert(Pool != D3DPOOL_MANAGED, D3DERR_INVALIDCALL);
1154
1155 memset(&templ, 0, sizeof(templ));
1156 templ.target = PIPE_TEXTURE_2D;
1157 templ.width0 = Width;
1158 templ.height0 = Height;
1159 templ.depth0 = 1;
1160 templ.array_size = 1;
1161 templ.last_level = 0;
1162 templ.nr_samples = (unsigned)MultiSample;
1163 templ.usage = PIPE_USAGE_DEFAULT;
1164 templ.flags = 0;
1165 templ.bind = PIPE_BIND_SAMPLER_VIEW; /* StretchRect */
1166 switch (type) {
1167 case 0: templ.bind |= PIPE_BIND_RENDER_TARGET; break;
1168 case 1: templ.bind = d3d9_get_pipe_depth_format_bindings(Format); break;
1169 default:
1170 assert(type == 2);
1171 break;
1172 }
1173 templ.format = d3d9_to_pipe_format_checked(screen, Format, templ.target,
1174 templ.nr_samples, templ.bind,
1175 FALSE, Pool == D3DPOOL_SCRATCH);
1176
1177 if (templ.format == PIPE_FORMAT_NONE && Format != D3DFMT_NULL)
1178 return D3DERR_INVALIDCALL;
1179
1180 desc.Format = Format;
1181 desc.Type = D3DRTYPE_SURFACE;
1182 desc.Usage = 0;
1183 desc.Pool = Pool;
1184 desc.MultiSampleType = MultiSample;
1185 desc.MultiSampleQuality = MultisampleQuality;
1186 desc.Width = Width;
1187 desc.Height = Height;
1188 switch (type) {
1189 case 0: desc.Usage = D3DUSAGE_RENDERTARGET; break;
1190 case 1: desc.Usage = D3DUSAGE_DEPTHSTENCIL; break;
1191 default: break;
1192 }
1193
1194 if (compressed_format(Format)) {
1195 const unsigned w = util_format_get_blockwidth(templ.format);
1196 const unsigned h = util_format_get_blockheight(templ.format);
1197
1198 user_assert(!(Width % w) && !(Height % h), D3DERR_INVALIDCALL);
1199 }
1200
1201 if (Pool == D3DPOOL_DEFAULT && Format != D3DFMT_NULL) {
1202 /* resource_create doesn't return an error code, so check format here */
1203 user_assert(templ.format != PIPE_FORMAT_NONE, D3DERR_INVALIDCALL);
1204 resource = screen->resource_create(screen, &templ);
1205 user_assert(resource, D3DERR_OUTOFVIDEOMEMORY);
1206 if (Discard_or_Lockable && (desc.Usage & D3DUSAGE_RENDERTARGET))
1207 resource->flags |= NINE_RESOURCE_FLAG_LOCKABLE;
1208 } else {
1209 resource = NULL;
1210 }
1211 hr = NineSurface9_new(This, NULL, resource, NULL, 0, 0, 0, &desc, &surface);
1212 pipe_resource_reference(&resource, NULL);
1213
1214 if (SUCCEEDED(hr))
1215 *ppSurface = (IDirect3DSurface9 *)surface;
1216 return hr;
1217 }
1218
1219 HRESULT NINE_WINAPI
1220 NineDevice9_CreateRenderTarget( struct NineDevice9 *This,
1221 UINT Width,
1222 UINT Height,
1223 D3DFORMAT Format,
1224 D3DMULTISAMPLE_TYPE MultiSample,
1225 DWORD MultisampleQuality,
1226 BOOL Lockable,
1227 IDirect3DSurface9 **ppSurface,
1228 HANDLE *pSharedHandle )
1229 {
1230 *ppSurface = NULL;
1231 return create_zs_or_rt_surface(This, 0, D3DPOOL_DEFAULT,
1232 Width, Height, Format,
1233 MultiSample, MultisampleQuality,
1234 Lockable, ppSurface, pSharedHandle);
1235 }
1236
1237 HRESULT NINE_WINAPI
1238 NineDevice9_CreateDepthStencilSurface( struct NineDevice9 *This,
1239 UINT Width,
1240 UINT Height,
1241 D3DFORMAT Format,
1242 D3DMULTISAMPLE_TYPE MultiSample,
1243 DWORD MultisampleQuality,
1244 BOOL Discard,
1245 IDirect3DSurface9 **ppSurface,
1246 HANDLE *pSharedHandle )
1247 {
1248 *ppSurface = NULL;
1249 if (!depth_stencil_format(Format))
1250 return D3DERR_NOTAVAILABLE;
1251 return create_zs_or_rt_surface(This, 1, D3DPOOL_DEFAULT,
1252 Width, Height, Format,
1253 MultiSample, MultisampleQuality,
1254 Discard, ppSurface, pSharedHandle);
1255 }
1256
1257 HRESULT NINE_WINAPI
1258 NineDevice9_UpdateSurface( struct NineDevice9 *This,
1259 IDirect3DSurface9 *pSourceSurface,
1260 const RECT *pSourceRect,
1261 IDirect3DSurface9 *pDestinationSurface,
1262 const POINT *pDestPoint )
1263 {
1264 struct NineSurface9 *dst = NineSurface9(pDestinationSurface);
1265 struct NineSurface9 *src = NineSurface9(pSourceSurface);
1266 int copy_width, copy_height;
1267 RECT destRect;
1268
1269 DBG("This=%p pSourceSurface=%p pDestinationSurface=%p "
1270 "pSourceRect=%p pDestPoint=%p\n", This,
1271 pSourceSurface, pDestinationSurface, pSourceRect, pDestPoint);
1272 if (pSourceRect)
1273 DBG("pSourceRect = (%u,%u)-(%u,%u)\n",
1274 pSourceRect->left, pSourceRect->top,
1275 pSourceRect->right, pSourceRect->bottom);
1276 if (pDestPoint)
1277 DBG("pDestPoint = (%u,%u)\n", pDestPoint->x, pDestPoint->y);
1278
1279 user_assert(dst && src, D3DERR_INVALIDCALL);
1280
1281 user_assert(dst->base.pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1282 user_assert(src->base.pool == D3DPOOL_SYSTEMMEM, D3DERR_INVALIDCALL);
1283
1284 user_assert(dst->desc.MultiSampleType == D3DMULTISAMPLE_NONE, D3DERR_INVALIDCALL);
1285 user_assert(src->desc.MultiSampleType == D3DMULTISAMPLE_NONE, D3DERR_INVALIDCALL);
1286
1287 user_assert(!src->lock_count, D3DERR_INVALIDCALL);
1288 user_assert(!dst->lock_count, D3DERR_INVALIDCALL);
1289
1290 user_assert(dst->desc.Format == src->desc.Format, D3DERR_INVALIDCALL);
1291 user_assert(!depth_stencil_format(dst->desc.Format), D3DERR_INVALIDCALL);
1292
1293 if (pSourceRect) {
1294 copy_width = pSourceRect->right - pSourceRect->left;
1295 copy_height = pSourceRect->bottom - pSourceRect->top;
1296
1297 user_assert(pSourceRect->left >= 0 &&
1298 copy_width > 0 &&
1299 pSourceRect->right <= src->desc.Width &&
1300 pSourceRect->top >= 0 &&
1301 copy_height > 0 &&
1302 pSourceRect->bottom <= src->desc.Height,
1303 D3DERR_INVALIDCALL);
1304 } else {
1305 copy_width = src->desc.Width;
1306 copy_height = src->desc.Height;
1307 }
1308
1309 destRect.right = copy_width;
1310 destRect.bottom = copy_height;
1311
1312 if (pDestPoint) {
1313 user_assert(pDestPoint->x >= 0 && pDestPoint->y >= 0,
1314 D3DERR_INVALIDCALL);
1315 destRect.right += pDestPoint->x;
1316 destRect.bottom += pDestPoint->y;
1317 }
1318
1319 user_assert(destRect.right <= dst->desc.Width &&
1320 destRect.bottom <= dst->desc.Height,
1321 D3DERR_INVALIDCALL);
1322
1323 if (compressed_format(dst->desc.Format)) {
1324 const unsigned w = util_format_get_blockwidth(dst->base.info.format);
1325 const unsigned h = util_format_get_blockheight(dst->base.info.format);
1326
1327 if (pDestPoint) {
1328 user_assert(!(pDestPoint->x % w) && !(pDestPoint->y % h),
1329 D3DERR_INVALIDCALL);
1330 }
1331
1332 if (pSourceRect) {
1333 user_assert(!(pSourceRect->left % w) && !(pSourceRect->top % h),
1334 D3DERR_INVALIDCALL);
1335 }
1336 if (!(copy_width == src->desc.Width &&
1337 copy_width == dst->desc.Width &&
1338 copy_height == src->desc.Height &&
1339 copy_height == dst->desc.Height)) {
1340 user_assert(!(copy_width % w) && !(copy_height % h),
1341 D3DERR_INVALIDCALL);
1342 }
1343 }
1344
1345 NineSurface9_CopyMemToDefault(dst, src, pDestPoint, pSourceRect);
1346
1347 return D3D_OK;
1348 }
1349
1350 HRESULT NINE_WINAPI
1351 NineDevice9_UpdateTexture( struct NineDevice9 *This,
1352 IDirect3DBaseTexture9 *pSourceTexture,
1353 IDirect3DBaseTexture9 *pDestinationTexture )
1354 {
1355 struct NineBaseTexture9 *dstb = NineBaseTexture9(pDestinationTexture);
1356 struct NineBaseTexture9 *srcb = NineBaseTexture9(pSourceTexture);
1357 unsigned l, m;
1358 unsigned last_src_level, last_dst_level;
1359 RECT rect;
1360
1361 DBG("This=%p pSourceTexture=%p pDestinationTexture=%p\n", This,
1362 pSourceTexture, pDestinationTexture);
1363
1364 user_assert(pSourceTexture && pDestinationTexture, D3DERR_INVALIDCALL);
1365 user_assert(pSourceTexture != pDestinationTexture, D3DERR_INVALIDCALL);
1366
1367 user_assert(dstb->base.pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1368 user_assert(srcb->base.pool == D3DPOOL_SYSTEMMEM, D3DERR_INVALIDCALL);
1369 user_assert(dstb->base.type == srcb->base.type, D3DERR_INVALIDCALL);
1370 user_assert(!(srcb->base.usage & D3DUSAGE_AUTOGENMIPMAP) ||
1371 dstb->base.usage & D3DUSAGE_AUTOGENMIPMAP, D3DERR_INVALIDCALL);
1372
1373 /* Spec: Failure if
1374 * . Different formats
1375 * . Fewer src levels than dst levels (if the opposite, only matching levels
1376 * are supposed to be copied)
1377 * . Levels do not match
1378 * DDI: Actually the above should pass because of legacy applications
1379 * Do what you want about these, but you shouldn't crash.
1380 * However driver can expect that the top dimension is greater for src than dst.
1381 * Wine tests: Every combination that passes the initial checks should pass.
1382 * . Different formats => conversion driver and format dependent.
1383 * . 1 level, but size not matching => copy is done (and even crash if src bigger
1384 * than dst. For the case where dst bigger, wine doesn't test if a stretch is applied
1385 * or if a subrect is copied).
1386 * . 8x8 4 sublevels -> 7x7 2 sublevels => driver dependent, On NV seems to be 4x4 subrect
1387 * copied to 7x7.
1388 *
1389 * From these, the proposal is:
1390 * . Different formats -> use util_format_translate to translate if possible for surfaces.
1391 * Accept ARGB/XRGB for Volumes. Do nothing for the other combinations
1392 * . First level copied -> the first level such that src is smaller or equal to dst first level
1393 * . number of levels copied -> as long as it fits and textures have levels
1394 * That should satisfy the constraints (and instead of crashing for some cases we return D3D_OK)
1395 */
1396
1397 last_src_level = (srcb->base.usage & D3DUSAGE_AUTOGENMIPMAP) ? 0 : srcb->base.info.last_level;
1398 last_dst_level = (dstb->base.usage & D3DUSAGE_AUTOGENMIPMAP) ? 0 : dstb->base.info.last_level;
1399
1400 for (m = 0; m <= last_src_level; ++m) {
1401 unsigned w = u_minify(srcb->base.info.width0, m);
1402 unsigned h = u_minify(srcb->base.info.height0, m);
1403 unsigned d = u_minify(srcb->base.info.depth0, m);
1404
1405 if (w <= dstb->base.info.width0 &&
1406 h <= dstb->base.info.height0 &&
1407 d <= dstb->base.info.depth0)
1408 break;
1409 }
1410 user_assert(m <= last_src_level, D3D_OK);
1411
1412 last_dst_level = MIN2(srcb->base.info.last_level - m, last_dst_level);
1413
1414 if (dstb->base.type == D3DRTYPE_TEXTURE) {
1415 struct NineTexture9 *dst = NineTexture9(dstb);
1416 struct NineTexture9 *src = NineTexture9(srcb);
1417
1418 if (src->dirty_rect.width == 0)
1419 return D3D_OK;
1420
1421 pipe_box_to_rect(&rect, &src->dirty_rect);
1422 for (l = 0; l < m; ++l)
1423 rect_minify_inclusive(&rect);
1424
1425 for (l = 0; l <= last_dst_level; ++l, ++m) {
1426 fit_rect_format_inclusive(dst->base.base.info.format,
1427 &rect,
1428 dst->surfaces[l]->desc.Width,
1429 dst->surfaces[l]->desc.Height);
1430 NineSurface9_CopyMemToDefault(dst->surfaces[l],
1431 src->surfaces[m],
1432 (POINT *)&rect,
1433 &rect);
1434 rect_minify_inclusive(&rect);
1435 }
1436 u_box_origin_2d(0, 0, &src->dirty_rect);
1437 } else
1438 if (dstb->base.type == D3DRTYPE_CUBETEXTURE) {
1439 struct NineCubeTexture9 *dst = NineCubeTexture9(dstb);
1440 struct NineCubeTexture9 *src = NineCubeTexture9(srcb);
1441 unsigned z;
1442
1443 /* GPUs usually have them stored as arrays of mip-mapped 2D textures. */
1444 for (z = 0; z < 6; ++z) {
1445 if (src->dirty_rect[z].width == 0)
1446 continue;
1447
1448 pipe_box_to_rect(&rect, &src->dirty_rect[z]);
1449 for (l = 0; l < m; ++l)
1450 rect_minify_inclusive(&rect);
1451
1452 for (l = 0; l <= last_dst_level; ++l, ++m) {
1453 fit_rect_format_inclusive(dst->base.base.info.format,
1454 &rect,
1455 dst->surfaces[l * 6 + z]->desc.Width,
1456 dst->surfaces[l * 6 + z]->desc.Height);
1457 NineSurface9_CopyMemToDefault(dst->surfaces[l * 6 + z],
1458 src->surfaces[m * 6 + z],
1459 (POINT *)&rect,
1460 &rect);
1461 rect_minify_inclusive(&rect);
1462 }
1463 u_box_origin_2d(0, 0, &src->dirty_rect[z]);
1464 m -= l;
1465 }
1466 } else
1467 if (dstb->base.type == D3DRTYPE_VOLUMETEXTURE) {
1468 struct NineVolumeTexture9 *dst = NineVolumeTexture9(dstb);
1469 struct NineVolumeTexture9 *src = NineVolumeTexture9(srcb);
1470
1471 if (src->dirty_box.width == 0)
1472 return D3D_OK;
1473 for (l = 0; l <= last_dst_level; ++l, ++m)
1474 NineVolume9_CopyMemToDefault(dst->volumes[l],
1475 src->volumes[m], 0, 0, 0, NULL);
1476 u_box_3d(0, 0, 0, 0, 0, 0, &src->dirty_box);
1477 } else{
1478 assert(!"invalid texture type");
1479 }
1480
1481 if (dstb->base.usage & D3DUSAGE_AUTOGENMIPMAP) {
1482 dstb->dirty_mip = TRUE;
1483 NineBaseTexture9_GenerateMipSubLevels(dstb);
1484 }
1485
1486 return D3D_OK;
1487 }
1488
1489 HRESULT NINE_WINAPI
1490 NineDevice9_GetRenderTargetData( struct NineDevice9 *This,
1491 IDirect3DSurface9 *pRenderTarget,
1492 IDirect3DSurface9 *pDestSurface )
1493 {
1494 struct NineSurface9 *dst = NineSurface9(pDestSurface);
1495 struct NineSurface9 *src = NineSurface9(pRenderTarget);
1496
1497 DBG("This=%p pRenderTarget=%p pDestSurface=%p\n",
1498 This, pRenderTarget, pDestSurface);
1499
1500 user_assert(dst->desc.Pool == D3DPOOL_SYSTEMMEM, D3DERR_INVALIDCALL);
1501 user_assert(src->desc.Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1502
1503 user_assert(dst->desc.MultiSampleType < 2, D3DERR_INVALIDCALL);
1504 user_assert(src->desc.MultiSampleType < 2, D3DERR_INVALIDCALL);
1505
1506 user_assert(src->desc.Width == dst->desc.Width, D3DERR_INVALIDCALL);
1507 user_assert(src->desc.Height == dst->desc.Height, D3DERR_INVALIDCALL);
1508
1509 NineSurface9_CopyDefaultToMem(dst, src);
1510
1511 return D3D_OK;
1512 }
1513
1514 HRESULT NINE_WINAPI
1515 NineDevice9_GetFrontBufferData( struct NineDevice9 *This,
1516 UINT iSwapChain,
1517 IDirect3DSurface9 *pDestSurface )
1518 {
1519 DBG("This=%p iSwapChain=%u pDestSurface=%p\n", This,
1520 iSwapChain, pDestSurface);
1521
1522 user_assert(pDestSurface != NULL, D3DERR_INVALIDCALL);
1523 user_assert(iSwapChain < This->nswapchains, D3DERR_INVALIDCALL);
1524
1525 return NineSwapChain9_GetFrontBufferData(This->swapchains[iSwapChain],
1526 pDestSurface);
1527 }
1528
1529 HRESULT NINE_WINAPI
1530 NineDevice9_StretchRect( struct NineDevice9 *This,
1531 IDirect3DSurface9 *pSourceSurface,
1532 const RECT *pSourceRect,
1533 IDirect3DSurface9 *pDestSurface,
1534 const RECT *pDestRect,
1535 D3DTEXTUREFILTERTYPE Filter )
1536 {
1537 struct pipe_screen *screen = This->screen;
1538 struct pipe_context *pipe = This->pipe;
1539 struct NineSurface9 *dst = NineSurface9(pDestSurface);
1540 struct NineSurface9 *src = NineSurface9(pSourceSurface);
1541 struct pipe_resource *dst_res = NineSurface9_GetResource(dst);
1542 struct pipe_resource *src_res = NineSurface9_GetResource(src);
1543 boolean zs;
1544 struct pipe_blit_info blit;
1545 boolean scaled, clamped, ms, flip_x = FALSE, flip_y = FALSE;
1546
1547 DBG("This=%p pSourceSurface=%p pSourceRect=%p pDestSurface=%p "
1548 "pDestRect=%p Filter=%u\n",
1549 This, pSourceSurface, pSourceRect, pDestSurface, pDestRect, Filter);
1550 if (pSourceRect)
1551 DBG("pSourceRect=(%u,%u)-(%u,%u)\n",
1552 pSourceRect->left, pSourceRect->top,
1553 pSourceRect->right, pSourceRect->bottom);
1554 if (pDestRect)
1555 DBG("pDestRect=(%u,%u)-(%u,%u)\n", pDestRect->left, pDestRect->top,
1556 pDestRect->right, pDestRect->bottom);
1557
1558 user_assert(dst->base.pool == D3DPOOL_DEFAULT &&
1559 src->base.pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1560 zs = util_format_is_depth_or_stencil(dst_res->format);
1561 user_assert(!zs || !This->in_scene, D3DERR_INVALIDCALL);
1562 user_assert(!zs || !pSourceRect ||
1563 (pSourceRect->left == 0 &&
1564 pSourceRect->top == 0 &&
1565 pSourceRect->right == src->desc.Width &&
1566 pSourceRect->bottom == src->desc.Height), D3DERR_INVALIDCALL);
1567 user_assert(!zs || !pDestRect ||
1568 (pDestRect->left == 0 &&
1569 pDestRect->top == 0 &&
1570 pDestRect->right == dst->desc.Width &&
1571 pDestRect->bottom == dst->desc.Height), D3DERR_INVALIDCALL);
1572 user_assert(!zs ||
1573 (dst->desc.Width == src->desc.Width &&
1574 dst->desc.Height == src->desc.Height), D3DERR_INVALIDCALL);
1575 user_assert(zs || !util_format_is_depth_or_stencil(src_res->format),
1576 D3DERR_INVALIDCALL);
1577 user_assert(!zs || dst->desc.Format == src->desc.Format,
1578 D3DERR_INVALIDCALL);
1579 user_assert(screen->is_format_supported(screen, src_res->format,
1580 src_res->target,
1581 src_res->nr_samples,
1582 PIPE_BIND_SAMPLER_VIEW),
1583 D3DERR_INVALIDCALL);
1584
1585 /* We might want to permit these, but wine thinks we shouldn't. */
1586 user_assert(!pDestRect ||
1587 (pDestRect->left <= pDestRect->right &&
1588 pDestRect->top <= pDestRect->bottom), D3DERR_INVALIDCALL);
1589 user_assert(!pSourceRect ||
1590 (pSourceRect->left <= pSourceRect->right &&
1591 pSourceRect->top <= pSourceRect->bottom), D3DERR_INVALIDCALL);
1592
1593 memset(&blit, 0, sizeof(blit));
1594 blit.dst.resource = dst_res;
1595 blit.dst.level = dst->level;
1596 blit.dst.box.z = dst->layer;
1597 blit.dst.box.depth = 1;
1598 blit.dst.format = dst_res->format;
1599 if (pDestRect) {
1600 flip_x = pDestRect->left > pDestRect->right;
1601 if (flip_x) {
1602 blit.dst.box.x = pDestRect->right;
1603 blit.dst.box.width = pDestRect->left - pDestRect->right;
1604 } else {
1605 blit.dst.box.x = pDestRect->left;
1606 blit.dst.box.width = pDestRect->right - pDestRect->left;
1607 }
1608 flip_y = pDestRect->top > pDestRect->bottom;
1609 if (flip_y) {
1610 blit.dst.box.y = pDestRect->bottom;
1611 blit.dst.box.height = pDestRect->top - pDestRect->bottom;
1612 } else {
1613 blit.dst.box.y = pDestRect->top;
1614 blit.dst.box.height = pDestRect->bottom - pDestRect->top;
1615 }
1616 } else {
1617 blit.dst.box.x = 0;
1618 blit.dst.box.y = 0;
1619 blit.dst.box.width = dst->desc.Width;
1620 blit.dst.box.height = dst->desc.Height;
1621 }
1622 blit.src.resource = src_res;
1623 blit.src.level = src->level;
1624 blit.src.box.z = src->layer;
1625 blit.src.box.depth = 1;
1626 blit.src.format = src_res->format;
1627 if (pSourceRect) {
1628 if (flip_x ^ (pSourceRect->left > pSourceRect->right)) {
1629 blit.src.box.x = pSourceRect->right;
1630 blit.src.box.width = pSourceRect->left - pSourceRect->right;
1631 } else {
1632 blit.src.box.x = pSourceRect->left;
1633 blit.src.box.width = pSourceRect->right - pSourceRect->left;
1634 }
1635 if (flip_y ^ (pSourceRect->top > pSourceRect->bottom)) {
1636 blit.src.box.y = pSourceRect->bottom;
1637 blit.src.box.height = pSourceRect->top - pSourceRect->bottom;
1638 } else {
1639 blit.src.box.y = pSourceRect->top;
1640 blit.src.box.height = pSourceRect->bottom - pSourceRect->top;
1641 }
1642 } else {
1643 blit.src.box.x = flip_x ? src->desc.Width : 0;
1644 blit.src.box.y = flip_y ? src->desc.Height : 0;
1645 blit.src.box.width = flip_x ? -src->desc.Width : src->desc.Width;
1646 blit.src.box.height = flip_y ? -src->desc.Height : src->desc.Height;
1647 }
1648 blit.mask = zs ? PIPE_MASK_ZS : PIPE_MASK_RGBA;
1649 blit.filter = Filter == D3DTEXF_LINEAR ?
1650 PIPE_TEX_FILTER_LINEAR : PIPE_TEX_FILTER_NEAREST;
1651 blit.scissor_enable = FALSE;
1652 blit.alpha_blend = FALSE;
1653
1654 /* If both of a src and dst dimension are negative, flip them. */
1655 if (blit.dst.box.width < 0 && blit.src.box.width < 0) {
1656 blit.dst.box.width = -blit.dst.box.width;
1657 blit.src.box.width = -blit.src.box.width;
1658 }
1659 if (blit.dst.box.height < 0 && blit.src.box.height < 0) {
1660 blit.dst.box.height = -blit.dst.box.height;
1661 blit.src.box.height = -blit.src.box.height;
1662 }
1663 scaled =
1664 blit.dst.box.width != blit.src.box.width ||
1665 blit.dst.box.height != blit.src.box.height;
1666
1667 user_assert(!scaled || dst != src, D3DERR_INVALIDCALL);
1668 user_assert(!scaled ||
1669 !NineSurface9_IsOffscreenPlain(dst), D3DERR_INVALIDCALL);
1670 user_assert(!NineSurface9_IsOffscreenPlain(dst) ||
1671 NineSurface9_IsOffscreenPlain(src), D3DERR_INVALIDCALL);
1672 user_assert(NineSurface9_IsOffscreenPlain(dst) ||
1673 dst->desc.Usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL),
1674 D3DERR_INVALIDCALL);
1675 user_assert(!scaled ||
1676 (!util_format_is_compressed(dst->base.info.format) &&
1677 !util_format_is_compressed(src->base.info.format)),
1678 D3DERR_INVALIDCALL);
1679
1680 user_warn(src == dst &&
1681 u_box_test_intersection_2d(&blit.src.box, &blit.dst.box));
1682
1683 /* Check for clipping/clamping: */
1684 {
1685 struct pipe_box box;
1686 int xy;
1687
1688 xy = u_box_clip_2d(&box, &blit.dst.box,
1689 dst->desc.Width, dst->desc.Height);
1690 if (xy < 0)
1691 return D3D_OK;
1692 if (xy == 0)
1693 xy = u_box_clip_2d(&box, &blit.src.box,
1694 src->desc.Width, src->desc.Height);
1695 clamped = !!xy;
1696 }
1697
1698 ms = (dst->desc.MultiSampleType | 1) != (src->desc.MultiSampleType | 1);
1699
1700 if (clamped || scaled || (blit.dst.format != blit.src.format) || ms) {
1701 DBG("using pipe->blit()\n");
1702 /* TODO: software scaling */
1703 user_assert(screen->is_format_supported(screen, dst_res->format,
1704 dst_res->target,
1705 dst_res->nr_samples,
1706 zs ? PIPE_BIND_DEPTH_STENCIL :
1707 PIPE_BIND_RENDER_TARGET),
1708 D3DERR_INVALIDCALL);
1709
1710 pipe->blit(pipe, &blit);
1711 } else {
1712 assert(blit.dst.box.x >= 0 && blit.dst.box.y >= 0 &&
1713 blit.src.box.x >= 0 && blit.src.box.y >= 0 &&
1714 blit.dst.box.x + blit.dst.box.width <= dst->desc.Width &&
1715 blit.src.box.x + blit.src.box.width <= src->desc.Width &&
1716 blit.dst.box.y + blit.dst.box.height <= dst->desc.Height &&
1717 blit.src.box.y + blit.src.box.height <= src->desc.Height);
1718 /* Or drivers might crash ... */
1719 DBG("Using resource_copy_region.\n");
1720 pipe->resource_copy_region(pipe,
1721 blit.dst.resource, blit.dst.level,
1722 blit.dst.box.x, blit.dst.box.y, blit.dst.box.z,
1723 blit.src.resource, blit.src.level,
1724 &blit.src.box);
1725 }
1726
1727 /* Communicate the container it needs to update sublevels - if apply */
1728 NineSurface9_MarkContainerDirty(dst);
1729
1730 return D3D_OK;
1731 }
1732
1733 HRESULT NINE_WINAPI
1734 NineDevice9_ColorFill( struct NineDevice9 *This,
1735 IDirect3DSurface9 *pSurface,
1736 const RECT *pRect,
1737 D3DCOLOR color )
1738 {
1739 struct pipe_context *pipe = This->pipe;
1740 struct NineSurface9 *surf = NineSurface9(pSurface);
1741 struct pipe_surface *psurf;
1742 unsigned x, y, w, h;
1743 union pipe_color_union rgba;
1744 boolean fallback;
1745
1746 DBG("This=%p pSurface=%p pRect=%p color=%08x\n", This,
1747 pSurface, pRect, color);
1748 if (pRect)
1749 DBG("pRect=(%u,%u)-(%u,%u)\n", pRect->left, pRect->top,
1750 pRect->right, pRect->bottom);
1751
1752 user_assert(surf->base.pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1753
1754 user_assert((surf->base.usage & D3DUSAGE_RENDERTARGET) ||
1755 NineSurface9_IsOffscreenPlain(surf), D3DERR_INVALIDCALL);
1756
1757 user_assert(surf->desc.Format != D3DFMT_NULL, D3D_OK);
1758
1759 if (pRect) {
1760 x = pRect->left;
1761 y = pRect->top;
1762 w = pRect->right - pRect->left;
1763 h = pRect->bottom - pRect->top;
1764 /* Wine tests: */
1765 if (compressed_format(surf->desc.Format)) {
1766 const unsigned bw = util_format_get_blockwidth(surf->base.info.format);
1767 const unsigned bh = util_format_get_blockheight(surf->base.info.format);
1768
1769 user_assert(!(x % bw) && !(y % bh) && !(w % bw) && !(h % bh),
1770 D3DERR_INVALIDCALL);
1771 }
1772 } else{
1773 x = 0;
1774 y = 0;
1775 w = surf->desc.Width;
1776 h = surf->desc.Height;
1777 }
1778 d3dcolor_to_pipe_color_union(&rgba, color);
1779
1780 fallback = !(surf->base.info.bind & PIPE_BIND_RENDER_TARGET);
1781
1782 if (!fallback) {
1783 psurf = NineSurface9_GetSurface(surf, 0);
1784 if (!psurf)
1785 fallback = TRUE;
1786 }
1787
1788 if (!fallback) {
1789 pipe->clear_render_target(pipe, psurf, &rgba, x, y, w, h, false);
1790 } else {
1791 D3DLOCKED_RECT lock;
1792 union util_color uc;
1793 HRESULT hr;
1794 /* XXX: lock pRect and fix util_fill_rect */
1795 hr = NineSurface9_LockRect(surf, &lock, NULL, 0);
1796 if (FAILED(hr))
1797 return hr;
1798 util_pack_color_ub(color >> 16, color >> 8, color >> 0, color >> 24,
1799 surf->base.info.format, &uc);
1800 util_fill_rect(lock.pBits, surf->base.info.format,lock.Pitch,
1801 x, y, w, h, &uc);
1802 NineSurface9_UnlockRect(surf);
1803 }
1804
1805 return D3D_OK;
1806 }
1807
1808 HRESULT NINE_WINAPI
1809 NineDevice9_CreateOffscreenPlainSurface( struct NineDevice9 *This,
1810 UINT Width,
1811 UINT Height,
1812 D3DFORMAT Format,
1813 D3DPOOL Pool,
1814 IDirect3DSurface9 **ppSurface,
1815 HANDLE *pSharedHandle )
1816 {
1817 HRESULT hr;
1818
1819 DBG("This=%p Width=%u Height=%u Format=%s(0x%x) Pool=%u "
1820 "ppSurface=%p pSharedHandle=%p\n", This,
1821 Width, Height, d3dformat_to_string(Format), Format, Pool,
1822 ppSurface, pSharedHandle);
1823
1824 *ppSurface = NULL;
1825 user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT
1826 || Pool == D3DPOOL_SYSTEMMEM, D3DERR_INVALIDCALL);
1827 user_assert(Pool != D3DPOOL_MANAGED, D3DERR_INVALIDCALL);
1828
1829 /* Can be used with StretchRect and ColorFill. It's also always lockable.
1830 */
1831 hr = create_zs_or_rt_surface(This, 2, Pool, Width, Height,
1832 Format,
1833 D3DMULTISAMPLE_NONE, 0,
1834 TRUE,
1835 ppSurface, pSharedHandle);
1836 if (FAILED(hr))
1837 DBG("Failed to create surface.\n");
1838 return hr;
1839 }
1840
1841 HRESULT NINE_WINAPI
1842 NineDevice9_SetRenderTarget( struct NineDevice9 *This,
1843 DWORD RenderTargetIndex,
1844 IDirect3DSurface9 *pRenderTarget )
1845 {
1846 struct NineSurface9 *rt = NineSurface9(pRenderTarget);
1847 const unsigned i = RenderTargetIndex;
1848
1849 DBG("This=%p RenderTargetIndex=%u pRenderTarget=%p\n", This,
1850 RenderTargetIndex, pRenderTarget);
1851
1852 user_assert(i < This->caps.NumSimultaneousRTs, D3DERR_INVALIDCALL);
1853 user_assert(i != 0 || pRenderTarget, D3DERR_INVALIDCALL);
1854 user_assert(!pRenderTarget ||
1855 rt->desc.Usage & D3DUSAGE_RENDERTARGET, D3DERR_INVALIDCALL);
1856
1857 if (i == 0) {
1858 This->state.viewport.X = 0;
1859 This->state.viewport.Y = 0;
1860 This->state.viewport.Width = rt->desc.Width;
1861 This->state.viewport.Height = rt->desc.Height;
1862 This->state.viewport.MinZ = 0.0f;
1863 This->state.viewport.MaxZ = 1.0f;
1864
1865 This->state.scissor.minx = 0;
1866 This->state.scissor.miny = 0;
1867 This->state.scissor.maxx = rt->desc.Width;
1868 This->state.scissor.maxy = rt->desc.Height;
1869
1870 This->state.changed.group |= NINE_STATE_VIEWPORT | NINE_STATE_SCISSOR | NINE_STATE_MULTISAMPLE;
1871 }
1872
1873 if (This->state.rt[i] != NineSurface9(pRenderTarget)) {
1874 nine_bind(&This->state.rt[i], pRenderTarget);
1875 This->state.changed.group |= NINE_STATE_FB;
1876 }
1877 return D3D_OK;
1878 }
1879
1880 HRESULT NINE_WINAPI
1881 NineDevice9_GetRenderTarget( struct NineDevice9 *This,
1882 DWORD RenderTargetIndex,
1883 IDirect3DSurface9 **ppRenderTarget )
1884 {
1885 const unsigned i = RenderTargetIndex;
1886
1887 user_assert(i < This->caps.NumSimultaneousRTs, D3DERR_INVALIDCALL);
1888 user_assert(ppRenderTarget, D3DERR_INVALIDCALL);
1889
1890 *ppRenderTarget = (IDirect3DSurface9 *)This->state.rt[i];
1891 if (!This->state.rt[i])
1892 return D3DERR_NOTFOUND;
1893
1894 NineUnknown_AddRef(NineUnknown(This->state.rt[i]));
1895 return D3D_OK;
1896 }
1897
1898 HRESULT NINE_WINAPI
1899 NineDevice9_SetDepthStencilSurface( struct NineDevice9 *This,
1900 IDirect3DSurface9 *pNewZStencil )
1901 {
1902 DBG("This=%p pNewZStencil=%p\n", This, pNewZStencil);
1903
1904 if (This->state.ds != NineSurface9(pNewZStencil)) {
1905 nine_bind(&This->state.ds, pNewZStencil);
1906 This->state.changed.group |= NINE_STATE_FB;
1907 }
1908 return D3D_OK;
1909 }
1910
1911 HRESULT NINE_WINAPI
1912 NineDevice9_GetDepthStencilSurface( struct NineDevice9 *This,
1913 IDirect3DSurface9 **ppZStencilSurface )
1914 {
1915 user_assert(ppZStencilSurface, D3DERR_INVALIDCALL);
1916
1917 *ppZStencilSurface = (IDirect3DSurface9 *)This->state.ds;
1918 if (!This->state.ds)
1919 return D3DERR_NOTFOUND;
1920
1921 NineUnknown_AddRef(NineUnknown(This->state.ds));
1922 return D3D_OK;
1923 }
1924
1925 HRESULT NINE_WINAPI
1926 NineDevice9_BeginScene( struct NineDevice9 *This )
1927 {
1928 DBG("This=%p\n", This);
1929 user_assert(!This->in_scene, D3DERR_INVALIDCALL);
1930 This->in_scene = TRUE;
1931 /* Do we want to do anything else here ? */
1932 return D3D_OK;
1933 }
1934
1935 HRESULT NINE_WINAPI
1936 NineDevice9_EndScene( struct NineDevice9 *This )
1937 {
1938 DBG("This=%p\n", This);
1939 user_assert(This->in_scene, D3DERR_INVALIDCALL);
1940 This->in_scene = FALSE;
1941 return D3D_OK;
1942 }
1943
1944 HRESULT NINE_WINAPI
1945 NineDevice9_Clear( struct NineDevice9 *This,
1946 DWORD Count,
1947 const D3DRECT *pRects,
1948 DWORD Flags,
1949 D3DCOLOR Color,
1950 float Z,
1951 DWORD Stencil )
1952 {
1953 const int sRGB = This->state.rs[D3DRS_SRGBWRITEENABLE] ? 1 : 0;
1954 struct pipe_surface *cbuf, *zsbuf;
1955 struct pipe_context *pipe = This->pipe;
1956 struct NineSurface9 *zsbuf_surf = This->state.ds;
1957 struct NineSurface9 *rt;
1958 unsigned bufs = 0;
1959 unsigned r, i;
1960 union pipe_color_union rgba;
1961 unsigned rt_mask = 0;
1962 D3DRECT rect;
1963
1964 DBG("This=%p Count=%u pRects=%p Flags=%x Color=%08x Z=%f Stencil=%x\n",
1965 This, Count, pRects, Flags, Color, Z, Stencil);
1966
1967 user_assert(This->state.ds || !(Flags & NINED3DCLEAR_DEPTHSTENCIL),
1968 D3DERR_INVALIDCALL);
1969 user_assert(!(Flags & D3DCLEAR_STENCIL) ||
1970 (zsbuf_surf &&
1971 util_format_is_depth_and_stencil(zsbuf_surf->base.info.format)),
1972 D3DERR_INVALIDCALL);
1973 #ifdef NINE_STRICT
1974 user_assert((Count && pRects) || (!Count && !pRects), D3DERR_INVALIDCALL);
1975 #else
1976 user_warn((pRects && !Count) || (!pRects && Count));
1977 if (pRects && !Count)
1978 return D3D_OK;
1979 if (!pRects)
1980 Count = 0;
1981 #endif
1982
1983 nine_update_state_framebuffer_clear(This);
1984
1985 if (Flags & D3DCLEAR_TARGET) bufs |= PIPE_CLEAR_COLOR;
1986 /* Ignore Z buffer if not bound */
1987 if (This->state.fb.zsbuf != NULL) {
1988 if (Flags & D3DCLEAR_ZBUFFER) bufs |= PIPE_CLEAR_DEPTH;
1989 if (Flags & D3DCLEAR_STENCIL) bufs |= PIPE_CLEAR_STENCIL;
1990 }
1991 if (!bufs)
1992 return D3D_OK;
1993 d3dcolor_to_pipe_color_union(&rgba, Color);
1994
1995 rect.x1 = This->state.viewport.X;
1996 rect.y1 = This->state.viewport.Y;
1997 rect.x2 = This->state.viewport.Width + rect.x1;
1998 rect.y2 = This->state.viewport.Height + rect.y1;
1999
2000 /* Both rectangles apply, which is weird, but that's D3D9. */
2001 if (This->state.rs[D3DRS_SCISSORTESTENABLE]) {
2002 rect.x1 = MAX2(rect.x1, This->state.scissor.minx);
2003 rect.y1 = MAX2(rect.y1, This->state.scissor.miny);
2004 rect.x2 = MIN2(rect.x2, This->state.scissor.maxx);
2005 rect.y2 = MIN2(rect.y2, This->state.scissor.maxy);
2006 }
2007
2008 if (Count) {
2009 /* Maybe apps like to specify a large rect ? */
2010 if (pRects[0].x1 <= rect.x1 && pRects[0].x2 >= rect.x2 &&
2011 pRects[0].y1 <= rect.y1 && pRects[0].y2 >= rect.y2) {
2012 DBG("First rect covers viewport.\n");
2013 Count = 0;
2014 pRects = NULL;
2015 }
2016 }
2017
2018 if (rect.x1 >= This->state.fb.width || rect.y1 >= This->state.fb.height)
2019 return D3D_OK;
2020
2021 for (i = 0; i < This->caps.NumSimultaneousRTs; ++i) {
2022 if (This->state.rt[i] && This->state.rt[i]->desc.Format != D3DFMT_NULL)
2023 rt_mask |= 1 << i;
2024 }
2025
2026 /* fast path, clears everything at once */
2027 if (!Count &&
2028 (!(bufs & PIPE_CLEAR_COLOR) || (rt_mask == This->state.rt_mask)) &&
2029 rect.x1 == 0 && rect.y1 == 0 &&
2030 /* Case we clear only render target. Check clear region vs rt. */
2031 ((!(bufs & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) &&
2032 rect.x2 >= This->state.fb.width &&
2033 rect.y2 >= This->state.fb.height) ||
2034 /* Case we clear depth buffer (and eventually rt too).
2035 * depth buffer size is always >= rt size. Compare to clear region */
2036 ((bufs & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) &&
2037 rect.x2 >= zsbuf_surf->desc.Width &&
2038 rect.y2 >= zsbuf_surf->desc.Height))) {
2039 DBG("Clear fast path\n");
2040 pipe->clear(pipe, bufs, &rgba, Z, Stencil);
2041 return D3D_OK;
2042 }
2043
2044 if (!Count) {
2045 Count = 1;
2046 pRects = &rect;
2047 }
2048
2049 for (i = 0; i < This->caps.NumSimultaneousRTs; ++i) {
2050 rt = This->state.rt[i];
2051 if (!rt || rt->desc.Format == D3DFMT_NULL ||
2052 !(bufs & PIPE_CLEAR_COLOR))
2053 continue; /* save space, compiler should hoist this */
2054 cbuf = NineSurface9_GetSurface(rt, sRGB);
2055 for (r = 0; r < Count; ++r) {
2056 /* Don't trust users to pass these in the right order. */
2057 unsigned x1 = MIN2(pRects[r].x1, pRects[r].x2);
2058 unsigned y1 = MIN2(pRects[r].y1, pRects[r].y2);
2059 unsigned x2 = MAX2(pRects[r].x1, pRects[r].x2);
2060 unsigned y2 = MAX2(pRects[r].y1, pRects[r].y2);
2061 #ifndef NINE_LAX
2062 /* Drop negative rectangles (like wine expects). */
2063 if (pRects[r].x1 > pRects[r].x2) continue;
2064 if (pRects[r].y1 > pRects[r].y2) continue;
2065 #endif
2066
2067 x1 = MAX2(x1, rect.x1);
2068 y1 = MAX2(y1, rect.y1);
2069 x2 = MIN3(x2, rect.x2, rt->desc.Width);
2070 y2 = MIN3(y2, rect.y2, rt->desc.Height);
2071
2072 DBG("Clearing (%u..%u)x(%u..%u)\n", x1, x2, y1, y2);
2073 pipe->clear_render_target(pipe, cbuf, &rgba,
2074 x1, y1, x2 - x1, y2 - y1, false);
2075 }
2076 }
2077 if (!(bufs & PIPE_CLEAR_DEPTHSTENCIL))
2078 return D3D_OK;
2079
2080 bufs &= PIPE_CLEAR_DEPTHSTENCIL;
2081
2082 for (r = 0; r < Count; ++r) {
2083 unsigned x1 = MIN2(pRects[r].x1, pRects[r].x2);
2084 unsigned y1 = MIN2(pRects[r].y1, pRects[r].y2);
2085 unsigned x2 = MAX2(pRects[r].x1, pRects[r].x2);
2086 unsigned y2 = MAX2(pRects[r].y1, pRects[r].y2);
2087 #ifndef NINE_LAX
2088 /* Drop negative rectangles. */
2089 if (pRects[r].x1 > pRects[r].x2) continue;
2090 if (pRects[r].y1 > pRects[r].y2) continue;
2091 #endif
2092
2093 x1 = MIN2(x1, rect.x1);
2094 y1 = MIN2(y1, rect.y1);
2095 x2 = MIN3(x2, rect.x2, zsbuf_surf->desc.Width);
2096 y2 = MIN3(y2, rect.y2, zsbuf_surf->desc.Height);
2097
2098 zsbuf = NineSurface9_GetSurface(zsbuf_surf, 0);
2099 assert(zsbuf);
2100 pipe->clear_depth_stencil(pipe, zsbuf, bufs, Z, Stencil,
2101 x1, y1, x2 - x1, y2 - y1, false);
2102 }
2103 return D3D_OK;
2104 }
2105
2106 HRESULT NINE_WINAPI
2107 NineDevice9_SetTransform( struct NineDevice9 *This,
2108 D3DTRANSFORMSTATETYPE State,
2109 const D3DMATRIX *pMatrix )
2110 {
2111 struct nine_state *state = This->update;
2112 D3DMATRIX *M = nine_state_access_transform(state, State, TRUE);
2113
2114 DBG("This=%p State=%d pMatrix=%p\n", This, State, pMatrix);
2115
2116 user_assert(M, D3DERR_INVALIDCALL);
2117
2118 *M = *pMatrix;
2119 state->ff.changed.transform[State / 32] |= 1 << (State % 32);
2120 state->changed.group |= NINE_STATE_FF;
2121
2122 return D3D_OK;
2123 }
2124
2125 HRESULT NINE_WINAPI
2126 NineDevice9_GetTransform( struct NineDevice9 *This,
2127 D3DTRANSFORMSTATETYPE State,
2128 D3DMATRIX *pMatrix )
2129 {
2130 D3DMATRIX *M = nine_state_access_transform(&This->state, State, FALSE);
2131 user_assert(M, D3DERR_INVALIDCALL);
2132 *pMatrix = *M;
2133 return D3D_OK;
2134 }
2135
2136 HRESULT NINE_WINAPI
2137 NineDevice9_MultiplyTransform( struct NineDevice9 *This,
2138 D3DTRANSFORMSTATETYPE State,
2139 const D3DMATRIX *pMatrix )
2140 {
2141 struct nine_state *state = This->update;
2142 D3DMATRIX T;
2143 D3DMATRIX *M = nine_state_access_transform(state, State, TRUE);
2144
2145 DBG("This=%p State=%d pMatrix=%p\n", This, State, pMatrix);
2146
2147 user_assert(M, D3DERR_INVALIDCALL);
2148
2149 nine_d3d_matrix_matrix_mul(&T, pMatrix, M);
2150 return NineDevice9_SetTransform(This, State, &T);
2151 }
2152
2153 HRESULT NINE_WINAPI
2154 NineDevice9_SetViewport( struct NineDevice9 *This,
2155 const D3DVIEWPORT9 *pViewport )
2156 {
2157 struct nine_state *state = This->update;
2158
2159 DBG("X=%u Y=%u W=%u H=%u MinZ=%f MaxZ=%f\n",
2160 pViewport->X, pViewport->Y, pViewport->Width, pViewport->Height,
2161 pViewport->MinZ, pViewport->MaxZ);
2162
2163 state->viewport = *pViewport;
2164 state->changed.group |= NINE_STATE_VIEWPORT;
2165
2166 return D3D_OK;
2167 }
2168
2169 HRESULT NINE_WINAPI
2170 NineDevice9_GetViewport( struct NineDevice9 *This,
2171 D3DVIEWPORT9 *pViewport )
2172 {
2173 *pViewport = This->state.viewport;
2174 return D3D_OK;
2175 }
2176
2177 HRESULT NINE_WINAPI
2178 NineDevice9_SetMaterial( struct NineDevice9 *This,
2179 const D3DMATERIAL9 *pMaterial )
2180 {
2181 struct nine_state *state = This->update;
2182
2183 DBG("This=%p pMaterial=%p\n", This, pMaterial);
2184 if (pMaterial)
2185 nine_dump_D3DMATERIAL9(DBG_FF, pMaterial);
2186
2187 user_assert(pMaterial, E_POINTER);
2188
2189 state->ff.material = *pMaterial;
2190 state->changed.group |= NINE_STATE_FF_MATERIAL;
2191
2192 return D3D_OK;
2193 }
2194
2195 HRESULT NINE_WINAPI
2196 NineDevice9_GetMaterial( struct NineDevice9 *This,
2197 D3DMATERIAL9 *pMaterial )
2198 {
2199 user_assert(pMaterial, E_POINTER);
2200 *pMaterial = This->state.ff.material;
2201 return D3D_OK;
2202 }
2203
2204 HRESULT NINE_WINAPI
2205 NineDevice9_SetLight( struct NineDevice9 *This,
2206 DWORD Index,
2207 const D3DLIGHT9 *pLight )
2208 {
2209 struct nine_state *state = This->update;
2210
2211 DBG("This=%p Index=%u pLight=%p\n", This, Index, pLight);
2212 if (pLight)
2213 nine_dump_D3DLIGHT9(DBG_FF, pLight);
2214
2215 user_assert(pLight, D3DERR_INVALIDCALL);
2216 user_assert(pLight->Type < NINED3DLIGHT_INVALID, D3DERR_INVALIDCALL);
2217
2218 user_assert(Index < NINE_MAX_LIGHTS, D3DERR_INVALIDCALL); /* sanity */
2219
2220 if (Index >= state->ff.num_lights) {
2221 unsigned n = state->ff.num_lights;
2222 unsigned N = Index + 1;
2223
2224 state->ff.light = REALLOC(state->ff.light, n * sizeof(D3DLIGHT9),
2225 N * sizeof(D3DLIGHT9));
2226 if (!state->ff.light)
2227 return E_OUTOFMEMORY;
2228 state->ff.num_lights = N;
2229
2230 for (; n < Index; ++n) {
2231 memset(&state->ff.light[n], 0, sizeof(D3DLIGHT9));
2232 state->ff.light[n].Type = (D3DLIGHTTYPE)NINED3DLIGHT_INVALID;
2233 }
2234 }
2235 state->ff.light[Index] = *pLight;
2236
2237 if (pLight->Type == D3DLIGHT_SPOT && pLight->Theta >= pLight->Phi) {
2238 DBG("Warning: clamping D3DLIGHT9.Theta\n");
2239 state->ff.light[Index].Theta = state->ff.light[Index].Phi;
2240 }
2241 if (pLight->Type != D3DLIGHT_DIRECTIONAL &&
2242 pLight->Attenuation0 == 0.0f &&
2243 pLight->Attenuation1 == 0.0f &&
2244 pLight->Attenuation2 == 0.0f) {
2245 DBG("Warning: all D3DLIGHT9.Attenuation[i] are 0\n");
2246 }
2247
2248 state->changed.group |= NINE_STATE_FF_LIGHTING;
2249
2250 return D3D_OK;
2251 }
2252
2253 HRESULT NINE_WINAPI
2254 NineDevice9_GetLight( struct NineDevice9 *This,
2255 DWORD Index,
2256 D3DLIGHT9 *pLight )
2257 {
2258 const struct nine_state *state = &This->state;
2259
2260 user_assert(pLight, D3DERR_INVALIDCALL);
2261 user_assert(Index < state->ff.num_lights, D3DERR_INVALIDCALL);
2262 user_assert(state->ff.light[Index].Type < NINED3DLIGHT_INVALID,
2263 D3DERR_INVALIDCALL);
2264
2265 *pLight = state->ff.light[Index];
2266
2267 return D3D_OK;
2268 }
2269
2270 HRESULT NINE_WINAPI
2271 NineDevice9_LightEnable( struct NineDevice9 *This,
2272 DWORD Index,
2273 BOOL Enable )
2274 {
2275 struct nine_state *state = This->update;
2276 unsigned i;
2277
2278 DBG("This=%p Index=%u Enable=%i\n", This, Index, Enable);
2279
2280 if (Index >= state->ff.num_lights ||
2281 state->ff.light[Index].Type == NINED3DLIGHT_INVALID) {
2282 /* This should create a default light. */
2283 D3DLIGHT9 light;
2284 memset(&light, 0, sizeof(light));
2285 light.Type = D3DLIGHT_DIRECTIONAL;
2286 light.Diffuse.r = 1.0f;
2287 light.Diffuse.g = 1.0f;
2288 light.Diffuse.b = 1.0f;
2289 light.Direction.z = 1.0f;
2290 NineDevice9_SetLight(This, Index, &light);
2291 }
2292 user_assert(Index < state->ff.num_lights, D3DERR_INVALIDCALL);
2293
2294 for (i = 0; i < state->ff.num_lights_active; ++i) {
2295 if (state->ff.active_light[i] == Index)
2296 break;
2297 }
2298
2299 if (Enable) {
2300 if (i < state->ff.num_lights_active)
2301 return D3D_OK;
2302 /* XXX wine thinks this should still succeed:
2303 */
2304 user_assert(i < NINE_MAX_LIGHTS_ACTIVE, D3DERR_INVALIDCALL);
2305
2306 state->ff.active_light[i] = Index;
2307 state->ff.num_lights_active++;
2308 } else {
2309 if (i == state->ff.num_lights_active)
2310 return D3D_OK;
2311 --state->ff.num_lights_active;
2312 for (; i < state->ff.num_lights_active; ++i)
2313 state->ff.active_light[i] = state->ff.active_light[i + 1];
2314 }
2315 state->changed.group |= NINE_STATE_FF_LIGHTING;
2316
2317 return D3D_OK;
2318 }
2319
2320 HRESULT NINE_WINAPI
2321 NineDevice9_GetLightEnable( struct NineDevice9 *This,
2322 DWORD Index,
2323 BOOL *pEnable )
2324 {
2325 const struct nine_state *state = &This->state;
2326 unsigned i;
2327
2328 user_assert(Index < state->ff.num_lights, D3DERR_INVALIDCALL);
2329 user_assert(state->ff.light[Index].Type < NINED3DLIGHT_INVALID,
2330 D3DERR_INVALIDCALL);
2331
2332 for (i = 0; i < state->ff.num_lights_active; ++i)
2333 if (state->ff.active_light[i] == Index)
2334 break;
2335
2336 *pEnable = i != state->ff.num_lights_active ? 128 : 0; // Taken from wine
2337
2338 return D3D_OK;
2339 }
2340
2341 HRESULT NINE_WINAPI
2342 NineDevice9_SetClipPlane( struct NineDevice9 *This,
2343 DWORD Index,
2344 const float *pPlane )
2345 {
2346 struct nine_state *state = This->update;
2347
2348 user_assert(pPlane, D3DERR_INVALIDCALL);
2349
2350 DBG("This=%p Index=%u pPlane=%f %f %f %f\n", This, Index,
2351 pPlane[0], pPlane[1],
2352 pPlane[2], pPlane[3]);
2353
2354 user_assert(Index < PIPE_MAX_CLIP_PLANES, D3DERR_INVALIDCALL);
2355
2356 memcpy(&state->clip.ucp[Index][0], pPlane, sizeof(state->clip.ucp[0]));
2357 state->changed.ucp |= 1 << Index;
2358
2359 return D3D_OK;
2360 }
2361
2362 HRESULT NINE_WINAPI
2363 NineDevice9_GetClipPlane( struct NineDevice9 *This,
2364 DWORD Index,
2365 float *pPlane )
2366 {
2367 const struct nine_state *state = &This->state;
2368
2369 user_assert(Index < PIPE_MAX_CLIP_PLANES, D3DERR_INVALIDCALL);
2370
2371 memcpy(pPlane, &state->clip.ucp[Index][0], sizeof(state->clip.ucp[0]));
2372 return D3D_OK;
2373 }
2374
2375 #define RESZ_CODE 0x7fa05000
2376
2377 static HRESULT
2378 NineDevice9_ResolveZ( struct NineDevice9 *This )
2379 {
2380 struct nine_state *state = &This->state;
2381 const struct util_format_description *desc;
2382 struct NineSurface9 *source = state->ds;
2383 struct NineBaseTexture9 *destination = state->texture[0];
2384 struct pipe_resource *src, *dst;
2385 struct pipe_blit_info blit;
2386
2387 DBG("RESZ resolve\n");
2388
2389 user_assert(source && destination &&
2390 destination->base.type == D3DRTYPE_TEXTURE, D3DERR_INVALIDCALL);
2391
2392 src = source->base.resource;
2393 dst = destination->base.resource;
2394
2395 user_assert(src && dst, D3DERR_INVALIDCALL);
2396
2397 /* check dst is depth format. we know already for src */
2398 desc = util_format_description(dst->format);
2399 user_assert(desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS, D3DERR_INVALIDCALL);
2400
2401 memset(&blit, 0, sizeof(blit));
2402 blit.src.resource = src;
2403 blit.src.level = 0;
2404 blit.src.format = src->format;
2405 blit.src.box.z = 0;
2406 blit.src.box.depth = 1;
2407 blit.src.box.x = 0;
2408 blit.src.box.y = 0;
2409 blit.src.box.width = src->width0;
2410 blit.src.box.height = src->height0;
2411
2412 blit.dst.resource = dst;
2413 blit.dst.level = 0;
2414 blit.dst.format = dst->format;
2415 blit.dst.box.z = 0;
2416 blit.dst.box.depth = 1;
2417 blit.dst.box.x = 0;
2418 blit.dst.box.y = 0;
2419 blit.dst.box.width = dst->width0;
2420 blit.dst.box.height = dst->height0;
2421
2422 blit.mask = PIPE_MASK_ZS;
2423 blit.filter = PIPE_TEX_FILTER_NEAREST;
2424 blit.scissor_enable = FALSE;
2425
2426 This->pipe->blit(This->pipe, &blit);
2427 return D3D_OK;
2428 }
2429
2430 #define ALPHA_TO_COVERAGE_ENABLE MAKEFOURCC('A', '2', 'M', '1')
2431 #define ALPHA_TO_COVERAGE_DISABLE MAKEFOURCC('A', '2', 'M', '0')
2432
2433 HRESULT NINE_WINAPI
2434 NineDevice9_SetRenderState( struct NineDevice9 *This,
2435 D3DRENDERSTATETYPE State,
2436 DWORD Value )
2437 {
2438 struct nine_state *state = This->update;
2439
2440 DBG("This=%p State=%u(%s) Value=%08x\n", This,
2441 State, nine_d3drs_to_string(State), Value);
2442
2443 user_assert(State < D3DRS_COUNT, D3DERR_INVALIDCALL);
2444
2445 if (state->rs_advertised[State] == Value && likely(!This->is_recording))
2446 return D3D_OK;
2447
2448 state->rs_advertised[State] = Value;
2449
2450 /* Amd hacks (equivalent to GL extensions) */
2451 if (unlikely(State == D3DRS_POINTSIZE)) {
2452 if (Value == RESZ_CODE)
2453 return NineDevice9_ResolveZ(This);
2454
2455 if (Value == ALPHA_TO_COVERAGE_ENABLE ||
2456 Value == ALPHA_TO_COVERAGE_DISABLE) {
2457 state->rs[NINED3DRS_ALPHACOVERAGE] = (Value == ALPHA_TO_COVERAGE_ENABLE);
2458 state->changed.group |= NINE_STATE_BLEND;
2459 return D3D_OK;
2460 }
2461 }
2462
2463 /* NV hack */
2464 if (unlikely(State == D3DRS_ADAPTIVETESS_Y)) {
2465 if (Value == D3DFMT_ATOC || (Value == D3DFMT_UNKNOWN && state->rs[NINED3DRS_ALPHACOVERAGE])) {
2466 state->rs[NINED3DRS_ALPHACOVERAGE] = (Value == D3DFMT_ATOC) ? 3 : 0;
2467 state->rs[NINED3DRS_ALPHACOVERAGE] &= state->rs[D3DRS_ALPHATESTENABLE] ? 3 : 2;
2468 state->changed.group |= NINE_STATE_BLEND;
2469 return D3D_OK;
2470 }
2471 }
2472 if (unlikely(State == D3DRS_ALPHATESTENABLE && (state->rs[NINED3DRS_ALPHACOVERAGE] & 2))) {
2473 DWORD alphacoverage_prev = state->rs[NINED3DRS_ALPHACOVERAGE];
2474 state->rs[NINED3DRS_ALPHACOVERAGE] = (Value ? 3 : 2);
2475 if (state->rs[NINED3DRS_ALPHACOVERAGE] != alphacoverage_prev)
2476 state->changed.group |= NINE_STATE_BLEND;
2477 }
2478
2479 state->rs[State] = nine_fix_render_state_value(State, Value);
2480 state->changed.rs[State / 32] |= 1 << (State % 32);
2481 state->changed.group |= nine_render_state_group[State];
2482
2483 return D3D_OK;
2484 }
2485
2486 HRESULT NINE_WINAPI
2487 NineDevice9_GetRenderState( struct NineDevice9 *This,
2488 D3DRENDERSTATETYPE State,
2489 DWORD *pValue )
2490 {
2491 user_assert(State < D3DRS_COUNT, D3DERR_INVALIDCALL);
2492
2493 *pValue = This->state.rs_advertised[State];
2494 return D3D_OK;
2495 }
2496
2497 HRESULT NINE_WINAPI
2498 NineDevice9_CreateStateBlock( struct NineDevice9 *This,
2499 D3DSTATEBLOCKTYPE Type,
2500 IDirect3DStateBlock9 **ppSB )
2501 {
2502 struct NineStateBlock9 *nsb;
2503 struct nine_state *dst;
2504 HRESULT hr;
2505 enum nine_stateblock_type type;
2506 unsigned s;
2507
2508 DBG("This=%p Type=%u ppSB=%p\n", This, Type, ppSB);
2509
2510 user_assert(Type == D3DSBT_ALL ||
2511 Type == D3DSBT_VERTEXSTATE ||
2512 Type == D3DSBT_PIXELSTATE, D3DERR_INVALIDCALL);
2513
2514 switch (Type) {
2515 case D3DSBT_VERTEXSTATE: type = NINESBT_VERTEXSTATE; break;
2516 case D3DSBT_PIXELSTATE: type = NINESBT_PIXELSTATE; break;
2517 default:
2518 type = NINESBT_ALL;
2519 break;
2520 }
2521
2522 hr = NineStateBlock9_new(This, &nsb, type);
2523 if (FAILED(hr))
2524 return hr;
2525 *ppSB = (IDirect3DStateBlock9 *)nsb;
2526 dst = &nsb->state;
2527
2528 dst->changed.group =
2529 NINE_STATE_TEXTURE |
2530 NINE_STATE_SAMPLER;
2531
2532 if (Type == D3DSBT_ALL || Type == D3DSBT_VERTEXSTATE) {
2533 dst->changed.group |=
2534 NINE_STATE_FF_LIGHTING |
2535 NINE_STATE_VS | NINE_STATE_VS_CONST |
2536 NINE_STATE_VDECL;
2537 /* TODO: texture/sampler state */
2538 memcpy(dst->changed.rs,
2539 nine_render_states_vertex, sizeof(dst->changed.rs));
2540 nine_ranges_insert(&dst->changed.vs_const_f, 0, This->may_swvp ? NINE_MAX_CONST_F_SWVP : This->max_vs_const_f,
2541 &This->range_pool);
2542 nine_ranges_insert(&dst->changed.vs_const_i, 0, This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I,
2543 &This->range_pool);
2544 nine_ranges_insert(&dst->changed.vs_const_b, 0, This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B,
2545 &This->range_pool);
2546 for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
2547 dst->changed.sampler[s] |= 1 << D3DSAMP_DMAPOFFSET;
2548 if (This->state.ff.num_lights) {
2549 dst->ff.num_lights = This->state.ff.num_lights;
2550 /* zero'd -> light type won't be NINED3DLIGHT_INVALID, so
2551 * all currently existing lights will be captured
2552 */
2553 dst->ff.light = CALLOC(This->state.ff.num_lights,
2554 sizeof(D3DLIGHT9));
2555 if (!dst->ff.light) {
2556 nine_bind(ppSB, NULL);
2557 return E_OUTOFMEMORY;
2558 }
2559 }
2560 }
2561 if (Type == D3DSBT_ALL || Type == D3DSBT_PIXELSTATE) {
2562 dst->changed.group |=
2563 NINE_STATE_PS | NINE_STATE_PS_CONST;
2564 /* TODO: texture/sampler state */
2565 memcpy(dst->changed.rs,
2566 nine_render_states_pixel, sizeof(dst->changed.rs));
2567 nine_ranges_insert(&dst->changed.ps_const_f, 0, This->max_ps_const_f,
2568 &This->range_pool);
2569 dst->changed.ps_const_i = 0xffff;
2570 dst->changed.ps_const_b = 0xffff;
2571 for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
2572 dst->changed.sampler[s] |= 0x1ffe;
2573 }
2574 if (Type == D3DSBT_ALL) {
2575 dst->changed.group |=
2576 NINE_STATE_VIEWPORT |
2577 NINE_STATE_SCISSOR |
2578 NINE_STATE_RASTERIZER |
2579 NINE_STATE_BLEND |
2580 NINE_STATE_DSA |
2581 NINE_STATE_IDXBUF |
2582 NINE_STATE_MATERIAL |
2583 NINE_STATE_BLEND_COLOR |
2584 NINE_STATE_SAMPLE_MASK;
2585 memset(dst->changed.rs, ~0, (D3DRS_COUNT / 32) * sizeof(uint32_t));
2586 dst->changed.rs[D3DRS_LAST / 32] |= (1 << (D3DRS_COUNT % 32)) - 1;
2587 dst->changed.vtxbuf = (1ULL << This->caps.MaxStreams) - 1;
2588 dst->changed.stream_freq = dst->changed.vtxbuf;
2589 dst->changed.ucp = (1 << PIPE_MAX_CLIP_PLANES) - 1;
2590 dst->changed.texture = (1 << NINE_MAX_SAMPLERS) - 1;
2591 }
2592 NineStateBlock9_Capture(NineStateBlock9(*ppSB));
2593
2594 /* TODO: fixed function state */
2595
2596 return D3D_OK;
2597 }
2598
2599 HRESULT NINE_WINAPI
2600 NineDevice9_BeginStateBlock( struct NineDevice9 *This )
2601 {
2602 HRESULT hr;
2603
2604 DBG("This=%p\n", This);
2605
2606 user_assert(!This->record, D3DERR_INVALIDCALL);
2607
2608 hr = NineStateBlock9_new(This, &This->record, NINESBT_CUSTOM);
2609 if (FAILED(hr))
2610 return hr;
2611 NineUnknown_ConvertRefToBind(NineUnknown(This->record));
2612
2613 This->update = &This->record->state;
2614 This->is_recording = TRUE;
2615
2616 return D3D_OK;
2617 }
2618
2619 HRESULT NINE_WINAPI
2620 NineDevice9_EndStateBlock( struct NineDevice9 *This,
2621 IDirect3DStateBlock9 **ppSB )
2622 {
2623 DBG("This=%p ppSB=%p\n", This, ppSB);
2624
2625 user_assert(This->record, D3DERR_INVALIDCALL);
2626
2627 This->update = &This->state;
2628 This->is_recording = FALSE;
2629
2630 NineUnknown_AddRef(NineUnknown(This->record));
2631 *ppSB = (IDirect3DStateBlock9 *)This->record;
2632 NineUnknown_Unbind(NineUnknown(This->record));
2633 This->record = NULL;
2634
2635 return D3D_OK;
2636 }
2637
2638 HRESULT NINE_WINAPI
2639 NineDevice9_SetClipStatus( struct NineDevice9 *This,
2640 const D3DCLIPSTATUS9 *pClipStatus )
2641 {
2642 STUB(D3DERR_INVALIDCALL);
2643 }
2644
2645 HRESULT NINE_WINAPI
2646 NineDevice9_GetClipStatus( struct NineDevice9 *This,
2647 D3DCLIPSTATUS9 *pClipStatus )
2648 {
2649 STUB(D3DERR_INVALIDCALL);
2650 }
2651
2652 HRESULT NINE_WINAPI
2653 NineDevice9_GetTexture( struct NineDevice9 *This,
2654 DWORD Stage,
2655 IDirect3DBaseTexture9 **ppTexture )
2656 {
2657 user_assert(Stage < This->caps.MaxSimultaneousTextures ||
2658 Stage == D3DDMAPSAMPLER ||
2659 (Stage >= D3DVERTEXTEXTURESAMPLER0 &&
2660 Stage <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2661 user_assert(ppTexture, D3DERR_INVALIDCALL);
2662
2663 if (Stage >= D3DDMAPSAMPLER)
2664 Stage = Stage - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2665
2666 *ppTexture = (IDirect3DBaseTexture9 *)This->state.texture[Stage];
2667
2668 if (This->state.texture[Stage])
2669 NineUnknown_AddRef(NineUnknown(This->state.texture[Stage]));
2670 return D3D_OK;
2671 }
2672
2673 HRESULT NINE_WINAPI
2674 NineDevice9_SetTexture( struct NineDevice9 *This,
2675 DWORD Stage,
2676 IDirect3DBaseTexture9 *pTexture )
2677 {
2678 struct nine_state *state = This->update;
2679 struct NineBaseTexture9 *tex = NineBaseTexture9(pTexture);
2680
2681 DBG("This=%p Stage=%u pTexture=%p\n", This, Stage, pTexture);
2682
2683 user_assert(Stage < This->caps.MaxSimultaneousTextures ||
2684 Stage == D3DDMAPSAMPLER ||
2685 (Stage >= D3DVERTEXTEXTURESAMPLER0 &&
2686 Stage <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2687 user_assert(!tex || (tex->base.pool != D3DPOOL_SCRATCH &&
2688 tex->base.pool != D3DPOOL_SYSTEMMEM), D3DERR_INVALIDCALL);
2689
2690 if (Stage >= D3DDMAPSAMPLER)
2691 Stage = Stage - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2692
2693 if (!This->is_recording) {
2694 struct NineBaseTexture9 *old = state->texture[Stage];
2695 if (old == tex)
2696 return D3D_OK;
2697
2698 state->samplers_shadow &= ~(1 << Stage);
2699 if (tex) {
2700 state->samplers_shadow |= tex->shadow << Stage;
2701
2702 if ((tex->managed.dirty | tex->dirty_mip) && LIST_IS_EMPTY(&tex->list))
2703 list_add(&tex->list, &This->update_textures);
2704
2705 tex->bind_count++;
2706 }
2707 if (old)
2708 old->bind_count--;
2709 }
2710 nine_bind(&state->texture[Stage], pTexture);
2711
2712 state->changed.texture |= 1 << Stage;
2713 state->changed.group |= NINE_STATE_TEXTURE;
2714
2715 return D3D_OK;
2716 }
2717
2718 HRESULT NINE_WINAPI
2719 NineDevice9_GetTextureStageState( struct NineDevice9 *This,
2720 DWORD Stage,
2721 D3DTEXTURESTAGESTATETYPE Type,
2722 DWORD *pValue )
2723 {
2724 const struct nine_state *state = &This->state;
2725
2726 user_assert(Stage < ARRAY_SIZE(state->ff.tex_stage), D3DERR_INVALIDCALL);
2727 user_assert(Type < ARRAY_SIZE(state->ff.tex_stage[0]), D3DERR_INVALIDCALL);
2728
2729 *pValue = state->ff.tex_stage[Stage][Type];
2730
2731 return D3D_OK;
2732 }
2733
2734 HRESULT NINE_WINAPI
2735 NineDevice9_SetTextureStageState( struct NineDevice9 *This,
2736 DWORD Stage,
2737 D3DTEXTURESTAGESTATETYPE Type,
2738 DWORD Value )
2739 {
2740 struct nine_state *state = This->update;
2741 int bumpmap_index = -1;
2742
2743 DBG("Stage=%u Type=%u Value=%08x\n", Stage, Type, Value);
2744 nine_dump_D3DTSS_value(DBG_FF, Type, Value);
2745
2746 user_assert(Stage < ARRAY_SIZE(state->ff.tex_stage), D3DERR_INVALIDCALL);
2747 user_assert(Type < ARRAY_SIZE(state->ff.tex_stage[0]), D3DERR_INVALIDCALL);
2748
2749 state->ff.tex_stage[Stage][Type] = Value;
2750 switch (Type) {
2751 case D3DTSS_BUMPENVMAT00:
2752 bumpmap_index = 4 * Stage;
2753 break;
2754 case D3DTSS_BUMPENVMAT10:
2755 bumpmap_index = 4 * Stage + 1;
2756 break;
2757 case D3DTSS_BUMPENVMAT01:
2758 bumpmap_index = 4 * Stage + 2;
2759 break;
2760 case D3DTSS_BUMPENVMAT11:
2761 bumpmap_index = 4 * Stage + 3;
2762 break;
2763 case D3DTSS_BUMPENVLSCALE:
2764 bumpmap_index = 4 * 8 + 2 * Stage;
2765 break;
2766 case D3DTSS_BUMPENVLOFFSET:
2767 bumpmap_index = 4 * 8 + 2 * Stage + 1;
2768 break;
2769 case D3DTSS_TEXTURETRANSFORMFLAGS:
2770 state->changed.group |= NINE_STATE_PS1X_SHADER;
2771 break;
2772 default:
2773 break;
2774 }
2775
2776 if (bumpmap_index >= 0) {
2777 state->bumpmap_vars[bumpmap_index] = Value;
2778 state->changed.group |= NINE_STATE_PS_CONST;
2779 }
2780
2781 state->changed.group |= NINE_STATE_FF_PSSTAGES;
2782 state->ff.changed.tex_stage[Stage][Type / 32] |= 1 << (Type % 32);
2783
2784 return D3D_OK;
2785 }
2786
2787 HRESULT NINE_WINAPI
2788 NineDevice9_GetSamplerState( struct NineDevice9 *This,
2789 DWORD Sampler,
2790 D3DSAMPLERSTATETYPE Type,
2791 DWORD *pValue )
2792 {
2793 user_assert(Sampler < This->caps.MaxSimultaneousTextures ||
2794 Sampler == D3DDMAPSAMPLER ||
2795 (Sampler >= D3DVERTEXTEXTURESAMPLER0 &&
2796 Sampler <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2797
2798 if (Sampler >= D3DDMAPSAMPLER)
2799 Sampler = Sampler - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2800
2801 *pValue = This->state.samp[Sampler][Type];
2802 return D3D_OK;
2803 }
2804
2805 HRESULT NINE_WINAPI
2806 NineDevice9_SetSamplerState( struct NineDevice9 *This,
2807 DWORD Sampler,
2808 D3DSAMPLERSTATETYPE Type,
2809 DWORD Value )
2810 {
2811 struct nine_state *state = This->update;
2812
2813 DBG("This=%p Sampler=%u Type=%s Value=%08x\n", This,
2814 Sampler, nine_D3DSAMP_to_str(Type), Value);
2815
2816 user_assert(Sampler < This->caps.MaxSimultaneousTextures ||
2817 Sampler == D3DDMAPSAMPLER ||
2818 (Sampler >= D3DVERTEXTEXTURESAMPLER0 &&
2819 Sampler <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2820
2821 if (Sampler >= D3DDMAPSAMPLER)
2822 Sampler = Sampler - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2823
2824 if (state->samp[Sampler][Type] != Value || unlikely(This->is_recording)) {
2825 state->samp[Sampler][Type] = Value;
2826 state->changed.group |= NINE_STATE_SAMPLER;
2827 state->changed.sampler[Sampler] |= 1 << Type;
2828 }
2829
2830 return D3D_OK;
2831 }
2832
2833 HRESULT NINE_WINAPI
2834 NineDevice9_ValidateDevice( struct NineDevice9 *This,
2835 DWORD *pNumPasses )
2836 {
2837 const struct nine_state *state = &This->state;
2838 unsigned i;
2839 unsigned w = 0, h = 0;
2840
2841 DBG("This=%p pNumPasses=%p\n", This, pNumPasses);
2842
2843 for (i = 0; i < ARRAY_SIZE(state->samp); ++i) {
2844 if (state->samp[i][D3DSAMP_MINFILTER] == D3DTEXF_NONE ||
2845 state->samp[i][D3DSAMP_MAGFILTER] == D3DTEXF_NONE)
2846 return D3DERR_UNSUPPORTEDTEXTUREFILTER;
2847 }
2848
2849 for (i = 0; i < This->caps.NumSimultaneousRTs; ++i) {
2850 if (!state->rt[i])
2851 continue;
2852 if (w == 0) {
2853 w = state->rt[i]->desc.Width;
2854 h = state->rt[i]->desc.Height;
2855 } else
2856 if (state->rt[i]->desc.Width != w || state->rt[i]->desc.Height != h) {
2857 return D3DERR_CONFLICTINGRENDERSTATE;
2858 }
2859 }
2860 if (state->ds &&
2861 (state->rs[D3DRS_ZENABLE] || state->rs[D3DRS_STENCILENABLE])) {
2862 if (w != 0 &&
2863 (state->ds->desc.Width != w || state->ds->desc.Height != h))
2864 return D3DERR_CONFLICTINGRENDERSTATE;
2865 }
2866
2867 if (pNumPasses)
2868 *pNumPasses = 1;
2869
2870 return D3D_OK;
2871 }
2872
2873 HRESULT NINE_WINAPI
2874 NineDevice9_SetPaletteEntries( struct NineDevice9 *This,
2875 UINT PaletteNumber,
2876 const PALETTEENTRY *pEntries )
2877 {
2878 STUB(D3D_OK); /* like wine */
2879 }
2880
2881 HRESULT NINE_WINAPI
2882 NineDevice9_GetPaletteEntries( struct NineDevice9 *This,
2883 UINT PaletteNumber,
2884 PALETTEENTRY *pEntries )
2885 {
2886 STUB(D3DERR_INVALIDCALL);
2887 }
2888
2889 HRESULT NINE_WINAPI
2890 NineDevice9_SetCurrentTexturePalette( struct NineDevice9 *This,
2891 UINT PaletteNumber )
2892 {
2893 STUB(D3D_OK); /* like wine */
2894 }
2895
2896 HRESULT NINE_WINAPI
2897 NineDevice9_GetCurrentTexturePalette( struct NineDevice9 *This,
2898 UINT *PaletteNumber )
2899 {
2900 STUB(D3DERR_INVALIDCALL);
2901 }
2902
2903 HRESULT NINE_WINAPI
2904 NineDevice9_SetScissorRect( struct NineDevice9 *This,
2905 const RECT *pRect )
2906 {
2907 struct nine_state *state = This->update;
2908
2909 DBG("x=(%u..%u) y=(%u..%u)\n",
2910 pRect->left, pRect->top, pRect->right, pRect->bottom);
2911
2912 state->scissor.minx = pRect->left;
2913 state->scissor.miny = pRect->top;
2914 state->scissor.maxx = pRect->right;
2915 state->scissor.maxy = pRect->bottom;
2916
2917 state->changed.group |= NINE_STATE_SCISSOR;
2918
2919 return D3D_OK;
2920 }
2921
2922 HRESULT NINE_WINAPI
2923 NineDevice9_GetScissorRect( struct NineDevice9 *This,
2924 RECT *pRect )
2925 {
2926 pRect->left = This->state.scissor.minx;
2927 pRect->top = This->state.scissor.miny;
2928 pRect->right = This->state.scissor.maxx;
2929 pRect->bottom = This->state.scissor.maxy;
2930
2931 return D3D_OK;
2932 }
2933
2934 HRESULT NINE_WINAPI
2935 NineDevice9_SetSoftwareVertexProcessing( struct NineDevice9 *This,
2936 BOOL bSoftware )
2937 {
2938 if (This->params.BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING) {
2939 This->swvp = bSoftware;
2940 This->state.changed.group |= NINE_STATE_SWVP;
2941 return D3D_OK;
2942 } else
2943 return D3DERR_INVALIDCALL; /* msdn. TODO: check in practice */
2944 }
2945
2946 BOOL NINE_WINAPI
2947 NineDevice9_GetSoftwareVertexProcessing( struct NineDevice9 *This )
2948 {
2949 return This->swvp;
2950 }
2951
2952 HRESULT NINE_WINAPI
2953 NineDevice9_SetNPatchMode( struct NineDevice9 *This,
2954 float nSegments )
2955 {
2956 return D3D_OK; /* Nothing to do because we don't advertise NPatch support */
2957 }
2958
2959 float NINE_WINAPI
2960 NineDevice9_GetNPatchMode( struct NineDevice9 *This )
2961 {
2962 STUB(0);
2963 }
2964
2965 static inline void
2966 init_draw_info(struct pipe_draw_info *info,
2967 struct NineDevice9 *dev, D3DPRIMITIVETYPE type, UINT count)
2968 {
2969 info->mode = d3dprimitivetype_to_pipe_prim(type);
2970 info->count = prim_count_to_vertex_count(type, count);
2971 info->start_instance = 0;
2972 info->instance_count = 1;
2973 if (dev->state.stream_instancedata_mask & dev->state.stream_usage_mask)
2974 info->instance_count = MAX2(dev->state.stream_freq[0] & 0x7FFFFF, 1);
2975 info->primitive_restart = FALSE;
2976 info->restart_index = 0;
2977 info->count_from_stream_output = NULL;
2978 info->indirect = NULL;
2979 info->indirect_params = NULL;
2980 }
2981
2982 HRESULT NINE_WINAPI
2983 NineDevice9_DrawPrimitive( struct NineDevice9 *This,
2984 D3DPRIMITIVETYPE PrimitiveType,
2985 UINT StartVertex,
2986 UINT PrimitiveCount )
2987 {
2988 struct pipe_draw_info info;
2989
2990 DBG("iface %p, PrimitiveType %u, StartVertex %u, PrimitiveCount %u\n",
2991 This, PrimitiveType, StartVertex, PrimitiveCount);
2992
2993 nine_update_state(This);
2994
2995 init_draw_info(&info, This, PrimitiveType, PrimitiveCount);
2996 info.indexed = FALSE;
2997 info.start = StartVertex;
2998 info.index_bias = 0;
2999 info.min_index = info.start;
3000 info.max_index = info.count - 1;
3001
3002 This->pipe->draw_vbo(This->pipe, &info);
3003
3004 return D3D_OK;
3005 }
3006
3007 HRESULT NINE_WINAPI
3008 NineDevice9_DrawIndexedPrimitive( struct NineDevice9 *This,
3009 D3DPRIMITIVETYPE PrimitiveType,
3010 INT BaseVertexIndex,
3011 UINT MinVertexIndex,
3012 UINT NumVertices,
3013 UINT StartIndex,
3014 UINT PrimitiveCount )
3015 {
3016 struct pipe_draw_info info;
3017
3018 DBG("iface %p, PrimitiveType %u, BaseVertexIndex %u, MinVertexIndex %u "
3019 "NumVertices %u, StartIndex %u, PrimitiveCount %u\n",
3020 This, PrimitiveType, BaseVertexIndex, MinVertexIndex, NumVertices,
3021 StartIndex, PrimitiveCount);
3022
3023 user_assert(This->state.idxbuf, D3DERR_INVALIDCALL);
3024 user_assert(This->state.vdecl, D3DERR_INVALIDCALL);
3025
3026 nine_update_state(This);
3027
3028 init_draw_info(&info, This, PrimitiveType, PrimitiveCount);
3029 info.indexed = TRUE;
3030 info.start = StartIndex;
3031 info.index_bias = BaseVertexIndex;
3032 /* These don't include index bias: */
3033 info.min_index = MinVertexIndex;
3034 info.max_index = MinVertexIndex + NumVertices - 1;
3035
3036 This->pipe->draw_vbo(This->pipe, &info);
3037
3038 return D3D_OK;
3039 }
3040
3041 HRESULT NINE_WINAPI
3042 NineDevice9_DrawPrimitiveUP( struct NineDevice9 *This,
3043 D3DPRIMITIVETYPE PrimitiveType,
3044 UINT PrimitiveCount,
3045 const void *pVertexStreamZeroData,
3046 UINT VertexStreamZeroStride )
3047 {
3048 struct pipe_vertex_buffer vtxbuf;
3049 struct pipe_draw_info info;
3050
3051 DBG("iface %p, PrimitiveType %u, PrimitiveCount %u, data %p, stride %u\n",
3052 This, PrimitiveType, PrimitiveCount,
3053 pVertexStreamZeroData, VertexStreamZeroStride);
3054
3055 user_assert(pVertexStreamZeroData && VertexStreamZeroStride,
3056 D3DERR_INVALIDCALL);
3057
3058 nine_update_state(This);
3059
3060 init_draw_info(&info, This, PrimitiveType, PrimitiveCount);
3061 info.indexed = FALSE;
3062 info.start = 0;
3063 info.index_bias = 0;
3064 info.min_index = 0;
3065 info.max_index = info.count - 1;
3066
3067 vtxbuf.stride = VertexStreamZeroStride;
3068 vtxbuf.buffer_offset = 0;
3069 vtxbuf.buffer = NULL;
3070 vtxbuf.user_buffer = pVertexStreamZeroData;
3071
3072 if (!This->driver_caps.user_vbufs) {
3073 u_upload_data(This->vertex_uploader,
3074 0,
3075 (info.max_index + 1) * VertexStreamZeroStride, /* XXX */
3076 4,
3077 vtxbuf.user_buffer,
3078 &vtxbuf.buffer_offset,
3079 &vtxbuf.buffer);
3080 u_upload_unmap(This->vertex_uploader);
3081 vtxbuf.user_buffer = NULL;
3082 }
3083
3084 This->pipe->set_vertex_buffers(This->pipe, 0, 1, &vtxbuf);
3085
3086 This->pipe->draw_vbo(This->pipe, &info);
3087
3088 NineDevice9_PauseRecording(This);
3089 NineDevice9_SetStreamSource(This, 0, NULL, 0, 0);
3090 NineDevice9_ResumeRecording(This);
3091
3092 pipe_resource_reference(&vtxbuf.buffer, NULL);
3093
3094 return D3D_OK;
3095 }
3096
3097 HRESULT NINE_WINAPI
3098 NineDevice9_DrawIndexedPrimitiveUP( struct NineDevice9 *This,
3099 D3DPRIMITIVETYPE PrimitiveType,
3100 UINT MinVertexIndex,
3101 UINT NumVertices,
3102 UINT PrimitiveCount,
3103 const void *pIndexData,
3104 D3DFORMAT IndexDataFormat,
3105 const void *pVertexStreamZeroData,
3106 UINT VertexStreamZeroStride )
3107 {
3108 struct pipe_draw_info info;
3109 struct pipe_vertex_buffer vbuf;
3110 struct pipe_index_buffer ibuf;
3111
3112 DBG("iface %p, PrimitiveType %u, MinVertexIndex %u, NumVertices %u "
3113 "PrimitiveCount %u, pIndexData %p, IndexDataFormat %u "
3114 "pVertexStreamZeroData %p, VertexStreamZeroStride %u\n",
3115 This, PrimitiveType, MinVertexIndex, NumVertices, PrimitiveCount,
3116 pIndexData, IndexDataFormat,
3117 pVertexStreamZeroData, VertexStreamZeroStride);
3118
3119 user_assert(pIndexData && pVertexStreamZeroData, D3DERR_INVALIDCALL);
3120 user_assert(VertexStreamZeroStride, D3DERR_INVALIDCALL);
3121 user_assert(IndexDataFormat == D3DFMT_INDEX16 ||
3122 IndexDataFormat == D3DFMT_INDEX32, D3DERR_INVALIDCALL);
3123
3124 nine_update_state(This);
3125
3126 init_draw_info(&info, This, PrimitiveType, PrimitiveCount);
3127 info.indexed = TRUE;
3128 info.start = 0;
3129 info.index_bias = 0;
3130 info.min_index = MinVertexIndex;
3131 info.max_index = MinVertexIndex + NumVertices - 1;
3132
3133 vbuf.stride = VertexStreamZeroStride;
3134 vbuf.buffer_offset = 0;
3135 vbuf.buffer = NULL;
3136 vbuf.user_buffer = pVertexStreamZeroData;
3137
3138 ibuf.index_size = (IndexDataFormat == D3DFMT_INDEX16) ? 2 : 4;
3139 ibuf.offset = 0;
3140 ibuf.buffer = NULL;
3141 ibuf.user_buffer = pIndexData;
3142
3143 if (!This->driver_caps.user_vbufs) {
3144 const unsigned base = info.min_index * VertexStreamZeroStride;
3145 u_upload_data(This->vertex_uploader,
3146 base,
3147 (info.max_index -
3148 info.min_index + 1) * VertexStreamZeroStride, /* XXX */
3149 4,
3150 (const uint8_t *)vbuf.user_buffer + base,
3151 &vbuf.buffer_offset,
3152 &vbuf.buffer);
3153 u_upload_unmap(This->vertex_uploader);
3154 /* Won't be used: */
3155 vbuf.buffer_offset -= base;
3156 vbuf.user_buffer = NULL;
3157 }
3158 if (!This->driver_caps.user_ibufs) {
3159 u_upload_data(This->index_uploader,
3160 0,
3161 info.count * ibuf.index_size,
3162 4,
3163 ibuf.user_buffer,
3164 &ibuf.offset,
3165 &ibuf.buffer);
3166 u_upload_unmap(This->index_uploader);
3167 ibuf.user_buffer = NULL;
3168 }
3169
3170 This->pipe->set_vertex_buffers(This->pipe, 0, 1, &vbuf);
3171 This->pipe->set_index_buffer(This->pipe, &ibuf);
3172
3173 This->pipe->draw_vbo(This->pipe, &info);
3174
3175 pipe_resource_reference(&vbuf.buffer, NULL);
3176 pipe_resource_reference(&ibuf.buffer, NULL);
3177
3178 NineDevice9_PauseRecording(This);
3179 NineDevice9_SetIndices(This, NULL);
3180 NineDevice9_SetStreamSource(This, 0, NULL, 0, 0);
3181 NineDevice9_ResumeRecording(This);
3182
3183 return D3D_OK;
3184 }
3185
3186 HRESULT NINE_WINAPI
3187 NineDevice9_ProcessVertices( struct NineDevice9 *This,
3188 UINT SrcStartIndex,
3189 UINT DestIndex,
3190 UINT VertexCount,
3191 IDirect3DVertexBuffer9 *pDestBuffer,
3192 IDirect3DVertexDeclaration9 *pVertexDecl,
3193 DWORD Flags )
3194 {
3195 struct pipe_screen *screen_sw = This->screen_sw;
3196 struct pipe_context *pipe_sw = This->pipe_sw;
3197 struct NineVertexDeclaration9 *vdecl = NineVertexDeclaration9(pVertexDecl);
3198 struct NineVertexBuffer9 *dst = NineVertexBuffer9(pDestBuffer);
3199 struct NineVertexShader9 *vs;
3200 struct pipe_resource *resource;
3201 struct pipe_transfer *transfer = NULL;
3202 struct pipe_stream_output_info so;
3203 struct pipe_stream_output_target *target;
3204 struct pipe_draw_info draw;
3205 struct pipe_box box;
3206 unsigned offsets[1] = {0};
3207 HRESULT hr;
3208 unsigned buffer_size;
3209 void *map;
3210
3211 DBG("This=%p SrcStartIndex=%u DestIndex=%u VertexCount=%u "
3212 "pDestBuffer=%p pVertexDecl=%p Flags=%d\n",
3213 This, SrcStartIndex, DestIndex, VertexCount, pDestBuffer,
3214 pVertexDecl, Flags);
3215
3216 if (!screen_sw->get_param(screen_sw, PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS)) {
3217 DBG("ProcessVertices not supported\n");
3218 return D3DERR_INVALIDCALL;
3219 }
3220
3221
3222 vs = This->state.programmable_vs ? This->state.vs : This->ff.vs;
3223 /* Note: version is 0 for ff */
3224 user_assert(vdecl || (vs->byte_code.version < 0x30 && dst->desc.FVF),
3225 D3DERR_INVALIDCALL);
3226 if (!vdecl) {
3227 DWORD FVF = dst->desc.FVF;
3228 vdecl = util_hash_table_get(This->ff.ht_fvf, &FVF);
3229 if (!vdecl) {
3230 hr = NineVertexDeclaration9_new_from_fvf(This, FVF, &vdecl);
3231 if (FAILED(hr))
3232 return hr;
3233 vdecl->fvf = FVF;
3234 util_hash_table_set(This->ff.ht_fvf, &vdecl->fvf, vdecl);
3235 NineUnknown_ConvertRefToBind(NineUnknown(vdecl));
3236 }
3237 }
3238
3239 /* Flags: Can be 0 or D3DPV_DONOTCOPYDATA, and/or lock flags
3240 * D3DPV_DONOTCOPYDATA -> Has effect only for ff. In particular
3241 * if not set, everything from src will be used, and dst
3242 * must match exactly the ff vs outputs.
3243 * TODO: Handle all the checks, etc for ff */
3244 user_assert(vdecl->position_t || This->state.programmable_vs,
3245 D3DERR_INVALIDCALL);
3246
3247 /* TODO: Support vs < 3 and ff */
3248 user_assert(vs->byte_code.version == 0x30,
3249 D3DERR_INVALIDCALL);
3250 /* TODO: Not hardcode the constant buffers for swvp */
3251 user_assert(This->may_swvp,
3252 D3DERR_INVALIDCALL);
3253
3254 nine_state_prepare_draw_sw(This, vdecl, SrcStartIndex, VertexCount, &so);
3255
3256 buffer_size = VertexCount * so.stride[0] * 4;
3257 {
3258 struct pipe_resource templ;
3259
3260 memset(&templ, 0, sizeof(templ));
3261 templ.target = PIPE_BUFFER;
3262 templ.format = PIPE_FORMAT_R8_UNORM;
3263 templ.width0 = buffer_size;
3264 templ.flags = 0;
3265 templ.bind = PIPE_BIND_STREAM_OUTPUT;
3266 templ.usage = PIPE_USAGE_STREAM;
3267 templ.height0 = templ.depth0 = templ.array_size = 1;
3268 templ.last_level = templ.nr_samples = 0;
3269
3270 resource = screen_sw->resource_create(screen_sw, &templ);
3271 if (!resource)
3272 return E_OUTOFMEMORY;
3273 }
3274 target = pipe_sw->create_stream_output_target(pipe_sw, resource,
3275 0, buffer_size);
3276 if (!target) {
3277 pipe_resource_reference(&resource, NULL);
3278 return D3DERR_DRIVERINTERNALERROR;
3279 }
3280
3281 init_draw_info(&draw, This, D3DPT_POINTLIST, VertexCount);
3282 draw.instance_count = 1;
3283 draw.indexed = FALSE;
3284 draw.start = 0;
3285 draw.index_bias = 0;
3286 draw.min_index = 0;
3287 draw.max_index = VertexCount - 1;
3288
3289
3290 pipe_sw->set_stream_output_targets(pipe_sw, 1, &target, offsets);
3291
3292 pipe_sw->draw_vbo(pipe_sw, &draw);
3293
3294 pipe_sw->set_stream_output_targets(pipe_sw, 0, NULL, 0);
3295 pipe_sw->stream_output_target_destroy(pipe_sw, target);
3296
3297 u_box_1d(0, VertexCount * so.stride[0] * 4, &box);
3298 map = pipe_sw->transfer_map(pipe_sw, resource, 0, PIPE_TRANSFER_READ, &box,
3299 &transfer);
3300 if (!map) {
3301 hr = D3DERR_DRIVERINTERNALERROR;
3302 goto out;
3303 }
3304
3305 hr = NineVertexDeclaration9_ConvertStreamOutput(vdecl,
3306 dst, DestIndex, VertexCount,
3307 map, &so);
3308 if (transfer)
3309 pipe_sw->transfer_unmap(pipe_sw, transfer);
3310
3311 out:
3312 nine_state_after_draw_sw(This);
3313 pipe_resource_reference(&resource, NULL);
3314 return hr;
3315 }
3316
3317 HRESULT NINE_WINAPI
3318 NineDevice9_CreateVertexDeclaration( struct NineDevice9 *This,
3319 const D3DVERTEXELEMENT9 *pVertexElements,
3320 IDirect3DVertexDeclaration9 **ppDecl )
3321 {
3322 struct NineVertexDeclaration9 *vdecl;
3323
3324 DBG("This=%p pVertexElements=%p ppDecl=%p\n",
3325 This, pVertexElements, ppDecl);
3326
3327 HRESULT hr = NineVertexDeclaration9_new(This, pVertexElements, &vdecl);
3328 if (SUCCEEDED(hr))
3329 *ppDecl = (IDirect3DVertexDeclaration9 *)vdecl;
3330
3331 return hr;
3332 }
3333
3334 HRESULT NINE_WINAPI
3335 NineDevice9_SetVertexDeclaration( struct NineDevice9 *This,
3336 IDirect3DVertexDeclaration9 *pDecl )
3337 {
3338 struct nine_state *state = This->update;
3339 BOOL was_programmable_vs = This->state.programmable_vs;
3340
3341 DBG("This=%p pDecl=%p\n", This, pDecl);
3342
3343 if (likely(!This->is_recording) && state->vdecl == NineVertexDeclaration9(pDecl))
3344 return D3D_OK;
3345
3346 nine_bind(&state->vdecl, pDecl);
3347
3348 This->state.programmable_vs = This->state.vs && !(This->state.vdecl && This->state.vdecl->position_t);
3349 if (likely(!This->is_recording) && was_programmable_vs != This->state.programmable_vs) {
3350 state->commit |= NINE_STATE_COMMIT_CONST_VS;
3351 state->changed.group |= NINE_STATE_VS;
3352 }
3353
3354 state->changed.group |= NINE_STATE_VDECL;
3355
3356 return D3D_OK;
3357 }
3358
3359 HRESULT NINE_WINAPI
3360 NineDevice9_GetVertexDeclaration( struct NineDevice9 *This,
3361 IDirect3DVertexDeclaration9 **ppDecl )
3362 {
3363 user_assert(ppDecl, D3DERR_INVALIDCALL);
3364
3365 *ppDecl = (IDirect3DVertexDeclaration9 *)This->state.vdecl;
3366 if (*ppDecl)
3367 NineUnknown_AddRef(NineUnknown(*ppDecl));
3368 return D3D_OK;
3369 }
3370
3371 HRESULT NINE_WINAPI
3372 NineDevice9_SetFVF( struct NineDevice9 *This,
3373 DWORD FVF )
3374 {
3375 struct NineVertexDeclaration9 *vdecl;
3376 HRESULT hr;
3377
3378 DBG("FVF = %08x\n", FVF);
3379 if (!FVF)
3380 return D3D_OK; /* like wine */
3381
3382 vdecl = util_hash_table_get(This->ff.ht_fvf, &FVF);
3383 if (!vdecl) {
3384 hr = NineVertexDeclaration9_new_from_fvf(This, FVF, &vdecl);
3385 if (FAILED(hr))
3386 return hr;
3387 vdecl->fvf = FVF;
3388 util_hash_table_set(This->ff.ht_fvf, &vdecl->fvf, vdecl);
3389 NineUnknown_ConvertRefToBind(NineUnknown(vdecl));
3390 }
3391 return NineDevice9_SetVertexDeclaration(
3392 This, (IDirect3DVertexDeclaration9 *)vdecl);
3393 }
3394
3395 HRESULT NINE_WINAPI
3396 NineDevice9_GetFVF( struct NineDevice9 *This,
3397 DWORD *pFVF )
3398 {
3399 *pFVF = This->state.vdecl ? This->state.vdecl->fvf : 0;
3400 return D3D_OK;
3401 }
3402
3403 HRESULT NINE_WINAPI
3404 NineDevice9_CreateVertexShader( struct NineDevice9 *This,
3405 const DWORD *pFunction,
3406 IDirect3DVertexShader9 **ppShader )
3407 {
3408 struct NineVertexShader9 *vs;
3409 HRESULT hr;
3410
3411 DBG("This=%p pFunction=%p ppShader=%p\n", This, pFunction, ppShader);
3412
3413 hr = NineVertexShader9_new(This, &vs, pFunction, NULL);
3414 if (FAILED(hr))
3415 return hr;
3416 *ppShader = (IDirect3DVertexShader9 *)vs;
3417 return D3D_OK;
3418 }
3419
3420 HRESULT NINE_WINAPI
3421 NineDevice9_SetVertexShader( struct NineDevice9 *This,
3422 IDirect3DVertexShader9 *pShader )
3423 {
3424 struct nine_state *state = This->update;
3425 BOOL was_programmable_vs = This->state.programmable_vs;
3426
3427 DBG("This=%p pShader=%p\n", This, pShader);
3428
3429 if (!This->is_recording && state->vs == (struct NineVertexShader9*)pShader)
3430 return D3D_OK;
3431
3432 nine_bind(&state->vs, pShader);
3433
3434 This->state.programmable_vs = This->state.vs && !(This->state.vdecl && This->state.vdecl->position_t);
3435
3436 /* ff -> non-ff: commit back non-ff constants */
3437 if (!was_programmable_vs && This->state.programmable_vs)
3438 state->commit |= NINE_STATE_COMMIT_CONST_VS;
3439
3440 state->changed.group |= NINE_STATE_VS;
3441
3442 return D3D_OK;
3443 }
3444
3445 HRESULT NINE_WINAPI
3446 NineDevice9_GetVertexShader( struct NineDevice9 *This,
3447 IDirect3DVertexShader9 **ppShader )
3448 {
3449 user_assert(ppShader, D3DERR_INVALIDCALL);
3450 nine_reference_set(ppShader, This->state.vs);
3451 return D3D_OK;
3452 }
3453
3454 HRESULT NINE_WINAPI
3455 NineDevice9_SetVertexShaderConstantF( struct NineDevice9 *This,
3456 UINT StartRegister,
3457 const float *pConstantData,
3458 UINT Vector4fCount )
3459 {
3460 struct nine_state *state = This->update;
3461 float *vs_const_f = This->may_swvp ? state->vs_const_f_swvp : state->vs_const_f;
3462
3463 DBG("This=%p StartRegister=%u pConstantData=%p Vector4fCount=%u\n",
3464 This, StartRegister, pConstantData, Vector4fCount);
3465
3466 user_assert(StartRegister < This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3467 user_assert(StartRegister + Vector4fCount <= This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3468
3469 if (!Vector4fCount)
3470 return D3D_OK;
3471 user_assert(pConstantData, D3DERR_INVALIDCALL);
3472
3473 if (!This->is_recording) {
3474 if (!memcmp(&vs_const_f[StartRegister * 4], pConstantData,
3475 Vector4fCount * 4 * sizeof(state->vs_const_f[0])))
3476 return D3D_OK;
3477 }
3478
3479 memcpy(&vs_const_f[StartRegister * 4],
3480 pConstantData,
3481 Vector4fCount * 4 * sizeof(state->vs_const_f[0]));
3482
3483 nine_ranges_insert(&state->changed.vs_const_f,
3484 StartRegister, StartRegister + Vector4fCount,
3485 &This->range_pool);
3486
3487 if (This->may_swvp) {
3488 Vector4fCount = MIN2(StartRegister + Vector4fCount, NINE_MAX_CONST_F) - StartRegister;
3489 if (StartRegister < NINE_MAX_CONST_F)
3490 memcpy(&state->vs_const_f[StartRegister * 4],
3491 pConstantData,
3492 Vector4fCount * 4 * sizeof(state->vs_const_f[0]));
3493 }
3494
3495 state->changed.group |= NINE_STATE_VS_CONST;
3496
3497 return D3D_OK;
3498 }
3499
3500 HRESULT NINE_WINAPI
3501 NineDevice9_GetVertexShaderConstantF( struct NineDevice9 *This,
3502 UINT StartRegister,
3503 float *pConstantData,
3504 UINT Vector4fCount )
3505 {
3506 const struct nine_state *state = &This->state;
3507 float *vs_const_f = This->may_swvp ? state->vs_const_f_swvp : state->vs_const_f;
3508
3509 user_assert(StartRegister < This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3510 user_assert(StartRegister + Vector4fCount <= This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3511 user_assert(pConstantData, D3DERR_INVALIDCALL);
3512
3513 memcpy(pConstantData,
3514 &vs_const_f[StartRegister * 4],
3515 Vector4fCount * 4 * sizeof(state->vs_const_f[0]));
3516
3517 return D3D_OK;
3518 }
3519
3520 HRESULT NINE_WINAPI
3521 NineDevice9_SetVertexShaderConstantI( struct NineDevice9 *This,
3522 UINT StartRegister,
3523 const int *pConstantData,
3524 UINT Vector4iCount )
3525 {
3526 struct nine_state *state = This->update;
3527 int i;
3528
3529 DBG("This=%p StartRegister=%u pConstantData=%p Vector4iCount=%u\n",
3530 This, StartRegister, pConstantData, Vector4iCount);
3531
3532 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3533 D3DERR_INVALIDCALL);
3534 user_assert(StartRegister + Vector4iCount <= (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3535 D3DERR_INVALIDCALL);
3536 user_assert(pConstantData, D3DERR_INVALIDCALL);
3537
3538 if (This->driver_caps.vs_integer) {
3539 if (!This->is_recording) {
3540 if (!memcmp(&state->vs_const_i[4 * StartRegister], pConstantData,
3541 Vector4iCount * sizeof(int[4])))
3542 return D3D_OK;
3543 }
3544 memcpy(&state->vs_const_i[4 * StartRegister],
3545 pConstantData,
3546 Vector4iCount * sizeof(int[4]));
3547 } else {
3548 for (i = 0; i < Vector4iCount; i++) {
3549 state->vs_const_i[4 * (StartRegister + i)] = fui((float)(pConstantData[4 * i]));
3550 state->vs_const_i[4 * (StartRegister + i) + 1] = fui((float)(pConstantData[4 * i + 1]));
3551 state->vs_const_i[4 * (StartRegister + i) + 2] = fui((float)(pConstantData[4 * i + 2]));
3552 state->vs_const_i[4 * (StartRegister + i) + 3] = fui((float)(pConstantData[4 * i + 3]));
3553 }
3554 }
3555
3556 nine_ranges_insert(&state->changed.vs_const_i,
3557 StartRegister, StartRegister + Vector4iCount,
3558 &This->range_pool);
3559 state->changed.group |= NINE_STATE_VS_CONST;
3560
3561 return D3D_OK;
3562 }
3563
3564 HRESULT NINE_WINAPI
3565 NineDevice9_GetVertexShaderConstantI( struct NineDevice9 *This,
3566 UINT StartRegister,
3567 int *pConstantData,
3568 UINT Vector4iCount )
3569 {
3570 const struct nine_state *state = &This->state;
3571 int i;
3572
3573 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3574 D3DERR_INVALIDCALL);
3575 user_assert(StartRegister + Vector4iCount <= (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3576 D3DERR_INVALIDCALL);
3577 user_assert(pConstantData, D3DERR_INVALIDCALL);
3578
3579 if (This->driver_caps.vs_integer) {
3580 memcpy(pConstantData,
3581 &state->vs_const_i[4 * StartRegister],
3582 Vector4iCount * sizeof(int[4]));
3583 } else {
3584 for (i = 0; i < Vector4iCount; i++) {
3585 pConstantData[4 * i] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i)]);
3586 pConstantData[4 * i + 1] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i) + 1]);
3587 pConstantData[4 * i + 2] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i) + 2]);
3588 pConstantData[4 * i + 3] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i) + 3]);
3589 }
3590 }
3591
3592 return D3D_OK;
3593 }
3594
3595 HRESULT NINE_WINAPI
3596 NineDevice9_SetVertexShaderConstantB( struct NineDevice9 *This,
3597 UINT StartRegister,
3598 const BOOL *pConstantData,
3599 UINT BoolCount )
3600 {
3601 struct nine_state *state = This->update;
3602 int i;
3603 uint32_t bool_true = This->driver_caps.vs_integer ? 0xFFFFFFFF : fui(1.0f);
3604
3605 DBG("This=%p StartRegister=%u pConstantData=%p BoolCount=%u\n",
3606 This, StartRegister, pConstantData, BoolCount);
3607
3608 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3609 D3DERR_INVALIDCALL);
3610 user_assert(StartRegister + BoolCount <= (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3611 D3DERR_INVALIDCALL);
3612 user_assert(pConstantData, D3DERR_INVALIDCALL);
3613
3614 if (!This->is_recording) {
3615 bool noChange = true;
3616 for (i = 0; i < BoolCount; i++) {
3617 if (!!state->vs_const_b[StartRegister + i] != !!pConstantData[i])
3618 noChange = false;
3619 }
3620 if (noChange)
3621 return D3D_OK;
3622 }
3623
3624 for (i = 0; i < BoolCount; i++)
3625 state->vs_const_b[StartRegister + i] = pConstantData[i] ? bool_true : 0;
3626
3627 nine_ranges_insert(&state->changed.vs_const_b,
3628 StartRegister, StartRegister + BoolCount,
3629 &This->range_pool);
3630 state->changed.group |= NINE_STATE_VS_CONST;
3631
3632 return D3D_OK;
3633 }
3634
3635 HRESULT NINE_WINAPI
3636 NineDevice9_GetVertexShaderConstantB( struct NineDevice9 *This,
3637 UINT StartRegister,
3638 BOOL *pConstantData,
3639 UINT BoolCount )
3640 {
3641 const struct nine_state *state = &This->state;
3642 int i;
3643
3644 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3645 D3DERR_INVALIDCALL);
3646 user_assert(StartRegister + BoolCount <= (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3647 D3DERR_INVALIDCALL);
3648 user_assert(pConstantData, D3DERR_INVALIDCALL);
3649
3650 for (i = 0; i < BoolCount; i++)
3651 pConstantData[i] = state->vs_const_b[StartRegister + i] != 0 ? TRUE : FALSE;
3652
3653 return D3D_OK;
3654 }
3655
3656 HRESULT NINE_WINAPI
3657 NineDevice9_SetStreamSource( struct NineDevice9 *This,
3658 UINT StreamNumber,
3659 IDirect3DVertexBuffer9 *pStreamData,
3660 UINT OffsetInBytes,
3661 UINT Stride )
3662 {
3663 struct nine_state *state = This->update;
3664 struct NineVertexBuffer9 *pVBuf9 = NineVertexBuffer9(pStreamData);
3665 const unsigned i = StreamNumber;
3666
3667 DBG("This=%p StreamNumber=%u pStreamData=%p OffsetInBytes=%u Stride=%u\n",
3668 This, StreamNumber, pStreamData, OffsetInBytes, Stride);
3669
3670 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3671 user_assert(Stride <= This->caps.MaxStreamStride, D3DERR_INVALIDCALL);
3672
3673 if (likely(!This->is_recording)) {
3674 if (state->stream[i] == NineVertexBuffer9(pStreamData) &&
3675 state->vtxbuf[i].stride == Stride &&
3676 state->vtxbuf[i].buffer_offset == OffsetInBytes)
3677 return D3D_OK;
3678 }
3679 nine_bind(&state->stream[i], pStreamData);
3680
3681 state->changed.vtxbuf |= 1 << StreamNumber;
3682
3683 if (pStreamData) {
3684 state->vtxbuf[i].stride = Stride;
3685 state->vtxbuf[i].buffer_offset = OffsetInBytes;
3686 }
3687 pipe_resource_reference(&state->vtxbuf[i].buffer,
3688 pStreamData ? NineVertexBuffer9_GetResource(pVBuf9) : NULL);
3689
3690 return D3D_OK;
3691 }
3692
3693 HRESULT NINE_WINAPI
3694 NineDevice9_GetStreamSource( struct NineDevice9 *This,
3695 UINT StreamNumber,
3696 IDirect3DVertexBuffer9 **ppStreamData,
3697 UINT *pOffsetInBytes,
3698 UINT *pStride )
3699 {
3700 const struct nine_state *state = &This->state;
3701 const unsigned i = StreamNumber;
3702
3703 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3704 user_assert(ppStreamData, D3DERR_INVALIDCALL);
3705
3706 nine_reference_set(ppStreamData, state->stream[i]);
3707 *pStride = state->vtxbuf[i].stride;
3708 *pOffsetInBytes = state->vtxbuf[i].buffer_offset;
3709
3710 return D3D_OK;
3711 }
3712
3713 HRESULT NINE_WINAPI
3714 NineDevice9_SetStreamSourceFreq( struct NineDevice9 *This,
3715 UINT StreamNumber,
3716 UINT Setting )
3717 {
3718 struct nine_state *state = This->update;
3719 /* const UINT freq = Setting & 0x7FFFFF; */
3720
3721 DBG("This=%p StreamNumber=%u FrequencyParameter=0x%x\n", This,
3722 StreamNumber, Setting);
3723
3724 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3725 user_assert(StreamNumber != 0 || !(Setting & D3DSTREAMSOURCE_INSTANCEDATA),
3726 D3DERR_INVALIDCALL);
3727 user_assert(!((Setting & D3DSTREAMSOURCE_INSTANCEDATA) &&
3728 (Setting & D3DSTREAMSOURCE_INDEXEDDATA)), D3DERR_INVALIDCALL);
3729 user_assert(Setting, D3DERR_INVALIDCALL);
3730
3731 if (likely(!This->is_recording) && state->stream_freq[StreamNumber] == Setting)
3732 return D3D_OK;
3733
3734 state->stream_freq[StreamNumber] = Setting;
3735
3736 if (Setting & D3DSTREAMSOURCE_INSTANCEDATA)
3737 state->stream_instancedata_mask |= 1 << StreamNumber;
3738 else
3739 state->stream_instancedata_mask &= ~(1 << StreamNumber);
3740
3741 state->changed.stream_freq |= 1 << StreamNumber; /* Used for stateblocks */
3742 if (StreamNumber != 0)
3743 state->changed.group |= NINE_STATE_STREAMFREQ;
3744 return D3D_OK;
3745 }
3746
3747 HRESULT NINE_WINAPI
3748 NineDevice9_GetStreamSourceFreq( struct NineDevice9 *This,
3749 UINT StreamNumber,
3750 UINT *pSetting )
3751 {
3752 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3753 *pSetting = This->state.stream_freq[StreamNumber];
3754 return D3D_OK;
3755 }
3756
3757 HRESULT NINE_WINAPI
3758 NineDevice9_SetIndices( struct NineDevice9 *This,
3759 IDirect3DIndexBuffer9 *pIndexData )
3760 {
3761 struct nine_state *state = This->update;
3762
3763 DBG("This=%p pIndexData=%p\n", This, pIndexData);
3764
3765 if (likely(!This->is_recording))
3766 if (state->idxbuf == NineIndexBuffer9(pIndexData))
3767 return D3D_OK;
3768 nine_bind(&state->idxbuf, pIndexData);
3769
3770 state->changed.group |= NINE_STATE_IDXBUF;
3771
3772 return D3D_OK;
3773 }
3774
3775 /* XXX: wine/d3d9 doesn't have pBaseVertexIndex, and it doesn't make sense
3776 * here because it's an argument passed to the Draw calls.
3777 */
3778 HRESULT NINE_WINAPI
3779 NineDevice9_GetIndices( struct NineDevice9 *This,
3780 IDirect3DIndexBuffer9 **ppIndexData)
3781 {
3782 user_assert(ppIndexData, D3DERR_INVALIDCALL);
3783 nine_reference_set(ppIndexData, This->state.idxbuf);
3784 return D3D_OK;
3785 }
3786
3787 HRESULT NINE_WINAPI
3788 NineDevice9_CreatePixelShader( struct NineDevice9 *This,
3789 const DWORD *pFunction,
3790 IDirect3DPixelShader9 **ppShader )
3791 {
3792 struct NinePixelShader9 *ps;
3793 HRESULT hr;
3794
3795 DBG("This=%p pFunction=%p ppShader=%p\n", This, pFunction, ppShader);
3796
3797 hr = NinePixelShader9_new(This, &ps, pFunction, NULL);
3798 if (FAILED(hr))
3799 return hr;
3800 *ppShader = (IDirect3DPixelShader9 *)ps;
3801 return D3D_OK;
3802 }
3803
3804 HRESULT NINE_WINAPI
3805 NineDevice9_SetPixelShader( struct NineDevice9 *This,
3806 IDirect3DPixelShader9 *pShader )
3807 {
3808 struct nine_state *state = This->update;
3809 unsigned old_mask = state->ps ? state->ps->rt_mask : 1;
3810 unsigned mask;
3811
3812 DBG("This=%p pShader=%p\n", This, pShader);
3813
3814 if (!This->is_recording && state->ps == (struct NinePixelShader9*)pShader)
3815 return D3D_OK;
3816
3817 /* ff -> non-ff: commit back non-ff constants */
3818 if (!state->ps && pShader)
3819 state->commit |= NINE_STATE_COMMIT_CONST_PS;
3820
3821 nine_bind(&state->ps, pShader);
3822
3823 state->changed.group |= NINE_STATE_PS;
3824
3825 mask = state->ps ? state->ps->rt_mask : 1;
3826 /* We need to update cbufs if the pixel shader would
3827 * write to different render targets */
3828 if (mask != old_mask)
3829 state->changed.group |= NINE_STATE_FB;
3830
3831 return D3D_OK;
3832 }
3833
3834 HRESULT NINE_WINAPI
3835 NineDevice9_GetPixelShader( struct NineDevice9 *This,
3836 IDirect3DPixelShader9 **ppShader )
3837 {
3838 user_assert(ppShader, D3DERR_INVALIDCALL);
3839 nine_reference_set(ppShader, This->state.ps);
3840 return D3D_OK;
3841 }
3842
3843 HRESULT NINE_WINAPI
3844 NineDevice9_SetPixelShaderConstantF( struct NineDevice9 *This,
3845 UINT StartRegister,
3846 const float *pConstantData,
3847 UINT Vector4fCount )
3848 {
3849 struct nine_state *state = This->update;
3850
3851 DBG("This=%p StartRegister=%u pConstantData=%p Vector4fCount=%u\n",
3852 This, StartRegister, pConstantData, Vector4fCount);
3853
3854 user_assert(StartRegister < NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3855 user_assert(StartRegister + Vector4fCount <= NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3856
3857 if (!Vector4fCount)
3858 return D3D_OK;
3859 user_assert(pConstantData, D3DERR_INVALIDCALL);
3860
3861 if (!This->is_recording) {
3862 if (!memcmp(&state->ps_const_f[StartRegister * 4], pConstantData,
3863 Vector4fCount * 4 * sizeof(state->ps_const_f[0])))
3864 return D3D_OK;
3865 }
3866
3867 memcpy(&state->ps_const_f[StartRegister * 4],
3868 pConstantData,
3869 Vector4fCount * 4 * sizeof(state->ps_const_f[0]));
3870
3871 nine_ranges_insert(&state->changed.ps_const_f,
3872 StartRegister, StartRegister + Vector4fCount,
3873 &This->range_pool);
3874
3875 state->changed.group |= NINE_STATE_PS_CONST;
3876
3877 return D3D_OK;
3878 }
3879
3880 HRESULT NINE_WINAPI
3881 NineDevice9_GetPixelShaderConstantF( struct NineDevice9 *This,
3882 UINT StartRegister,
3883 float *pConstantData,
3884 UINT Vector4fCount )
3885 {
3886 const struct nine_state *state = &This->state;
3887
3888 user_assert(StartRegister < NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3889 user_assert(StartRegister + Vector4fCount <= NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3890 user_assert(pConstantData, D3DERR_INVALIDCALL);
3891
3892 memcpy(pConstantData,
3893 &state->ps_const_f[StartRegister * 4],
3894 Vector4fCount * 4 * sizeof(state->ps_const_f[0]));
3895
3896 return D3D_OK;
3897 }
3898
3899 HRESULT NINE_WINAPI
3900 NineDevice9_SetPixelShaderConstantI( struct NineDevice9 *This,
3901 UINT StartRegister,
3902 const int *pConstantData,
3903 UINT Vector4iCount )
3904 {
3905 struct nine_state *state = This->update;
3906 int i;
3907
3908 DBG("This=%p StartRegister=%u pConstantData=%p Vector4iCount=%u\n",
3909 This, StartRegister, pConstantData, Vector4iCount);
3910
3911 user_assert(StartRegister < NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3912 user_assert(StartRegister + Vector4iCount <= NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3913 user_assert(pConstantData, D3DERR_INVALIDCALL);
3914
3915 if (This->driver_caps.ps_integer) {
3916 if (!This->is_recording) {
3917 if (!memcmp(&state->ps_const_i[StartRegister][0], pConstantData,
3918 Vector4iCount * sizeof(state->ps_const_i[0])))
3919 return D3D_OK;
3920 }
3921 memcpy(&state->ps_const_i[StartRegister][0],
3922 pConstantData,
3923 Vector4iCount * sizeof(state->ps_const_i[0]));
3924 } else {
3925 for (i = 0; i < Vector4iCount; i++) {
3926 state->ps_const_i[StartRegister+i][0] = fui((float)(pConstantData[4*i]));
3927 state->ps_const_i[StartRegister+i][1] = fui((float)(pConstantData[4*i+1]));
3928 state->ps_const_i[StartRegister+i][2] = fui((float)(pConstantData[4*i+2]));
3929 state->ps_const_i[StartRegister+i][3] = fui((float)(pConstantData[4*i+3]));
3930 }
3931 }
3932 state->changed.ps_const_i |= ((1 << Vector4iCount) - 1) << StartRegister;
3933 state->changed.group |= NINE_STATE_PS_CONST;
3934
3935 return D3D_OK;
3936 }
3937
3938 HRESULT NINE_WINAPI
3939 NineDevice9_GetPixelShaderConstantI( struct NineDevice9 *This,
3940 UINT StartRegister,
3941 int *pConstantData,
3942 UINT Vector4iCount )
3943 {
3944 const struct nine_state *state = &This->state;
3945 int i;
3946
3947 user_assert(StartRegister < NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3948 user_assert(StartRegister + Vector4iCount <= NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3949 user_assert(pConstantData, D3DERR_INVALIDCALL);
3950
3951 if (This->driver_caps.ps_integer) {
3952 memcpy(pConstantData,
3953 &state->ps_const_i[StartRegister][0],
3954 Vector4iCount * sizeof(state->ps_const_i[0]));
3955 } else {
3956 for (i = 0; i < Vector4iCount; i++) {
3957 pConstantData[4*i] = (int32_t) uif(state->ps_const_i[StartRegister+i][0]);
3958 pConstantData[4*i+1] = (int32_t) uif(state->ps_const_i[StartRegister+i][1]);
3959 pConstantData[4*i+2] = (int32_t) uif(state->ps_const_i[StartRegister+i][2]);
3960 pConstantData[4*i+3] = (int32_t) uif(state->ps_const_i[StartRegister+i][3]);
3961 }
3962 }
3963
3964 return D3D_OK;
3965 }
3966
3967 HRESULT NINE_WINAPI
3968 NineDevice9_SetPixelShaderConstantB( struct NineDevice9 *This,
3969 UINT StartRegister,
3970 const BOOL *pConstantData,
3971 UINT BoolCount )
3972 {
3973 struct nine_state *state = This->update;
3974 int i;
3975 uint32_t bool_true = This->driver_caps.ps_integer ? 0xFFFFFFFF : fui(1.0f);
3976
3977 DBG("This=%p StartRegister=%u pConstantData=%p BoolCount=%u\n",
3978 This, StartRegister, pConstantData, BoolCount);
3979
3980 user_assert(StartRegister < NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
3981 user_assert(StartRegister + BoolCount <= NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
3982 user_assert(pConstantData, D3DERR_INVALIDCALL);
3983
3984 if (!This->is_recording) {
3985 bool noChange = true;
3986 for (i = 0; i < BoolCount; i++) {
3987 if (!!state->ps_const_b[StartRegister + i] != !!pConstantData[i])
3988 noChange = false;
3989 }
3990 if (noChange)
3991 return D3D_OK;
3992 }
3993
3994 for (i = 0; i < BoolCount; i++)
3995 state->ps_const_b[StartRegister + i] = pConstantData[i] ? bool_true : 0;
3996
3997 state->changed.ps_const_b |= ((1 << BoolCount) - 1) << StartRegister;
3998 state->changed.group |= NINE_STATE_PS_CONST;
3999
4000 return D3D_OK;
4001 }
4002
4003 HRESULT NINE_WINAPI
4004 NineDevice9_GetPixelShaderConstantB( struct NineDevice9 *This,
4005 UINT StartRegister,
4006 BOOL *pConstantData,
4007 UINT BoolCount )
4008 {
4009 const struct nine_state *state = &This->state;
4010 int i;
4011
4012 user_assert(StartRegister < NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
4013 user_assert(StartRegister + BoolCount <= NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
4014 user_assert(pConstantData, D3DERR_INVALIDCALL);
4015
4016 for (i = 0; i < BoolCount; i++)
4017 pConstantData[i] = state->ps_const_b[StartRegister + i] ? TRUE : FALSE;
4018
4019 return D3D_OK;
4020 }
4021
4022 HRESULT NINE_WINAPI
4023 NineDevice9_DrawRectPatch( struct NineDevice9 *This,
4024 UINT Handle,
4025 const float *pNumSegs,
4026 const D3DRECTPATCH_INFO *pRectPatchInfo )
4027 {
4028 STUB(D3DERR_INVALIDCALL);
4029 }
4030
4031 HRESULT NINE_WINAPI
4032 NineDevice9_DrawTriPatch( struct NineDevice9 *This,
4033 UINT Handle,
4034 const float *pNumSegs,
4035 const D3DTRIPATCH_INFO *pTriPatchInfo )
4036 {
4037 STUB(D3DERR_INVALIDCALL);
4038 }
4039
4040 HRESULT NINE_WINAPI
4041 NineDevice9_DeletePatch( struct NineDevice9 *This,
4042 UINT Handle )
4043 {
4044 STUB(D3DERR_INVALIDCALL);
4045 }
4046
4047 HRESULT NINE_WINAPI
4048 NineDevice9_CreateQuery( struct NineDevice9 *This,
4049 D3DQUERYTYPE Type,
4050 IDirect3DQuery9 **ppQuery )
4051 {
4052 struct NineQuery9 *query;
4053 HRESULT hr;
4054
4055 DBG("This=%p Type=%d ppQuery=%p\n", This, Type, ppQuery);
4056
4057 hr = nine_is_query_supported(This->screen, Type);
4058 if (!ppQuery || hr != D3D_OK)
4059 return hr;
4060
4061 hr = NineQuery9_new(This, &query, Type);
4062 if (FAILED(hr))
4063 return hr;
4064 *ppQuery = (IDirect3DQuery9 *)query;
4065 return D3D_OK;
4066 }
4067
4068 IDirect3DDevice9Vtbl NineDevice9_vtable = {
4069 (void *)NineUnknown_QueryInterface,
4070 (void *)NineUnknown_AddRef,
4071 (void *)NineUnknown_Release,
4072 (void *)NineDevice9_TestCooperativeLevel,
4073 (void *)NineDevice9_GetAvailableTextureMem,
4074 (void *)NineDevice9_EvictManagedResources,
4075 (void *)NineDevice9_GetDirect3D,
4076 (void *)NineDevice9_GetDeviceCaps,
4077 (void *)NineDevice9_GetDisplayMode,
4078 (void *)NineDevice9_GetCreationParameters,
4079 (void *)NineDevice9_SetCursorProperties,
4080 (void *)NineDevice9_SetCursorPosition,
4081 (void *)NineDevice9_ShowCursor,
4082 (void *)NineDevice9_CreateAdditionalSwapChain,
4083 (void *)NineDevice9_GetSwapChain,
4084 (void *)NineDevice9_GetNumberOfSwapChains,
4085 (void *)NineDevice9_Reset,
4086 (void *)NineDevice9_Present,
4087 (void *)NineDevice9_GetBackBuffer,
4088 (void *)NineDevice9_GetRasterStatus,
4089 (void *)NineDevice9_SetDialogBoxMode,
4090 (void *)NineDevice9_SetGammaRamp,
4091 (void *)NineDevice9_GetGammaRamp,
4092 (void *)NineDevice9_CreateTexture,
4093 (void *)NineDevice9_CreateVolumeTexture,
4094 (void *)NineDevice9_CreateCubeTexture,
4095 (void *)NineDevice9_CreateVertexBuffer,
4096 (void *)NineDevice9_CreateIndexBuffer,
4097 (void *)NineDevice9_CreateRenderTarget,
4098 (void *)NineDevice9_CreateDepthStencilSurface,
4099 (void *)NineDevice9_UpdateSurface,
4100 (void *)NineDevice9_UpdateTexture,
4101 (void *)NineDevice9_GetRenderTargetData,
4102 (void *)NineDevice9_GetFrontBufferData,
4103 (void *)NineDevice9_StretchRect,
4104 (void *)NineDevice9_ColorFill,
4105 (void *)NineDevice9_CreateOffscreenPlainSurface,
4106 (void *)NineDevice9_SetRenderTarget,
4107 (void *)NineDevice9_GetRenderTarget,
4108 (void *)NineDevice9_SetDepthStencilSurface,
4109 (void *)NineDevice9_GetDepthStencilSurface,
4110 (void *)NineDevice9_BeginScene,
4111 (void *)NineDevice9_EndScene,
4112 (void *)NineDevice9_Clear,
4113 (void *)NineDevice9_SetTransform,
4114 (void *)NineDevice9_GetTransform,
4115 (void *)NineDevice9_MultiplyTransform,
4116 (void *)NineDevice9_SetViewport,
4117 (void *)NineDevice9_GetViewport,
4118 (void *)NineDevice9_SetMaterial,
4119 (void *)NineDevice9_GetMaterial,
4120 (void *)NineDevice9_SetLight,
4121 (void *)NineDevice9_GetLight,
4122 (void *)NineDevice9_LightEnable,
4123 (void *)NineDevice9_GetLightEnable,
4124 (void *)NineDevice9_SetClipPlane,
4125 (void *)NineDevice9_GetClipPlane,
4126 (void *)NineDevice9_SetRenderState,
4127 (void *)NineDevice9_GetRenderState,
4128 (void *)NineDevice9_CreateStateBlock,
4129 (void *)NineDevice9_BeginStateBlock,
4130 (void *)NineDevice9_EndStateBlock,
4131 (void *)NineDevice9_SetClipStatus,
4132 (void *)NineDevice9_GetClipStatus,
4133 (void *)NineDevice9_GetTexture,
4134 (void *)NineDevice9_SetTexture,
4135 (void *)NineDevice9_GetTextureStageState,
4136 (void *)NineDevice9_SetTextureStageState,
4137 (void *)NineDevice9_GetSamplerState,
4138 (void *)NineDevice9_SetSamplerState,
4139 (void *)NineDevice9_ValidateDevice,
4140 (void *)NineDevice9_SetPaletteEntries,
4141 (void *)NineDevice9_GetPaletteEntries,
4142 (void *)NineDevice9_SetCurrentTexturePalette,
4143 (void *)NineDevice9_GetCurrentTexturePalette,
4144 (void *)NineDevice9_SetScissorRect,
4145 (void *)NineDevice9_GetScissorRect,
4146 (void *)NineDevice9_SetSoftwareVertexProcessing,
4147 (void *)NineDevice9_GetSoftwareVertexProcessing,
4148 (void *)NineDevice9_SetNPatchMode,
4149 (void *)NineDevice9_GetNPatchMode,
4150 (void *)NineDevice9_DrawPrimitive,
4151 (void *)NineDevice9_DrawIndexedPrimitive,
4152 (void *)NineDevice9_DrawPrimitiveUP,
4153 (void *)NineDevice9_DrawIndexedPrimitiveUP,
4154 (void *)NineDevice9_ProcessVertices,
4155 (void *)NineDevice9_CreateVertexDeclaration,
4156 (void *)NineDevice9_SetVertexDeclaration,
4157 (void *)NineDevice9_GetVertexDeclaration,
4158 (void *)NineDevice9_SetFVF,
4159 (void *)NineDevice9_GetFVF,
4160 (void *)NineDevice9_CreateVertexShader,
4161 (void *)NineDevice9_SetVertexShader,
4162 (void *)NineDevice9_GetVertexShader,
4163 (void *)NineDevice9_SetVertexShaderConstantF,
4164 (void *)NineDevice9_GetVertexShaderConstantF,
4165 (void *)NineDevice9_SetVertexShaderConstantI,
4166 (void *)NineDevice9_GetVertexShaderConstantI,
4167 (void *)NineDevice9_SetVertexShaderConstantB,
4168 (void *)NineDevice9_GetVertexShaderConstantB,
4169 (void *)NineDevice9_SetStreamSource,
4170 (void *)NineDevice9_GetStreamSource,
4171 (void *)NineDevice9_SetStreamSourceFreq,
4172 (void *)NineDevice9_GetStreamSourceFreq,
4173 (void *)NineDevice9_SetIndices,
4174 (void *)NineDevice9_GetIndices,
4175 (void *)NineDevice9_CreatePixelShader,
4176 (void *)NineDevice9_SetPixelShader,
4177 (void *)NineDevice9_GetPixelShader,
4178 (void *)NineDevice9_SetPixelShaderConstantF,
4179 (void *)NineDevice9_GetPixelShaderConstantF,
4180 (void *)NineDevice9_SetPixelShaderConstantI,
4181 (void *)NineDevice9_GetPixelShaderConstantI,
4182 (void *)NineDevice9_SetPixelShaderConstantB,
4183 (void *)NineDevice9_GetPixelShaderConstantB,
4184 (void *)NineDevice9_DrawRectPatch,
4185 (void *)NineDevice9_DrawTriPatch,
4186 (void *)NineDevice9_DeletePatch,
4187 (void *)NineDevice9_CreateQuery
4188 };
4189
4190 static const GUID *NineDevice9_IIDs[] = {
4191 &IID_IDirect3DDevice9,
4192 &IID_IUnknown,
4193 NULL
4194 };
4195
4196 HRESULT
4197 NineDevice9_new( struct pipe_screen *pScreen,
4198 D3DDEVICE_CREATION_PARAMETERS *pCreationParameters,
4199 D3DCAPS9 *pCaps,
4200 D3DPRESENT_PARAMETERS *pPresentationParameters,
4201 IDirect3D9 *pD3D9,
4202 ID3DPresentGroup *pPresentationGroup,
4203 struct d3dadapter9_context *pCTX,
4204 boolean ex,
4205 D3DDISPLAYMODEEX *pFullscreenDisplayMode,
4206 struct NineDevice9 **ppOut,
4207 int minorVersionNum )
4208 {
4209 BOOL lock;
4210 lock = !!(pCreationParameters->BehaviorFlags & D3DCREATE_MULTITHREADED);
4211
4212 NINE_NEW(Device9, ppOut, lock, /* args */
4213 pScreen, pCreationParameters, pCaps,
4214 pPresentationParameters, pD3D9, pPresentationGroup, pCTX,
4215 ex, pFullscreenDisplayMode, minorVersionNum );
4216 }