VirtualBox

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

最後變更 在這個檔案從23571是 23571,由 vboxsync 提交於 15 年 前

crOpenGL: update to wine 1.1.30

  • 屬性 svn:eol-style 設為 native
檔案大小: 40.3 KB
 
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, const struct wined3d_stream_info *si,
211 UINT attrib_idx, const BOOL check_d3dcolor, const BOOL is_ffp_position, const BOOL is_ffp_color,
212 DWORD *stride_this_run, BOOL *float16_used)
213{
214 const struct wined3d_stream_info_element *attrib = &si->elements[attrib_idx];
215 BOOL ret = FALSE;
216 WINED3DFORMAT format;
217
218 /* Ignore attributes that do not have our vbo. After that check we can be sure that the attribute is
219 * there, on nonexistent attribs the vbo is 0.
220 */
221 if (!(si->use_map & (1 << attrib_idx))
222 || attrib->buffer_object != This->buffer_object)
223 return FALSE;
224
225 format = attrib->format_desc->format;
226 /* Look for newly appeared conversion */
227 if (!GL_SUPPORT(ARB_HALF_FLOAT_VERTEX) && (format == WINED3DFMT_R16G16_FLOAT || format == WINED3DFMT_R16G16B16A16_FLOAT))
228 {
229 ret = buffer_process_converted_attribute(This, CONV_FLOAT16_2, attrib, stride_this_run);
230
231 if (is_ffp_position) FIXME("Test FLOAT16 fixed function processing positions\n");
232 else if (is_ffp_color) FIXME("test FLOAT16 fixed function processing colors\n");
233 *float16_used = TRUE;
234 }
235 else if (check_d3dcolor && format == WINED3DFMT_B8G8R8A8_UNORM)
236 {
237 ret = buffer_process_converted_attribute(This, CONV_D3DCOLOR, attrib, stride_this_run);
238
239 if (!is_ffp_color) FIXME("Test for non-color fixed function WINED3DFMT_B8G8R8A8_UNORM format\n");
240 }
241 else if (is_ffp_position && format == WINED3DFMT_R32G32B32A32_FLOAT)
242 {
243 ret = buffer_process_converted_attribute(This, CONV_POSITIONT, attrib, stride_this_run);
244 }
245 else if (This->conversion_map)
246 {
247 ret = buffer_process_converted_attribute(This, CONV_NONE, attrib, stride_this_run);
248 }
249
250 return ret;
251}
252
253static UINT *find_conversion_shift(struct wined3d_buffer *This,
254 const struct wined3d_stream_info *strided, UINT stride)
255{
256 UINT *ret, i, j, shift, orig_type_size;
257
258 if (!stride)
259 {
260 TRACE("No shift\n");
261 return NULL;
262 }
263
264 This->conversion_stride = stride;
265 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DWORD) * stride);
266 for (i = 0; i < MAX_ATTRIBS; ++i)
267 {
268 WINED3DFORMAT format;
269
270 if (!(strided->use_map & (1 << i)) || strided->elements[i].buffer_object != This->buffer_object) continue;
271
272 format = strided->elements[i].format_desc->format;
273 if (format == WINED3DFMT_R16G16_FLOAT)
274 {
275 shift = 4;
276 }
277 else if (format == WINED3DFMT_R16G16B16A16_FLOAT)
278 {
279 shift = 8;
280 /* Pre-shift the last 4 bytes in the FLOAT16_4 by 4 bytes - this makes FLOAT16_2 and FLOAT16_4 conversions
281 * compatible
282 */
283 for (j = 4; j < 8; ++j)
284 {
285 ret[(DWORD_PTR)strided->elements[i].data + j] += 4;
286 }
287 }
288 else
289 {
290 shift = 0;
291 }
292 This->conversion_stride += shift;
293
294 if (shift)
295 {
296 orig_type_size = strided->elements[i].format_desc->component_count
297 * strided->elements[i].format_desc->component_size;
298 for (j = (DWORD_PTR)strided->elements[i].data + orig_type_size; j < stride; ++j)
299 {
300 ret[j] += shift;
301 }
302 }
303 }
304
305 if (TRACE_ON(d3d))
306 {
307 TRACE("Dumping conversion shift:\n");
308 for (i = 0; i < stride; ++i)
309 {
310 TRACE("[%d]", ret[i]);
311 }
312 TRACE("\n");
313 }
314
315 return ret;
316}
317
318static BOOL buffer_find_decl(struct wined3d_buffer *This)
319{
320 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
321 const struct wined3d_stream_info *si = &device->strided_streams;
322 UINT stride_this_run = 0;
323 BOOL float16_used = FALSE;
324 BOOL ret = FALSE;
325 unsigned int i;
326
327 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
328 * Once we have our declaration there is no need to look it up again. Index buffers also never need
329 * conversion, so once the (empty) conversion structure is created don't bother checking again
330 */
331 if (This->flags & WINED3D_BUFFER_HASDESC)
332 {
333 if(((IWineD3DImpl *)device->wineD3D)->dxVersion == 7 ||
334 This->resource.format_desc->format != WINED3DFMT_VERTEXDATA) return FALSE;
335 }
336
337 TRACE("Finding vertex buffer conversion information\n");
338 /* Certain declaration types need some fixups before we can pass them to
339 * opengl. This means D3DCOLOR attributes with fixed function vertex
340 * processing, FLOAT4 POSITIONT with fixed function, and FLOAT16 if
341 * GL_ARB_half_float_vertex is not supported.
342 *
343 * Note for d3d8 and d3d9:
344 * The vertex buffer FVF doesn't help with finding them, we have to use
345 * the decoded vertex declaration and pick the things that concern the
346 * current buffer. A problem with this is that this can change between
347 * draws, so we have to validate the information and reprocess the buffer
348 * if it changes, and avoid false positives for performance reasons.
349 * WineD3D doesn't even know the vertex buffer any more, it is managed
350 * by the client libraries and passed to SetStreamSource and ProcessVertices
351 * as needed.
352 *
353 * We have to distinguish between vertex shaders and fixed function to
354 * pick the way we access the strided vertex information.
355 *
356 * This code sets up a per-byte array with the size of the detected
357 * stride of the arrays in the buffer. For each byte we have a field
358 * that marks the conversion needed on this byte. For example, the
359 * following declaration with fixed function vertex processing:
360 *
361 * POSITIONT, FLOAT4
362 * NORMAL, FLOAT3
363 * DIFFUSE, FLOAT16_4
364 * SPECULAR, D3DCOLOR
365 *
366 * Will result in
367 * { POSITIONT }{ NORMAL }{ DIFFUSE }{SPECULAR }
368 * [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]
369 *
370 * Where in this example map P means 4 component position conversion, 0
371 * means no conversion, F means FLOAT16_2 conversion and C means D3DCOLOR
372 * conversion (red / blue swizzle).
373 *
374 * If we're doing conversion and the stride changes we have to reconvert
375 * the whole buffer. Note that we do not mind if the semantic changes,
376 * we only care for the conversion type. So if the NORMAL is replaced
377 * with a TEXCOORD, nothing has to be done, or if the DIFFUSE is replaced
378 * with a D3DCOLOR BLENDWEIGHT we can happily dismiss the change. Some
379 * conversion types depend on the semantic as well, for example a FLOAT4
380 * texcoord needs no conversion while a FLOAT4 positiont needs one
381 */
382 if (use_vs(device->stateBlock))
383 {
384 TRACE("vshader\n");
385 /* If the current vertex declaration is marked for no half float conversion don't bother to
386 * analyse the strided streams in depth, just set them up for no conversion. Return decl changed
387 * if we used conversion before
388 */
389 if (!((IWineD3DVertexDeclarationImpl *) device->stateBlock->vertexDecl)->half_float_conv_needed)
390 {
391 if (This->conversion_map)
392 {
393 TRACE("Now using shaders without conversion, but conversion used before\n");
394 HeapFree(GetProcessHeap(), 0, This->conversion_map);
395 HeapFree(GetProcessHeap(), 0, This->conversion_shift);
396 This->conversion_map = NULL;
397 This->stride = 0;
398 This->conversion_shift = NULL;
399 This->conversion_stride = 0;
400 return TRUE;
401 }
402 else
403 {
404 return FALSE;
405 }
406 }
407 for (i = 0; i < MAX_ATTRIBS; ++i)
408 {
409 ret = buffer_check_attribute(This, si, i, FALSE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
410 }
411
412 /* Recalculate the conversion shift map if the declaration has changed,
413 * and we're using float16 conversion or used it on the last run
414 */
415 if (ret && (float16_used || This->conversion_map))
416 {
417 HeapFree(GetProcessHeap(), 0, This->conversion_shift);
418 This->conversion_shift = find_conversion_shift(This, si, This->stride);
419 }
420 }
421 else
422 {
423 /* Fixed function is a bit trickier. We have to take care for D3DCOLOR types, FLOAT4 positions and of course
424 * FLOAT16s if not supported. Also, we can't iterate over the array, so use macros to generate code for all
425 * the attributes that our current fixed function pipeline implementation cares for.
426 */
427 BOOL support_d3dcolor = GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA);
428 ret = buffer_check_attribute(This, si, WINED3D_FFP_POSITION,
429 TRUE, TRUE, FALSE, &stride_this_run, &float16_used) || ret;
430 ret = buffer_check_attribute(This, si, WINED3D_FFP_NORMAL,
431 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
432 ret = buffer_check_attribute(This, si, WINED3D_FFP_DIFFUSE,
433 !support_d3dcolor, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
434 ret = buffer_check_attribute(This, si, WINED3D_FFP_SPECULAR,
435 !support_d3dcolor, FALSE, TRUE, &stride_this_run, &float16_used) || ret;
436 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD0,
437 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
438 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD1,
439 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
440 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD2,
441 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
442 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD3,
443 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
444 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD4,
445 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
446 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD5,
447 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
448 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD6,
449 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
450 ret = buffer_check_attribute(This, si, WINED3D_FFP_TEXCOORD7,
451 TRUE, FALSE, FALSE, &stride_this_run, &float16_used) || ret;
452
453 if (float16_used) FIXME("Float16 conversion used with fixed function vertex processing\n");
454 }
455
456 if (stride_this_run == 0 && This->conversion_map)
457 {
458 /* Sanity test */
459 if (!ret) ERR("no converted attributes found, old conversion map exists, and no declaration change?\n");
460 HeapFree(GetProcessHeap(), 0, This->conversion_map);
461 This->conversion_map = NULL;
462 This->stride = 0;
463 }
464
465 if (ret) TRACE("Conversion information changed\n");
466
467 return ret;
468}
469
470/* Context activation is done by the caller. */
471static void buffer_check_buffer_object_size(struct wined3d_buffer *This)
472{
473 UINT size = This->conversion_stride ?
474 This->conversion_stride * (This->resource.size / This->stride) : This->resource.size;
475 if (This->buffer_object_size != size)
476 {
477 TRACE("Old size %u, creating new size %u\n", This->buffer_object_size, size);
478
479 if(This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
480 {
481 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_INDEXBUFFER);
482 }
483
484 /* Rescue the data before resizing the buffer object if we do not have our backup copy */
485 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER))
486 {
487 buffer_get_sysmem(This);
488 }
489
490 ENTER_GL();
491 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
492 checkGLcall("glBindBufferARB");
493 GL_EXTCALL(glBufferDataARB(This->buffer_type_hint, size, NULL, This->buffer_object_usage));
494 This->buffer_object_size = size;
495 checkGLcall("glBufferDataARB");
496 LEAVE_GL();
497 }
498}
499
500static inline void fixup_d3dcolor(DWORD *dst_color)
501{
502 DWORD src_color = *dst_color;
503
504 /* Color conversion like in drawStridedSlow. watch out for little endianity
505 * If we want that stuff to work on big endian machines too we have to consider more things
506 *
507 * 0xff000000: Alpha mask
508 * 0x00ff0000: Blue mask
509 * 0x0000ff00: Green mask
510 * 0x000000ff: Red mask
511 */
512 *dst_color = 0;
513 *dst_color |= (src_color & 0xff00ff00); /* Alpha Green */
514 *dst_color |= (src_color & 0x00ff0000) >> 16; /* Red */
515 *dst_color |= (src_color & 0x000000ff) << 16; /* Blue */
516}
517
518static inline void fixup_transformed_pos(float *p)
519{
520 /* rhw conversion like in position_float4(). */
521 if (p[3] != 1.0f && p[3] != 0.0f)
522 {
523 float w = 1.0f / p[3];
524 p[0] *= w;
525 p[1] *= w;
526 p[2] *= w;
527 p[3] = w;
528 }
529}
530
531/* Context activation is done by the caller. */
532const BYTE *buffer_get_memory(IWineD3DBuffer *iface, UINT offset, GLuint *buffer_object)
533{
534 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
535
536 *buffer_object = This->buffer_object;
537 if (!This->buffer_object)
538 {
539 if (This->flags & WINED3D_BUFFER_CREATEBO)
540 {
541 buffer_create_buffer_object(This);
542 This->flags &= ~WINED3D_BUFFER_CREATEBO;
543 if (This->buffer_object)
544 {
545 *buffer_object = This->buffer_object;
546 return (const BYTE *)offset;
547 }
548 }
549 return This->resource.allocatedMemory + offset;
550 }
551 else
552 {
553 return (const BYTE *)offset;
554 }
555}
556
557/* IUnknown methods */
558
559static HRESULT STDMETHODCALLTYPE buffer_QueryInterface(IWineD3DBuffer *iface,
560 REFIID riid, void **object)
561{
562 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
563
564 if (IsEqualGUID(riid, &IID_IWineD3DBuffer)
565 || IsEqualGUID(riid, &IID_IWineD3DResource)
566 || IsEqualGUID(riid, &IID_IWineD3DBase)
567 || IsEqualGUID(riid, &IID_IUnknown))
568 {
569 IUnknown_AddRef(iface);
570 *object = iface;
571 return S_OK;
572 }
573
574 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
575
576 *object = NULL;
577
578 return E_NOINTERFACE;
579}
580
581static ULONG STDMETHODCALLTYPE buffer_AddRef(IWineD3DBuffer *iface)
582{
583 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
584 ULONG refcount = InterlockedIncrement(&This->resource.ref);
585
586 TRACE("%p increasing refcount to %u\n", This, refcount);
587
588 return refcount;
589}
590
591/* Context activation is done by the caller. */
592BYTE *buffer_get_sysmem(struct wined3d_buffer *This)
593{
594 /* AllocatedMemory exists if the buffer is double buffered or has no buffer object at all */
595 if(This->resource.allocatedMemory) return This->resource.allocatedMemory;
596
597 This->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size + RESOURCE_ALIGNMENT);
598 This->resource.allocatedMemory = (BYTE *)(((ULONG_PTR)This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
599 ENTER_GL();
600 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
601 GL_EXTCALL(glGetBufferSubDataARB(This->buffer_type_hint, 0, This->resource.size, This->resource.allocatedMemory));
602 LEAVE_GL();
603 This->flags |= WINED3D_BUFFER_DOUBLEBUFFER;
604
605 return This->resource.allocatedMemory;
606}
607
608static void STDMETHODCALLTYPE buffer_UnLoad(IWineD3DBuffer *iface)
609{
610 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
611
612 TRACE("iface %p\n", iface);
613
614 if (This->buffer_object)
615 {
616 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
617
618 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
619
620 /* Download the buffer, but don't permanently enable double buffering */
621 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER))
622 {
623 buffer_get_sysmem(This);
624 This->flags &= ~WINED3D_BUFFER_DOUBLEBUFFER;
625 }
626
627 ENTER_GL();
628 GL_EXTCALL(glDeleteBuffersARB(1, &This->buffer_object));
629 checkGLcall("glDeleteBuffersARB");
630 LEAVE_GL();
631 This->buffer_object = 0;
632 This->flags |= WINED3D_BUFFER_CREATEBO; /* Recreate the buffer object next load */
633 }
634}
635
636static ULONG STDMETHODCALLTYPE buffer_Release(IWineD3DBuffer *iface)
637{
638 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
639 ULONG refcount = InterlockedDecrement(&This->resource.ref);
640
641 TRACE("%p decreasing refcount to %u\n", This, refcount);
642
643 if (!refcount)
644 {
645 buffer_UnLoad(iface);
646 resource_cleanup((IWineD3DResource *)iface);
647 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
648 HeapFree(GetProcessHeap(), 0, This);
649 }
650
651 return refcount;
652}
653
654/* IWineD3DBase methods */
655
656static HRESULT STDMETHODCALLTYPE buffer_GetParent(IWineD3DBuffer *iface, IUnknown **parent)
657{
658 return resource_get_parent((IWineD3DResource *)iface, parent);
659}
660
661/* IWineD3DResource methods */
662
663static HRESULT STDMETHODCALLTYPE buffer_GetDevice(IWineD3DBuffer *iface, IWineD3DDevice **device)
664{
665 return resource_get_device((IWineD3DResource *)iface, device);
666}
667
668static HRESULT STDMETHODCALLTYPE buffer_SetPrivateData(IWineD3DBuffer *iface,
669 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
670{
671 return resource_set_private_data((IWineD3DResource *)iface, guid, data, data_size, flags);
672}
673
674static HRESULT STDMETHODCALLTYPE buffer_GetPrivateData(IWineD3DBuffer *iface,
675 REFGUID guid, void *data, DWORD *data_size)
676{
677 return resource_get_private_data((IWineD3DResource *)iface, guid, data, data_size);
678}
679
680static HRESULT STDMETHODCALLTYPE buffer_FreePrivateData(IWineD3DBuffer *iface, REFGUID guid)
681{
682 return resource_free_private_data((IWineD3DResource *)iface, guid);
683}
684
685static DWORD STDMETHODCALLTYPE buffer_SetPriority(IWineD3DBuffer *iface, DWORD priority)
686{
687 return resource_set_priority((IWineD3DResource *)iface, priority);
688}
689
690static DWORD STDMETHODCALLTYPE buffer_GetPriority(IWineD3DBuffer *iface)
691{
692 return resource_get_priority((IWineD3DResource *)iface);
693}
694
695static void STDMETHODCALLTYPE buffer_PreLoad(IWineD3DBuffer *iface)
696{
697 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
698 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
699 UINT start = 0, end = 0, vertices;
700 BOOL decl_changed = FALSE;
701 unsigned int i, j;
702 BYTE *data;
703
704 TRACE("iface %p\n", iface);
705
706 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
707
708 if (!This->buffer_object)
709 {
710 /* TODO: Make converting independent from VBOs */
711 if (This->flags & WINED3D_BUFFER_CREATEBO)
712 {
713 buffer_create_buffer_object(This);
714 This->flags &= ~WINED3D_BUFFER_CREATEBO;
715 }
716 else
717 {
718 return; /* Not doing any conversion */
719 }
720 }
721
722 /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
723 if (device->isInDraw && This->bind_count > 0)
724 {
725 decl_changed = buffer_find_decl(This);
726 This->flags |= WINED3D_BUFFER_HASDESC;
727 }
728
729 if (!decl_changed && !(This->flags & WINED3D_BUFFER_HASDESC && This->flags & WINED3D_BUFFER_DIRTY)) return;
730
731 /* If applications change the declaration over and over, reconverting all the time is a huge
732 * performance hit. So count the declaration changes and release the VBO if there are too many
733 * of them (and thus stop converting)
734 */
735 if (decl_changed)
736 {
737 ++This->conversion_count;
738 This->draw_count = 0;
739
740 if (This->conversion_count > VB_MAXDECLCHANGES)
741 {
742 FIXME("Too many declaration changes, stopping converting\n");
743
744 ENTER_GL();
745 GL_EXTCALL(glDeleteBuffersARB(1, &This->buffer_object));
746 checkGLcall("glDeleteBuffersARB");
747 LEAVE_GL();
748 This->buffer_object = 0;
749 HeapFree(GetProcessHeap(), 0, This->conversion_shift);
750
751 /* The stream source state handler might have read the memory of the vertex buffer already
752 * and got the memory in the vbo which is not valid any longer. Dirtify the stream source
753 * to force a reload. This happens only once per changed vertexbuffer and should occur rather
754 * rarely
755 */
756 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_STREAMSRC);
757
758 return;
759 }
760 buffer_check_buffer_object_size(This);
761 }
762 else
763 {
764 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
765 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
766 * decl changes and reset the decl change count after a specific number of them
767 */
768 ++This->draw_count;
769 if (This->draw_count > VB_RESETDECLCHANGE) This->conversion_count = 0;
770 }
771
772 if (decl_changed)
773 {
774 /* The declaration changed, reload the whole buffer */
775 WARN("Reloading buffer because of decl change\n");
776 start = 0;
777 end = This->resource.size;
778 }
779 else
780 {
781 /* No decl change, but dirty data, reload the changed stuff */
782 if (This->conversion_shift)
783 {
784 if (This->dirty_start != 0 || This->dirty_end != 0)
785 {
786 FIXME("Implement partial buffer loading with shifted conversion\n");
787 }
788 }
789 start = This->dirty_start;
790 end = This->dirty_end;
791 }
792
793 /* Mark the buffer clean */
794 This->flags &= ~WINED3D_BUFFER_DIRTY;
795 This->dirty_start = 0;
796 This->dirty_end = 0;
797
798 if(This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
799 {
800 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_INDEXBUFFER);
801 }
802
803 if (!This->conversion_map)
804 {
805 /* That means that there is nothing to fixup. Just upload from This->resource.allocatedMemory
806 * directly into the vbo. Do not free the system memory copy because drawPrimitive may need it if
807 * the stride is 0, for instancing emulation, vertex blending emulation or shader emulation.
808 */
809 TRACE("No conversion needed\n");
810
811 /* Nothing to do because we locked directly into the vbo */
812 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER)) return;
813
814 if (!device->isInDraw)
815 {
816 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
817 }
818 ENTER_GL();
819 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
820 checkGLcall("glBindBufferARB");
821 GL_EXTCALL(glBufferSubDataARB(This->buffer_type_hint, start, end-start, This->resource.allocatedMemory + start));
822 checkGLcall("glBufferSubDataARB");
823 LEAVE_GL();
824 return;
825 }
826
827 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER))
828 {
829 buffer_get_sysmem(This);
830 }
831
832 /* Now for each vertex in the buffer that needs conversion */
833 vertices = This->resource.size / This->stride;
834
835 if (This->conversion_shift)
836 {
837 TRACE("Shifted conversion\n");
838 data = HeapAlloc(GetProcessHeap(), 0, vertices * This->conversion_stride);
839
840 for (i = start / This->stride; i < min((end / This->stride) + 1, vertices); ++i)
841 {
842 for (j = 0; j < This->stride; ++j)
843 {
844 switch(This->conversion_map[j])
845 {
846 case CONV_NONE:
847 data[This->conversion_stride * i + j + This->conversion_shift[j]]
848 = This->resource.allocatedMemory[This->stride * i + j];
849 break;
850
851 case CONV_FLOAT16_2:
852 {
853 float *out = (float *)(&data[This->conversion_stride * i + j + This->conversion_shift[j]]);
854 const WORD *in = (WORD *)(&This->resource.allocatedMemory[i * This->stride + j]);
855
856 out[1] = float_16_to_32(in + 1);
857 out[0] = float_16_to_32(in + 0);
858 j += 3; /* Skip 3 additional bytes,as a FLOAT16_2 has 4 bytes */
859 break;
860 }
861
862 default:
863 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conversion_map[j]);
864 break;
865 }
866 }
867 }
868
869 ENTER_GL();
870 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
871 checkGLcall("glBindBufferARB");
872 GL_EXTCALL(glBufferSubDataARB(This->buffer_type_hint, 0, vertices * This->conversion_stride, data));
873 checkGLcall("glBufferSubDataARB");
874 LEAVE_GL();
875 }
876 else
877 {
878 data = HeapAlloc(GetProcessHeap(), 0, This->resource.size);
879 memcpy(data + start, This->resource.allocatedMemory + start, end - start);
880 for (i = start / This->stride; i < min((end / This->stride) + 1, vertices); ++i)
881 {
882 for (j = 0; j < This->stride; ++j)
883 {
884 switch(This->conversion_map[j])
885 {
886 case CONV_NONE:
887 /* Done already */
888 j += 3;
889 break;
890 case CONV_D3DCOLOR:
891 fixup_d3dcolor((DWORD *) (data + i * This->stride + j));
892 j += 3;
893 break;
894
895 case CONV_POSITIONT:
896 fixup_transformed_pos((float *) (data + i * This->stride + j));
897 j += 15;
898 break;
899
900 case CONV_FLOAT16_2:
901 ERR("Did not expect FLOAT16 conversion in unshifted conversion\n");
902 default:
903 FIXME("Unimplemented conversion %d in shifted conversion\n", This->conversion_map[j]);
904 }
905 }
906 }
907
908 ENTER_GL();
909 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
910 checkGLcall("glBindBufferARB");
911 GL_EXTCALL(glBufferSubDataARB(This->buffer_type_hint, start, end - start, data + start));
912 checkGLcall("glBufferSubDataARB");
913 LEAVE_GL();
914 }
915
916 HeapFree(GetProcessHeap(), 0, data);
917}
918
919static WINED3DRESOURCETYPE STDMETHODCALLTYPE buffer_GetType(IWineD3DBuffer *iface)
920{
921 return resource_get_type((IWineD3DResource *)iface);
922}
923
924/* IWineD3DBuffer methods */
925
926static HRESULT STDMETHODCALLTYPE buffer_Map(IWineD3DBuffer *iface, UINT offset, UINT size, BYTE **data, DWORD flags)
927{
928 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
929 LONG count;
930
931 TRACE("iface %p, offset %u, size %u, data %p, flags %#x\n", iface, offset, size, data, flags);
932
933 count = InterlockedIncrement(&This->lock_count);
934
935 if (This->flags & WINED3D_BUFFER_DIRTY)
936 {
937 if (This->dirty_start > offset) This->dirty_start = offset;
938
939 if (size)
940 {
941 if (This->dirty_end < offset + size) This->dirty_end = offset + size;
942 }
943 else
944 {
945 This->dirty_end = This->resource.size;
946 }
947 }
948 else
949 {
950 This->dirty_start = offset;
951 if (size) This->dirty_end = offset + size;
952 else This->dirty_end = This->resource.size;
953 }
954
955 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER) && This->buffer_object)
956 {
957 if(count == 1)
958 {
959 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
960
961 if(This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
962 {
963 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_INDEXBUFFER);
964 }
965
966 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
967 ENTER_GL();
968 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
969 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(This->buffer_type_hint, GL_READ_WRITE_ARB));
970 LEAVE_GL();
971 }
972 }
973 else
974 {
975 This->flags |= WINED3D_BUFFER_DIRTY;
976 }
977
978 *data = This->resource.allocatedMemory + offset;
979
980 TRACE("Returning memory at %p (base %p, offset %u)\n", *data, This->resource.allocatedMemory, offset);
981 /* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
982
983 return WINED3D_OK;
984}
985
986static HRESULT STDMETHODCALLTYPE buffer_Unmap(IWineD3DBuffer *iface)
987{
988 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
989
990 TRACE("(%p)\n", This);
991
992 /* In the case that the number of Unmap calls > the
993 * number of Map calls, d3d returns always D3D_OK.
994 * This is also needed to prevent Map from returning garbage on
995 * the next call (this will happen if the lock_count is < 0). */
996 if(This->lock_count == 0)
997 {
998 TRACE("Unmap called without a previous Map call!\n");
999 return WINED3D_OK;
1000 }
1001
1002 if (InterlockedDecrement(&This->lock_count))
1003 {
1004 /* Delay loading the buffer until everything is unlocked */
1005 TRACE("Ignoring unlock\n");
1006 return WINED3D_OK;
1007 }
1008
1009 if(!(This->flags & WINED3D_BUFFER_DOUBLEBUFFER) && This->buffer_object)
1010 {
1011 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1012
1013 if(This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
1014 {
1015 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_INDEXBUFFER);
1016 }
1017
1018 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
1019 ENTER_GL();
1020 GL_EXTCALL(glBindBufferARB(This->buffer_type_hint, This->buffer_object));
1021 GL_EXTCALL(glUnmapBufferARB(This->buffer_type_hint));
1022 LEAVE_GL();
1023
1024 This->resource.allocatedMemory = NULL;
1025 }
1026 else if (This->flags & WINED3D_BUFFER_HASDESC)
1027 {
1028 buffer_PreLoad(iface);
1029 }
1030
1031 return WINED3D_OK;
1032}
1033
1034static HRESULT STDMETHODCALLTYPE buffer_GetDesc(IWineD3DBuffer *iface, WINED3DBUFFER_DESC *desc)
1035{
1036 struct wined3d_buffer *This = (struct wined3d_buffer *)iface;
1037
1038 TRACE("(%p)\n", This);
1039
1040 desc->Type = This->resource.resourceType;
1041 desc->Usage = This->resource.usage;
1042 desc->Pool = This->resource.pool;
1043 desc->Size = This->resource.size;
1044
1045 return WINED3D_OK;
1046}
1047
1048static const struct IWineD3DBufferVtbl wined3d_buffer_vtbl =
1049{
1050 /* IUnknown methods */
1051 buffer_QueryInterface,
1052 buffer_AddRef,
1053 buffer_Release,
1054 /* IWineD3DBase methods */
1055 buffer_GetParent,
1056 /* IWineD3DResource methods */
1057 buffer_GetDevice,
1058 buffer_SetPrivateData,
1059 buffer_GetPrivateData,
1060 buffer_FreePrivateData,
1061 buffer_SetPriority,
1062 buffer_GetPriority,
1063 buffer_PreLoad,
1064 buffer_UnLoad,
1065 buffer_GetType,
1066 /* IWineD3DBuffer methods */
1067 buffer_Map,
1068 buffer_Unmap,
1069 buffer_GetDesc,
1070};
1071
1072HRESULT buffer_init(struct wined3d_buffer *buffer, IWineD3DDeviceImpl *device,
1073 UINT size, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool, GLenum bind_hint,
1074 const char *data, IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
1075{
1076 const struct GlPixelFormatDesc *format_desc = getFormatDescEntry(format, &device->adapter->gl_info);
1077 HRESULT hr;
1078
1079 if (!size)
1080 {
1081 WARN("Size 0 requested, returning WINED3DERR_INVALIDCALL\n");
1082 return WINED3DERR_INVALIDCALL;
1083 }
1084
1085 buffer->vtbl = &wined3d_buffer_vtbl;
1086
1087 hr = resource_init((IWineD3DResource *)buffer, WINED3DRTYPE_BUFFER,
1088 device, size, usage, format_desc, pool, parent, parent_ops);
1089 if (FAILED(hr))
1090 {
1091 WARN("Failed to initialize resource, hr %#x\n", hr);
1092 return hr;
1093 }
1094 buffer->buffer_type_hint = bind_hint;
1095
1096 TRACE("size %#x, usage %#x, format %s, memory @ %p, iface @ %p.\n", buffer->resource.size, buffer->resource.usage,
1097 debug_d3dformat(buffer->resource.format_desc->format), buffer->resource.allocatedMemory, buffer);
1098
1099 if (data)
1100 {
1101 BYTE *ptr;
1102
1103 hr = IWineD3DBuffer_Map((IWineD3DBuffer *)buffer, 0, size, &ptr, 0);
1104 if (FAILED(hr))
1105 {
1106 ERR("Failed to map buffer, hr %#x\n", hr);
1107 buffer_UnLoad((IWineD3DBuffer *)buffer);
1108 resource_cleanup((IWineD3DResource *)buffer);
1109 return hr;
1110 }
1111
1112 memcpy(ptr, data, size);
1113
1114 hr = IWineD3DBuffer_Unmap((IWineD3DBuffer *)buffer);
1115 if (FAILED(hr))
1116 {
1117 ERR("Failed to unmap buffer, hr %#x\n", hr);
1118 buffer_UnLoad((IWineD3DBuffer *)buffer);
1119 resource_cleanup((IWineD3DResource *)buffer);
1120 return hr;
1121 }
1122 }
1123
1124 return WINED3D_OK;
1125}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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