st/nine: Back all shader constants to nine_context
[mesa.git] / src / gallium / state_trackers / nine / swapchain9.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 "swapchain9.h"
24 #include "surface9.h"
25 #include "device9.h"
26
27 #include "nine_helpers.h"
28 #include "nine_pipe.h"
29 #include "nine_dump.h"
30
31 #include "util/u_inlines.h"
32 #include "util/u_surface.h"
33 #include "hud/hud_context.h"
34 #include "state_tracker/drm_driver.h"
35
36 #include "threadpool.h"
37
38 #define DBG_CHANNEL DBG_SWAPCHAIN
39
40 #define UNTESTED(n) DBG("UNTESTED point %d. Please tell if it worked\n", n)
41
42 HRESULT
43 NineSwapChain9_ctor( struct NineSwapChain9 *This,
44 struct NineUnknownParams *pParams,
45 BOOL implicit,
46 ID3DPresent *pPresent,
47 D3DPRESENT_PARAMETERS *pPresentationParameters,
48 struct d3dadapter9_context *pCTX,
49 HWND hFocusWindow,
50 D3DDISPLAYMODEEX *mode )
51 {
52 HRESULT hr;
53
54 DBG("This=%p pDevice=%p pPresent=%p pCTX=%p hFocusWindow=%p\n",
55 This, pParams->device, pPresent, pCTX, hFocusWindow);
56
57 hr = NineUnknown_ctor(&This->base, pParams);
58 if (FAILED(hr))
59 return hr;
60
61 This->screen = NineDevice9_GetScreen(This->base.device);
62 This->pipe = NineDevice9_GetPipe(This->base.device);
63 This->cso = NineDevice9_GetCSO(This->base.device);
64 This->implicit = implicit;
65 This->actx = pCTX;
66 This->present = pPresent;
67 This->mode = NULL;
68
69 ID3DPresent_AddRef(pPresent);
70 if (!This->actx->thread_submit &&
71 This->base.device->minor_version_num > 2) {
72 D3DPRESENT_PARAMETERS2 params2;
73
74 memset(&params2, 0, sizeof(D3DPRESENT_PARAMETERS2));
75 params2.AllowDISCARDDelayedRelease = This->actx->discard_delayed_release;
76 params2.TearFreeDISCARD = This->actx->tearfree_discard;
77 ID3DPresent_SetPresentParameters2(pPresent, &params2);
78 }
79
80 if (!pPresentationParameters->hDeviceWindow)
81 pPresentationParameters->hDeviceWindow = hFocusWindow;
82
83 This->rendering_done = FALSE;
84 This->pool = NULL;
85 return NineSwapChain9_Resize(This, pPresentationParameters, mode);
86 }
87
88 static D3DWindowBuffer *
89 D3DWindowBuffer_create(struct NineSwapChain9 *This,
90 struct pipe_resource *resource,
91 int depth,
92 int for_frontbuffer_reading)
93 {
94 D3DWindowBuffer *ret;
95 struct winsys_handle whandle;
96 int stride, dmaBufFd;
97 HRESULT hr;
98
99 memset(&whandle, 0, sizeof(whandle));
100 whandle.type = DRM_API_HANDLE_TYPE_FD;
101 This->screen->resource_get_handle(This->screen, This->pipe, resource,
102 &whandle,
103 for_frontbuffer_reading ?
104 PIPE_HANDLE_USAGE_WRITE :
105 PIPE_HANDLE_USAGE_EXPLICIT_FLUSH |
106 PIPE_HANDLE_USAGE_READ);
107 stride = whandle.stride;
108 dmaBufFd = whandle.handle;
109 hr = ID3DPresent_NewD3DWindowBufferFromDmaBuf(This->present,
110 dmaBufFd,
111 resource->width0,
112 resource->height0,
113 stride,
114 depth,
115 32,
116 &ret);
117 assert (SUCCEEDED(hr));
118
119 if (FAILED(hr)) {
120 ERR("Failed to create new D3DWindowBufferFromDmaBuf\n");
121 return NULL;
122 }
123 return ret;
124 }
125
126 static int
127 NineSwapChain9_GetBackBufferCountForParams( struct NineSwapChain9 *This,
128 D3DPRESENT_PARAMETERS *pParams );
129
130 HRESULT
131 NineSwapChain9_Resize( struct NineSwapChain9 *This,
132 D3DPRESENT_PARAMETERS *pParams,
133 D3DDISPLAYMODEEX *mode )
134 {
135 struct NineDevice9 *pDevice = This->base.device;
136 D3DSURFACE_DESC desc;
137 HRESULT hr;
138 struct pipe_resource *resource, tmplt;
139 enum pipe_format pf;
140 BOOL has_present_buffers = FALSE;
141 int depth;
142 unsigned i, oldBufferCount, newBufferCount;
143 D3DMULTISAMPLE_TYPE multisample_type;
144
145 DBG("This=%p pParams=%p\n", This, pParams);
146 user_assert(pParams != NULL, E_POINTER);
147 user_assert(pParams->SwapEffect, D3DERR_INVALIDCALL);
148 user_assert((pParams->SwapEffect != D3DSWAPEFFECT_COPY) ||
149 (pParams->BackBufferCount <= 1), D3DERR_INVALIDCALL);
150 user_assert(pDevice->ex || pParams->BackBufferCount <=
151 D3DPRESENT_BACK_BUFFERS_MAX, D3DERR_INVALIDCALL);
152 user_assert(!pDevice->ex || pParams->BackBufferCount <=
153 D3DPRESENT_BACK_BUFFERS_MAX_EX, D3DERR_INVALIDCALL);
154 user_assert(pDevice->ex ||
155 (pParams->SwapEffect == D3DSWAPEFFECT_FLIP) ||
156 (pParams->SwapEffect == D3DSWAPEFFECT_COPY) ||
157 (pParams->SwapEffect == D3DSWAPEFFECT_DISCARD), D3DERR_INVALIDCALL);
158
159 DBG("pParams(%p):\n"
160 "BackBufferWidth: %u\n"
161 "BackBufferHeight: %u\n"
162 "BackBufferFormat: %s\n"
163 "BackBufferCount: %u\n"
164 "MultiSampleType: %u\n"
165 "MultiSampleQuality: %u\n"
166 "SwapEffect: %u\n"
167 "hDeviceWindow: %p\n"
168 "Windowed: %i\n"
169 "EnableAutoDepthStencil: %i\n"
170 "AutoDepthStencilFormat: %s\n"
171 "Flags: %s\n"
172 "FullScreen_RefreshRateInHz: %u\n"
173 "PresentationInterval: %x\n", pParams,
174 pParams->BackBufferWidth, pParams->BackBufferHeight,
175 d3dformat_to_string(pParams->BackBufferFormat),
176 pParams->BackBufferCount,
177 pParams->MultiSampleType, pParams->MultiSampleQuality,
178 pParams->SwapEffect, pParams->hDeviceWindow, pParams->Windowed,
179 pParams->EnableAutoDepthStencil,
180 d3dformat_to_string(pParams->AutoDepthStencilFormat),
181 nine_D3DPRESENTFLAG_to_str(pParams->Flags),
182 pParams->FullScreen_RefreshRateInHz,
183 pParams->PresentationInterval);
184
185 if (pParams->BackBufferCount == 0) {
186 pParams->BackBufferCount = 1;
187 }
188
189 if (pParams->BackBufferFormat == D3DFMT_UNKNOWN) {
190 pParams->BackBufferFormat = D3DFMT_A8R8G8B8;
191 }
192
193 This->desired_fences = This->actx->throttling ? This->actx->throttling_value + 1 : 0;
194 /* +1 because we add the fence of the current buffer before popping an old one */
195 if (This->desired_fences > DRI_SWAP_FENCES_MAX)
196 This->desired_fences = DRI_SWAP_FENCES_MAX;
197
198 if (This->actx->vblank_mode == 0)
199 pParams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
200 else if (This->actx->vblank_mode == 3)
201 pParams->PresentationInterval = D3DPRESENT_INTERVAL_ONE;
202
203 if (mode && This->mode) {
204 *(This->mode) = *mode;
205 } else if (mode) {
206 This->mode = malloc(sizeof(D3DDISPLAYMODEEX));
207 memcpy(This->mode, mode, sizeof(D3DDISPLAYMODEEX));
208 } else {
209 free(This->mode);
210 This->mode = NULL;
211 }
212
213 /* Note: It is the role of the backend to fill if necessary
214 * BackBufferWidth and BackBufferHeight */
215 hr = ID3DPresent_SetPresentParameters(This->present, pParams, This->mode);
216 if (hr != D3D_OK)
217 return hr;
218
219 oldBufferCount = This->num_back_buffers;
220 newBufferCount = NineSwapChain9_GetBackBufferCountForParams(This, pParams);
221
222 multisample_type = pParams->MultiSampleType;
223
224 /* Map MultiSampleQuality to MultiSampleType */
225 hr = d3dmultisample_type_check(This->screen, pParams->BackBufferFormat,
226 &multisample_type,
227 pParams->MultiSampleQuality,
228 NULL);
229 if (FAILED(hr)) {
230 return hr;
231 }
232
233 pf = d3d9_to_pipe_format_checked(This->screen, pParams->BackBufferFormat,
234 PIPE_TEXTURE_2D, multisample_type,
235 PIPE_BIND_RENDER_TARGET, FALSE, FALSE);
236
237 if (This->actx->linear_framebuffer ||
238 (pf != PIPE_FORMAT_B8G8R8X8_UNORM &&
239 pf != PIPE_FORMAT_B8G8R8A8_UNORM) ||
240 pParams->SwapEffect != D3DSWAPEFFECT_DISCARD ||
241 multisample_type >= 2 ||
242 (This->actx->ref && This->actx->ref == This->screen))
243 has_present_buffers = TRUE;
244
245 /* Note: the buffer depth has to match the window depth.
246 * In practice, ARGB buffers can be used with windows
247 * of depth 24. Windows of depth 32 are extremely rare.
248 * So even if the buffer is ARGB, say it is depth 24.
249 * It is common practice, for example that's how
250 * glamor implements depth 24.
251 * TODO: handle windows with other depths. Not possible in the short term.
252 * For example 16 bits.*/
253 depth = 24;
254
255 memset(&tmplt, 0, sizeof(tmplt));
256 tmplt.target = PIPE_TEXTURE_2D;
257 tmplt.width0 = pParams->BackBufferWidth;
258 tmplt.height0 = pParams->BackBufferHeight;
259 tmplt.depth0 = 1;
260 tmplt.last_level = 0;
261 tmplt.array_size = 1;
262 tmplt.usage = PIPE_USAGE_DEFAULT;
263 tmplt.flags = 0;
264
265 desc.Type = D3DRTYPE_SURFACE;
266 desc.Pool = D3DPOOL_DEFAULT;
267 desc.MultiSampleType = pParams->MultiSampleType;
268 desc.MultiSampleQuality = pParams->MultiSampleQuality;
269 desc.Width = pParams->BackBufferWidth;
270 desc.Height = pParams->BackBufferHeight;
271
272 for (i = 0; i < oldBufferCount; i++) {
273 if (This->tasks[i])
274 _mesa_threadpool_wait_for_task(This->pool, &(This->tasks[i]));
275 }
276 memset(This->tasks, 0, sizeof(This->tasks));
277
278 if (This->pool) {
279 _mesa_threadpool_destroy(This, This->pool);
280 This->pool = NULL;
281 }
282 This->enable_threadpool = This->actx->thread_submit && (pParams->SwapEffect != D3DSWAPEFFECT_COPY);
283 if (This->enable_threadpool)
284 This->pool = _mesa_threadpool_create(This);
285 if (!This->pool)
286 This->enable_threadpool = FALSE;
287
288 for (i = 0; i < oldBufferCount; i++) {
289 ID3DPresent_DestroyD3DWindowBuffer(This->present, This->present_handles[i]);
290 This->present_handles[i] = NULL;
291 if (This->present_buffers[i])
292 pipe_resource_reference(&(This->present_buffers[i]), NULL);
293 }
294
295 if (newBufferCount != oldBufferCount) {
296 for (i = newBufferCount; i < oldBufferCount;
297 ++i)
298 NineUnknown_Detach(NineUnknown(This->buffers[i]));
299
300 for (i = oldBufferCount; i < newBufferCount; ++i) {
301 This->buffers[i] = NULL;
302 This->present_handles[i] = NULL;
303 }
304 }
305 This->num_back_buffers = newBufferCount;
306
307 for (i = 0; i < newBufferCount; ++i) {
308 tmplt.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
309 tmplt.nr_samples = multisample_type;
310 if (!has_present_buffers)
311 tmplt.bind |= NINE_BIND_PRESENTBUFFER_FLAGS;
312 tmplt.format = d3d9_to_pipe_format_checked(This->screen,
313 pParams->BackBufferFormat,
314 PIPE_TEXTURE_2D,
315 tmplt.nr_samples,
316 tmplt.bind, FALSE, FALSE);
317 if (tmplt.format == PIPE_FORMAT_NONE)
318 return D3DERR_INVALIDCALL;
319 resource = This->screen->resource_create(This->screen, &tmplt);
320 if (!resource) {
321 DBG("Failed to create pipe_resource.\n");
322 return D3DERR_OUTOFVIDEOMEMORY;
323 }
324 if (pParams->Flags & D3DPRESENTFLAG_LOCKABLE_BACKBUFFER)
325 resource->flags |= NINE_RESOURCE_FLAG_LOCKABLE;
326 if (This->buffers[i]) {
327 NineSurface9_SetMultiSampleType(This->buffers[i], desc.MultiSampleType);
328 NineSurface9_SetResourceResize(This->buffers[i], resource);
329 if (has_present_buffers)
330 pipe_resource_reference(&resource, NULL);
331 } else {
332 desc.Format = pParams->BackBufferFormat;
333 desc.Usage = D3DUSAGE_RENDERTARGET;
334 hr = NineSurface9_new(pDevice, NineUnknown(This), resource, NULL, 0,
335 0, 0, &desc, &This->buffers[i]);
336 if (has_present_buffers)
337 pipe_resource_reference(&resource, NULL);
338 if (FAILED(hr)) {
339 DBG("Failed to create RT surface.\n");
340 return hr;
341 }
342 This->buffers[i]->base.base.forward = FALSE;
343 }
344 if (has_present_buffers) {
345 tmplt.format = PIPE_FORMAT_B8G8R8X8_UNORM;
346 tmplt.bind = NINE_BIND_PRESENTBUFFER_FLAGS;
347 tmplt.nr_samples = 0;
348 if (This->actx->linear_framebuffer)
349 tmplt.bind |= PIPE_BIND_LINEAR;
350 if (pParams->SwapEffect != D3DSWAPEFFECT_DISCARD)
351 tmplt.bind |= PIPE_BIND_RENDER_TARGET;
352 resource = This->screen->resource_create(This->screen, &tmplt);
353 pipe_resource_reference(&(This->present_buffers[i]), resource);
354 }
355 This->present_handles[i] = D3DWindowBuffer_create(This, resource, depth, false);
356 pipe_resource_reference(&resource, NULL);
357 if (!This->present_handles[i]) {
358 return D3DERR_DRIVERINTERNALERROR;
359 }
360 }
361 if (pParams->EnableAutoDepthStencil) {
362 tmplt.bind = d3d9_get_pipe_depth_format_bindings(pParams->AutoDepthStencilFormat);
363 tmplt.nr_samples = multisample_type;
364 tmplt.format = d3d9_to_pipe_format_checked(This->screen,
365 pParams->AutoDepthStencilFormat,
366 PIPE_TEXTURE_2D,
367 tmplt.nr_samples,
368 tmplt.bind,
369 FALSE, FALSE);
370
371 if (tmplt.format == PIPE_FORMAT_NONE)
372 return D3DERR_INVALIDCALL;
373
374 if (This->zsbuf) {
375 resource = This->screen->resource_create(This->screen, &tmplt);
376 if (!resource) {
377 DBG("Failed to create pipe_resource for depth buffer.\n");
378 return D3DERR_OUTOFVIDEOMEMORY;
379 }
380
381 NineSurface9_SetMultiSampleType(This->zsbuf, desc.MultiSampleType);
382 NineSurface9_SetResourceResize(This->zsbuf, resource);
383 pipe_resource_reference(&resource, NULL);
384 } else {
385 hr = NineDevice9_CreateDepthStencilSurface(pDevice,
386 pParams->BackBufferWidth,
387 pParams->BackBufferHeight,
388 pParams->AutoDepthStencilFormat,
389 pParams->MultiSampleType,
390 pParams->MultiSampleQuality,
391 0,
392 (IDirect3DSurface9 **)&This->zsbuf,
393 NULL);
394 if (FAILED(hr)) {
395 DBG("Failed to create ZS surface.\n");
396 return hr;
397 }
398 NineUnknown_ConvertRefToBind(NineUnknown(This->zsbuf));
399 }
400 }
401
402 This->params = *pParams;
403
404 return D3D_OK;
405 }
406
407 /* Throttling: code adapted from the dri state tracker */
408
409 /**
410 * swap_fences_pop_front - pull a fence from the throttle queue
411 *
412 * If the throttle queue is filled to the desired number of fences,
413 * pull fences off the queue until the number is less than the desired
414 * number of fences, and return the last fence pulled.
415 */
416 static struct pipe_fence_handle *
417 swap_fences_pop_front(struct NineSwapChain9 *This)
418 {
419 struct pipe_screen *screen = This->screen;
420 struct pipe_fence_handle *fence = NULL;
421
422 if (This->desired_fences == 0)
423 return NULL;
424
425 if (This->cur_fences >= This->desired_fences) {
426 screen->fence_reference(screen, &fence, This->swap_fences[This->tail]);
427 screen->fence_reference(screen, &This->swap_fences[This->tail++], NULL);
428 This->tail &= DRI_SWAP_FENCES_MASK;
429 --This->cur_fences;
430 }
431 return fence;
432 }
433
434
435 /**
436 * swap_fences_see_front - same than swap_fences_pop_front without
437 * pulling
438 *
439 */
440
441 static struct pipe_fence_handle *
442 swap_fences_see_front(struct NineSwapChain9 *This)
443 {
444 struct pipe_screen *screen = This->screen;
445 struct pipe_fence_handle *fence = NULL;
446
447 if (This->desired_fences == 0)
448 return NULL;
449
450 if (This->cur_fences >= This->desired_fences) {
451 screen->fence_reference(screen, &fence, This->swap_fences[This->tail]);
452 }
453 return fence;
454 }
455
456
457 /**
458 * swap_fences_push_back - push a fence onto the throttle queue at the back
459 *
460 * push a fence onto the throttle queue and pull fences of the queue
461 * so that the desired number of fences are on the queue.
462 */
463 static void
464 swap_fences_push_back(struct NineSwapChain9 *This,
465 struct pipe_fence_handle *fence)
466 {
467 struct pipe_screen *screen = This->screen;
468
469 if (!fence || This->desired_fences == 0)
470 return;
471
472 while(This->cur_fences == This->desired_fences)
473 swap_fences_pop_front(This);
474
475 This->cur_fences++;
476 screen->fence_reference(screen, &This->swap_fences[This->head++],
477 fence);
478 This->head &= DRI_SWAP_FENCES_MASK;
479 }
480
481
482 /**
483 * swap_fences_unref - empty the throttle queue
484 *
485 * pulls fences of the throttle queue until it is empty.
486 */
487 static void
488 swap_fences_unref(struct NineSwapChain9 *This)
489 {
490 struct pipe_screen *screen = This->screen;
491
492 while(This->cur_fences) {
493 screen->fence_reference(screen, &This->swap_fences[This->tail++], NULL);
494 This->tail &= DRI_SWAP_FENCES_MASK;
495 --This->cur_fences;
496 }
497 }
498
499 void
500 NineSwapChain9_dtor( struct NineSwapChain9 *This )
501 {
502 unsigned i;
503
504 DBG("This=%p\n", This);
505
506 if (This->pool)
507 _mesa_threadpool_destroy(This, This->pool);
508
509 for (i = 0; i < This->num_back_buffers; i++) {
510 if (This->buffers[i])
511 NineUnknown_Release(NineUnknown(This->buffers[i]));
512 if (This->present_handles[i])
513 ID3DPresent_DestroyD3DWindowBuffer(This->present, This->present_handles[i]);
514 if (This->present_buffers[i])
515 pipe_resource_reference(&(This->present_buffers[i]), NULL);
516 }
517 if (This->zsbuf)
518 NineUnknown_Unbind(NineUnknown(This->zsbuf));
519
520 if (This->present)
521 ID3DPresent_Release(This->present);
522
523 swap_fences_unref(This);
524 NineUnknown_dtor(&This->base);
525 }
526
527 static void
528 create_present_buffer( struct NineSwapChain9 *This,
529 unsigned int width, unsigned int height,
530 struct pipe_resource **resource,
531 D3DWindowBuffer **present_handle)
532 {
533 struct pipe_resource tmplt;
534
535 memset(&tmplt, 0, sizeof(tmplt));
536 tmplt.target = PIPE_TEXTURE_2D;
537 tmplt.width0 = width;
538 tmplt.height0 = height;
539 tmplt.depth0 = 1;
540 tmplt.last_level = 0;
541 tmplt.array_size = 1;
542 tmplt.usage = PIPE_USAGE_DEFAULT;
543 tmplt.flags = 0;
544 tmplt.format = PIPE_FORMAT_B8G8R8X8_UNORM;
545 tmplt.bind = NINE_BIND_BACKBUFFER_FLAGS |
546 NINE_BIND_PRESENTBUFFER_FLAGS;
547 tmplt.nr_samples = 0;
548 if (This->actx->linear_framebuffer)
549 tmplt.bind |= PIPE_BIND_LINEAR;
550 *resource = This->screen->resource_create(This->screen, &tmplt);
551
552 *present_handle = D3DWindowBuffer_create(This, *resource, 24, true);
553
554 if (!*present_handle) {
555 pipe_resource_reference(resource, NULL);
556 }
557 }
558
559 static void
560 handle_draw_cursor_and_hud( struct NineSwapChain9 *This, struct pipe_resource *resource)
561 {
562 struct NineDevice9 *device = This->base.device;
563 struct pipe_blit_info blit;
564
565 if (device->cursor.software && device->cursor.visible && device->cursor.w) {
566 memset(&blit, 0, sizeof(blit));
567 blit.src.resource = device->cursor.image;
568 blit.src.level = 0;
569 blit.src.format = device->cursor.image->format;
570 blit.src.box.x = 0;
571 blit.src.box.y = 0;
572 blit.src.box.z = 0;
573 blit.src.box.depth = 1;
574 blit.src.box.width = device->cursor.w;
575 blit.src.box.height = device->cursor.h;
576
577 blit.dst.resource = resource;
578 blit.dst.level = 0;
579 blit.dst.format = resource->format;
580 blit.dst.box.z = 0;
581 blit.dst.box.depth = 1;
582
583 blit.mask = PIPE_MASK_RGBA;
584 blit.filter = PIPE_TEX_FILTER_NEAREST;
585 blit.scissor_enable = FALSE;
586
587 /* NOTE: blit messes up when box.x + box.width < 0, fix driver
588 * NOTE2: device->cursor.pos contains coordinates relative to the screen.
589 * This happens to be also the position of the cursor when we are fullscreen.
590 * We don't use sw cursor for Windowed mode */
591 blit.dst.box.x = MAX2(device->cursor.pos.x, 0) - device->cursor.hotspot.x;
592 blit.dst.box.y = MAX2(device->cursor.pos.y, 0) - device->cursor.hotspot.y;
593 blit.dst.box.width = blit.src.box.width;
594 blit.dst.box.height = blit.src.box.height;
595
596 DBG("Blitting cursor(%ux%u) to (%i,%i).\n",
597 blit.src.box.width, blit.src.box.height,
598 blit.dst.box.x, blit.dst.box.y);
599
600 blit.alpha_blend = TRUE;
601 This->pipe->blit(This->pipe, &blit);
602 }
603
604 if (device->hud && resource) {
605 hud_draw(device->hud, resource); /* XXX: no offset */
606 /* HUD doesn't clobber stipple */
607 nine_state_restore_non_cso(device);
608 }
609 }
610
611 struct end_present_struct {
612 struct pipe_screen *screen;
613 struct pipe_fence_handle *fence_to_wait;
614 ID3DPresent *present;
615 D3DWindowBuffer *present_handle;
616 HWND hDestWindowOverride;
617 };
618
619 static void work_present(void *data)
620 {
621 struct end_present_struct *work = data;
622 if (work->fence_to_wait) {
623 (void) work->screen->fence_finish(work->screen, NULL, work->fence_to_wait, PIPE_TIMEOUT_INFINITE);
624 work->screen->fence_reference(work->screen, &(work->fence_to_wait), NULL);
625 }
626 ID3DPresent_PresentBuffer(work->present, work->present_handle, work->hDestWindowOverride, NULL, NULL, NULL, 0);
627 free(work);
628 }
629
630 static void pend_present(struct NineSwapChain9 *This,
631 HWND hDestWindowOverride)
632 {
633 struct end_present_struct *work = calloc(1, sizeof(struct end_present_struct));
634
635 work->screen = This->screen;
636 work->fence_to_wait = swap_fences_pop_front(This);
637 work->present = This->present;
638 work->present_handle = This->present_handles[0];
639 work->hDestWindowOverride = hDestWindowOverride;
640 This->tasks[0] = _mesa_threadpool_queue_task(This->pool, work_present, work);
641
642 return;
643 }
644
645 static inline HRESULT
646 present( struct NineSwapChain9 *This,
647 const RECT *pSourceRect,
648 const RECT *pDestRect,
649 HWND hDestWindowOverride,
650 const RGNDATA *pDirtyRegion,
651 DWORD dwFlags )
652 {
653 struct pipe_resource *resource;
654 struct pipe_fence_handle *fence;
655 HRESULT hr;
656 struct pipe_blit_info blit;
657
658 DBG("present: This=%p pSourceRect=%p pDestRect=%p "
659 "pDirtyRegion=%p hDestWindowOverride=%p"
660 "dwFlags=%d resource=%p\n",
661 This, pSourceRect, pDestRect, pDirtyRegion,
662 hDestWindowOverride, (int)dwFlags, This->buffers[0]->base.resource);
663
664 if (pSourceRect)
665 DBG("pSourceRect = (%u..%u)x(%u..%u)\n",
666 pSourceRect->left, pSourceRect->right,
667 pSourceRect->top, pSourceRect->bottom);
668 if (pDestRect)
669 DBG("pDestRect = (%u..%u)x(%u..%u)\n",
670 pDestRect->left, pDestRect->right,
671 pDestRect->top, pDestRect->bottom);
672
673 /* TODO: in the case the source and destination rect have different size:
674 * We need to allocate a new buffer, and do a blit to it to resize.
675 * We can't use the present_buffer for that since when we created it,
676 * we couldn't guess which size would have been needed.
677 * If pDestRect or pSourceRect is null, we have to check the sizes
678 * from the source size, and the destination window size.
679 * In this case, either resize rngdata, or pass NULL instead
680 */
681 /* Note: This->buffers[0]->level should always be 0 */
682
683 if (This->rendering_done)
684 goto bypass_rendering;
685
686 resource = This->buffers[0]->base.resource;
687
688 if (This->params.SwapEffect == D3DSWAPEFFECT_DISCARD)
689 handle_draw_cursor_and_hud(This, resource);
690
691 if (This->present_buffers[0]) {
692 memset(&blit, 0, sizeof(blit));
693 blit.src.resource = resource;
694 blit.src.level = 0;
695 blit.src.format = resource->format;
696 blit.src.box.z = 0;
697 blit.src.box.depth = 1;
698 blit.src.box.x = 0;
699 blit.src.box.y = 0;
700 blit.src.box.width = resource->width0;
701 blit.src.box.height = resource->height0;
702
703 resource = This->present_buffers[0];
704
705 blit.dst.resource = resource;
706 blit.dst.level = 0;
707 blit.dst.format = resource->format;
708 blit.dst.box.z = 0;
709 blit.dst.box.depth = 1;
710 blit.dst.box.x = 0;
711 blit.dst.box.y = 0;
712 blit.dst.box.width = resource->width0;
713 blit.dst.box.height = resource->height0;
714
715 blit.mask = PIPE_MASK_RGBA;
716 blit.filter = PIPE_TEX_FILTER_NEAREST;
717 blit.scissor_enable = FALSE;
718 blit.alpha_blend = FALSE;
719
720 This->pipe->blit(This->pipe, &blit);
721 }
722
723 /* The resource we present has to resolve fast clears
724 * if needed (and other things) */
725 This->pipe->flush_resource(This->pipe, resource);
726
727 if (This->params.SwapEffect != D3DSWAPEFFECT_DISCARD)
728 handle_draw_cursor_and_hud(This, resource);
729
730 fence = NULL;
731 This->pipe->flush(This->pipe, &fence, PIPE_FLUSH_END_OF_FRAME);
732 if (fence) {
733 swap_fences_push_back(This, fence);
734 This->screen->fence_reference(This->screen, &fence, NULL);
735 }
736
737 This->rendering_done = TRUE;
738 bypass_rendering:
739
740 if (dwFlags & D3DPRESENT_DONOTWAIT) {
741 UNTESTED(2);
742 BOOL still_draw = FALSE;
743 fence = swap_fences_see_front(This);
744 if (fence) {
745 still_draw = !This->screen->fence_finish(This->screen, NULL, fence, 0);
746 This->screen->fence_reference(This->screen, &fence, NULL);
747 }
748 if (still_draw)
749 return D3DERR_WASSTILLDRAWING;
750 }
751
752 if (!This->enable_threadpool) {
753 This->tasks[0]=NULL;
754 fence = swap_fences_pop_front(This);
755 if (fence) {
756 (void) This->screen->fence_finish(This->screen, NULL, fence, PIPE_TIMEOUT_INFINITE);
757 This->screen->fence_reference(This->screen, &fence, NULL);
758 }
759
760 hr = ID3DPresent_PresentBuffer(This->present, This->present_handles[0], hDestWindowOverride, pSourceRect, pDestRect, pDirtyRegion, dwFlags);
761
762 if (FAILED(hr)) { UNTESTED(3);return hr; }
763 } else {
764 pend_present(This, hDestWindowOverride);
765 }
766 This->rendering_done = FALSE;
767
768 return D3D_OK;
769 }
770
771 HRESULT NINE_WINAPI
772 NineSwapChain9_Present( struct NineSwapChain9 *This,
773 const RECT *pSourceRect,
774 const RECT *pDestRect,
775 HWND hDestWindowOverride,
776 const RGNDATA *pDirtyRegion,
777 DWORD dwFlags )
778 {
779 struct pipe_resource *res = NULL;
780 D3DWindowBuffer *handle_temp;
781 struct threadpool_task *task_temp;
782 int i;
783 HRESULT hr;
784
785 DBG("This=%p pSourceRect=%p pDestRect=%p hDestWindowOverride=%p "
786 "pDirtyRegion=%p dwFlags=%d\n",
787 This, pSourceRect, pDestRect, hDestWindowOverride,
788 pDirtyRegion,dwFlags);
789
790 if (This->base.device->ex) {
791 if (NineSwapChain9_GetOccluded(This)) {
792 DBG("Present is occluded. Returning S_PRESENT_OCCLUDED.\n");
793 return S_PRESENT_OCCLUDED;
794 }
795 } else {
796 if (NineSwapChain9_GetOccluded(This) ||
797 NineSwapChain9_ResolutionMismatch(This)) {
798 This->base.device->device_needs_reset = TRUE;
799 }
800 if (This->base.device->device_needs_reset) {
801 DBG("Device is lost. Returning D3DERR_DEVICELOST.\n");
802 return D3DERR_DEVICELOST;
803 }
804 }
805
806 hr = present(This, pSourceRect, pDestRect,
807 hDestWindowOverride, pDirtyRegion, dwFlags);
808 if (hr == D3DERR_WASSTILLDRAWING)
809 return hr;
810
811 if (This->base.device->minor_version_num > 2 &&
812 This->params.SwapEffect == D3DSWAPEFFECT_DISCARD &&
813 This->params.PresentationInterval == D3DPRESENT_INTERVAL_IMMEDIATE &&
814 !This->actx->thread_submit) {
815 int next_buffer = -1;
816
817 while (next_buffer == -1) {
818 /* Find a free backbuffer */
819 for (i = 1; i < This->num_back_buffers; i++) {
820 if (ID3DPresent_IsBufferReleased(This->present, This->present_handles[i])) {
821 DBG("Found buffer released: %d\n", i);
822 next_buffer = i;
823 break;
824 }
825 }
826 if (next_buffer == -1) {
827 DBG("Found no buffer released. Waiting for event\n");
828 ID3DPresent_WaitBufferReleaseEvent(This->present);
829 }
830 }
831 /* Switch with the released buffer */
832 pipe_resource_reference(&res, This->buffers[0]->base.resource);
833 NineSurface9_SetResourceResize(
834 This->buffers[0], This->buffers[next_buffer]->base.resource);
835 NineSurface9_SetResourceResize(
836 This->buffers[next_buffer], res);
837 pipe_resource_reference(&res, NULL);
838
839 if (This->present_buffers[0]) {
840 pipe_resource_reference(&res, This->present_buffers[0]);
841 pipe_resource_reference(&This->present_buffers[0], This->present_buffers[next_buffer]);
842 pipe_resource_reference(&This->present_buffers[next_buffer], res);
843 pipe_resource_reference(&res, NULL);
844 }
845
846 handle_temp = This->present_handles[0];
847 This->present_handles[0] = This->present_handles[next_buffer];
848 This->present_handles[next_buffer] = handle_temp;
849
850 /* Path not yet compatible with thread_submit */
851 assert(!This->tasks[0] && !This->tasks[next_buffer]);
852 } else {
853 switch (This->params.SwapEffect) {
854 case D3DSWAPEFFECT_OVERLAY: /* Not implemented, fallback to FLIP */
855 case D3DSWAPEFFECT_FLIPEX: /* Allows optimizations over FLIP for windowed mode. */
856 case D3DSWAPEFFECT_DISCARD: /* Allows optimizations over FLIP */
857 case D3DSWAPEFFECT_FLIP:
858 /* rotate the queue */
859 pipe_resource_reference(&res, This->buffers[0]->base.resource);
860 for (i = 1; i < This->num_back_buffers; i++) {
861 NineSurface9_SetResourceResize(This->buffers[i - 1],
862 This->buffers[i]->base.resource);
863 }
864 NineSurface9_SetResourceResize(
865 This->buffers[This->num_back_buffers - 1], res);
866 pipe_resource_reference(&res, NULL);
867
868 if (This->present_buffers[0]) {
869 pipe_resource_reference(&res, This->present_buffers[0]);
870 for (i = 1; i < This->num_back_buffers; i++)
871 pipe_resource_reference(&(This->present_buffers[i-1]), This->present_buffers[i]);
872 pipe_resource_reference(&(This->present_buffers[This->num_back_buffers - 1]), res);
873 pipe_resource_reference(&res, NULL);
874 }
875
876 handle_temp = This->present_handles[0];
877 for (i = 1; i < This->num_back_buffers; i++) {
878 This->present_handles[i-1] = This->present_handles[i];
879 }
880 This->present_handles[This->num_back_buffers - 1] = handle_temp;
881 task_temp = This->tasks[0];
882 for (i = 1; i < This->num_back_buffers; i++) {
883 This->tasks[i-1] = This->tasks[i];
884 }
885 This->tasks[This->num_back_buffers - 1] = task_temp;
886 break;
887
888 case D3DSWAPEFFECT_COPY:
889 /* do nothing */
890 break;
891 }
892
893 if (This->tasks[0])
894 _mesa_threadpool_wait_for_task(This->pool, &(This->tasks[0]));
895
896 ID3DPresent_WaitBufferReleased(This->present, This->present_handles[0]);
897 }
898
899 This->base.device->state.changed.group |= NINE_STATE_FB;
900
901 return hr;
902 }
903
904 HRESULT NINE_WINAPI
905 NineSwapChain9_GetFrontBufferData( struct NineSwapChain9 *This,
906 IDirect3DSurface9 *pDestSurface )
907 {
908 struct NineSurface9 *dest_surface = NineSurface9(pDestSurface);
909 struct NineDevice9 *pDevice = This->base.device;
910 unsigned int width, height;
911 struct pipe_resource *temp_resource;
912 struct NineSurface9 *temp_surface;
913 D3DWindowBuffer *temp_handle;
914 D3DSURFACE_DESC desc;
915 HRESULT hr;
916
917 DBG("GetFrontBufferData: This=%p pDestSurface=%p\n",
918 This, pDestSurface);
919
920 user_assert(dest_surface->base.pool == D3DPOOL_SYSTEMMEM, D3DERR_INVALIDCALL);
921
922 width = dest_surface->desc.Width;
923 height = dest_surface->desc.Height;
924
925 /* Note: front window size and destination size are supposed
926 * to match. However it's not very clear what should get taken in Windowed
927 * mode. It may need a fix */
928 create_present_buffer(This, width, height, &temp_resource, &temp_handle);
929
930 if (!temp_resource || !temp_handle) {
931 return D3DERR_INVALIDCALL;
932 }
933
934 desc.Type = D3DRTYPE_SURFACE;
935 desc.Pool = D3DPOOL_DEFAULT;
936 desc.MultiSampleType = D3DMULTISAMPLE_NONE;
937 desc.MultiSampleQuality = 0;
938 desc.Width = width;
939 desc.Height = height;
940 /* NineSurface9_CopyDefaultToMem needs same format. */
941 desc.Format = dest_surface->desc.Format;
942 desc.Usage = D3DUSAGE_RENDERTARGET;
943 hr = NineSurface9_new(pDevice, NineUnknown(This), temp_resource, NULL, 0,
944 0, 0, &desc, &temp_surface);
945 pipe_resource_reference(&temp_resource, NULL);
946 if (FAILED(hr)) {
947 DBG("Failed to create temp FrontBuffer surface.\n");
948 return hr;
949 }
950
951 ID3DPresent_FrontBufferCopy(This->present, temp_handle);
952
953 NineSurface9_CopyDefaultToMem(dest_surface, temp_surface);
954
955 ID3DPresent_DestroyD3DWindowBuffer(This->present, temp_handle);
956 NineUnknown_Destroy(NineUnknown(temp_surface));
957
958 return D3D_OK;
959 }
960
961 HRESULT NINE_WINAPI
962 NineSwapChain9_GetBackBuffer( struct NineSwapChain9 *This,
963 UINT iBackBuffer,
964 D3DBACKBUFFER_TYPE Type,
965 IDirect3DSurface9 **ppBackBuffer )
966 {
967 DBG("GetBackBuffer: This=%p iBackBuffer=%d Type=%d ppBackBuffer=%p\n",
968 This, iBackBuffer, Type, ppBackBuffer);
969 (void)user_error(Type == D3DBACKBUFFER_TYPE_MONO);
970 /* don't touch ppBackBuffer on error */
971 user_assert(ppBackBuffer != NULL, D3DERR_INVALIDCALL);
972 user_assert(iBackBuffer < This->params.BackBufferCount, D3DERR_INVALIDCALL);
973
974 NineUnknown_AddRef(NineUnknown(This->buffers[iBackBuffer]));
975 *ppBackBuffer = (IDirect3DSurface9 *)This->buffers[iBackBuffer];
976 return D3D_OK;
977 }
978
979 HRESULT NINE_WINAPI
980 NineSwapChain9_GetRasterStatus( struct NineSwapChain9 *This,
981 D3DRASTER_STATUS *pRasterStatus )
982 {
983 DBG("GetRasterStatus: This=%p pRasterStatus=%p\n",
984 This, pRasterStatus);
985 user_assert(pRasterStatus != NULL, E_POINTER);
986 return ID3DPresent_GetRasterStatus(This->present, pRasterStatus);
987 }
988
989 HRESULT NINE_WINAPI
990 NineSwapChain9_GetDisplayMode( struct NineSwapChain9 *This,
991 D3DDISPLAYMODE *pMode )
992 {
993 D3DDISPLAYMODEEX mode;
994 D3DDISPLAYROTATION rot;
995 HRESULT hr;
996
997 DBG("GetDisplayMode: This=%p pMode=%p\n",
998 This, pMode);
999 user_assert(pMode != NULL, E_POINTER);
1000
1001 hr = ID3DPresent_GetDisplayMode(This->present, &mode, &rot);
1002 if (SUCCEEDED(hr)) {
1003 pMode->Width = mode.Width;
1004 pMode->Height = mode.Height;
1005 pMode->RefreshRate = mode.RefreshRate;
1006 pMode->Format = mode.Format;
1007 }
1008 return hr;
1009 }
1010
1011 HRESULT NINE_WINAPI
1012 NineSwapChain9_GetPresentParameters( struct NineSwapChain9 *This,
1013 D3DPRESENT_PARAMETERS *pPresentationParameters )
1014 {
1015 DBG("GetPresentParameters: This=%p pPresentationParameters=%p\n",
1016 This, pPresentationParameters);
1017 user_assert(pPresentationParameters != NULL, E_POINTER);
1018 *pPresentationParameters = This->params;
1019 return D3D_OK;
1020 }
1021
1022 IDirect3DSwapChain9Vtbl NineSwapChain9_vtable = {
1023 (void *)NineUnknown_QueryInterface,
1024 (void *)NineUnknown_AddRef,
1025 (void *)NineUnknown_Release,
1026 (void *)NineSwapChain9_Present,
1027 (void *)NineSwapChain9_GetFrontBufferData,
1028 (void *)NineSwapChain9_GetBackBuffer,
1029 (void *)NineSwapChain9_GetRasterStatus,
1030 (void *)NineSwapChain9_GetDisplayMode,
1031 (void *)NineUnknown_GetDevice, /* actually part of SwapChain9 iface */
1032 (void *)NineSwapChain9_GetPresentParameters
1033 };
1034
1035 static const GUID *NineSwapChain9_IIDs[] = {
1036 &IID_IDirect3DSwapChain9,
1037 &IID_IUnknown,
1038 NULL
1039 };
1040
1041 HRESULT
1042 NineSwapChain9_new( struct NineDevice9 *pDevice,
1043 BOOL implicit,
1044 ID3DPresent *pPresent,
1045 D3DPRESENT_PARAMETERS *pPresentationParameters,
1046 struct d3dadapter9_context *pCTX,
1047 HWND hFocusWindow,
1048 struct NineSwapChain9 **ppOut )
1049 {
1050 NINE_DEVICE_CHILD_NEW(SwapChain9, ppOut, pDevice, /* args */
1051 implicit, pPresent, pPresentationParameters,
1052 pCTX, hFocusWindow, NULL);
1053 }
1054
1055 BOOL
1056 NineSwapChain9_GetOccluded( struct NineSwapChain9 *This )
1057 {
1058 if (This->base.device->minor_version_num > 0) {
1059 return ID3DPresent_GetWindowOccluded(This->present);
1060 }
1061
1062 return FALSE;
1063 }
1064
1065 BOOL
1066 NineSwapChain9_ResolutionMismatch( struct NineSwapChain9 *This )
1067 {
1068 if (This->base.device->minor_version_num > 1) {
1069 return ID3DPresent_ResolutionMismatch(This->present);
1070 }
1071
1072 return FALSE;
1073 }
1074
1075 HANDLE
1076 NineSwapChain9_CreateThread( struct NineSwapChain9 *This,
1077 void *pFuncAddress,
1078 void *pParam )
1079 {
1080 if (This->base.device->minor_version_num > 1) {
1081 return ID3DPresent_CreateThread(This->present, pFuncAddress, pParam);
1082 }
1083
1084 return NULL;
1085 }
1086
1087 void
1088 NineSwapChain9_WaitForThread( struct NineSwapChain9 *This,
1089 HANDLE thread )
1090 {
1091 if (This->base.device->minor_version_num > 1) {
1092 (void) ID3DPresent_WaitForThread(This->present, thread);
1093 }
1094 }
1095
1096 static int
1097 NineSwapChain9_GetBackBufferCountForParams( struct NineSwapChain9 *This,
1098 D3DPRESENT_PARAMETERS *pParams )
1099 {
1100 int count = pParams->BackBufferCount;
1101
1102 /* When we have flip behaviour, d3d9 expects we get back the screen buffer when we flip.
1103 * Here we don't get back the initial content of the screen. To emulate the behaviour
1104 * we allocate an additional buffer */
1105 if (pParams->SwapEffect != D3DSWAPEFFECT_COPY)
1106 count++;
1107 /* With DISCARD, as there is no guarantee about the buffer contents, we can use
1108 * an arbitrary number of buffers */
1109 if (pParams->SwapEffect == D3DSWAPEFFECT_DISCARD) {
1110 /* thread_submit has a throttling equivalent to the throttling
1111 * with throttling_value set to count-1. Most drivers use
1112 * 2 for throttling_value. For performance use count of at least 3
1113 * for thread_submit. */
1114 if (This->actx->thread_submit && count < 3)
1115 count = 3;
1116 /* When we enable AllowDISCARDDelayedRelease, we must ensure
1117 * to have at least 4 buffers to meet INTERVAL_IMMEDIATE,
1118 * since the display server/compositor can hold 3 buffers
1119 * without releasing them:
1120 * . Buffer on screen.
1121 * . Buffer scheduled kernel side to be next on screen.
1122 * . Last buffer sent.
1123 * For some reasons, 5 buffers are actually needed, because in
1124 * case a pageflip is missed because rendering wasn't finished,
1125 * the Xserver will hold 4 buffers. */
1126 if (!This->actx->thread_submit &&
1127 This->base.device->minor_version_num > 2 &&
1128 pParams->PresentationInterval == D3DPRESENT_INTERVAL_IMMEDIATE &&
1129 count < 5)
1130 count = 5;
1131 }
1132
1133 return count;
1134 }