d3dx9: Handle pool and device in ID3DXEffect.
[wine] / dlls / d3dx9_36 / core.c
1 /*
2  *
3  * Copyright 2002 Raphael Junqueira
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <stdarg.h>
21
22 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28
29 #include "d3dx9_36_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
32
33 typedef struct ID3DXBufferImpl
34 {
35     ID3DXBuffer ID3DXBuffer_iface;
36     LONG ref;
37
38     DWORD *buffer;
39     DWORD bufferSize;
40 } ID3DXBufferImpl;
41
42 static inline ID3DXBufferImpl *impl_from_ID3DXBuffer(ID3DXBuffer *iface)
43 {
44     return CONTAINING_RECORD(iface, ID3DXBufferImpl, ID3DXBuffer_iface);
45 }
46
47 static HRESULT WINAPI ID3DXBufferImpl_QueryInterface(ID3DXBuffer *iface, REFIID riid, void **ppobj)
48 {
49     ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
50
51     if (IsEqualGUID(riid, &IID_IUnknown)
52       || IsEqualGUID(riid, &IID_ID3DXBuffer))
53     {
54         IUnknown_AddRef(iface);
55         *ppobj = This;
56         return D3D_OK;
57     }
58
59     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
60     return E_NOINTERFACE;
61 }
62
63 static ULONG WINAPI ID3DXBufferImpl_AddRef(ID3DXBuffer *iface)
64 {
65     ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
66     ULONG ref = InterlockedIncrement(&This->ref);
67
68     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
69
70     return ref;
71 }
72
73 static ULONG WINAPI ID3DXBufferImpl_Release(ID3DXBuffer *iface)
74 {
75     ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
76     ULONG ref = InterlockedDecrement(&This->ref);
77
78     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
79
80     if (ref == 0)
81     {
82         HeapFree(GetProcessHeap(), 0, This->buffer);
83         HeapFree(GetProcessHeap(), 0, This);
84     }
85     return ref;
86 }
87
88 static LPVOID WINAPI ID3DXBufferImpl_GetBufferPointer(ID3DXBuffer *iface)
89 {
90     ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
91     return This->buffer;
92 }
93
94 static DWORD WINAPI ID3DXBufferImpl_GetBufferSize(ID3DXBuffer *iface)
95 {
96     ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
97     return This->bufferSize;
98 }
99
100 const ID3DXBufferVtbl D3DXBuffer_Vtbl =
101 {
102     ID3DXBufferImpl_QueryInterface,
103     ID3DXBufferImpl_AddRef,
104     ID3DXBufferImpl_Release,
105     ID3DXBufferImpl_GetBufferPointer,
106     ID3DXBufferImpl_GetBufferSize
107 };
108
109 HRESULT WINAPI D3DXCreateBuffer(DWORD NumBytes, LPD3DXBUFFER* ppBuffer)
110 {
111     ID3DXBufferImpl *object;
112
113     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXBufferImpl));
114     if (object == NULL)
115     {
116         *ppBuffer = NULL;
117         return E_OUTOFMEMORY;
118     }
119     object->ID3DXBuffer_iface.lpVtbl = &D3DXBuffer_Vtbl;
120     object->ref = 1;
121     object->bufferSize = NumBytes;
122     object->buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, NumBytes);
123     if (object->buffer == NULL)
124     {
125         HeapFree(GetProcessHeap(), 0, object);
126         *ppBuffer = NULL;
127         return E_OUTOFMEMORY;
128     }
129
130     *ppBuffer = &object->ID3DXBuffer_iface;
131     return D3D_OK;
132 }