st/nine: Unset vtxbuf on reset
[mesa.git] / src / gallium / state_trackers / nine / indexbuffer9.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 "indexbuffer9.h"
24 #include "device9.h"
25 #include "nine_helpers.h"
26 #include "nine_pipe.h"
27 #include "nine_dump.h"
28
29 #include "pipe/p_screen.h"
30 #include "pipe/p_context.h"
31 #include "pipe/p_state.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_format.h"
34 #include "util/u_box.h"
35
36 #define DBG_CHANNEL DBG_INDEXBUFFER
37
38 HRESULT
39 NineIndexBuffer9_ctor( struct NineIndexBuffer9 *This,
40 struct NineUnknownParams *pParams,
41 D3DINDEXBUFFER_DESC *pDesc )
42 {
43 struct pipe_resource *info = &This->base.info;
44 HRESULT hr;
45 DBG("This=%p pParams=%p pDesc=%p Usage=%s\n",
46 This, pParams, pDesc, nine_D3DUSAGE_to_str(pDesc->Usage));
47
48 This->pipe = pParams->device->pipe;
49
50 info->screen = pParams->device->screen;
51 info->target = PIPE_BUFFER;
52 info->format = PIPE_FORMAT_R8_UNORM;
53 info->width0 = pDesc->Size;
54 info->flags = 0;
55
56 info->bind = PIPE_BIND_INDEX_BUFFER | PIPE_BIND_TRANSFER_WRITE;
57 if (!(pDesc->Usage & D3DUSAGE_WRITEONLY))
58 info->bind |= PIPE_BIND_TRANSFER_READ;
59
60 info->usage = PIPE_USAGE_DEFAULT;
61 if (pDesc->Usage & D3DUSAGE_DYNAMIC)
62 info->usage = PIPE_USAGE_STREAM;
63 if (pDesc->Pool == D3DPOOL_SYSTEMMEM)
64 info->usage = PIPE_USAGE_STAGING;
65
66 /* if (pDesc->Usage & D3DUSAGE_DONOTCLIP) { } */
67 /* if (pDesc->Usage & D3DUSAGE_NONSECURE) { } */
68 /* if (pDesc->Usage & D3DUSAGE_NPATCHES) { } */
69 /* if (pDesc->Usage & D3DUSAGE_POINTS) { } */
70 /* if (pDesc->Usage & D3DUSAGE_RTPATCHES) { } */
71 if (pDesc->Usage & D3DUSAGE_SOFTWAREPROCESSING)
72 DBG("Application asked for Software Vertex Processing, "
73 "but this is unimplemented\n");
74
75 info->height0 = 1;
76 info->depth0 = 1;
77 info->array_size = 1;
78 info->last_level = 0;
79 info->nr_samples = 0;
80
81 hr = NineResource9_ctor(&This->base, pParams, NULL, TRUE, D3DRTYPE_INDEXBUFFER,
82 pDesc->Pool, pDesc->Usage);
83 if (FAILED(hr))
84 return hr;
85
86 This->buffer.buffer = This->base.resource;
87 This->buffer.offset = 0;
88 This->map_count = 0;
89
90 switch (pDesc->Format) {
91 case D3DFMT_INDEX16: This->buffer.index_size = 2; break;
92 case D3DFMT_INDEX32: This->buffer.index_size = 4; break;
93 default:
94 user_assert(!"Invalid index format.", D3DERR_INVALIDCALL);
95 break;
96 }
97 This->buffer.user_buffer = NULL;
98
99 pDesc->Type = D3DRTYPE_INDEXBUFFER;
100 This->desc = *pDesc;
101
102 return D3D_OK;
103 }
104
105 void
106 NineIndexBuffer9_dtor( struct NineIndexBuffer9 *This )
107 {
108 if (This->transfer) { NineIndexBuffer9_Unlock(This); }
109
110 NineResource9_dtor(&This->base);
111 }
112
113 const struct pipe_index_buffer *
114 NineIndexBuffer9_GetBuffer( struct NineIndexBuffer9 *This )
115 {
116 return &This->buffer;
117 }
118
119 HRESULT WINAPI
120 NineIndexBuffer9_Lock( struct NineIndexBuffer9 *This,
121 UINT OffsetToLock,
122 UINT SizeToLock,
123 void **ppbData,
124 DWORD Flags )
125 {
126 struct pipe_box box;
127 void *data;
128 UINT count;
129 const unsigned usage = d3dlock_buffer_to_pipe_transfer_usage(Flags);
130
131 DBG("This=%p OffsetToLock=%u SizeToLock=%u ppbData=%p Flags=%i "
132 "transfer=%p map_count=%u\n", This, OffsetToLock,
133 SizeToLock, ppbData, Flags, This->transfer, This->map_count);
134
135 count = ++This->map_count;
136
137 if (SizeToLock == 0) {
138 SizeToLock = This->desc.Size - OffsetToLock;
139 user_warn(OffsetToLock != 0);
140 }
141
142 u_box_1d(OffsetToLock, SizeToLock, &box);
143
144 if (unlikely(count != 1)) {
145 DBG("Lock has been called on already locked buffer."
146 "Unmapping before mapping again.");
147 This->pipe->transfer_unmap(This->pipe, This->transfer);
148 }
149 data = This->pipe->transfer_map(This->pipe, This->base.resource, 0,
150 usage, &box, &This->transfer);
151 if (!This->transfer) {
152 DBG("pipe::transfer_map failed\n"
153 " usage = %u\n"
154 " box.x = %u\n"
155 " box.width = %u\n",
156 usage, box.x, box.width);
157 }
158 *ppbData = data;
159 DBG("Returning memory at %p at address %p\n", *ppbData, ppbData);
160
161 return D3D_OK;
162 }
163
164 HRESULT WINAPI
165 NineIndexBuffer9_Unlock( struct NineIndexBuffer9 *This )
166 {
167 DBG("This=%p\n", This);
168 if (!This->map_count) {
169 DBG("Unmap called without a previous map call.\n");
170 return D3D_OK;
171 }
172 if (--This->map_count) {
173 DBG("Ignoring unmap.\n");
174 return D3D_OK;
175 }
176 This->pipe->transfer_unmap(This->pipe, This->transfer);
177 This->transfer = NULL;
178 return D3D_OK;
179 }
180
181 HRESULT WINAPI
182 NineIndexBuffer9_GetDesc( struct NineIndexBuffer9 *This,
183 D3DINDEXBUFFER_DESC *pDesc )
184 {
185 user_assert(pDesc, E_POINTER);
186 *pDesc = This->desc;
187 return D3D_OK;
188 }
189
190 IDirect3DIndexBuffer9Vtbl NineIndexBuffer9_vtable = {
191 (void *)NineUnknown_QueryInterface,
192 (void *)NineUnknown_AddRef,
193 (void *)NineUnknown_Release,
194 (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
195 (void *)NineResource9_SetPrivateData,
196 (void *)NineResource9_GetPrivateData,
197 (void *)NineResource9_FreePrivateData,
198 (void *)NineResource9_SetPriority,
199 (void *)NineResource9_GetPriority,
200 (void *)NineResource9_PreLoad,
201 (void *)NineResource9_GetType,
202 (void *)NineIndexBuffer9_Lock,
203 (void *)NineIndexBuffer9_Unlock,
204 (void *)NineIndexBuffer9_GetDesc
205 };
206
207 static const GUID *NineIndexBuffer9_IIDs[] = {
208 &IID_IDirect3DIndexBuffer9,
209 &IID_IDirect3DResource9,
210 &IID_IUnknown,
211 NULL
212 };
213
214 HRESULT
215 NineIndexBuffer9_new( struct NineDevice9 *pDevice,
216 D3DINDEXBUFFER_DESC *pDesc,
217 struct NineIndexBuffer9 **ppOut )
218 {
219 NINE_DEVICE_CHILD_NEW(IndexBuffer9, ppOut, /* args */ pDevice, pDesc);
220 }