VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/buffer.c@ 22652

Last change on this file since 22652 was 22496, checked in by vboxsync, 15 years ago

crOpenGL: update wine to 1.1.27 and better fix for depthstencil surface refcounting

  • Property svn:eol-style set to native
File size: 38.6 KB
Line 
1/*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2007 Stefan Dösinger for CodeWeavers
7 * Copyright 2009 Henri Verbeet for CodeWeavers
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 *
23 */
24
25/*
26 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
27 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
28 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
29 * a choice of LGPL license versions is made available with the language indicating
30 * that LGPLv2 or any later version may be used, or where a choice of which version
31 * of the LGPL is applied is otherwise unspecified.
32 */
33
34#include "config.h"
35#include "wine/port.h"
36
37#include "wined3d_private.h"
38
39WINE_DEFAULT_DEBUG_CHANNEL(d3d);
40
41#define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
42
43#define VB_MAXDECLCHANGES 100 /* After that number we stop converting */
44#define VB_RESETDECLCHANGE 1000 /* Reset the changecount after that number of draws */
45
46/* Context activation is done by the caller. */
47static void buffer_create_buffer_object(struct wined3d_buffer *This)
48{
49 GLenum error, gl_usage;
50
51 TRACE("Creating an OpenGL vertex buffer object for IWineD3DVertexBuffer %p Usage(%s)\n",
52 This, debug_d3dusage(This->resource.usage));
53
54 ENTER_GL();
55
56 /* Make sure that the gl error is cleared. Do not use checkGLcall
57 * here because checkGLcall just prints a fixme and continues. However,
58 * if an error during VBO creation occurs we can fall back to non-vbo operation
59 * with full functionality(but performance loss)
60 */
61 while (glGetError() != GL_NO_ERROR);
62
63 /* Basically the FVF parameter passed to CreateVertexBuffer is no good
64 * It is the FVF set with IWineD3DDevice::SetFVF or the Vertex Declaration set with
65 * IWineD3DDevice::SetVertexDeclaration that decides how the vertices in the buffer
66 * look like. This means that on each DrawPrimitive call the vertex buffer has to be verified
67 * to check if the rhw and color values are in the correct format.
68 */
69
70 GL_EXTCALL(glGenBuffersARB(1, &This->buffer_object));
71 error = glGetError();
72 if (!This->buffer_object || error != GL_NO_ERROR)
73 {
74 ERR("Failed to create a VBO with error %s (%#x)\n", debug_glerror(error), error);
75 goto fail;
76 }
77
78 if(This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
79 {
80 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_INDEXBUFFER);
81 }
82 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
83 error = glGetError();
84 if (error != GL_NO_ERROR)
85 {
86 ERR("Failed to bind the VBO with error %s (%#x)\n", debug_glerror(error), error);
87 goto fail;
88 }
89
90 /* Don't use static, because dx apps tend to update the buffer
91 * quite often even if they specify 0 usage. Because we always keep the local copy
92 * we never read from the vbo and can create a write only opengl buffer.
93 */
94 switch(This->resource.usage & (WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_DYNAMIC))
95 {
96 case WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_DYNAMIC:
97 case WINED3DUSAGE_DYNAMIC:
98 TRACE("Gl usage = GL_STREAM_DRAW\n");
99 gl_usage = GL_STREAM_DRAW_ARB;
100 break;
101
102 case WINED3DUSAGE_WRITEONLY:
103 default:
104 TRACE("Gl usage = GL_DYNAMIC_DRAW\n");
105 gl_usage = GL_DYNAMIC_DRAW_ARB;
106 break;
107 }
108
109 /* Reserve memory for the buffer. The amount of data won't change
110 * so we are safe with calling glBufferData once and
111 * calling glBufferSubData on updates. Upload the actual data in case
112 * we're not double buffering, so we can release the heap mem afterwards
113 */
114 GL_EXTCALL(glBufferDataARB(This->buffer_type_hint, This->resource.size, This->resource.allocatedMemory, gl_usage));
115 error = glGetError();
116 if (error != GL_NO_ERROR)
117 {
118 ERR("glBufferDataARB failed with error %s (%#x)\n", debug_glerror(error), error);
119 goto fail;
120 }
121
122 LEAVE_GL();
123
124 This->buffer_object_size = This->resource.size;
125 This->buffer_object_usage = gl_usage;
126 This->dirty_start = 0;
127 This->dirty_end = This->resource.size;
128
129 if(This->flags & WINED3D_BUFFER_DOUBLEBUFFER)
130 {
131 This->flags |= WINED3D_BUFFER_DIRTY;
132 }
133 else
134 {
135 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
136 This->resource.allocatedMemory = NULL;
137 This->resource.heapMemory = NULL;
138 This->flags &= ~WINED3D_BUFFER_DIRTY;
139 }
140
141 return;
142
143fail:
144 /* Clean up all vbo init, but continue because we can work without a vbo :-) */
145 ERR("Failed to create a vertex buffer object. Continuing, but performance issues may occur\n");
146 if (This->buffer_object) GL_EXTCALL(glDeleteBuffersARB(1, &This->buffer_object));
147 This->buffer_object = 0;
148 LEAVE_GL();
149
150 return;
151}
152
153static BOOL buffer_process_converted_attribute(struct wined3d_buffer *This,
154 const enum wined3d_buffer_conversion_type conversion_type,
155 const struct wined3d_stream_info_element *attrib, DWORD *stride_this_run)
156{
157 DWORD attrib_size;
158 BOOL ret = FALSE;
159 unsigned int i;
160 DWORD offset = This->resource.wineD3DDevice->stateBlock->streamOffset[attrib->stream_idx];
161 DWORD_PTR data;
162
163 /* Check for some valid situations which cause us pain. One is if the buffer is used for
164 * constant attributes(stride = 0), the other one is if the buffer is used on two streams
165 * with different strides. In the 2nd case we might have to drop conversion entirely,
166 * it is possible that the same bytes are once read as FLOAT2 and once as UBYTE4N.
167 */
168 if (!attrib->stride)
169 {
170 FIXME("%s used with stride 0, let's hope we get the vertex stride from somewhere else\n",
171 debug_d3dformat(attrib->format_desc->format));
172 }
173 else if(attrib->stride != *stride_this_run && *stride_this_run)
174 {
175 FIXME("Got two concurrent strides, %d and %d\n", attrib->stride, *stride_this_run);
176 }
177 else
178 {
179 *stride_this_run = attrib->stride;
180 if (This->stride != *stride_this_run)
181 {
182 /* We rely that this happens only on the first converted attribute that is found,
183 * if at all. See above check
184 */
185 TRACE("Reconverting because converted attributes occur, and the stride changed\n");
186 This->stride = *stride_this_run;
187 HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, This->conversion_map);
188 This->conversion_map = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
189 sizeof(*This->conversion_map) * This->stride);
190 ret = TRUE;
191 }
192 }
193
194 data = (((DWORD_PTR)attrib->data) + offset) % This->stride;
195 attrib_size = attrib->format_desc->component_count * attrib->format_desc->component_size;
196 for (i = 0; i < attrib_size; ++i)
197 {
198 if (This->conversion_map[data + i] != conversion_type)
199 {
200 TRACE("Byte %ld in vertex changed\n", i + data);
201 TRACE("It was type %d, is %d now\n", This->conversion_map[data + i], conversion_type);
202 ret = TRUE;
203 This->conversion_map[data + i] = conversion_type;
204 }
205 }
206
207 return ret;
208}
209
210static BOOL buffer_check_attribute(struct wined3d_buffer *This,
211 const struct wined3d_stream_info_element *attrib, const BOOL check_d3dcolor, const BOOL is_ffp_position,
212 const BOOL is_ffp_color, DWORD *stride_this_run, BOOL *float16_used)
213{
214 BOOL ret = FALSE;
215 WINED3DFORMAT format;
216
217 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
218 * there, on nonexistent attribs the vbo is 0.
219 */
220 if (attrib->buffer_object != This->buffer_object) return FALSE;
221
222 format = attrib->format_desc->format;
223 /* Look for newly appeared conversion */
224 if (!GL_SUPPORT(ARB_HALF_FLOAT_VERTEX) && (format == WINED3DFMT_R16G16_FLOAT || format == WINED3DFMT_R16G16B16A16_FLOAT))
225 {
226 ret = buffer_process_converted_attribute(This, CONV_FLOAT16_2, attrib, stride_this_run);
227
228 if (is_ffp_position) FIXME("Test FLOAT16 fixed function processing positions\n");
229 else if (is_ffp_color) FIXME("test FLOAT16 fixed function processing colors\n");
230 *float16_used = TRUE;
231 }
232 else if (check_d3dcolor && format == WINED3DFMT_A8R8G8B8)
233 {
234 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
235
236 if (!is_ffp_color) FIXME("Test for non-color fixed function WINED3DFMT_A8R8G8B8 format\n");
237 }
238 else if (is_ffp_position && format == WINED3DFMT_R32G32B32A32_FLOAT)
239 {
240 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
241 }
242 else if (This->conversion_map)
243 {
244 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
245 }
246
247 return ret;
248}
249
250static UINT *find_conversion_shift(struct wined3d_buffer *This,
251 const struct wined3d_stream_info *strided, UINT stride)
252{
253 UINT *ret, i, j, shift, orig_type_size;
254
255 if (!stride)
256 {
257 TRACE("No shift\n");
258 return NULL;
259 }
260
261 This->conversion_stride = stride;
262 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DWORD) * stride);
263 for (i = 0; i < MAX_ATTRIBS; ++i)
264 {
265 WINED3DFORMAT format;
266
267 if (strided->elements[i].buffer_object != This->buffer_object) continue;
268
269 format = strided->elements[i].format_desc->format;
270 if (format == WINED3DFMT_R16G16_FLOAT)
271 {
272 shift = 4;
273 }
274 else if (format == WINED3DFMT_R16G16B16A16_FLOAT)
275 {
276 shift = 8;
277 /* Pre-shift the last 4 bytes in the FLOAT16_4 by 4 bytes - this makes FLOAT16_2 and FLOAT16_4 conversions
278 * compatible
279 */
280 for (j = 4; j < 8; ++j)
281 {
282 ret[(DWORD_PTR)strided->elements[i].data + j] += 4;
283 }
284 }
285 else
286 {
287 shift = 0;
288 }
289 This->conversion_stride += shift;
290
291 if (shift)
292 {
293 orig_type_size = strided->elements[i].format_desc->component_count
294 * strided->elements[i].format_desc->component_size;
295 for (j = (DWORD_PTR)strided->elements[i].data + orig_type_size; j < stride; ++j)
296 {
297 ret[j] += shift;
298 }
299 }
300 }
301
302 if (TRACE_ON(d3d))
303 {
304 TRACE("Dumping conversion shift:\n");
305 for (i = 0; i < stride; ++i)
306 {
307 TRACE("[%d]", ret[i]);
308 }
309 TRACE("\n");
310 }
311
312 return ret;
313}
314
315static BOOL buffer_find_decl(struct wined3d_buffer *This)
316{
317 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
318 UINT stride_this_run = 0;
319 BOOL float16_used = FALSE;
320 BOOL ret = FALSE;
321 unsigned int i;
322
323 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
324 * Once we have our declaration there is no need to look it up again. Index buffers also never need
325 * conversion, so once the (empty) conversion structure is created don't bother checking again
326 */
327 if (This->flags & WINED3D_BUFFER_HASDESC)
328 {
329 if(((IWineD3DImpl *)device->wineD3D)->dxVersion == 7 ||
330 This->resource.format_desc->format != WINED3DFMT_VERTEXDATA) return FALSE;
331 }
332
333 TRACE("Finding vertex buffer conversion information\n");
334 /* Certain declaration types need some fixups before we can pass them to
335 * opengl. This means D3DCOLOR attributes with fixed function vertex
336 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
337 * GL_ARB_half_float_vertex is not supported.
338 *
339 * Note for d3d8 and d3d9:
340 * The vertex buffer FVF doesn't help with finding them, we have to use
341 * the decoded vertex declaration and pick the things that concern the
342 * current buffer. A problem with this is that this can change between
343 * draws, so we have to validate the information and reprocess the buffer
344 * if it changes, and avoid false positives for performance reasons.
345 * WineD3D doesn't even know the vertex buffer any more, it is managed
346 * by the client libraries and passed to SetStreamSource and ProcessVertices
347 * as needed.
348 *
349 * We have to distinguish between vertex shaders and fixed function to
350 * pick the way we access the strided vertex information.
351 *
352 * This code sets up a per-byte array with the size of the detected
353 * stride of the arrays in the buffer. For each byte we have a field
354 * that marks the conversion needed on this byte. For example, the
355 * following declaration with fixed function vertex processing:
356 *
357 * POSITIONT, FLOAT4
358 * NORMAL, FLOAT3
359 * DIFFUSE, FLOAT16_4
360 * SPECULAR, D3DCOLOR
361 *
362 * Will result in
363 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
364 * [P][P][P][P][P][P][P][P][P][P][P][P][P][P][P][P][0][0][0][0][0][0][0][0][0][0][0][0][F][F][F][F][F][F][F][F][C][C][C][C]
365 *
366 * Where in this example map P means 4 component position conversion, 0
367 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
368 * conversion (red / blue swizzle).
369 *
370 * If we're doing conversion and the stride changes we have to reconvert
371 * the whole buffer. Note that we do not mind if the semantic changes,
372 * we only care for the conversion type. So if the NORMAL is replaced
373 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
374 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
375 * conversion types depend on the semantic as well, for example a FLOAT4
376 * texcoord needs no conversion while a FLOAT4 positiont needs one
377 */
378 if (use_vs(device->stateBlock))
379 {
380 TRACE("vshader\n");
381 /* If the current vertex declaration is marked for no half float conversion don't bother to
382 * analyse the strided streams in depth, just set them up for no conversion. Return decl changed
383 * if we used conversion before
384 */
385 if (!((IWineD3DVertexDeclarationImpl *) device->stateBlock->vertexDecl)->half_float_conv_needed)
386 {
387 if (This->conversion_map)
388 {
389 TRACE("Now using shaders without conversion, but conversion used before\n");
390 HeapFree(GetProcessHeap(), 0, This->conversion_map);
391 HeapFree(GetProcessHeap(), 0, This->conversion_shift);
392 This->conversion_map = NULL;
393 This->stride = 0;
394 This->conversion_shift = NULL;
395 This->conversion_stride = 0;
396 return TRUE;
397 }
398 else
399 {
400 return FALSE;
401 }
402 }
403 for (i = 0; i < MAX_ATTRIBS; ++i)
404 {
405 ret = buffer_check_attribute(This, &device->strided_streams.elements[i],
406 FALSE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
407 }
408
409 /* Recalculate the conversion shift map if the declaration has changed,
410 * and we're using float16 conversion or used it on the last run
411 */
412 if (ret && (float16_used || This->conversion_map))
413 {
414 HeapFree(GetProcessHeap(), 0, This->conversion_shift);
415 This->conversion_shift = find_conversion_shift(This, &device->strided_streams, This->stride);
416 }
417 }
418 else
419 {
420 /* Fixed function is a bit trickier. We have to take care for D3DCOLOR types, FLOAT4 positions and of course
421 * FLOAT16s if not supported. Also, we can't iterate over the array, so use macros to generate code for all
422 * the attributes that our current fixed function pipeline implementation cares for.
423 */
424 BOOL support_d3dcolor = GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA);
425 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_POSITION],
426 TRUE, TRUE, FALSE, &stride_this_run, &float16_used) || ret;
427 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_NORMAL],
428 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
429 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_DIFFUSE],
430 !support_d3dcolor, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
431 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_SPECULAR],
432 !support_d3dcolor, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
433 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD0],
434 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
435 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD1],
436 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
437 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD2],
438 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
439 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD3],
440 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
441 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD4],
442 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
443 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD5],
444 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
445 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD6],
446 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
447 ret = buffer_check_attribute(This, &device->strided_streams.elements[WINED3D_FFP_TEXCOORD7],
448 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
449
450 if (float16_used) FIXME("Float16 conversion used with fixed function vertex processing\n");
451 }
452
453 if (stride_this_run == 0 && This->conversion_map)
454 {
455 /* Sanity test */
456 if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
457 HeapFree(GetProcessHeap(), 0, This->conversion_map);
458 This->conversion_map = NULL;
459 This->stride = 0;
460 }
461
462 if (ret) TRACE("Conversion information changed\n");
463
464 return ret;
465}
466
467/* Context activation is done by the caller. */
468static void buffer_check_buffer_object_size(struct wined3d_buffer *This)
469{
470 UINT size = This->conversion_stride ?
471 This->conversion_stride * (This->resource.size / This->stride) : This->resource.size;
472 if (This->buffer_object_size != size)
473 {
474 TRACE("Old size %u, creating new size %u\n", This->buffer_object_size, size);
475
476 if(This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
477 {
478 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_INDEXBUFFER);
479 }
480
481 /* Rescue the data before resizing the buffer object if we do not have our backup copy */
482 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER))
483 {
484 buffer_get_sysmem(This);
485 }
486
487 ENTER_GL();
488 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
489 checkGLcall("glBindBufferARB");
490 GL_EXTCALL(glBufferDataARB(This->buffer_type_hint, size, NULL, This->buffer_object_usage));
491 This->buffer_object_size = size;
492 checkGLcall("glBufferDataARB");
493 LEAVE_GL();
494 }
495}
496
497static inline void fixup_d3dcolor(DWORD *dst_color)
498{
499 DWORD src_color = *dst_color;
500
501 /* Color conversion like in drawStridedSlow. watch out for little endianity
502 * If we want that stuff to work on big endian machines too we have to consider more things
503 *
504 * 0xff000000: Alpha mask
505 * 0x00ff0000: Blue mask
506 * 0x0000ff00: Green mask
507 * 0x000000ff: Red mask
508 */
509 *dst_color = 0;
510 *dst_color |= (src_color & 0xff00ff00); /* Alpha Green */
511 *dst_color |= (src_color & 0x00ff0000) >> 16; /* Red */
512 *dst_color |= (src_color & 0x000000ff) << 16; /* Blue */
513}
514
515static inline void fixup_transformed_pos(float *p)
516{
517 /* rhw conversion like in position_float4(). */
518 if (p[3] != 1.0f && p[3] != 0.0f)
519 {
520 float w = 1.0f / p[3];
521 p[0] *= w;
522 p[1] *= w;
523 p[2] *= w;
524 p[3] = w;
525 }
526}
527
528/* Context activation is done by the caller. */
529const BYTE *buffer_get_memory(IWineD3DBuffer *iface, UINT offset, GLuint *buffer_object)
530{
531 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
532
533 *buffer_object = This->buffer_object;
534 if (!This->buffer_object)
535 {
536 if (This->flags & WINED3D_BUFFER_CREATEBO)
537 {
538 buffer_create_buffer_object(This);
539 This->flags &= ~WINED3D_BUFFER_CREATEBO;
540 if (This->buffer_object)
541 {
542 *buffer_object = This->buffer_object;
543 return (const BYTE *)offset;
544 }
545 }
546 return This->resource.allocatedMemory + offset;
547 }
548 else
549 {
550 return (const BYTE *)offset;
551 }
552}
553
554/* IUnknown methods */
555
556static HRESULT STDMETHODCALLTYPE buffer_QueryInterface(IWineD3DBuffer *iface,
557 REFIID riid, void **object)
558{
559 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
560
561 if (IsEqualGUID(riid, &IID_IWineD3DBuffer)
562 || IsEqualGUID(riid, &IID_IWineD3DResource)
563 || IsEqualGUID(riid, &IID_IWineD3DBase)
564 || IsEqualGUID(riid, &IID_IUnknown))
565 {
566 IUnknown_AddRef(iface);
567 *object = iface;
568 return S_OK;
569 }
570
571 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
572
573 *object = NULL;
574
575 return E_NOINTERFACE;
576}
577
578static ULONG STDMETHODCALLTYPE buffer_AddRef(IWineD3DBuffer *iface)
579{
580 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
581 ULONG refcount = InterlockedIncrement(&This->resource.ref);
582
583 TRACE("%p increasing refcount to %u\n", This, refcount);
584
585 return refcount;
586}
587
588/* Context activation is done by the caller. */
589BYTE *buffer_get_sysmem(struct wined3d_buffer *This)
590{
591 /* AllocatedMemory exists if the buffer is double buffered or has no buffer object at all */
592 if(This->resource.allocatedMemory) return This->resource.allocatedMemory;
593
594 This->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size + RESOURCE_ALIGNMENT);
595 This->resource.allocatedMemory = (BYTE *)(((ULONG_PTR)This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
596 ENTER_GL();
597 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
598 GL_EXTCALL(glGetBufferSubDataARB(This->buffer_type_hint, 0, This->resource.size, This->resource.allocatedMemory));
599 LEAVE_GL();
600 This->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
601
602 return This->resource.allocatedMemory;
603}
604
605static void STDMETHODCALLTYPE buffer_UnLoad(IWineD3DBuffer *iface)
606{
607 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
608
609 TRACE("iface %p\n", iface);
610
611 if (This->buffer_object)
612 {
613 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
614
615 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
616
617 /* Download the buffer, but don't permanently enable double buffering */
618 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER))
619 {
620 buffer_get_sysmem(This);
621 This->flags &= ~WINED3D_BUFFER_DOUBLEBUFFER;
622 }
623
624 ENTER_GL();
625 GL_EXTCALL(glDeleteBuffersARB(1, &This->buffer_object));
626 checkGLcall("glDeleteBuffersARB");
627 LEAVE_GL();
628 This->buffer_object = 0;
629 This->flags |= WINED3D_BUFFER_CREATEBO; /* Recreate the buffer object next load */
630 }
631}
632
633static ULONG STDMETHODCALLTYPE buffer_Release(IWineD3DBuffer *iface)
634{
635 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
636 ULONG refcount = InterlockedDecrement(&This->resource.ref);
637
638 TRACE("%p decreasing refcount to %u\n", This, refcount);
639
640 if (!refcount)
641 {
642 buffer_UnLoad(iface);
643 resource_cleanup((IWineD3DResource *)iface);
644 HeapFree(GetProcessHeap(), 0, This);
645 }
646
647 return refcount;
648}
649
650/* IWineD3DBase methods */
651
652static HRESULT STDMETHODCALLTYPE buffer_GetParent(IWineD3DBuffer *iface, IUnknown **parent)
653{
654 return resource_get_parent((IWineD3DResource *)iface, parent);
655}
656
657/* IWineD3DResource methods */
658
659static HRESULT STDMETHODCALLTYPE buffer_GetDevice(IWineD3DBuffer *iface, IWineD3DDevice **device)
660{
661 return resource_get_device((IWineD3DResource *)iface, device);
662}
663
664static HRESULT STDMETHODCALLTYPE buffer_SetPrivateData(IWineD3DBuffer *iface,
665 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
666{
667 return resource_set_private_data((IWineD3DResource *)iface, guid, data, data_size, flags);
668}
669
670static HRESULT STDMETHODCALLTYPE buffer_GetPrivateData(IWineD3DBuffer *iface,
671 REFGUID guid, void *data, DWORD *data_size)
672{
673 return resource_get_private_data((IWineD3DResource *)iface, guid, data, data_size);
674}
675
676static HRESULT STDMETHODCALLTYPE buffer_FreePrivateData(IWineD3DBuffer *iface, REFGUID guid)
677{
678 return resource_free_private_data((IWineD3DResource *)iface, guid);
679}
680
681static DWORD STDMETHODCALLTYPE buffer_SetPriority(IWineD3DBuffer *iface, DWORD priority)
682{
683 return resource_set_priority((IWineD3DResource *)iface, priority);
684}
685
686static DWORD STDMETHODCALLTYPE buffer_GetPriority(IWineD3DBuffer *iface)
687{
688 return resource_get_priority((IWineD3DResource *)iface);
689}
690
691static void STDMETHODCALLTYPE buffer_PreLoad(IWineD3DBuffer *iface)
692{
693 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
694 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
695 UINT start = 0, end = 0, vertices;
696 BOOL decl_changed = FALSE;
697 unsigned int i, j;
698 BYTE *data;
699
700 TRACE("iface %p\n", iface);
701
702 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
703
704 if (!This->buffer_object)
705 {
706 /* TODO: Make converting independent from VBOs */
707 if (This->flags & WINED3D_BUFFER_CREATEBO)
708 {
709 buffer_create_buffer_object(This);
710 This->flags &= ~WINED3D_BUFFER_CREATEBO;
711 }
712 else
713 {
714 return; /* Not doing any conversion */
715 }
716 }
717
718 /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
719 if (device->isInDraw && This->bind_count > 0)
720 {
721 decl_changed = buffer_find_decl(This);
722 This->flags |= WINED3D_BUFFER_HASDESC;
723 }
724
725 if (!decl_changed && !(This->flags & WINED3D_BUFFER_HASDESC && This->flags & WINED3D_BUFFER_DIRTY)) return;
726
727 /* If applications change the declaration over and over, reconverting all the time is a huge
728 * performance hit. So count the declaration changes and release the VBO if there are too many
729 * of them (and thus stop converting)
730 */
731 if (decl_changed)
732 {
733 ++This->conversion_count;
734 This->draw_count = 0;
735
736 if (This->conversion_count > VB_MAXDECLCHANGES)
737 {
738 FIXME("Too many declaration changes, stopping converting\n");
739
740 ENTER_GL();
741 GL_EXTCALL(glDeleteBuffersARB(1, &This->buffer_object));
742 checkGLcall("glDeleteBuffersARB");
743 LEAVE_GL();
744 This->buffer_object = 0;
745 HeapFree(GetProcessHeap(), 0, This->conversion_shift);
746
747 /* The stream source state handler might have read the memory of the vertex buffer already
748 * and got the memory in the vbo which is not valid any longer. Dirtify the stream source
749 * to force a reload. This happens only once per changed vertexbuffer and should occur rather
750 * rarely
751 */
752 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_STREAMSRC);
753
754 return;
755 }
756 buffer_check_buffer_object_size(This);
757 }
758 else
759 {
760 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
761 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
762 * decl changes and reset the decl change count after a specific number of them
763 */
764 ++This->draw_count;
765 if (This->draw_count > VB_RESETDECLCHANGE) This->conversion_count = 0;
766 }
767
768 if (decl_changed)
769 {
770 /* The declaration changed, reload the whole buffer */
771 WARN("Reloading buffer because of decl change\n");
772 start = 0;
773 end = This->resource.size;
774 }
775 else
776 {
777 /* No decl change, but dirty data, reload the changed stuff */
778 if (This->conversion_shift)
779 {
780 if (This->dirty_start != 0 || This->dirty_end != 0)
781 {
782 FIXME("Implement partial buffer loading with shifted conversion\n");
783 }
784 }
785 start = This->dirty_start;
786 end = This->dirty_end;
787 }
788
789 /* Mark the buffer clean */
790 This->flags &= ~WINED3D_BUFFER_DIRTY;
791 This->dirty_start = 0;
792 This->dirty_end = 0;
793
794 if(This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
795 {
796 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_INDEXBUFFER);
797 }
798
799 if (!This->conversion_map)
800 {
801 /* That means that there is nothing to fixup. Just upload from This->resource.allocatedMemory
802 * directly into the vbo. Do not free the system memory copy because drawPrimitive may need it if
803 * the stride is 0, for instancing emulation, vertex blending emulation or shader emulation.
804 */
805 TRACE("No conversion needed\n");
806
807 /* Nothing to do because we locked directly into the vbo */
808 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER)) return;
809
810 if (!device->isInDraw)
811 {
812 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
813 }
814 ENTER_GL();
815 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
816 checkGLcall("glBindBufferARB");
817 GL_EXTCALL(glBufferSubDataARB(This->buffer_type_hint, start, end-start, This->resource.allocatedMemory + start));
818 checkGLcall("glBufferSubDataARB");
819 LEAVE_GL();
820 return;
821 }
822
823 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER))
824 {
825 buffer_get_sysmem(This);
826 }
827
828 /* Now for each vertex in the buffer that needs conversion */
829 vertices = This->resource.size / This->stride;
830
831 if (This->conversion_shift)
832 {
833 TRACE("Shifted conversion\n");
834 data = HeapAlloc(GetProcessHeap(), 0, vertices * This->conversion_stride);
835
836 for (i = start / This->stride; i < min((end / This->stride) + 1, vertices); ++i)
837 {
838 for (j = 0; j < This->stride; ++j)
839 {
840 switch(This->conversion_map[j])
841 {
842 case CONV_NONE:
843 data[This->conversion_stride * i + j + This->conversion_shift[j]]
844 = This->resource.allocatedMemory[This->stride * i + j];
845 break;
846
847 case CONV_FLOAT16_2:
848 {
849 float *out = (float *)(&data[This->conversion_stride * i + j + This->conversion_shift[j]]);
850 const WORD *in = (WORD *)(&This->resource.allocatedMemory[i * This->stride + j]);
851
852 out[1] = float_16_to_32(in + 1);
853 out[0] = float_16_to_32(in + 0);
854 j += 3; /* Skip 3 additional bytes,as a FLOAT16_2 has 4 bytes */
855 break;
856 }
857
858 default:
859 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conversion_map[j]);
860 break;
861 }
862 }
863 }
864
865 ENTER_GL();
866 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
867 checkGLcall("glBindBufferARB");
868 GL_EXTCALL(glBufferSubDataARB(This->buffer_type_hint, 0, vertices * This->conversion_stride, data));
869 checkGLcall("glBufferSubDataARB");
870 LEAVE_GL();
871 }
872 else
873 {
874 data = HeapAlloc(GetProcessHeap(), 0, This->resource.size);
875 memcpy(data + start, This->resource.allocatedMemory + start, end - start);
876 for (i = start / This->stride; i < min((end / This->stride) + 1, vertices); ++i)
877 {
878 for (j = 0; j < This->stride; ++j)
879 {
880 switch(This->conversion_map[j])
881 {
882 case CONV_NONE:
883 /* Done already */
884 j += 3;
885 break;
886 case CONV_D3DCOLOR:
887 fixup_d3dcolor((DWORD *) (data + i * This->stride + j));
888 j += 3;
889 break;
890
891 case CONV_POSITIONT:
892 fixup_transformed_pos((float *) (data + i * This->stride + j));
893 j += 15;
894 break;
895
896 case CONV_FLOAT16_2:
897 ERR("Did not expect FLOAT16 conversion in unshifted conversion\n");
898 default:
899 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conversion_map[j]);
900 }
901 }
902 }
903
904 ENTER_GL();
905 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
906 checkGLcall("glBindBufferARB");
907 GL_EXTCALL(glBufferSubDataARB(This->buffer_type_hint, start, end - start, data + start));
908 checkGLcall("glBufferSubDataARB");
909 LEAVE_GL();
910 }
911
912 HeapFree(GetProcessHeap(), 0, data);
913}
914
915static WINED3DRESOURCETYPE STDMETHODCALLTYPE buffer_GetType(IWineD3DBuffer *iface)
916{
917 return resource_get_type((IWineD3DResource *)iface);
918}
919
920/* IWineD3DBuffer methods */
921
922static HRESULT STDMETHODCALLTYPE buffer_Map(IWineD3DBuffer *iface, UINT offset, UINT size, BYTE **data, DWORD flags)
923{
924 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
925 LONG count;
926
927 TRACE("iface %p, offset %u, size %u, data %p, flags %#x\n", iface, offset, size, data, flags);
928
929 count = InterlockedIncrement(&This->lock_count);
930
931 if (This->flags & WINED3D_BUFFER_DIRTY)
932 {
933 if (This->dirty_start > offset) This->dirty_start = offset;
934
935 if (size)
936 {
937 if (This->dirty_end < offset + size) This->dirty_end = offset + size;
938 }
939 else
940 {
941 This->dirty_end = This->resource.size;
942 }
943 }
944 else
945 {
946 This->dirty_start = offset;
947 if (size) This->dirty_end = offset + size;
948 else This->dirty_end = This->resource.size;
949 }
950
951 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER) && This->buffer_object)
952 {
953 if(count == 1)
954 {
955 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
956
957 if(This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
958 {
959 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_INDEXBUFFER);
960 }
961
962 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
963 ENTER_GL();
964 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
965 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(This->buffer_type_hint, GL_READ_WRITE_ARB));
966 LEAVE_GL();
967 }
968 }
969 else
970 {
971 This->flags |= WINED3D_BUFFER_DIRTY;
972 }
973
974 *data = This->resource.allocatedMemory + offset;
975
976 TRACE("Returning memory at %p (base %p, offset %u)\n", *data, This->resource.allocatedMemory, offset);
977 /* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
978
979 return WINED3D_OK;
980}
981
982static HRESULT STDMETHODCALLTYPE buffer_Unmap(IWineD3DBuffer *iface)
983{
984 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
985
986 TRACE("(%p)\n", This);
987
988 /* In the case that the number of Unmap calls > the
989 * number of Map calls, d3d returns always D3D_OK.
990 * This is also needed to prevent Map from returning garbage on
991 * the next call (this will happen if the lock_count is < 0). */
992 if(This->lock_count == 0)
993 {
994 TRACE("Unmap called without a previous Map call!\n");
995 return WINED3D_OK;
996 }
997
998 if (InterlockedDecrement(&This->lock_count))
999 {
1000 /* Delay loading the buffer until everything is unlocked */
1001 TRACE("Ignoring unlock\n");
1002 return WINED3D_OK;
1003 }
1004
1005 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER) && This->buffer_object)
1006 {
1007 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1008
1009 if(This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
1010 {
1011 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_INDEXBUFFER);
1012 }
1013
1014 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
1015 ENTER_GL();
1016 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
1017 GL_EXTCALL(glUnmapBufferARB(This->buffer_type_hint));
1018 LEAVE_GL();
1019
1020 This->resource.allocatedMemory = NULL;
1021 }
1022 else if (This->flags & WINED3D_BUFFER_HASDESC)
1023 {
1024 buffer_PreLoad(iface);
1025 }
1026
1027 return WINED3D_OK;
1028}
1029
1030static HRESULT STDMETHODCALLTYPE buffer_GetDesc(IWineD3DBuffer *iface, WINED3DBUFFER_DESC *desc)
1031{
1032 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
1033
1034 TRACE("(%p)\n", This);
1035
1036 desc->Type = This->resource.resourceType;
1037 desc->Usage = This->resource.usage;
1038 desc->Pool = This->resource.pool;
1039 desc->Size = This->resource.size;
1040
1041 return WINED3D_OK;
1042}
1043
1044const struct IWineD3DBufferVtbl wined3d_buffer_vtbl =
1045{
1046 /* IUnknown methods */
1047 buffer_QueryInterface,
1048 buffer_AddRef,
1049 buffer_Release,
1050 /* IWineD3DBase methods */
1051 buffer_GetParent,
1052 /* IWineD3DResource methods */
1053 buffer_GetDevice,
1054 buffer_SetPrivateData,
1055 buffer_GetPrivateData,
1056 buffer_FreePrivateData,
1057 buffer_SetPriority,
1058 buffer_GetPriority,
1059 buffer_PreLoad,
1060 buffer_UnLoad,
1061 buffer_GetType,
1062 /* IWineD3DBuffer methods */
1063 buffer_Map,
1064 buffer_Unmap,
1065 buffer_GetDesc,
1066};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette