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