VirtualBox

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

Last change on this file since 19678 was 19678, checked in by vboxsync, 16 years ago

opengl: update wine to 1.1.21, add d3d9.dll to build list

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

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