d3d9: Fix DrawIndexedPrimitiveUP with non-zero min_vertex_idx.
[wine.git] / dlls / d3dx9_36 / core.c
blob1eb289ece6b1cc88b57bf667448fb510a9640dd7
1 /*
3 * Copyright 2002 Raphael Junqueira
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include "d3dx9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
27 struct ID3DXBufferImpl
29 ID3DXBuffer ID3DXBuffer_iface;
30 LONG ref;
32 void *buffer;
33 DWORD size;
36 static inline struct ID3DXBufferImpl *impl_from_ID3DXBuffer(ID3DXBuffer *iface)
38 return CONTAINING_RECORD(iface, struct ID3DXBufferImpl, ID3DXBuffer_iface);
41 static HRESULT WINAPI ID3DXBufferImpl_QueryInterface(ID3DXBuffer *iface, REFIID riid, void **ppobj)
43 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), ppobj);
45 if (IsEqualGUID(riid, &IID_IUnknown)
46 || IsEqualGUID(riid, &IID_ID3DXBuffer))
48 IUnknown_AddRef(iface);
49 *ppobj = iface;
50 return D3D_OK;
53 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
55 return E_NOINTERFACE;
58 static ULONG WINAPI ID3DXBufferImpl_AddRef(ID3DXBuffer *iface)
60 struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
61 ULONG ref = InterlockedIncrement(&This->ref);
63 TRACE("%p increasing refcount to %u\n", This, ref);
65 return ref;
68 static ULONG WINAPI ID3DXBufferImpl_Release(ID3DXBuffer *iface)
70 struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
71 ULONG ref = InterlockedDecrement(&This->ref);
73 TRACE("%p decreasing refcount to %u\n", This, ref);
75 if (ref == 0)
77 HeapFree(GetProcessHeap(), 0, This->buffer);
78 HeapFree(GetProcessHeap(), 0, This);
81 return ref;
84 static void * WINAPI ID3DXBufferImpl_GetBufferPointer(ID3DXBuffer *iface)
86 struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
88 TRACE("iface %p\n", iface);
90 return This->buffer;
93 static DWORD WINAPI ID3DXBufferImpl_GetBufferSize(ID3DXBuffer *iface)
95 struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
97 TRACE("iface %p\n", iface);
99 return This->size;
102 static const struct ID3DXBufferVtbl ID3DXBufferImpl_Vtbl =
104 /* IUnknown methods */
105 ID3DXBufferImpl_QueryInterface,
106 ID3DXBufferImpl_AddRef,
107 ID3DXBufferImpl_Release,
108 /* ID3DXBuffer methods */
109 ID3DXBufferImpl_GetBufferPointer,
110 ID3DXBufferImpl_GetBufferSize
113 static HRESULT d3dx9_buffer_init(struct ID3DXBufferImpl *buffer, DWORD size)
115 buffer->ID3DXBuffer_iface.lpVtbl = &ID3DXBufferImpl_Vtbl;
116 buffer->ref = 1;
117 buffer->size = size;
119 buffer->buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
120 if (!buffer->buffer)
122 ERR("Failed to allocate buffer memory\n");
123 return E_OUTOFMEMORY;
126 return D3D_OK;
129 HRESULT WINAPI D3DXCreateBuffer(DWORD size, ID3DXBuffer **buffer)
131 struct ID3DXBufferImpl *object;
132 HRESULT hr;
134 if (!buffer)
136 WARN("Invalid buffer specified.\n");
137 return D3DERR_INVALIDCALL;
140 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
141 if (!object)
142 return E_OUTOFMEMORY;
144 hr = d3dx9_buffer_init(object, size);
145 if (FAILED(hr))
147 WARN("Failed to initialize buffer, hr %#x.\n", hr);
148 HeapFree(GetProcessHeap(), 0, object);
149 return hr;
152 *buffer = &object->ID3DXBuffer_iface;
154 TRACE("Created ID3DXBuffer %p.\n", *buffer);
156 return D3D_OK;