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