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