st/nine: Add debug warning when application uses sw processing
[mesa.git] / src / gallium / state_trackers / nine / vertexbuffer9.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "vertexbuffer9.h"
24 #include "device9.h"
25 #include "nine_helpers.h"
26 #include "nine_pipe.h"
27
28 #include "pipe/p_screen.h"
29 #include "pipe/p_context.h"
30 #include "pipe/p_state.h"
31 #include "pipe/p_defines.h"
32 #include "pipe/p_format.h"
33 #include "util/u_box.h"
34
35 #define DBG_CHANNEL DBG_VERTEXBUFFER
36
37 HRESULT
38 NineVertexBuffer9_ctor( struct NineVertexBuffer9 *This,
39 struct NineUnknownParams *pParams,
40 D3DVERTEXBUFFER_DESC *pDesc )
41 {
42 struct pipe_resource *info = &This->base.info;
43 HRESULT hr;
44
45 DBG("This=%p Size=0x%x Usage=%x Pool=%u\n", This,
46 pDesc->Size, pDesc->Usage, pDesc->Pool);
47
48 user_assert(pDesc->Pool != D3DPOOL_SCRATCH, D3DERR_INVALIDCALL);
49
50 This->maps = MALLOC(sizeof(struct pipe_transfer *));
51 if (!This->maps)
52 return E_OUTOFMEMORY;
53 This->nmaps = 0;
54 This->maxmaps = 1;
55
56 This->pipe = pParams->device->pipe;
57
58 info->screen = pParams->device->screen;
59 info->target = PIPE_BUFFER;
60 info->format = PIPE_FORMAT_R8_UNORM;
61 info->width0 = pDesc->Size;
62 info->flags = 0;
63
64 info->bind = PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_TRANSFER_WRITE;
65 if (!(pDesc->Usage & D3DUSAGE_WRITEONLY))
66 info->bind |= PIPE_BIND_TRANSFER_READ;
67
68 info->usage = PIPE_USAGE_DEFAULT;
69 if (pDesc->Usage & D3DUSAGE_DYNAMIC)
70 info->usage = PIPE_USAGE_STREAM;
71 if (pDesc->Pool == D3DPOOL_SYSTEMMEM)
72 info->usage = PIPE_USAGE_STAGING;
73
74 /* if (pDesc->Usage & D3DUSAGE_DONOTCLIP) { } */
75 /* if (pDesc->Usage & D3DUSAGE_NONSECURE) { } */
76 /* if (pDesc->Usage & D3DUSAGE_NPATCHES) { } */
77 /* if (pDesc->Usage & D3DUSAGE_POINTS) { } */
78 /* if (pDesc->Usage & D3DUSAGE_RTPATCHES) { } */
79 if (pDesc->Usage & D3DUSAGE_SOFTWAREPROCESSING)
80 DBG("Application asked for Software Vertex Processing, "
81 "but this is unimplemented\n");
82 /* if (pDesc->Usage & D3DUSAGE_TEXTAPI) { } */
83
84 info->height0 = 1;
85 info->depth0 = 1;
86 info->array_size = 1;
87 info->last_level = 0;
88 info->nr_samples = 0;
89
90 hr = NineResource9_ctor(&This->base, pParams, NULL, TRUE,
91 D3DRTYPE_VERTEXBUFFER, pDesc->Pool, pDesc->Usage);
92 if (FAILED(hr))
93 return hr;
94
95 pDesc->Type = D3DRTYPE_VERTEXBUFFER;
96 pDesc->Format = D3DFMT_VERTEXDATA;
97 This->desc = *pDesc;
98
99 return D3D_OK;
100 }
101
102 void
103 NineVertexBuffer9_dtor( struct NineVertexBuffer9 *This )
104 {
105 if (This->maps) {
106 while (This->nmaps) {
107 NineVertexBuffer9_Unlock(This);
108 }
109 FREE(This->maps);
110 }
111
112 NineResource9_dtor(&This->base);
113 }
114
115 HRESULT WINAPI
116 NineVertexBuffer9_Lock( struct NineVertexBuffer9 *This,
117 UINT OffsetToLock,
118 UINT SizeToLock,
119 void **ppbData,
120 DWORD Flags )
121 {
122 struct pipe_box box;
123 void *data;
124 const unsigned usage = d3dlock_buffer_to_pipe_transfer_usage(Flags);
125
126 DBG("This=%p(pipe=%p) OffsetToLock=0x%x, SizeToLock=0x%x, Flags=0x%x\n",
127 This, This->base.resource,
128 OffsetToLock, SizeToLock, Flags);
129
130 user_assert(ppbData, E_POINTER);
131 user_assert(!(Flags & ~(D3DLOCK_DISCARD |
132 D3DLOCK_DONOTWAIT |
133 D3DLOCK_NO_DIRTY_UPDATE |
134 D3DLOCK_NOSYSLOCK |
135 D3DLOCK_READONLY |
136 D3DLOCK_NOOVERWRITE)), D3DERR_INVALIDCALL);
137
138 if (This->nmaps == This->maxmaps) {
139 struct pipe_transfer **newmaps =
140 REALLOC(This->maps, sizeof(struct pipe_transfer *)*This->maxmaps,
141 sizeof(struct pipe_transfer *)*(This->maxmaps << 1));
142 if (newmaps == NULL)
143 return E_OUTOFMEMORY;
144
145 This->maxmaps <<= 1;
146 This->maps = newmaps;
147 }
148
149 if (SizeToLock == 0) {
150 SizeToLock = This->desc.Size - OffsetToLock;
151 user_warn(OffsetToLock != 0);
152 }
153
154 u_box_1d(OffsetToLock, SizeToLock, &box);
155
156 data = This->pipe->transfer_map(This->pipe, This->base.resource, 0,
157 usage, &box, &This->maps[This->nmaps]);
158 if (!data) {
159 DBG("pipe::transfer_map failed\n"
160 " usage = %x\n"
161 " box.x = %u\n"
162 " box.width = %u\n",
163 usage, box.x, box.width);
164 /* not sure what to return, msdn suggests this */
165 if (Flags & D3DLOCK_DONOTWAIT)
166 return D3DERR_WASSTILLDRAWING;
167 return D3DERR_INVALIDCALL;
168 }
169
170 This->nmaps++;
171 *ppbData = data;
172
173 return D3D_OK;
174 }
175
176 HRESULT WINAPI
177 NineVertexBuffer9_Unlock( struct NineVertexBuffer9 *This )
178 {
179 DBG("This=%p\n", This);
180
181 user_assert(This->nmaps > 0, D3DERR_INVALIDCALL);
182 This->pipe->transfer_unmap(This->pipe, This->maps[--(This->nmaps)]);
183 return D3D_OK;
184 }
185
186 HRESULT WINAPI
187 NineVertexBuffer9_GetDesc( struct NineVertexBuffer9 *This,
188 D3DVERTEXBUFFER_DESC *pDesc )
189 {
190 user_assert(pDesc, E_POINTER);
191 *pDesc = This->desc;
192 return D3D_OK;
193 }
194
195 IDirect3DVertexBuffer9Vtbl NineVertexBuffer9_vtable = {
196 (void *)NineUnknown_QueryInterface,
197 (void *)NineUnknown_AddRef,
198 (void *)NineUnknown_Release,
199 (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
200 (void *)NineResource9_SetPrivateData,
201 (void *)NineResource9_GetPrivateData,
202 (void *)NineResource9_FreePrivateData,
203 (void *)NineResource9_SetPriority,
204 (void *)NineResource9_GetPriority,
205 (void *)NineResource9_PreLoad,
206 (void *)NineResource9_GetType,
207 (void *)NineVertexBuffer9_Lock,
208 (void *)NineVertexBuffer9_Unlock,
209 (void *)NineVertexBuffer9_GetDesc
210 };
211
212 static const GUID *NineVertexBuffer9_IIDs[] = {
213 &IID_IDirect3DVertexBuffer9,
214 &IID_IDirect3DResource9,
215 &IID_IUnknown,
216 NULL
217 };
218
219 HRESULT
220 NineVertexBuffer9_new( struct NineDevice9 *pDevice,
221 D3DVERTEXBUFFER_DESC *pDesc,
222 struct NineVertexBuffer9 **ppOut )
223 {
224 NINE_DEVICE_CHILD_NEW(VertexBuffer9, ppOut, /* args */ pDevice, pDesc);
225 }