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