1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/*
* $RCSfile$
*
* Copyright 2000-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
* $Revision$
* $Date$
* $State$
*/
#include "StdAfx.h"
#include "D3dVertexBuffer.hpp"
D3dVertexBuffer::D3dVertexBuffer()
{
buffer = NULL;
indexBuffer = NULL;
numVertices = NULL;
numVerticesLen = 0;
isIndexPrimitive = FALSE;
nextVB = NULL;
stripLen = 0;
totalVertexCount = 0;
ctx = NULL;
next = NULL;
previous = NULL;
isPointFlagUsed = FALSE;
primitiveType = D3DPT_FORCE_DWORD;
}
D3dVertexBuffer::~D3dVertexBuffer()
{
release();
}
VOID D3dVertexBuffer::release()
{
SafeRelease(buffer);
SafeRelease(indexBuffer);
SafeDelete(numVertices);
numVerticesLen = 0;
isIndexPrimitive = FALSE;
isPointFlagUsed = FALSE;
stripLen = 0;
totalVertexCount = 0;
// recursively free the list
SafeDelete(nextVB);
}
VOID D3dVertexBuffer::render(D3dCtx *d3dCtx)
{
D3DPRIMITIVETYPE oldPrimitiveType;
BOOL renderPoint = false;
BOOL restorePointSize = false;
float oldPointSize = 1.0f;
if ((buffer != NULL) && (numVertices != NULL)) {
// device is already check for NULL in callDisplayList
LPDIRECT3DDEVICE9 device = d3dCtx->pDevice;
BOOL setAmbientLight = false;
if (((vertexFormat & D3DFVF_DIFFUSE) == 0) &&
(!d3dCtx->isLightEnable)) {
setAmbientLight = true;
if (totalVertexCount > 0) {
// This is the first Node in the list
d3dCtx->setAmbientLightMaterial();
}
}
if ((d3dCtx->pointSize > 1) &&
((d3dCtx->fillMode == D3DFILL_POINT) ||
(primitiveType == D3DPT_POINTLIST))) {
// Some driver may cull the point away if not
// set to CULL_NONE
if (!isPointFlagUsed) {
// restore point size to 1
if (debug) {
printf("VB render with pointSize %d without D3DPOINT flag set\n", d3dCtx->pointSize);
}
device->SetRenderState(D3DRS_POINTSIZE, *((LPDWORD)
&oldPointSize));
restorePointSize = true;
} else {
device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
// workaround for driver bug, otherwise you will
// see four corner points instead of one big point
// if fill mode is POINT
device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
if (d3dCtx->deviceInfo->maxPointSize < d3dCtx->pointSize) {
// Use software vertex processing mode
//device->SetRenderState(D3DRS_SOFTWAREVERTEXPROCESSING, TRUE);
device->SetSoftwareVertexProcessing(TRUE);
}
oldPrimitiveType = primitiveType;
// For Polygon D3DFill_POINT mode we need to
// temporary switch primitive to point list
primitiveType = D3DPT_POINTLIST;
renderPoint = true;
}
}
device->SetStreamSource(0, buffer,0, stride);
//device->SetVertexShader(vertexFormat);
device->SetVertexShader(NULL);
device->SetFVF(vertexFormat);
int startIdx=0;
int vc, i;
if (!isIndexPrimitive ||
((indexBuffer == NULL) && renderPoint)) {
for (i = 0; i < stripLen; i++) {
vc = numVertices[i];
device->DrawPrimitive(primitiveType,
startIdx,
getPrimitiveNum(primitiveType,vc));
startIdx += vc;
}
} else {
if (indexBuffer != NULL) {
device->SetIndices(indexBuffer);
for (i = 0; i < stripLen; i++) {
vc = numVertices[i];
device->DrawIndexedPrimitive(primitiveType,0,
0,
vcount,
startIdx,
getPrimitiveNum(primitiveType, vc));
startIdx += vc;
}
} else {
if (d3dCtx->quadIndexBufferSize > 0) {
// Index is successfully set
device->SetIndices(d3dCtx->quadIndexBuffer);
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,
0,
numVertices[0],
0,
numVertices[0] >> 1);
}
// Otherwise not enough memory when index buffer
// is created, so draw nothing.
}
}
if (setAmbientLight && (nextVB == NULL)) {
// This is the last Node in the list
d3dCtx->restoreDefaultLightMaterial();
}
if (renderPoint) {
device->SetRenderState(D3DRS_CULLMODE, d3dCtx->cullMode);
device->SetRenderState(D3DRS_FILLMODE, d3dCtx->fillMode);
/** device->SetRenderState(D3DRS_SOFTWAREVERTEXPROCESSING,
d3dCtx->softwareVertexProcessing);
**/
device->SetSoftwareVertexProcessing(d3dCtx->softwareVertexProcessing);
primitiveType = oldPrimitiveType;
} else if (restorePointSize) {
device->SetRenderState(D3DRS_POINTSIZE,
*((LPDWORD) &d3dCtx->pointSize));
}
}
if (nextVB != NULL) {
nextVB->render(d3dCtx);
}
}
VOID D3dVertexBuffer::addStride(int len)
{
if (numVerticesLen <= stripLen) {
if (numVerticesLen == 0) {
numVertices = new USHORT[1];
if (numVertices == NULL) {
D3dCtx::d3dWarning(OUTOFMEMORY);
return;
}
numVerticesLen = 1;
} else {
int size = numVerticesLen << 1;
USHORT *p = new USHORT[size];
if (p == NULL) {
D3dCtx::d3dWarning(OUTOFMEMORY);
return;
}
CopyMemory(p, numVertices, numVerticesLen*sizeof(USHORT));
delete numVertices;
numVertices = p;
numVerticesLen = size;
}
}
numVertices[stripLen++] = len;
}
/*
* This is used by Strip GeometryArray
* Replace all previously define stripLen by this one.
*/
VOID D3dVertexBuffer::addStrides(jint len, jint* strips)
{
int i = len;
if (numVerticesLen < len) {
if (numVertices) {
delete numVertices;
}
numVertices = new USHORT[len];
numVerticesLen = len;
}
USHORT *q = numVertices;
while (--i >= 0) {
*q++ = *strips++;
}
stripLen = len;
}
/*
* This is used by D3dDisplayList optimize()
* Append this one to the current strip define.
*/
VOID D3dVertexBuffer::appendStrides(jint len, USHORT* strips)
{
int i;
USHORT *oldVertices;
if (numVerticesLen < stripLen + len) {
oldVertices = numVertices;
numVertices = new USHORT[len + stripLen];
numVerticesLen = len + stripLen;
}
USHORT *q = numVertices;
USHORT *p = oldVertices;
if (oldVertices != NULL) {
i = stripLen;
while (--i >= 0) {
*q++ = *p++;
}
delete oldVertices;
}
i = len;
while (--i >= 0) {
*q++ = *strips++;
}
stripLen = numVerticesLen;
}
|