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