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