2
|
1 |
/*
|
887
|
2 |
* Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
887
|
26 |
#include "D3DPipeline.h"
|
2
|
27 |
#include "jlong.h"
|
|
28 |
|
|
29 |
#include "GraphicsPrimitiveMgr.h"
|
887
|
30 |
#include "D3DContext.h"
|
2
|
31 |
#include "D3DSurfaceData.h"
|
887
|
32 |
#include "D3DBufImgOps.h"
|
|
33 |
#include "D3DPaints.h"
|
|
34 |
#include "D3DRenderQueue.h"
|
|
35 |
#include "D3DShaders.h"
|
|
36 |
#include "D3DTextRenderer.h"
|
|
37 |
#include "D3DPipelineManager.h"
|
|
38 |
#include "D3DGlyphCache.h"
|
2
|
39 |
|
|
40 |
typedef struct {
|
|
41 |
D3DBLEND src;
|
|
42 |
D3DBLEND dst;
|
|
43 |
} D3DBlendRule;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* This table contains the standard blending rules (or Porter-Duff compositing
|
|
47 |
* factors) used in SetRenderState(), indexed by the rule constants from the
|
|
48 |
* AlphaComposite class.
|
|
49 |
*/
|
|
50 |
D3DBlendRule StdBlendRules[] = {
|
|
51 |
{ D3DBLEND_ZERO, D3DBLEND_ZERO }, /* 0 - Nothing */
|
|
52 |
{ D3DBLEND_ZERO, D3DBLEND_ZERO }, /* 1 - RULE_Clear */
|
|
53 |
{ D3DBLEND_ONE, D3DBLEND_ZERO }, /* 2 - RULE_Src */
|
|
54 |
{ D3DBLEND_ONE, D3DBLEND_INVSRCALPHA }, /* 3 - RULE_SrcOver */
|
|
55 |
{ D3DBLEND_INVDESTALPHA, D3DBLEND_ONE }, /* 4 - RULE_DstOver */
|
|
56 |
{ D3DBLEND_DESTALPHA, D3DBLEND_ZERO }, /* 5 - RULE_SrcIn */
|
|
57 |
{ D3DBLEND_ZERO, D3DBLEND_SRCALPHA }, /* 6 - RULE_DstIn */
|
|
58 |
{ D3DBLEND_INVDESTALPHA, D3DBLEND_ZERO }, /* 7 - RULE_SrcOut */
|
|
59 |
{ D3DBLEND_ZERO, D3DBLEND_INVSRCALPHA }, /* 8 - RULE_DstOut */
|
|
60 |
{ D3DBLEND_ZERO, D3DBLEND_ONE }, /* 9 - RULE_Dst */
|
|
61 |
{ D3DBLEND_DESTALPHA, D3DBLEND_INVSRCALPHA }, /*10 - RULE_SrcAtop */
|
|
62 |
{ D3DBLEND_INVDESTALPHA, D3DBLEND_SRCALPHA }, /*11 - RULE_DstAtop */
|
|
63 |
{ D3DBLEND_INVDESTALPHA, D3DBLEND_INVSRCALPHA }, /*12 - RULE_AlphaXor*/
|
|
64 |
};
|
|
65 |
|
887
|
66 |
void
|
|
67 |
D3DUtils_SetOrthoMatrixOffCenterLH(D3DMATRIX *m,
|
|
68 |
float width, float height)
|
|
69 |
{
|
|
70 |
ZeroMemory(m, sizeof(D3DMATRIX));
|
|
71 |
m->_11 = 2.0f/width;
|
|
72 |
m->_22 = -2.0f/height;
|
|
73 |
m->_33 = 0.5f;
|
|
74 |
m->_44 = 1.0f;
|
|
75 |
|
|
76 |
m->_41 = -1.0f;
|
|
77 |
m->_42 = 1.0f;
|
|
78 |
m->_43 = 0.5f;
|
|
79 |
}
|
|
80 |
|
|
81 |
void
|
|
82 |
D3DUtils_SetIdentityMatrix(D3DMATRIX *m)
|
2
|
83 |
{
|
887
|
84 |
m->_12 = m->_13 = m->_14 = m->_21 = m->_23 = m->_24 = 0.0f;
|
|
85 |
m->_31 = m->_32 = m->_34 = m->_41 = m->_42 = m->_43 = 0.0f;
|
|
86 |
m->_11 = m->_22 = m->_33 = m->_44 = 1.0f;
|
|
87 |
}
|
|
88 |
|
|
89 |
// the following methods are copies of the AffineTransform's class
|
|
90 |
// corresponding methods, with these changes to the indexes:
|
|
91 |
// 00 -> 11
|
|
92 |
// 11 -> 22
|
|
93 |
// 01 -> 21
|
|
94 |
// 10 -> 12
|
|
95 |
// 02 -> 41
|
|
96 |
// 12 -> 42
|
|
97 |
|
|
98 |
void
|
|
99 |
D3DUtils_2DConcatenateM(D3DMATRIX *m, D3DMATRIX *m1)
|
|
100 |
{
|
|
101 |
float M0, M1;
|
|
102 |
float T00, T10, T01, T11;
|
|
103 |
float T02, T12;
|
|
104 |
|
|
105 |
T00 = m1->_11; T01 = m1->_21; T02 = m1->_41;
|
|
106 |
T10 = m1->_12; T11 = m1->_22; T12 = m1->_42;
|
|
107 |
|
|
108 |
M0 = m->_11;
|
|
109 |
M1 = m->_21;
|
|
110 |
m->_11 = T00 * M0 + T10 * M1;
|
|
111 |
m->_21 = T01 * M0 + T11 * M1;
|
|
112 |
m->_41 += T02 * M0 + T12 * M1;
|
|
113 |
|
|
114 |
M0 = m->_12;
|
|
115 |
M1 = m->_22;
|
|
116 |
m->_12 = T00 * M0 + T10 * M1;
|
|
117 |
m->_22 = T01 * M0 + T11 * M1;
|
|
118 |
m->_42 += T02 * M0 + T12 * M1;
|
2
|
119 |
}
|
|
120 |
|
887
|
121 |
#ifdef UPDATE_TX
|
|
122 |
|
|
123 |
void
|
|
124 |
D3DUtils_2DScaleM(D3DMATRIX *m, float sx, float sy)
|
|
125 |
{
|
|
126 |
m->_11 *= sx;
|
|
127 |
m->_22 *= sy;
|
|
128 |
}
|
|
129 |
|
|
130 |
void
|
|
131 |
D3DUtils_2DInvertM(D3DMATRIX *m)
|
2
|
132 |
{
|
887
|
133 |
float M11, M21, M41;
|
|
134 |
float M12, M22, M42;
|
|
135 |
float det;
|
2
|
136 |
|
887
|
137 |
M11 = m->_11; M21 = m->_21; M41 = m->_41;
|
|
138 |
M12 = m->_12; M22 = m->_22; M42 = m->_42;
|
|
139 |
det = M11 * M22 - M21 * M12;
|
|
140 |
if (fabs(det) <= 0.0000000001f) {
|
|
141 |
memset(m, 0, sizeof(D3DMATRIX));
|
|
142 |
return;
|
|
143 |
}
|
|
144 |
m->_11 = M22 / det;
|
|
145 |
m->_12 = -M12 / det;
|
|
146 |
m->_21 = -M21 / det;
|
|
147 |
m->_22 = M11 / det;
|
|
148 |
m->_41 = (M21 * M42 - M22 * M41) / det;
|
|
149 |
m->_42 = (M12 * M41 - M11 * M42) / det;
|
|
150 |
}
|
2
|
151 |
|
887
|
152 |
void
|
|
153 |
D3DUtils_2DTranslateM(D3DMATRIX *m, float tx, float ty)
|
|
154 |
{
|
|
155 |
m->_41 = tx * m->_11 + ty * m->_21 + m->_41;
|
|
156 |
m->_42 = tx * m->_12 + ty * m->_22 + m->_42;
|
|
157 |
}
|
|
158 |
|
|
159 |
void
|
|
160 |
D3DUtils_2DTransformXY(D3DMATRIX *m, float *px, float *py)
|
|
161 |
{
|
|
162 |
float x = *px;
|
|
163 |
float y = *py;
|
|
164 |
|
|
165 |
*px = x * m->_11 + y * m->_21 + m->_41;
|
|
166 |
*py = x * m->_12 + y * m->_22 + m->_42;
|
|
167 |
}
|
|
168 |
|
|
169 |
void
|
|
170 |
D3DUtils_2DInverseTransformXY(D3DMATRIX *m, float *px, float *py)
|
|
171 |
{
|
|
172 |
float x = *px, y = *py;
|
|
173 |
|
|
174 |
x -= m->_41;
|
|
175 |
y -= m->_42;
|
|
176 |
|
|
177 |
float det = m->_11 * m->_22 - m->_21 * m->_12;
|
|
178 |
if (fabs(det) < 0.0000000001f) {
|
|
179 |
*px = 0.0f;
|
|
180 |
*py = 0.0f;
|
2
|
181 |
} else {
|
887
|
182 |
*px = (x * m->_22 - y * m->_21) / det;
|
|
183 |
*py = (y * m->_11 - x * m->_12) / det;
|
2
|
184 |
}
|
|
185 |
}
|
|
186 |
|
887
|
187 |
#endif // UPDATE_TX
|
|
188 |
|
|
189 |
static void
|
|
190 |
D3DContext_DisposeShader(jlong programID)
|
|
191 |
{
|
|
192 |
IDirect3DPixelShader9 *shader =
|
|
193 |
(IDirect3DPixelShader9 *)jlong_to_ptr(programID);
|
|
194 |
|
|
195 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_DisposeShader");
|
|
196 |
|
|
197 |
SAFE_RELEASE(shader);
|
|
198 |
}
|
|
199 |
|
|
200 |
// static
|
|
201 |
HRESULT
|
|
202 |
D3DContext::CreateInstance(IDirect3D9 *pd3d9, UINT adapter, D3DContext **ppCtx)
|
|
203 |
{
|
|
204 |
HRESULT res;
|
|
205 |
*ppCtx = new D3DContext(pd3d9, adapter);
|
|
206 |
if (FAILED(res = (*ppCtx)->InitContext())) {
|
|
207 |
delete *ppCtx;
|
|
208 |
*ppCtx = NULL;
|
|
209 |
}
|
|
210 |
return res;
|
|
211 |
}
|
2
|
212 |
|
887
|
213 |
D3DContext::D3DContext(IDirect3D9 *pd3d, UINT adapter)
|
|
214 |
{
|
|
215 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::D3DContext");
|
|
216 |
J2dTraceLn1(J2D_TRACE_VERBOSE, " pd3d=0x%x", pd3d);
|
|
217 |
pd3dObject = pd3d;
|
|
218 |
pd3dDevice = NULL;
|
|
219 |
adapterOrdinal = adapter;
|
|
220 |
|
|
221 |
pResourceMgr = NULL;
|
|
222 |
pMaskCache = NULL;
|
|
223 |
pVCacher = NULL;
|
|
224 |
|
|
225 |
pSyncQuery = NULL;
|
|
226 |
pSyncRTRes = NULL;
|
|
227 |
pStateBlock = NULL;
|
2
|
228 |
|
887
|
229 |
D3DC_INIT_SHADER_LIST(convolvePrograms, MAX_CONVOLVE);
|
|
230 |
D3DC_INIT_SHADER_LIST(rescalePrograms, MAX_RESCALE);
|
|
231 |
D3DC_INIT_SHADER_LIST(lookupPrograms, MAX_LOOKUP);
|
|
232 |
D3DC_INIT_SHADER_LIST(basicGradPrograms, 4);
|
|
233 |
D3DC_INIT_SHADER_LIST(linearGradPrograms, 8);
|
|
234 |
D3DC_INIT_SHADER_LIST(radialGradPrograms, 8);
|
|
235 |
|
|
236 |
pLCDGlyphCache= NULL;
|
|
237 |
pGrayscaleGlyphCache= NULL;
|
|
238 |
lcdTextProgram = NULL;
|
|
239 |
aaPgramProgram = NULL;
|
|
240 |
|
|
241 |
contextCaps = CAPS_EMPTY;
|
|
242 |
bBeginScenePending = FALSE;
|
|
243 |
|
|
244 |
ZeroMemory(&devCaps, sizeof(D3DCAPS9));
|
|
245 |
ZeroMemory(&curParams, sizeof(curParams));
|
|
246 |
|
|
247 |
extraAlpha = 1.0f;
|
2
|
248 |
}
|
|
249 |
|
887
|
250 |
void D3DContext::ReleaseDefPoolResources()
|
|
251 |
{
|
|
252 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::ReleaseDefPoolResources");
|
2
|
253 |
|
887
|
254 |
EndScene();
|
2
|
255 |
|
887
|
256 |
D3DPipelineManager::NotifyAdapterEventListeners(devCaps.AdapterOrdinal,
|
|
257 |
DEVICE_RESET);
|
2
|
258 |
|
887
|
259 |
contextCaps = CAPS_EMPTY;
|
2
|
260 |
|
887
|
261 |
SAFE_RELEASE(pSyncQuery);
|
|
262 |
SAFE_RELEASE(pStateBlock);
|
2
|
263 |
|
887
|
264 |
if (pVCacher != NULL) {
|
|
265 |
pVCacher->ReleaseDefPoolResources();
|
|
266 |
}
|
|
267 |
if (pMaskCache != NULL) {
|
|
268 |
pMaskCache->ReleaseDefPoolResources();
|
|
269 |
}
|
|
270 |
if (pLCDGlyphCache != NULL) {
|
|
271 |
pLCDGlyphCache->ReleaseDefPoolResources();
|
2
|
272 |
}
|
887
|
273 |
if (pGrayscaleGlyphCache != NULL) {
|
|
274 |
pGrayscaleGlyphCache->ReleaseDefPoolResources();
|
|
275 |
}
|
|
276 |
if (pResourceMgr != NULL) {
|
|
277 |
if (pSyncRTRes != NULL) {
|
|
278 |
pResourceMgr->ReleaseResource(pSyncRTRes);
|
|
279 |
pSyncRTRes = NULL;
|
|
280 |
}
|
|
281 |
pResourceMgr->ReleaseDefPoolResources();
|
|
282 |
}
|
|
283 |
ZeroMemory(lastTexture, sizeof(lastTexture));
|
|
284 |
ZeroMemory(lastTextureColorState, sizeof(lastTextureColorState));
|
|
285 |
}
|
2
|
286 |
|
887
|
287 |
void D3DContext::ReleaseContextResources()
|
|
288 |
{
|
|
289 |
J2dTraceLn1(J2D_TRACE_INFO,
|
|
290 |
"D3DContext::ReleaseContextResources: pd3dDevice = 0x%x",
|
|
291 |
pd3dDevice);
|
|
292 |
|
|
293 |
ReleaseDefPoolResources();
|
|
294 |
|
|
295 |
D3DPipelineManager::NotifyAdapterEventListeners(devCaps.AdapterOrdinal,
|
|
296 |
DEVICE_DISPOSED);
|
|
297 |
|
|
298 |
// dispose shader lists
|
|
299 |
ShaderList_Dispose(&convolvePrograms);
|
|
300 |
ShaderList_Dispose(&rescalePrograms);
|
|
301 |
ShaderList_Dispose(&lookupPrograms);
|
|
302 |
ShaderList_Dispose(&basicGradPrograms);
|
|
303 |
ShaderList_Dispose(&linearGradPrograms);
|
|
304 |
ShaderList_Dispose(&radialGradPrograms);
|
|
305 |
|
|
306 |
SAFE_DELETE(pLCDGlyphCache);
|
|
307 |
SAFE_DELETE(pGrayscaleGlyphCache);
|
|
308 |
|
|
309 |
SAFE_RELEASE(lcdTextProgram);
|
|
310 |
SAFE_RELEASE(aaPgramProgram);
|
|
311 |
|
|
312 |
SAFE_DELETE(pVCacher);
|
|
313 |
SAFE_DELETE(pMaskCache);
|
|
314 |
SAFE_DELETE(pResourceMgr);
|
2
|
315 |
}
|
|
316 |
|
|
317 |
D3DContext::~D3DContext() {
|
|
318 |
J2dTraceLn2(J2D_TRACE_INFO,
|
887
|
319 |
"~D3DContext: pd3dDevice=0x%x, pd3dObject =0x%x",
|
|
320 |
pd3dDevice, pd3dObject);
|
|
321 |
ReleaseContextResources();
|
|
322 |
SAFE_RELEASE(pd3dDevice);
|
2
|
323 |
}
|
|
324 |
|
|
325 |
HRESULT
|
887
|
326 |
D3DContext::InitDevice(IDirect3DDevice9 *pd3dDevice)
|
2
|
327 |
{
|
887
|
328 |
HRESULT res = S_OK;
|
|
329 |
|
|
330 |
pd3dDevice->GetDeviceCaps(&devCaps);
|
|
331 |
|
2
|
332 |
J2dRlsTraceLn1(J2D_TRACE_INFO,
|
887
|
333 |
"D3DContext::InitDevice: device %d", adapterOrdinal);
|
2
|
334 |
|
|
335 |
// disable some of the unneeded and costly d3d functionality
|
887
|
336 |
pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
|
337 |
pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, FALSE);
|
|
338 |
pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
|
|
339 |
pd3dDevice->SetRenderState(D3DRS_CLIPPING, FALSE);
|
|
340 |
pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
|
|
341 |
pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, D3DZB_FALSE);
|
|
342 |
pd3dDevice->SetRenderState(D3DRS_COLORVERTEX, FALSE);
|
|
343 |
pd3dDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
|
2
|
344 |
|
887
|
345 |
// set the default texture addressing mode
|
|
346 |
pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
|
|
347 |
pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
|
|
348 |
|
|
349 |
// REMIND: check supported filters with
|
|
350 |
// IDirect3D9::CheckDeviceFormat with D3DUSAGE_QUERY_FILTER
|
|
351 |
pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
|
352 |
pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
2
|
353 |
|
|
354 |
// these states never change
|
887
|
355 |
pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
|
|
356 |
pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
|
|
357 |
pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
|
|
358 |
pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
|
|
359 |
pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
|
|
360 |
pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
|
|
361 |
pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
|
|
362 |
pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
|
|
363 |
|
2
|
364 |
// init the array of latest textures
|
887
|
365 |
ZeroMemory(lastTexture, sizeof(lastTexture));
|
|
366 |
ZeroMemory(lastTextureColorState, sizeof(lastTextureColorState));
|
|
367 |
|
|
368 |
opState = STATE_CHANGE;
|
|
369 |
|
|
370 |
if (pResourceMgr == NULL) {
|
|
371 |
res = D3DResourceManager::CreateInstance(this, &pResourceMgr);
|
|
372 |
} else {
|
|
373 |
res = pResourceMgr->Init(this);
|
|
374 |
}
|
|
375 |
RETURN_STATUS_IF_FAILED(res);
|
|
376 |
|
|
377 |
if (pVCacher == NULL) {
|
|
378 |
res = D3DVertexCacher::CreateInstance(this, &pVCacher);
|
|
379 |
} else {
|
|
380 |
res = pVCacher->Init(this);
|
|
381 |
}
|
|
382 |
RETURN_STATUS_IF_FAILED(res);
|
|
383 |
|
|
384 |
if (pMaskCache == NULL) {
|
|
385 |
res = D3DMaskCache::CreateInstance(this, &pMaskCache);
|
|
386 |
} else{
|
|
387 |
res = pMaskCache->Init(this);
|
|
388 |
}
|
|
389 |
RETURN_STATUS_IF_FAILED(res);
|
|
390 |
|
|
391 |
if (pLCDGlyphCache != NULL) {
|
|
392 |
if (FAILED(res = pLCDGlyphCache->Init(this))) {
|
|
393 |
// we can live without the cache
|
|
394 |
SAFE_DELETE(pLCDGlyphCache);
|
|
395 |
res = S_OK;
|
|
396 |
}
|
|
397 |
}
|
|
398 |
|
|
399 |
if (pGrayscaleGlyphCache != NULL) {
|
|
400 |
if (FAILED(res = pGrayscaleGlyphCache->Init(this))) {
|
|
401 |
// we can live without the cache
|
|
402 |
SAFE_DELETE(pGrayscaleGlyphCache);
|
|
403 |
res = S_OK;
|
|
404 |
}
|
|
405 |
}
|
2
|
406 |
|
|
407 |
D3DMATRIX tx;
|
|
408 |
D3DUtils_SetIdentityMatrix(&tx);
|
887
|
409 |
pd3dDevice->SetTransform(D3DTS_WORLD, &tx);
|
|
410 |
bIsIdentityTx = TRUE;
|
|
411 |
|
|
412 |
if (pSyncQuery == NULL) {
|
|
413 |
// this is allowed to fail, do not propagate the error
|
|
414 |
if (FAILED(pd3dDevice->CreateQuery(D3DQUERYTYPE_EVENT, &pSyncQuery))) {
|
|
415 |
J2dRlsTraceLn(J2D_TRACE_WARNING,
|
|
416 |
"D3DContext::InitDevice: sync query not available");
|
|
417 |
pSyncQuery = NULL;
|
|
418 |
}
|
|
419 |
}
|
|
420 |
if (pSyncRTRes == NULL) {
|
|
421 |
D3DFORMAT format;
|
|
422 |
if (FAILED(GetResourceManager()->
|
|
423 |
CreateRTSurface(32, 32, TRUE, TRUE, &format, &pSyncRTRes))) {
|
|
424 |
J2dRlsTraceLn(J2D_TRACE_WARNING,
|
|
425 |
"D3DContext::InitDevice: "
|
|
426 |
"error creating sync surface");
|
|
427 |
}
|
|
428 |
}
|
2
|
429 |
|
|
430 |
bBeginScenePending = FALSE;
|
|
431 |
|
887
|
432 |
J2dRlsTraceLn1(J2D_TRACE_INFO,
|
|
433 |
"D3DContext::InitDefice: successfully initialized device %d",
|
|
434 |
adapterOrdinal);
|
|
435 |
|
|
436 |
return res;
|
|
437 |
}
|
|
438 |
|
|
439 |
HRESULT
|
|
440 |
D3DContext::CheckAndResetDevice()
|
|
441 |
{
|
|
442 |
HRESULT res = E_FAIL;
|
|
443 |
|
|
444 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::CheckAndResetDevice");
|
2
|
445 |
|
887
|
446 |
if (pd3dDevice != NULL) {
|
|
447 |
if (FAILED(res = pd3dDevice->TestCooperativeLevel())) {
|
|
448 |
if (res == D3DERR_DEVICELOST) {
|
|
449 |
J2dTraceLn1(J2D_TRACE_VERBOSE, " device %d is still lost",
|
|
450 |
adapterOrdinal);
|
|
451 |
// nothing to be done here, wait for D3DERR_DEVICENOTRESET
|
|
452 |
return res;
|
|
453 |
} else if (res == D3DERR_DEVICENOTRESET) {
|
|
454 |
J2dTraceLn1(J2D_TRACE_VERBOSE, " device %d needs to be reset",
|
|
455 |
adapterOrdinal);
|
|
456 |
res = ResetContext();
|
|
457 |
} else {
|
|
458 |
// some unexpected error
|
|
459 |
DebugPrintD3DError(res, "D3DContext::CheckAndResetDevice: "\
|
|
460 |
"unknown error %x from TestCooperativeLevel");
|
|
461 |
}
|
|
462 |
} else {
|
|
463 |
J2dTraceLn1(J2D_TRACE_VERBOSE, " device %d is not lost",
|
|
464 |
adapterOrdinal);
|
2
|
465 |
}
|
|
466 |
} else {
|
887
|
467 |
J2dTraceLn(J2D_TRACE_VERBOSE, " null device");
|
|
468 |
}
|
|
469 |
return res;
|
|
470 |
}
|
|
471 |
|
|
472 |
HRESULT
|
|
473 |
D3DContext::ResetContext()
|
|
474 |
{
|
|
475 |
HRESULT res = E_FAIL;
|
|
476 |
|
|
477 |
J2dRlsTraceLn(J2D_TRACE_INFO, "D3DContext::ResetContext");
|
|
478 |
if (pd3dDevice != NULL) {
|
|
479 |
D3DPRESENT_PARAMETERS newParams;
|
|
480 |
|
|
481 |
newParams = curParams;
|
|
482 |
|
|
483 |
if (newParams.Windowed) {
|
|
484 |
// reset to the current display mode if we're windowed,
|
|
485 |
// otherwise to the display mode we were in when the device
|
|
486 |
// was lost
|
|
487 |
newParams.BackBufferFormat = D3DFMT_UNKNOWN;
|
|
488 |
newParams.FullScreen_RefreshRateInHz = 0;
|
|
489 |
newParams.BackBufferWidth = 0;
|
|
490 |
newParams.BackBufferHeight = 0;
|
|
491 |
}
|
|
492 |
res = ConfigureContext(&newParams);
|
2
|
493 |
}
|
|
494 |
return res;
|
|
495 |
}
|
|
496 |
|
|
497 |
HRESULT
|
887
|
498 |
D3DContext::ConfigureContext(D3DPRESENT_PARAMETERS *pNewParams)
|
2
|
499 |
{
|
887
|
500 |
J2dRlsTraceLn1(J2D_TRACE_INFO, "D3DContext::ConfigureContext device %d",
|
|
501 |
adapterOrdinal);
|
|
502 |
HRESULT res = S_OK;
|
|
503 |
D3DFORMAT stencilFormat;
|
|
504 |
HWND focusHWND = D3DPipelineManager::GetInstance()->GetCurrentFocusWindow();
|
|
505 |
D3DDEVTYPE devType = D3DPipelineManager::GetInstance()->GetDeviceType();
|
|
506 |
// this is needed so that we can find the stencil buffer format
|
|
507 |
if (pNewParams->BackBufferFormat == D3DFMT_UNKNOWN) {
|
|
508 |
D3DDISPLAYMODE dm;
|
|
509 |
|
|
510 |
pd3dObject->GetAdapterDisplayMode(adapterOrdinal, &dm);
|
|
511 |
pNewParams->BackBufferFormat = dm.Format;
|
|
512 |
}
|
|
513 |
|
|
514 |
stencilFormat =
|
|
515 |
D3DPipelineManager::GetInstance()->GetMatchingDepthStencilFormat(
|
|
516 |
adapterOrdinal,
|
|
517 |
pNewParams->BackBufferFormat, pNewParams->BackBufferFormat);
|
|
518 |
|
|
519 |
pNewParams->EnableAutoDepthStencil = TRUE;
|
|
520 |
pNewParams->AutoDepthStencilFormat = stencilFormat;
|
|
521 |
|
|
522 |
// do not set device window in the windowed mode, we use additional
|
|
523 |
// swap chains for rendering, the default chain is not used. otherwise
|
|
524 |
// our scratch focus window will be made visible
|
|
525 |
J2dTraceLn1(J2D_TRACE_VERBOSE, " windowed=%d",pNewParams->Windowed);
|
|
526 |
if (pNewParams->Windowed) {
|
|
527 |
pNewParams->hDeviceWindow = (HWND)0;
|
|
528 |
}
|
|
529 |
|
|
530 |
// The focus window may change when we're entering/exiting the full-screen
|
|
531 |
// mode. It may either be set to the default focus window (when there are
|
|
532 |
// no more devices in fs mode), or to fs window for another device
|
|
533 |
// in fs mode. See D3DPipelineManager::GetCurrentFocusWindow.
|
|
534 |
if (pd3dDevice != NULL) {
|
|
535 |
D3DDEVICE_CREATION_PARAMETERS cParams;
|
|
536 |
pd3dDevice->GetCreationParameters(&cParams);
|
|
537 |
if (cParams.hFocusWindow != focusHWND) {
|
|
538 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
539 |
" focus window changed, need to recreate the device");
|
|
540 |
|
|
541 |
// if fs -> windowed, first exit fs, then recreate, otherwise
|
|
542 |
// the screen might be left in a different display mode
|
|
543 |
if (pNewParams->Windowed && !curParams.Windowed) {
|
|
544 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
545 |
" exiting full-screen mode, reset the device");
|
|
546 |
curParams.Windowed = FALSE;
|
|
547 |
ReleaseDefPoolResources();
|
|
548 |
res = pd3dDevice->Reset(&curParams);
|
|
549 |
|
|
550 |
if (FAILED(res)) {
|
|
551 |
DebugPrintD3DError(res, "D3DContext::ConfigureContext: "\
|
|
552 |
"cound not reset the device");
|
|
553 |
}
|
|
554 |
}
|
|
555 |
|
|
556 |
// note that here we should release all device resources, not only
|
|
557 |
// thos in the default pool since the device is released
|
|
558 |
ReleaseContextResources();
|
|
559 |
SAFE_RELEASE(pd3dDevice);
|
|
560 |
}
|
2
|
561 |
}
|
|
562 |
|
887
|
563 |
if (pd3dDevice != NULL) {
|
|
564 |
J2dTraceLn(J2D_TRACE_VERBOSE, " resetting the device");
|
|
565 |
|
|
566 |
ReleaseDefPoolResources();
|
|
567 |
|
|
568 |
if (pNewParams->PresentationInterval == D3DPRESENT_INTERVAL_IMMEDIATE &&
|
|
569 |
!IsImmediateIntervalSupported())
|
|
570 |
{
|
|
571 |
pNewParams->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
|
|
572 |
}
|
|
573 |
|
|
574 |
res = pd3dDevice->Reset(pNewParams);
|
|
575 |
if (FAILED(res)) {
|
|
576 |
DebugPrintD3DError(res,
|
|
577 |
"D3DContext::ConfigureContext: cound not reset the device");
|
|
578 |
return res;
|
|
579 |
}
|
|
580 |
J2dRlsTraceLn1(J2D_TRACE_INFO,
|
|
581 |
"D3DContext::ConfigureContext: successfully reset device: %d",
|
|
582 |
adapterOrdinal);
|
|
583 |
} else {
|
|
584 |
D3DCAPS9 d3dCaps;
|
|
585 |
DWORD dwBehaviorFlags;
|
|
586 |
|
|
587 |
J2dTraceLn(J2D_TRACE_VERBOSE, " creating a new device");
|
|
588 |
|
|
589 |
if (FAILED(res = pd3dObject->GetDeviceCaps(adapterOrdinal,
|
|
590 |
devType, &d3dCaps)))
|
|
591 |
{
|
|
592 |
DebugPrintD3DError(res,
|
|
593 |
"D3DContext::ConfigureContext: failed to get caps");
|
|
594 |
return res;
|
|
595 |
}
|
|
596 |
|
|
597 |
if (pNewParams->PresentationInterval == D3DPRESENT_INTERVAL_IMMEDIATE &&
|
|
598 |
!(d3dCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE))
|
|
599 |
{
|
|
600 |
pNewParams->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
|
|
601 |
}
|
2
|
602 |
|
887
|
603 |
// not preserving fpu control word could cause issues (4860749)
|
|
604 |
dwBehaviorFlags = D3DCREATE_FPU_PRESERVE;
|
|
605 |
|
|
606 |
J2dRlsTrace(J2D_TRACE_VERBOSE,
|
|
607 |
"[V] dwBehaviorFlags=D3DCREATE_FPU_PRESERVE|");
|
|
608 |
if (d3dCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) {
|
|
609 |
J2dRlsTrace(J2D_TRACE_VERBOSE,
|
|
610 |
"D3DCREATE_HARDWARE_VERTEXPROCESSING");
|
|
611 |
dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
|
|
612 |
} else {
|
|
613 |
J2dRlsTrace(J2D_TRACE_VERBOSE,
|
|
614 |
"D3DCREATE_SOFTWARE_VERTEXPROCESSING");
|
|
615 |
dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
|
|
616 |
}
|
|
617 |
// Handling focus changes by ourselves proved to be problematic,
|
|
618 |
// so we're reverting back to D3D handling
|
|
619 |
// dwBehaviorFlags |= D3DCREATE_NOWINDOWCHANGES;
|
|
620 |
J2dRlsTrace(J2D_TRACE_VERBOSE,"\n");
|
|
621 |
|
|
622 |
if (FAILED(res = pd3dObject->CreateDevice(adapterOrdinal, devType,
|
|
623 |
focusHWND,
|
|
624 |
dwBehaviorFlags,
|
|
625 |
pNewParams, &pd3dDevice)))
|
|
626 |
{
|
|
627 |
DebugPrintD3DError(res,
|
|
628 |
"D3DContext::ConfigureContext: error creating d3d device");
|
|
629 |
return res;
|
|
630 |
}
|
|
631 |
J2dRlsTraceLn1(J2D_TRACE_INFO,
|
|
632 |
"D3DContext::ConfigureContext: successfully created device: %d",
|
|
633 |
adapterOrdinal);
|
|
634 |
bIsHWRasterizer = (devType == D3DDEVTYPE_HAL);
|
|
635 |
}
|
|
636 |
|
|
637 |
curParams = *pNewParams;
|
|
638 |
// during the creation of the device d3d modifies this field, we reset
|
|
639 |
// it back to 0
|
|
640 |
curParams.Flags = 0;
|
|
641 |
|
|
642 |
if (FAILED(res = InitDevice(pd3dDevice))) {
|
|
643 |
ReleaseContextResources();
|
2
|
644 |
return res;
|
|
645 |
}
|
|
646 |
|
887
|
647 |
res = InitContextCaps();
|
|
648 |
|
|
649 |
return res;
|
|
650 |
}
|
|
651 |
|
|
652 |
HRESULT
|
|
653 |
D3DContext::InitContext()
|
|
654 |
{
|
|
655 |
J2dRlsTraceLn1(J2D_TRACE_INFO, "D3DContext::InitContext device %d",
|
|
656 |
adapterOrdinal);
|
|
657 |
|
|
658 |
D3DPRESENT_PARAMETERS params;
|
|
659 |
ZeroMemory(¶ms, sizeof(D3DPRESENT_PARAMETERS));
|
|
660 |
|
|
661 |
params.hDeviceWindow = 0;
|
|
662 |
params.Windowed = TRUE;
|
|
663 |
params.BackBufferCount = 1;
|
|
664 |
params.BackBufferFormat = D3DFMT_UNKNOWN;
|
|
665 |
params.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
|
666 |
params.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
|
|
667 |
|
|
668 |
return ConfigureContext(¶ms);
|
|
669 |
}
|
|
670 |
|
|
671 |
HRESULT
|
|
672 |
D3DContext::Sync()
|
|
673 |
{
|
|
674 |
HRESULT res = S_OK;
|
|
675 |
|
|
676 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::Sync");
|
|
677 |
|
|
678 |
if (pSyncQuery != NULL) {
|
|
679 |
J2dTrace(J2D_TRACE_VERBOSE, " flushing the device queue..");
|
|
680 |
while (S_FALSE ==
|
|
681 |
(res = pSyncQuery->GetData(NULL, 0, D3DGETDATA_FLUSH))) ;
|
|
682 |
J2dTrace(J2D_TRACE_VERBOSE, ".. done\n");
|
|
683 |
}
|
|
684 |
if (pSyncRTRes != NULL) {
|
|
685 |
D3DLOCKED_RECT lr;
|
|
686 |
IDirect3DSurface9 *pSurface = pSyncRTRes->GetSurface();
|
|
687 |
if (SUCCEEDED(pSurface->LockRect(&lr, NULL, D3DLOCK_NOSYSLOCK))) {
|
|
688 |
pSurface->UnlockRect();
|
|
689 |
}
|
|
690 |
}
|
|
691 |
return res;
|
|
692 |
}
|
|
693 |
|
|
694 |
HRESULT
|
|
695 |
D3DContext::SaveState()
|
|
696 |
{
|
|
697 |
HRESULT res;
|
|
698 |
|
|
699 |
RETURN_STATUS_IF_NULL(pd3dDevice, S_OK);
|
|
700 |
|
|
701 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::SaveState");
|
|
702 |
|
|
703 |
FlushVertexQueue();
|
|
704 |
UpdateState(STATE_CHANGE);
|
|
705 |
|
|
706 |
if (pStateBlock != NULL) {
|
|
707 |
J2dTraceLn(J2D_TRACE_WARNING,
|
|
708 |
"D3DContext::SaveState: existing state block!");
|
|
709 |
SAFE_RELEASE(pStateBlock);
|
2
|
710 |
}
|
|
711 |
|
887
|
712 |
if (SUCCEEDED(res =
|
|
713 |
pd3dDevice->CreateStateBlock(D3DSBT_ALL, &pStateBlock)))
|
|
714 |
{
|
|
715 |
J2dTraceLn(J2D_TRACE_VERBOSE, " created state block");
|
|
716 |
} else {
|
|
717 |
J2dTraceLn(J2D_TRACE_WARNING,
|
|
718 |
"D3DContext::SaveState: failed to create state block");
|
|
719 |
}
|
|
720 |
ZeroMemory(lastTexture, sizeof(lastTexture));
|
|
721 |
|
|
722 |
return res;
|
|
723 |
}
|
|
724 |
|
|
725 |
HRESULT
|
|
726 |
D3DContext::RestoreState()
|
|
727 |
{
|
|
728 |
HRESULT res = S_OK;
|
|
729 |
|
|
730 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::RestoreState");
|
|
731 |
|
|
732 |
FlushVertexQueue();
|
|
733 |
UpdateState(STATE_CHANGE);
|
|
734 |
|
|
735 |
if (pStateBlock != NULL) {
|
|
736 |
if (SUCCEEDED(res = pStateBlock->Apply())) {
|
|
737 |
J2dTraceLn(J2D_TRACE_VERBOSE, " restored device state");
|
|
738 |
} else {
|
|
739 |
J2dTraceLn(J2D_TRACE_WARNING,
|
|
740 |
"D3DContext::RestoreState: failed to restore state");
|
|
741 |
}
|
|
742 |
SAFE_RELEASE(pStateBlock);
|
|
743 |
} else {
|
|
744 |
J2dTraceLn(J2D_TRACE_WARNING,
|
|
745 |
"D3DContext::RestoreState: empty state block!");
|
|
746 |
}
|
|
747 |
ZeroMemory(lastTexture, sizeof(lastTexture));
|
|
748 |
|
|
749 |
return res;
|
|
750 |
}
|
|
751 |
|
|
752 |
#define POINT_FILTER_CAP (D3DPTFILTERCAPS_MAGFPOINT|D3DPTFILTERCAPS_MINFPOINT)
|
|
753 |
#define LINEAR_FILTER_CAP (D3DPTFILTERCAPS_MAGFLINEAR|D3DPTFILTERCAPS_MINFLINEAR)
|
|
754 |
|
|
755 |
BOOL
|
|
756 |
D3DContext::IsStretchRectFilteringSupported(D3DTEXTUREFILTERTYPE fType)
|
|
757 |
{
|
|
758 |
if (fType == D3DTEXF_POINT) {
|
|
759 |
return ((devCaps.StretchRectFilterCaps & POINT_FILTER_CAP) != 0);
|
|
760 |
}
|
|
761 |
if (fType == D3DTEXF_LINEAR) {
|
|
762 |
return ((devCaps.StretchRectFilterCaps & LINEAR_FILTER_CAP) != 0);
|
|
763 |
}
|
|
764 |
return FALSE;
|
|
765 |
}
|
|
766 |
|
|
767 |
BOOL
|
|
768 |
D3DContext::IsTextureFilteringSupported(D3DTEXTUREFILTERTYPE fType)
|
|
769 |
{
|
|
770 |
if (fType == D3DTEXF_POINT) {
|
|
771 |
return ((devCaps.TextureFilterCaps & POINT_FILTER_CAP) != 0);
|
|
772 |
}
|
|
773 |
if (fType == D3DTEXF_LINEAR) {
|
|
774 |
return ((devCaps.TextureFilterCaps & LINEAR_FILTER_CAP) != 0);
|
|
775 |
}
|
|
776 |
return FALSE;
|
|
777 |
}
|
|
778 |
|
|
779 |
BOOL
|
|
780 |
D3DContext::IsTextureFormatSupported(D3DFORMAT format, DWORD usage)
|
|
781 |
{
|
|
782 |
HRESULT hr = pd3dObject->CheckDeviceFormat(adapterOrdinal,
|
|
783 |
devCaps.DeviceType,
|
|
784 |
curParams.BackBufferFormat,
|
|
785 |
usage,
|
|
786 |
D3DRTYPE_TEXTURE,
|
|
787 |
format);
|
|
788 |
return SUCCEEDED( hr );
|
|
789 |
}
|
|
790 |
|
|
791 |
BOOL
|
|
792 |
D3DContext::IsDepthStencilBufferOk(D3DSURFACE_DESC *pTargetDesc)
|
|
793 |
{
|
|
794 |
IDirect3DSurface9 *pStencil;
|
|
795 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::IsDepthStencilBufferOk");
|
|
796 |
|
|
797 |
if (SUCCEEDED(pd3dDevice->GetDepthStencilSurface(&pStencil))) {
|
|
798 |
D3DSURFACE_DESC descStencil;
|
|
799 |
pStencil->GetDesc(&descStencil);
|
|
800 |
pStencil->Release();
|
|
801 |
|
|
802 |
D3DDISPLAYMODE dm;
|
|
803 |
return
|
|
804 |
(SUCCEEDED(pd3dDevice->GetDisplayMode(0, &dm)) &&
|
|
805 |
pTargetDesc->Width <= descStencil.Width &&
|
|
806 |
pTargetDesc->Height <= descStencil.Height &&
|
|
807 |
SUCCEEDED(pd3dObject->CheckDepthStencilMatch(
|
|
808 |
adapterOrdinal,
|
|
809 |
devCaps.DeviceType,
|
|
810 |
dm.Format, pTargetDesc->Format,
|
|
811 |
descStencil.Format)));
|
|
812 |
}
|
|
813 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
814 |
" current stencil buffer is not compatible with new Render Target");
|
|
815 |
|
|
816 |
return false;
|
|
817 |
}
|
|
818 |
|
|
819 |
|
|
820 |
|
|
821 |
HRESULT
|
|
822 |
D3DContext::InitDepthStencilBuffer(D3DSURFACE_DESC *pTargetDesc)
|
|
823 |
{
|
|
824 |
HRESULT res;
|
|
825 |
IDirect3DSurface9 *pBB;
|
|
826 |
D3DDISPLAYMODE dm;
|
|
827 |
|
|
828 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::InitDepthStencilBuffer");
|
|
829 |
|
|
830 |
if (FAILED(res = pd3dDevice->GetDisplayMode(0, &dm))) {
|
|
831 |
return res;
|
|
832 |
}
|
|
833 |
|
|
834 |
D3DFORMAT newFormat =
|
|
835 |
D3DPipelineManager::GetInstance()->GetMatchingDepthStencilFormat(
|
|
836 |
adapterOrdinal, dm.Format, pTargetDesc->Format);
|
|
837 |
|
|
838 |
res = pd3dDevice->CreateDepthStencilSurface(
|
|
839 |
pTargetDesc->Width, pTargetDesc->Height,
|
|
840 |
newFormat, D3DMULTISAMPLE_NONE, 0, false, &pBB, 0);
|
|
841 |
if (SUCCEEDED(res)) {
|
|
842 |
res = pd3dDevice->SetDepthStencilSurface(pBB);
|
|
843 |
pBB->Release();
|
2
|
844 |
}
|
|
845 |
|
|
846 |
return res;
|
|
847 |
}
|
|
848 |
|
|
849 |
|
|
850 |
HRESULT
|
887
|
851 |
D3DContext::SetRenderTarget(IDirect3DSurface9 *pSurface)
|
2
|
852 |
{
|
|
853 |
static D3DMATRIX tx;
|
887
|
854 |
HRESULT res;
|
|
855 |
D3DSURFACE_DESC descNew;
|
|
856 |
IDirect3DSurface9 *pCurrentTarget;
|
2
|
857 |
|
887
|
858 |
J2dTraceLn1(J2D_TRACE_INFO,
|
|
859 |
"D3DContext::SetRenderTarget: pSurface=0x%x",
|
|
860 |
pSurface);
|
2
|
861 |
|
887
|
862 |
RETURN_STATUS_IF_NULL(pd3dDevice, E_FAIL);
|
|
863 |
RETURN_STATUS_IF_NULL(pSurface, E_FAIL);
|
2
|
864 |
|
887
|
865 |
pSurface->GetDesc(&descNew);
|
2
|
866 |
|
887
|
867 |
if (SUCCEEDED(res = pd3dDevice->GetRenderTarget(0, &pCurrentTarget))) {
|
|
868 |
if (pCurrentTarget != pSurface) {
|
|
869 |
FlushVertexQueue();
|
|
870 |
if (FAILED(res = pd3dDevice->SetRenderTarget(0, pSurface))) {
|
|
871 |
DebugPrintD3DError(res, "D3DContext::SetRenderTarget: "\
|
|
872 |
"error setting render target");
|
|
873 |
SAFE_RELEASE(pCurrentTarget);
|
|
874 |
return res;
|
|
875 |
}
|
2
|
876 |
|
887
|
877 |
if (!IsDepthStencilBufferOk(&descNew)) {
|
|
878 |
if (FAILED(res = InitDepthStencilBuffer(&descNew))) {
|
|
879 |
SAFE_RELEASE(pCurrentTarget);
|
|
880 |
return res;
|
|
881 |
}
|
|
882 |
}
|
|
883 |
}
|
|
884 |
SAFE_RELEASE(pCurrentTarget);
|
2
|
885 |
}
|
887
|
886 |
// we set the transform even if the render target didn't change;
|
|
887 |
// this is because in some cases (fs mode) we use the default SwapChain of
|
|
888 |
// the device, and its render target will be the same as the device's, and
|
|
889 |
// we have to set the matrix correctly. This shouldn't be a performance
|
|
890 |
// issue as render target changes are relatively rare
|
|
891 |
D3DUtils_SetOrthoMatrixOffCenterLH(&tx,
|
|
892 |
(float)descNew.Width,
|
|
893 |
(float)descNew.Height);
|
|
894 |
pd3dDevice->SetTransform(D3DTS_PROJECTION, &tx);
|
2
|
895 |
|
887
|
896 |
J2dTraceLn1(J2D_TRACE_VERBOSE, " current render target=0x%x", pSurface);
|
2
|
897 |
return res;
|
|
898 |
}
|
|
899 |
|
|
900 |
HRESULT
|
887
|
901 |
D3DContext::ResetTransform()
|
|
902 |
{
|
|
903 |
HRESULT res = S_OK;
|
|
904 |
D3DMATRIX tx;
|
|
905 |
|
|
906 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::ResetTransform");
|
|
907 |
if (pd3dDevice == NULL) {
|
|
908 |
return E_FAIL;
|
|
909 |
}
|
|
910 |
|
|
911 |
// no need for state change, just flush the queue
|
|
912 |
FlushVertexQueue();
|
|
913 |
|
|
914 |
D3DUtils_SetIdentityMatrix(&tx);
|
|
915 |
if (FAILED(res = pd3dDevice->SetTransform(D3DTS_WORLD, &tx))) {
|
|
916 |
DebugPrintD3DError(res, "D3DContext::SetTransform failed");
|
|
917 |
}
|
|
918 |
bIsIdentityTx = TRUE;
|
|
919 |
return res;
|
|
920 |
}
|
|
921 |
|
|
922 |
HRESULT
|
|
923 |
D3DContext::SetTransform(jdouble m00, jdouble m10,
|
2
|
924 |
jdouble m01, jdouble m11,
|
|
925 |
jdouble m02, jdouble m12)
|
|
926 |
{
|
887
|
927 |
HRESULT res = S_OK;
|
|
928 |
D3DMATRIX tx, tx1;
|
2
|
929 |
|
|
930 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::SetTransform");
|
887
|
931 |
if (pd3dDevice == NULL) {
|
|
932 |
return E_FAIL;
|
2
|
933 |
}
|
|
934 |
|
887
|
935 |
// no need for state change, just flush the queue
|
|
936 |
FlushVertexQueue();
|
|
937 |
|
|
938 |
// In order to correctly map texels to pixels we need to
|
|
939 |
// adjust geometry by -0.5f in the transformed space.
|
|
940 |
// In order to do that we first create a translated matrix
|
|
941 |
// and then concatenate it with the world transform.
|
|
942 |
//
|
|
943 |
// Note that we only use non-id transform with DrawTexture,
|
|
944 |
// the rest is rendered pre-transformed.
|
|
945 |
//
|
|
946 |
// The identity transform for textures is handled in
|
|
947 |
// D3DVertexCacher::DrawTexture() because shifting by -0.5 for id
|
|
948 |
// transform breaks lines rendering.
|
|
949 |
|
|
950 |
ZeroMemory(&tx1, sizeof(D3DMATRIX));
|
|
951 |
|
|
952 |
tx1._11 = (float)m00;
|
|
953 |
tx1._12 = (float)m10;
|
|
954 |
tx1._21 = (float)m01;
|
|
955 |
tx1._22 = (float)m11;
|
|
956 |
tx1._41 = (float)m02;
|
|
957 |
tx1._42 = (float)m12;
|
|
958 |
|
|
959 |
tx1._33 = 1.0f;
|
|
960 |
tx1._44 = 1.0f;
|
|
961 |
|
|
962 |
D3DUtils_SetIdentityMatrix(&tx);
|
|
963 |
tx._41 = -0.5f;
|
|
964 |
tx._42 = -0.5f;
|
|
965 |
D3DUtils_2DConcatenateM(&tx, &tx1);
|
|
966 |
|
2
|
967 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
968 |
" %5f %5f %5f %5f", tx._11, tx._12, tx._13, tx._14);
|
|
969 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
970 |
" %5f %5f %5f %5f", tx._21, tx._22, tx._23, tx._24);
|
|
971 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
972 |
" %5f %5f %5f %5f", tx._31, tx._32, tx._33, tx._34);
|
|
973 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
974 |
" %5f %5f %5f %5f", tx._41, tx._42, tx._43, tx._44);
|
887
|
975 |
if (FAILED(res = pd3dDevice->SetTransform(D3DTS_WORLD, &tx))) {
|
|
976 |
DebugPrintD3DError(res, "D3DContext::SetTransform failed");
|
|
977 |
}
|
|
978 |
bIsIdentityTx = FALSE;
|
|
979 |
|
|
980 |
return res;
|
|
981 |
}
|
|
982 |
|
|
983 |
HRESULT
|
|
984 |
D3DContext::SetRectClip(int x1, int y1, int x2, int y2)
|
|
985 |
{
|
|
986 |
HRESULT res = S_OK;
|
|
987 |
D3DSURFACE_DESC desc;
|
|
988 |
IDirect3DSurface9 *pCurrentTarget;
|
|
989 |
|
|
990 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::SetRectClip");
|
|
991 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
992 |
" x1=%-4d y1=%-4d x2=%-4d y2=%-4d",
|
|
993 |
x1, y1, x2, y2);
|
|
994 |
|
|
995 |
RETURN_STATUS_IF_NULL(pd3dDevice, E_FAIL);
|
|
996 |
|
|
997 |
// no need for state change, just flush the queue
|
|
998 |
FlushVertexQueue();
|
|
999 |
|
|
1000 |
pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
|
|
1001 |
|
|
1002 |
res = pd3dDevice->GetRenderTarget(0, &pCurrentTarget);
|
|
1003 |
RETURN_STATUS_IF_FAILED(res);
|
|
1004 |
|
|
1005 |
pCurrentTarget->GetDesc(&desc);
|
|
1006 |
SAFE_RELEASE(pCurrentTarget);
|
|
1007 |
|
|
1008 |
if (x1 <= 0 && y1 <= 0 &&
|
|
1009 |
(UINT)x2 >= desc.Width && (UINT)y2 >= desc.Height)
|
|
1010 |
{
|
|
1011 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
1012 |
" disabling clip (== render target dimensions)");
|
|
1013 |
return pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
|
2
|
1014 |
}
|
|
1015 |
|
887
|
1016 |
// clip to the dimensions of the target surface, otherwise
|
|
1017 |
// SetScissorRect will fail
|
|
1018 |
if (x1 < 0) x1 = 0;
|
|
1019 |
if (y1 < 0) y1 = 0;
|
|
1020 |
if ((UINT)x2 > desc.Width) x2 = desc.Width;
|
|
1021 |
if ((UINT)y2 > desc.Height) y2 = desc.Height;
|
|
1022 |
if (x1 > x2) x2 = x1 = 0;
|
|
1023 |
if (y1 > y2) y2 = y1 = 0;
|
|
1024 |
RECT newRect = { x1, y1, x2, y2 };
|
|
1025 |
if (SUCCEEDED(res = pd3dDevice->SetScissorRect(&newRect))) {
|
|
1026 |
res = pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
|
|
1027 |
} else {
|
|
1028 |
DebugPrintD3DError(res, "Error setting scissor rect");
|
|
1029 |
J2dRlsTraceLn4(J2D_TRACE_ERROR,
|
|
1030 |
" x1=%-4d y1=%-4d x2=%-4d y2=%-4d",
|
|
1031 |
x1, y1, x2, y2);
|
|
1032 |
}
|
|
1033 |
|
2
|
1034 |
return res;
|
|
1035 |
}
|
|
1036 |
|
887
|
1037 |
HRESULT
|
|
1038 |
D3DContext::ResetClip()
|
|
1039 |
{
|
|
1040 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::ResetClip");
|
|
1041 |
// no need for state change, just flush the queue
|
|
1042 |
FlushVertexQueue();
|
|
1043 |
pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
|
|
1044 |
return pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
|
|
1045 |
}
|
|
1046 |
|
|
1047 |
ClipType
|
|
1048 |
D3DContext::GetClipType()
|
|
1049 |
{
|
|
1050 |
// REMIND: this method could be optimized: we could keep the
|
|
1051 |
// clip state around when re/setting the clip instead of asking
|
|
1052 |
// every time.
|
|
1053 |
DWORD zEnabled = 0;
|
|
1054 |
DWORD stEnabled = 0;
|
|
1055 |
|
|
1056 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::GetClipType");
|
|
1057 |
pd3dDevice->GetRenderState(D3DRS_SCISSORTESTENABLE, &stEnabled);
|
|
1058 |
if (stEnabled) {
|
|
1059 |
return CLIP_RECT;
|
|
1060 |
}
|
|
1061 |
pd3dDevice->GetRenderState(D3DRS_ZENABLE, &zEnabled);
|
|
1062 |
if (zEnabled) {
|
|
1063 |
return CLIP_SHAPE;
|
|
1064 |
}
|
|
1065 |
return CLIP_NONE;
|
|
1066 |
}
|
|
1067 |
|
|
1068 |
|
2
|
1069 |
/**
|
|
1070 |
* This method assumes that ::SetRenderTarget has already
|
|
1071 |
* been called. SetRenderTarget creates and attaches a
|
|
1072 |
* depth buffer to the target surface prior to setting it
|
|
1073 |
* as target surface to the device.
|
|
1074 |
*/
|
887
|
1075 |
DWORD dwAlphaSt, dwSrcBlendSt, dwDestBlendSt;
|
|
1076 |
D3DMATRIX tx, idTx;
|
|
1077 |
|
2
|
1078 |
HRESULT
|
887
|
1079 |
D3DContext::BeginShapeClip()
|
2
|
1080 |
{
|
887
|
1081 |
HRESULT res = S_OK;
|
|
1082 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::BeginShapeClip");
|
2
|
1083 |
|
887
|
1084 |
UpdateState(STATE_CHANGE);
|
2
|
1085 |
|
887
|
1086 |
pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
|
2
|
1087 |
|
887
|
1088 |
// save alpha blending state
|
|
1089 |
pd3dDevice->GetRenderState(D3DRS_ALPHABLENDENABLE, &dwAlphaSt);
|
|
1090 |
pd3dDevice->GetRenderState(D3DRS_SRCBLEND, &dwSrcBlendSt);
|
|
1091 |
pd3dDevice->GetRenderState(D3DRS_DESTBLEND, &dwDestBlendSt);
|
2
|
1092 |
|
887
|
1093 |
pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
|
|
1094 |
pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
|
|
1095 |
pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
|
2
|
1096 |
|
887
|
1097 |
pd3dDevice->GetTransform(D3DTS_WORLD, &tx);
|
2
|
1098 |
D3DUtils_SetIdentityMatrix(&idTx);
|
887
|
1099 |
// translate the clip spans by 1.0f in z direction so that the
|
|
1100 |
// clip spans are rendered to the z buffer
|
|
1101 |
idTx._43 = 1.0f;
|
|
1102 |
pd3dDevice->SetTransform(D3DTS_WORLD, &idTx);
|
2
|
1103 |
|
|
1104 |
// The depth buffer is first cleared with zeroes, which is the farthest
|
|
1105 |
// plane from the viewer (our projection matrix is an inversed orthogonal
|
|
1106 |
// transform).
|
|
1107 |
// To set the clip we'll render the clip spans with Z coordinates of 1.0f
|
|
1108 |
// (the closest to the viewer). Since all rendering primitives
|
|
1109 |
// have their vertices' Z coordinate set to 0.0, they will effectively be
|
|
1110 |
// clipped because the Z depth test for them will fail (vertex with 1.0
|
|
1111 |
// depth is closer than the one with 0.0f)
|
887
|
1112 |
pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
|
|
1113 |
pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
|
|
1114 |
pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
|
|
1115 |
pd3dDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0L, 0.0f, 0x0L);
|
2
|
1116 |
|
887
|
1117 |
//res = BeginScene(STATE_SHAPE_CLIPOP);
|
2
|
1118 |
|
|
1119 |
return res;
|
|
1120 |
}
|
|
1121 |
|
|
1122 |
HRESULT
|
887
|
1123 |
D3DContext::EndShapeClip()
|
2
|
1124 |
{
|
887
|
1125 |
HRESULT res;
|
2
|
1126 |
|
887
|
1127 |
// no need for state change, just flush the queue
|
|
1128 |
res = FlushVertexQueue();
|
2
|
1129 |
|
887
|
1130 |
// restore alpha blending state
|
|
1131 |
pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, dwAlphaSt);
|
|
1132 |
pd3dDevice->SetRenderState(D3DRS_SRCBLEND, dwSrcBlendSt);
|
|
1133 |
pd3dDevice->SetRenderState(D3DRS_DESTBLEND, dwDestBlendSt);
|
2
|
1134 |
|
887
|
1135 |
// resore the transform
|
|
1136 |
pd3dDevice->SetTransform(D3DTS_WORLD, &tx);
|
2
|
1137 |
|
887
|
1138 |
// Enable the depth buffer.
|
|
1139 |
// We disable further updates to the depth buffer: it should only
|
|
1140 |
// be updated in SetClip method.
|
|
1141 |
pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
|
|
1142 |
pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
|
2
|
1143 |
|
|
1144 |
return res;
|
|
1145 |
}
|
|
1146 |
|
887
|
1147 |
HRESULT
|
|
1148 |
D3DContext::UploadTileToTexture(D3DResource *pTextureRes, void *pixels,
|
|
1149 |
jint dstx, jint dsty,
|
|
1150 |
jint srcx, jint srcy,
|
|
1151 |
jint srcWidth, jint srcHeight,
|
|
1152 |
jint srcStride,
|
|
1153 |
TileFormat srcFormat,
|
|
1154 |
jint *pPixelsTouchedL,
|
|
1155 |
jint* pPixelsTouchedR)
|
2
|
1156 |
{
|
887
|
1157 |
#ifndef PtrAddBytes
|
|
1158 |
#define PtrAddBytes(p, b) ((void *) (((intptr_t) (p)) + (b)))
|
|
1159 |
#define PtrCoord(p, x, xinc, y, yinc) PtrAddBytes(p, (y)*(yinc) + (x)*(xinc))
|
|
1160 |
#endif // PtrAddBytes
|
|
1161 |
|
|
1162 |
HRESULT res = S_OK;
|
|
1163 |
IDirect3DTexture9 *pTexture = pTextureRes->GetTexture();
|
|
1164 |
D3DSURFACE_DESC *pDesc = pTextureRes->GetDesc();
|
|
1165 |
RECT r = { dstx, dsty, dstx+srcWidth, dsty+srcHeight };
|
|
1166 |
RECT *pR = &r;
|
|
1167 |
D3DLOCKED_RECT lockedRect;
|
|
1168 |
DWORD dwLockFlags = D3DLOCK_NOSYSLOCK;
|
|
1169 |
// these are only counted for LCD glyph uploads
|
|
1170 |
jint pixelsTouchedL = 0, pixelsTouchedR = 0;
|
|
1171 |
|
|
1172 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::UploadTileToTexture");
|
|
1173 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
1174 |
" rect={%-4d, %-4d, %-4d, %-4d}",
|
|
1175 |
r.left, r.top, r.right, r.bottom);
|
|
1176 |
|
|
1177 |
// REMIND: we should also check for dstx, dsty being 0 here,
|
|
1178 |
// but they're always 0 in dynamic texture case
|
|
1179 |
if (pDesc->Usage == D3DUSAGE_DYNAMIC &&
|
|
1180 |
srcWidth == pDesc->Width && srcHeight == pDesc->Height)
|
|
1181 |
{
|
|
1182 |
dwLockFlags |= D3DLOCK_DISCARD;
|
|
1183 |
pR = NULL;
|
|
1184 |
}
|
|
1185 |
|
|
1186 |
if (FAILED(res = pTexture->LockRect(0, &lockedRect, pR, dwLockFlags))) {
|
|
1187 |
DebugPrintD3DError(res,
|
|
1188 |
"D3DContext::UploadImageToTexture: could "\
|
|
1189 |
"not lock texture");
|
|
1190 |
return res;
|
|
1191 |
}
|
2
|
1192 |
|
887
|
1193 |
if (srcFormat == TILEFMT_1BYTE_ALPHA) {
|
|
1194 |
// either a MaskFill tile, or a grayscale glyph
|
|
1195 |
if (pDesc->Format == D3DFMT_A8) {
|
|
1196 |
void *pSrcPixels = PtrCoord(pixels, srcx, 1, srcy, srcStride);
|
|
1197 |
void *pDstPixels = lockedRect.pBits;
|
|
1198 |
do {
|
|
1199 |
memcpy(pDstPixels, pSrcPixels, srcWidth);
|
|
1200 |
pSrcPixels = PtrAddBytes(pSrcPixels, srcStride);
|
|
1201 |
pDstPixels = PtrAddBytes(pDstPixels, lockedRect.Pitch);
|
|
1202 |
} while (--srcHeight > 0);
|
|
1203 |
}
|
|
1204 |
else if (pDesc->Format == D3DFMT_A8R8G8B8) {
|
|
1205 |
jubyte *pSrcPixels = (jubyte*)
|
|
1206 |
PtrCoord(pixels, srcx, 1, srcy, srcStride);
|
|
1207 |
jint *pDstPixels = (jint*)lockedRect.pBits;
|
|
1208 |
for (int yy = 0; yy < srcHeight; yy++) {
|
|
1209 |
for (int xx = 0; xx < srcWidth; xx++) {
|
|
1210 |
// only need to set the alpha channel (the D3D texture
|
|
1211 |
// state will be setup in this case to replicate the
|
|
1212 |
// alpha channel as needed)
|
|
1213 |
pDstPixels[xx] = pSrcPixels[xx] << 24;
|
|
1214 |
}
|
|
1215 |
pSrcPixels = (jubyte*)PtrAddBytes(pSrcPixels, srcStride);
|
|
1216 |
pDstPixels = (jint*)PtrAddBytes(pDstPixels, lockedRect.Pitch);
|
|
1217 |
}
|
|
1218 |
}
|
|
1219 |
} else if (srcFormat == TILEFMT_3BYTE_RGB) {
|
|
1220 |
// LCD glyph with RGB order
|
|
1221 |
if (pDesc->Format == D3DFMT_R8G8B8) {
|
|
1222 |
jubyte *pSrcPixels = (jubyte*)
|
|
1223 |
PtrCoord(pixels, srcx, 3, srcy, srcStride);
|
|
1224 |
jubyte *pDstPixels = (jubyte*)lockedRect.pBits;
|
|
1225 |
for (int yy = 0; yy < srcHeight; yy++) {
|
|
1226 |
for (int xx = 0; xx < srcWidth*3; xx+=3) {
|
|
1227 |
// alpha channel is ignored in this case
|
|
1228 |
// (note that this is backwards from what one might
|
|
1229 |
// expect; it appears that D3DFMT_R8G8B8 is actually
|
|
1230 |
// laid out in BGR order in memory)
|
|
1231 |
pDstPixels[xx+0] = pSrcPixels[xx+2];
|
|
1232 |
pDstPixels[xx+1] = pSrcPixels[xx+1];
|
|
1233 |
pDstPixels[xx+2] = pSrcPixels[xx+0];
|
|
1234 |
}
|
|
1235 |
pixelsTouchedL +=
|
|
1236 |
(pDstPixels[0+0]|pDstPixels[0+1]|pDstPixels[0+2]) ? 1 : 0;
|
|
1237 |
jint i = 3*(srcWidth-1);
|
|
1238 |
pixelsTouchedR +=
|
|
1239 |
(pDstPixels[i+0]|pDstPixels[i+1]|pDstPixels[i+2]) ? 1 : 0;
|
|
1240 |
|
|
1241 |
pSrcPixels = (jubyte*)PtrAddBytes(pSrcPixels, srcStride);
|
|
1242 |
pDstPixels = (jubyte*)PtrAddBytes(pDstPixels, lockedRect.Pitch);
|
|
1243 |
}
|
|
1244 |
}
|
|
1245 |
else if (pDesc->Format == D3DFMT_A8R8G8B8) {
|
|
1246 |
jubyte *pSrcPixels = (jubyte*)
|
|
1247 |
PtrCoord(pixels, srcx, 3, srcy, srcStride);
|
|
1248 |
jint *pDstPixels = (jint*)lockedRect.pBits;
|
|
1249 |
for (int yy = 0; yy < srcHeight; yy++) {
|
|
1250 |
for (int dx = 0, sx = 0; dx < srcWidth; dx++, sx+=3) {
|
|
1251 |
// alpha channel is ignored in this case
|
|
1252 |
jubyte r = pSrcPixels[sx+0];
|
|
1253 |
jubyte g = pSrcPixels[sx+1];
|
|
1254 |
jubyte b = pSrcPixels[sx+2];
|
|
1255 |
pDstPixels[dx] = (r << 16) | (g << 8) | (b);
|
|
1256 |
}
|
|
1257 |
pixelsTouchedL += (pDstPixels[0] ? 1 : 0);
|
|
1258 |
pixelsTouchedR += (pDstPixels[srcWidth-1] ? 1 : 0);
|
2
|
1259 |
|
887
|
1260 |
pSrcPixels = (jubyte*)PtrAddBytes(pSrcPixels, srcStride);
|
|
1261 |
pDstPixels = (jint*)PtrAddBytes(pDstPixels, lockedRect.Pitch);
|
|
1262 |
}
|
|
1263 |
}
|
|
1264 |
} else if (srcFormat == TILEFMT_3BYTE_BGR) {
|
|
1265 |
// LCD glyph with BGR order
|
|
1266 |
if (pDesc->Format == D3DFMT_R8G8B8) {
|
|
1267 |
void *pSrcPixels = PtrCoord(pixels, srcx, 3, srcy, srcStride);
|
|
1268 |
void *pDstPixels = lockedRect.pBits;
|
|
1269 |
jubyte *pbDst;
|
|
1270 |
do {
|
|
1271 |
// alpha channel is ignored in this case
|
|
1272 |
// (note that this is backwards from what one might
|
|
1273 |
// expect; it appears that D3DFMT_R8G8B8 is actually
|
|
1274 |
// laid out in BGR order in memory)
|
|
1275 |
memcpy(pDstPixels, pSrcPixels, srcWidth * 3);
|
|
1276 |
|
|
1277 |
pbDst = (jubyte*)pDstPixels;
|
|
1278 |
pixelsTouchedL +=(pbDst[0+0]|pbDst[0+1]|pbDst[0+2]) ? 1 : 0;
|
|
1279 |
jint i = 3*(srcWidth-1);
|
|
1280 |
pixelsTouchedR +=(pbDst[i+0]|pbDst[i+1]|pbDst[i+2]) ? 1 : 0;
|
|
1281 |
|
|
1282 |
pSrcPixels = PtrAddBytes(pSrcPixels, srcStride);
|
|
1283 |
pDstPixels = PtrAddBytes(pDstPixels, lockedRect.Pitch);
|
|
1284 |
} while (--srcHeight > 0);
|
|
1285 |
}
|
|
1286 |
else if (pDesc->Format == D3DFMT_A8R8G8B8) {
|
|
1287 |
jubyte *pSrcPixels = (jubyte*)
|
|
1288 |
PtrCoord(pixels, srcx, 3, srcy, srcStride);
|
|
1289 |
jint *pDstPixels = (jint*)lockedRect.pBits;
|
|
1290 |
for (int yy = 0; yy < srcHeight; yy++) {
|
|
1291 |
for (int dx = 0, sx = 0; dx < srcWidth; dx++, sx+=3) {
|
|
1292 |
// alpha channel is ignored in this case
|
|
1293 |
jubyte b = pSrcPixels[sx+0];
|
|
1294 |
jubyte g = pSrcPixels[sx+1];
|
|
1295 |
jubyte r = pSrcPixels[sx+2];
|
|
1296 |
pDstPixels[dx] = (r << 16) | (g << 8) | (b);
|
|
1297 |
}
|
|
1298 |
pixelsTouchedL += (pDstPixels[0] ? 1 : 0);
|
|
1299 |
pixelsTouchedR += (pDstPixels[srcWidth-1] ? 1 : 0);
|
|
1300 |
|
|
1301 |
pSrcPixels = (jubyte*)PtrAddBytes(pSrcPixels, srcStride);
|
|
1302 |
pDstPixels = (jint*)PtrAddBytes(pDstPixels, lockedRect.Pitch);
|
|
1303 |
}
|
|
1304 |
}
|
|
1305 |
} else if (srcFormat == TILEFMT_4BYTE_ARGB_PRE) {
|
|
1306 |
// MaskBlit tile
|
|
1307 |
if (pDesc->Format == D3DFMT_A8R8G8B8) {
|
|
1308 |
void *pSrcPixels = PtrCoord(pixels, srcx, 4, srcy, srcStride);
|
|
1309 |
void *pDstPixels = lockedRect.pBits;
|
|
1310 |
do {
|
|
1311 |
memcpy(pDstPixels, pSrcPixels, srcWidth * 4);
|
|
1312 |
pSrcPixels = PtrAddBytes(pSrcPixels, srcStride);
|
|
1313 |
pDstPixels = PtrAddBytes(pDstPixels, lockedRect.Pitch);
|
|
1314 |
} while (--srcHeight > 0);
|
|
1315 |
}
|
2
|
1316 |
} else {
|
887
|
1317 |
// should not happen, no-op just in case...
|
|
1318 |
}
|
|
1319 |
|
|
1320 |
if (pPixelsTouchedL) {
|
|
1321 |
*pPixelsTouchedL = pixelsTouchedL;
|
2
|
1322 |
}
|
887
|
1323 |
if (pPixelsTouchedR) {
|
|
1324 |
*pPixelsTouchedR = pixelsTouchedR;
|
|
1325 |
}
|
|
1326 |
|
|
1327 |
return pTexture->UnlockRect(0);
|
2
|
1328 |
}
|
|
1329 |
|
887
|
1330 |
HRESULT
|
|
1331 |
D3DContext::InitLCDGlyphCache()
|
|
1332 |
{
|
|
1333 |
if (pLCDGlyphCache == NULL) {
|
|
1334 |
return D3DGlyphCache::CreateInstance(this, CACHE_LCD, &pLCDGlyphCache);
|
|
1335 |
}
|
|
1336 |
return S_OK;
|
|
1337 |
}
|
|
1338 |
|
|
1339 |
HRESULT
|
|
1340 |
D3DContext::InitGrayscaleGlyphCache()
|
|
1341 |
{
|
|
1342 |
if (pGrayscaleGlyphCache == NULL) {
|
|
1343 |
return D3DGlyphCache::CreateInstance(this, CACHE_GRAY,
|
|
1344 |
&pGrayscaleGlyphCache);
|
|
1345 |
}
|
|
1346 |
return S_OK;
|
|
1347 |
}
|
|
1348 |
|
|
1349 |
HRESULT
|
2
|
1350 |
D3DContext::ResetComposite()
|
|
1351 |
{
|
|
1352 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::ResetComposite");
|
887
|
1353 |
|
|
1354 |
RETURN_STATUS_IF_NULL(pd3dDevice, E_FAIL);
|
|
1355 |
|
|
1356 |
HRESULT res = UpdateState(STATE_CHANGE);
|
|
1357 |
pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
|
2
|
1358 |
extraAlpha = 1.0f;
|
887
|
1359 |
return res;
|
2
|
1360 |
}
|
|
1361 |
|
887
|
1362 |
HRESULT
|
2
|
1363 |
D3DContext::SetAlphaComposite(jint rule, jfloat ea, jint flags)
|
|
1364 |
{
|
887
|
1365 |
HRESULT res;
|
2
|
1366 |
J2dTraceLn3(J2D_TRACE_INFO,
|
|
1367 |
"D3DContext::SetAlphaComposite: rule=%-1d ea=%f flags=%d",
|
|
1368 |
rule, ea, flags);
|
|
1369 |
|
887
|
1370 |
RETURN_STATUS_IF_NULL(pd3dDevice, E_FAIL);
|
|
1371 |
|
|
1372 |
res = UpdateState(STATE_CHANGE);
|
2
|
1373 |
|
|
1374 |
// we can safely disable blending when:
|
|
1375 |
// - comp is SrcNoEa or SrcOverNoEa, and
|
|
1376 |
// - the source is opaque
|
887
|
1377 |
// (turning off blending can have a large positive impact on performance)
|
2
|
1378 |
if ((rule == RULE_Src || rule == RULE_SrcOver) &&
|
|
1379 |
(ea == 1.0f) &&
|
|
1380 |
(flags & D3DC_SRC_IS_OPAQUE))
|
887
|
1381 |
{
|
|
1382 |
J2dTraceLn1(J2D_TRACE_VERBOSE,
|
|
1383 |
" disabling alpha comp rule=%-1d ea=1.0 src=opq)", rule);
|
|
1384 |
pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
|
|
1385 |
} else {
|
2
|
1386 |
J2dTraceLn2(J2D_TRACE_VERBOSE,
|
|
1387 |
" enabling alpha comp (rule=%-1d ea=%f)", rule, ea);
|
887
|
1388 |
pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
|
2
|
1389 |
|
887
|
1390 |
pd3dDevice->SetRenderState(D3DRS_SRCBLEND,
|
|
1391 |
StdBlendRules[rule].src);
|
|
1392 |
pd3dDevice->SetRenderState(D3DRS_DESTBLEND,
|
|
1393 |
StdBlendRules[rule].dst);
|
2
|
1394 |
}
|
|
1395 |
|
887
|
1396 |
extraAlpha = ea;
|
2
|
1397 |
return res;
|
|
1398 |
}
|
|
1399 |
|
887
|
1400 |
#ifdef UPDATE_TX
|
2
|
1401 |
|
887
|
1402 |
// Note: this method of adjusting pixel to texel mapping proved to be
|
|
1403 |
// difficult to perfect. The current variation works great for id,
|
|
1404 |
// scale (including all kinds of flips) transforms, but not still not
|
|
1405 |
// for generic transforms.
|
|
1406 |
//
|
|
1407 |
// Since we currently only do DrawTexture with non-id transform we instead
|
|
1408 |
// adjust the geometry (see D3DVertexCacher::DrawTexture(), SetTransform())
|
|
1409 |
//
|
|
1410 |
// In order to enable this code path UpdateTextureTransforms needs to
|
|
1411 |
// be called in SetTexture(), SetTransform() and ResetTranform().
|
|
1412 |
HRESULT
|
|
1413 |
D3DContext::UpdateTextureTransforms(DWORD dwSamplerToUpdate)
|
|
1414 |
{
|
|
1415 |
HRESULT res = S_OK;
|
|
1416 |
DWORD dwSampler, dwMaxSampler;
|
2
|
1417 |
|
887
|
1418 |
if (dwSamplerToUpdate == -1) {
|
|
1419 |
// update all used samplers, dwMaxSampler will be set to max
|
|
1420 |
dwSampler = 0;
|
|
1421 |
dwSampler = MAX_USED_TEXTURE_SAMPLER;
|
|
1422 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::UpdateTextureTransforms: "\
|
|
1423 |
"updating all samplers");
|
|
1424 |
} else {
|
|
1425 |
// update only given sampler, dwMaxSampler will be set to it as well
|
|
1426 |
dwSampler = dwSamplerToUpdate;
|
|
1427 |
dwMaxSampler = dwSamplerToUpdate;
|
|
1428 |
J2dTraceLn1(J2D_TRACE_INFO, "D3DContext::UpdateTextureTransforms: "\
|
|
1429 |
"updating sampler %d", dwSampler);
|
2
|
1430 |
}
|
|
1431 |
|
887
|
1432 |
do {
|
|
1433 |
D3DTRANSFORMSTATETYPE state =
|
|
1434 |
(D3DTRANSFORMSTATETYPE)(D3DTS_TEXTURE0 + dwSampler);
|
|
1435 |
IDirect3DTexture9 *pTexture = lastTexture[dwSampler];
|
|
1436 |
|
|
1437 |
if (pTexture != NULL) {
|
|
1438 |
D3DMATRIX mt, tx;
|
|
1439 |
D3DSURFACE_DESC texDesc;
|
|
1440 |
|
|
1441 |
pd3dDevice->GetTransform(D3DTS_WORLD, &tx);
|
|
1442 |
J2dTraceLn4(10,
|
|
1443 |
" %5f %5f %5f %5f", tx._11, tx._12, tx._13, tx._14);
|
|
1444 |
J2dTraceLn4(10,
|
|
1445 |
" %5f %5f %5f %5f", tx._21, tx._22, tx._23, tx._24);
|
|
1446 |
J2dTraceLn4(10,
|
|
1447 |
" %5f %5f %5f %5f", tx._31, tx._32, tx._33, tx._34);
|
|
1448 |
J2dTraceLn4(10,
|
|
1449 |
" %5f %5f %5f %5f", tx._41, tx._42, tx._43, tx._44);
|
2
|
1450 |
|
887
|
1451 |
// this formula works for scales and flips
|
|
1452 |
if (tx._11 == 0.0f) {
|
|
1453 |
tx._11 = tx._12;
|
|
1454 |
}
|
|
1455 |
if (tx._22 == 0.0f) {
|
|
1456 |
tx._22 = tx._21;
|
|
1457 |
}
|
|
1458 |
|
|
1459 |
pTexture->GetLevelDesc(0, &texDesc);
|
|
1460 |
|
|
1461 |
// shift by .5 texel, but take into account
|
|
1462 |
// the scale factor of the device transform
|
|
1463 |
|
|
1464 |
// REMIND: this approach is not entirely correct,
|
|
1465 |
// as it only takes into account the scale of the device
|
|
1466 |
// transform.
|
|
1467 |
mt._31 = (1.0f / (2.0f * texDesc.Width * tx._11));
|
|
1468 |
mt._32 = (1.0f / (2.0f * texDesc.Height * tx._22));
|
|
1469 |
J2dTraceLn2(J2D_TRACE_VERBOSE, " offsets: tx=%f ty=%f",
|
|
1470 |
mt._31, mt._32);
|
|
1471 |
|
|
1472 |
pd3dDevice->SetTextureStageState(dwSampler,
|
|
1473 |
D3DTSS_TEXTURETRANSFORMFLAGS,
|
|
1474 |
D3DTTFF_COUNT2);
|
|
1475 |
res = pd3dDevice->SetTransform(state, &mt);
|
|
1476 |
} else {
|
|
1477 |
res = pd3dDevice->SetTextureStageState(dwSampler,
|
|
1478 |
D3DTSS_TEXTURETRANSFORMFLAGS,
|
|
1479 |
D3DTTFF_DISABLE);
|
2
|
1480 |
}
|
887
|
1481 |
dwSampler++;
|
|
1482 |
} while (dwSampler <= dwMaxSampler);
|
2
|
1483 |
|
|
1484 |
return res;
|
|
1485 |
}
|
887
|
1486 |
#endif // UPDATE_TX
|
2
|
1487 |
|
|
1488 |
/**
|
|
1489 |
* We go into the pains of maintaining the list of set textures
|
|
1490 |
* instead of just calling GetTexture() and comparing the old one
|
|
1491 |
* with the new one because it's actually noticeably slower to call
|
|
1492 |
* GetTexture() (note that we'd have to then call Release() on the
|
|
1493 |
* texture since GetTexture() increases texture's ref. count).
|
|
1494 |
*/
|
887
|
1495 |
HRESULT
|
|
1496 |
D3DContext::SetTexture(IDirect3DTexture9 *pTexture, DWORD dwSampler)
|
2
|
1497 |
{
|
887
|
1498 |
HRESULT res = S_OK;
|
|
1499 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::SetTexture");
|
2
|
1500 |
|
887
|
1501 |
if (dwSampler < 0 || dwSampler > MAX_USED_TEXTURE_SAMPLER) {
|
2
|
1502 |
J2dTraceLn1(J2D_TRACE_ERROR,
|
887
|
1503 |
"D3DContext::SetTexture: incorrect sampler: %d", dwSampler);
|
|
1504 |
return E_FAIL;
|
2
|
1505 |
}
|
887
|
1506 |
if (lastTexture[dwSampler] != pTexture) {
|
|
1507 |
if (FAILED(res = FlushVertexQueue())) {
|
|
1508 |
return res;
|
|
1509 |
}
|
|
1510 |
J2dTraceLn2(J2D_TRACE_VERBOSE,
|
|
1511 |
" new texture=0x%x on sampler %d", pTexture, dwSampler);
|
|
1512 |
res = pd3dDevice->SetTexture(dwSampler, pTexture);
|
|
1513 |
if (SUCCEEDED(res)) {
|
|
1514 |
lastTexture[dwSampler] = pTexture;
|
|
1515 |
// REMIND: see comment at UpdateTextureTransforms
|
|
1516 |
#ifdef UPDATE_TX
|
|
1517 |
res = UpdateTextureTransforms(dwSampler);
|
|
1518 |
#endif
|
|
1519 |
} else {
|
|
1520 |
lastTexture[dwSampler] = NULL;
|
|
1521 |
}
|
2
|
1522 |
}
|
|
1523 |
return res;
|
|
1524 |
}
|
|
1525 |
|
887
|
1526 |
HRESULT
|
|
1527 |
D3DContext::UpdateTextureColorState(DWORD dwState, DWORD dwSampler)
|
2
|
1528 |
{
|
887
|
1529 |
HRESULT res = S_OK;
|
2
|
1530 |
|
887
|
1531 |
if (dwState != lastTextureColorState[dwSampler]) {
|
|
1532 |
res = pd3dDevice->SetTextureStageState(dwSampler,
|
|
1533 |
D3DTSS_ALPHAARG1, dwState);
|
|
1534 |
res = pd3dDevice->SetTextureStageState(dwSampler,
|
|
1535 |
D3DTSS_COLORARG1, dwState);
|
|
1536 |
lastTextureColorState[dwSampler] = dwState;
|
2
|
1537 |
}
|
887
|
1538 |
|
|
1539 |
return res;
|
2
|
1540 |
}
|
|
1541 |
|
887
|
1542 |
HRESULT /*NOLOCK*/
|
2
|
1543 |
D3DContext::UpdateState(jbyte newState)
|
|
1544 |
{
|
887
|
1545 |
HRESULT res = S_OK;
|
|
1546 |
|
|
1547 |
if (opState == newState) {
|
|
1548 |
// The op is the same as last time, so we can return immediately.
|
|
1549 |
return res;
|
|
1550 |
} else if (opState != STATE_CHANGE) {
|
|
1551 |
res = FlushVertexQueue();
|
|
1552 |
}
|
2
|
1553 |
|
887
|
1554 |
switch (opState) {
|
|
1555 |
case STATE_MASKOP:
|
|
1556 |
pMaskCache->Disable();
|
|
1557 |
break;
|
|
1558 |
case STATE_GLYPHOP:
|
|
1559 |
D3DTR_DisableGlyphVertexCache(this);
|
|
1560 |
break;
|
|
1561 |
case STATE_TEXTUREOP:
|
|
1562 |
// optimization: certain state changes (those marked STATE_CHANGE)
|
|
1563 |
// are allowed while texturing is enabled.
|
|
1564 |
// In this case, we can allow previousOp to remain as it is and
|
|
1565 |
// then return early.
|
|
1566 |
if (newState == STATE_CHANGE) {
|
|
1567 |
return res;
|
|
1568 |
}
|
|
1569 |
// REMIND: not necessary if we are switching to MASKOP or GLYPHOP
|
|
1570 |
// (or a complex paint, for that matter), but would that be a
|
|
1571 |
// worthwhile optimization?
|
|
1572 |
SetTexture(NULL);
|
|
1573 |
break;
|
|
1574 |
case STATE_AAPGRAMOP:
|
|
1575 |
res = DisableAAParallelogramProgram();
|
|
1576 |
break;
|
|
1577 |
default:
|
|
1578 |
break;
|
|
1579 |
}
|
2
|
1580 |
|
887
|
1581 |
switch (newState) {
|
|
1582 |
case STATE_MASKOP:
|
|
1583 |
pMaskCache->Enable();
|
|
1584 |
UpdateTextureColorState(D3DTA_TEXTURE | D3DTA_ALPHAREPLICATE);
|
|
1585 |
break;
|
|
1586 |
case STATE_GLYPHOP:
|
|
1587 |
D3DTR_EnableGlyphVertexCache(this);
|
|
1588 |
UpdateTextureColorState(D3DTA_TEXTURE | D3DTA_ALPHAREPLICATE);
|
|
1589 |
break;
|
|
1590 |
case STATE_TEXTUREOP:
|
|
1591 |
UpdateTextureColorState(D3DTA_TEXTURE);
|
|
1592 |
break;
|
|
1593 |
case STATE_AAPGRAMOP:
|
|
1594 |
res = EnableAAParallelogramProgram();
|
|
1595 |
break;
|
|
1596 |
default:
|
|
1597 |
break;
|
2
|
1598 |
}
|
887
|
1599 |
|
|
1600 |
opState = newState;
|
|
1601 |
|
|
1602 |
return res;
|
|
1603 |
}
|
|
1604 |
|
|
1605 |
HRESULT D3DContext::FlushVertexQueue()
|
|
1606 |
{
|
|
1607 |
if (pVCacher != NULL) {
|
|
1608 |
return pVCacher->Render();
|
|
1609 |
}
|
|
1610 |
return E_FAIL;
|
2
|
1611 |
}
|
|
1612 |
|
|
1613 |
HRESULT D3DContext::BeginScene(jbyte newState)
|
|
1614 |
{
|
887
|
1615 |
if (!pd3dDevice) {
|
|
1616 |
return E_FAIL;
|
2
|
1617 |
} else {
|
|
1618 |
UpdateState(newState);
|
|
1619 |
if (!bBeginScenePending) {
|
|
1620 |
bBeginScenePending = TRUE;
|
887
|
1621 |
HRESULT res = pd3dDevice->BeginScene();
|
2
|
1622 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::BeginScene");
|
|
1623 |
if (FAILED(res)) {
|
|
1624 |
// this will cause context reinitialization
|
887
|
1625 |
opState = STATE_CHANGE;
|
2
|
1626 |
}
|
|
1627 |
return res;
|
|
1628 |
}
|
887
|
1629 |
return S_OK;
|
2
|
1630 |
}
|
|
1631 |
}
|
|
1632 |
|
887
|
1633 |
HRESULT D3DContext::EndScene() {
|
|
1634 |
if (bBeginScenePending) {
|
|
1635 |
FlushVertexQueue();
|
|
1636 |
bBeginScenePending = FALSE;
|
|
1637 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::EndScene");
|
|
1638 |
return pd3dDevice->EndScene();
|
|
1639 |
}
|
|
1640 |
return S_OK;
|
|
1641 |
}
|
|
1642 |
|
|
1643 |
/**
|
|
1644 |
* Compiles and links the given fragment shader program. If
|
|
1645 |
* successful, this function returns a handle to the newly created shader
|
|
1646 |
* program; otherwise returns 0.
|
2
|
1647 |
*/
|
887
|
1648 |
IDirect3DPixelShader9 *D3DContext::CreateFragmentProgram(DWORD **shaders,
|
|
1649 |
ShaderList *programs,
|
|
1650 |
jint flags)
|
2
|
1651 |
{
|
887
|
1652 |
DWORD *sourceCode;
|
|
1653 |
IDirect3DPixelShader9 *pProgram;
|
|
1654 |
|
|
1655 |
J2dTraceLn1(J2D_TRACE_INFO,
|
|
1656 |
"D3DContext::CreateFragmentProgram: flags=%d",
|
|
1657 |
flags);
|
2
|
1658 |
|
887
|
1659 |
sourceCode = shaders[flags];
|
|
1660 |
if (FAILED(pd3dDevice->CreatePixelShader(sourceCode, &pProgram))) {
|
|
1661 |
J2dRlsTraceLn(J2D_TRACE_ERROR,
|
|
1662 |
"D3DContext::CreateFragmentProgram: error creating program");
|
|
1663 |
return NULL;
|
2
|
1664 |
}
|
887
|
1665 |
|
|
1666 |
// add it to the cache
|
|
1667 |
ShaderList_AddProgram(programs, ptr_to_jlong(pProgram),
|
|
1668 |
0 /*unused*/, 0 /*unused*/, flags);
|
|
1669 |
|
|
1670 |
return pProgram;
|
2
|
1671 |
}
|
|
1672 |
|
887
|
1673 |
/**
|
|
1674 |
* Locates and enables a fragment program given a list of shader programs
|
|
1675 |
* (ShaderInfos), using this context's state and flags as search
|
|
1676 |
* parameters. The "flags" parameter is a bitwise-or'd value that helps
|
|
1677 |
* differentiate one program for another; the interpretation of this value
|
|
1678 |
* varies depending on the type of shader (BufImgOp, Paint, etc) but here
|
|
1679 |
* it is only used to find another ShaderInfo with that same "flags" value.
|
2
|
1680 |
*/
|
887
|
1681 |
HRESULT D3DContext::EnableFragmentProgram(DWORD **shaders,
|
|
1682 |
ShaderList *programList,
|
|
1683 |
jint flags)
|
2
|
1684 |
{
|
887
|
1685 |
HRESULT res;
|
|
1686 |
jlong programID;
|
|
1687 |
IDirect3DPixelShader9 *pProgram;
|
|
1688 |
|
|
1689 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::EnableFragmentProgram");
|
|
1690 |
|
|
1691 |
programID =
|
|
1692 |
ShaderList_FindProgram(programList,
|
|
1693 |
0 /*unused*/, 0 /*unused*/, flags);
|
2
|
1694 |
|
887
|
1695 |
pProgram = (IDirect3DPixelShader9 *)jlong_to_ptr(programID);
|
|
1696 |
if (pProgram == NULL) {
|
|
1697 |
pProgram = CreateFragmentProgram(shaders, programList, flags);
|
|
1698 |
if (pProgram == NULL) {
|
|
1699 |
return E_FAIL;
|
|
1700 |
}
|
2
|
1701 |
}
|
887
|
1702 |
|
|
1703 |
if (FAILED(res = pd3dDevice->SetPixelShader(pProgram))) {
|
|
1704 |
J2dRlsTraceLn(J2D_TRACE_ERROR,
|
|
1705 |
"D3DContext::EnableFragmentProgram: error setting pixel shader");
|
|
1706 |
return res;
|
|
1707 |
}
|
|
1708 |
|
|
1709 |
return S_OK;
|
2
|
1710 |
}
|
|
1711 |
|
887
|
1712 |
HRESULT D3DContext::EnableBasicGradientProgram(jint flags)
|
2
|
1713 |
{
|
887
|
1714 |
return EnableFragmentProgram((DWORD **)gradShaders,
|
|
1715 |
&basicGradPrograms, flags);
|
|
1716 |
}
|
2
|
1717 |
|
887
|
1718 |
HRESULT D3DContext::EnableLinearGradientProgram(jint flags)
|
|
1719 |
{
|
|
1720 |
return EnableFragmentProgram((DWORD **)linearShaders,
|
|
1721 |
&linearGradPrograms, flags);
|
2
|
1722 |
}
|
|
1723 |
|
887
|
1724 |
HRESULT D3DContext::EnableRadialGradientProgram(jint flags)
|
|
1725 |
{
|
|
1726 |
return EnableFragmentProgram((DWORD **)radialShaders,
|
|
1727 |
&radialGradPrograms, flags);
|
|
1728 |
}
|
|
1729 |
|
|
1730 |
HRESULT D3DContext::EnableConvolveProgram(jint flags)
|
2
|
1731 |
{
|
887
|
1732 |
return EnableFragmentProgram((DWORD **)convolveShaders,
|
|
1733 |
&convolvePrograms, flags);
|
|
1734 |
}
|
2
|
1735 |
|
887
|
1736 |
HRESULT D3DContext::EnableRescaleProgram(jint flags)
|
|
1737 |
{
|
|
1738 |
return EnableFragmentProgram((DWORD **)rescaleShaders,
|
|
1739 |
&rescalePrograms, flags);
|
|
1740 |
}
|
|
1741 |
|
|
1742 |
HRESULT D3DContext::EnableLookupProgram(jint flags)
|
|
1743 |
{
|
|
1744 |
return EnableFragmentProgram((DWORD **)lookupShaders,
|
|
1745 |
&lookupPrograms, flags);
|
2
|
1746 |
}
|
|
1747 |
|
887
|
1748 |
HRESULT D3DContext::EnableLCDTextProgram()
|
2
|
1749 |
{
|
887
|
1750 |
HRESULT res;
|
|
1751 |
|
|
1752 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::EnableLCDTextProgram");
|
2
|
1753 |
|
887
|
1754 |
if (lcdTextProgram == NULL) {
|
|
1755 |
if (FAILED(res = pd3dDevice->CreatePixelShader(lcdtext0,
|
|
1756 |
&lcdTextProgram)))
|
|
1757 |
{
|
|
1758 |
return res;
|
|
1759 |
}
|
|
1760 |
}
|
|
1761 |
|
|
1762 |
if (FAILED(res = pd3dDevice->SetPixelShader(lcdTextProgram))) {
|
|
1763 |
J2dRlsTraceLn(J2D_TRACE_ERROR,
|
|
1764 |
"D3DContext::EnableLCDTextProgram: error setting pixel shader");
|
|
1765 |
return res;
|
2
|
1766 |
}
|
|
1767 |
|
887
|
1768 |
return S_OK;
|
|
1769 |
}
|
|
1770 |
|
|
1771 |
HRESULT D3DContext::EnableAAParallelogramProgram()
|
|
1772 |
{
|
|
1773 |
HRESULT res;
|
|
1774 |
|
|
1775 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::EnableAAParallelogramProgram");
|
|
1776 |
|
|
1777 |
if (aaPgramProgram == NULL) {
|
|
1778 |
if (FAILED(res = pd3dDevice->CreatePixelShader(aapgram0,
|
|
1779 |
&aaPgramProgram))) {
|
|
1780 |
DebugPrintD3DError(res, "D3DContext::EnableAAParallelogramProgram: "
|
|
1781 |
"error creating pixel shader");
|
|
1782 |
return res;
|
|
1783 |
}
|
|
1784 |
}
|
|
1785 |
|
|
1786 |
if (FAILED(res = pd3dDevice->SetPixelShader(aaPgramProgram))) {
|
|
1787 |
DebugPrintD3DError(res, "D3DContext::EnableAAParallelogramProgram: "
|
|
1788 |
"error setting pixel shader");
|
|
1789 |
return res;
|
|
1790 |
}
|
|
1791 |
|
|
1792 |
return S_OK;
|
2
|
1793 |
}
|
|
1794 |
|
887
|
1795 |
HRESULT D3DContext::DisableAAParallelogramProgram()
|
2
|
1796 |
{
|
887
|
1797 |
HRESULT res;
|
|
1798 |
|
|
1799 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::DisableAAParallelogramProgram");
|
|
1800 |
|
|
1801 |
if (aaPgramProgram != NULL) {
|
|
1802 |
if (FAILED(res = pd3dDevice->SetPixelShader(NULL))) {
|
|
1803 |
DebugPrintD3DError(res,
|
|
1804 |
"D3DContext::DisableAAParallelogramProgram: "
|
|
1805 |
"error clearing pixel shader");
|
|
1806 |
return res;
|
|
1807 |
}
|
|
1808 |
}
|
2
|
1809 |
|
887
|
1810 |
return S_OK;
|
|
1811 |
}
|
|
1812 |
|
|
1813 |
BOOL D3DContext::IsAlphaRTSurfaceSupported()
|
|
1814 |
{
|
|
1815 |
HRESULT res = pd3dObject->CheckDeviceFormat(adapterOrdinal,
|
|
1816 |
devCaps.DeviceType,
|
|
1817 |
curParams.BackBufferFormat,
|
|
1818 |
D3DUSAGE_RENDERTARGET,
|
|
1819 |
D3DRTYPE_SURFACE,
|
|
1820 |
D3DFMT_A8R8G8B8);
|
|
1821 |
return SUCCEEDED(res);
|
2
|
1822 |
}
|
|
1823 |
|
887
|
1824 |
BOOL D3DContext::IsAlphaRTTSupported()
|
2
|
1825 |
{
|
887
|
1826 |
HRESULT res = pd3dObject->CheckDeviceFormat(adapterOrdinal,
|
|
1827 |
devCaps.DeviceType,
|
|
1828 |
curParams.BackBufferFormat,
|
|
1829 |
D3DUSAGE_RENDERTARGET,
|
|
1830 |
D3DRTYPE_TEXTURE,
|
|
1831 |
D3DFMT_A8R8G8B8);
|
|
1832 |
return SUCCEEDED(res);
|
|
1833 |
}
|
2
|
1834 |
|
887
|
1835 |
BOOL D3DContext::IsOpaqueRTTSupported()
|
|
1836 |
{
|
|
1837 |
HRESULT res = pd3dObject->CheckDeviceFormat(adapterOrdinal,
|
|
1838 |
devCaps.DeviceType,
|
|
1839 |
curParams.BackBufferFormat,
|
|
1840 |
D3DUSAGE_RENDERTARGET,
|
|
1841 |
D3DRTYPE_TEXTURE,
|
|
1842 |
curParams.BackBufferFormat);
|
|
1843 |
return SUCCEEDED(res);
|
2
|
1844 |
}
|
|
1845 |
|
887
|
1846 |
HRESULT D3DContext::InitContextCaps() {
|
|
1847 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::InitContextCaps");
|
|
1848 |
J2dTraceLn1(J2D_TRACE_VERBOSE, " caps for adapter %d :", adapterOrdinal);
|
|
1849 |
|
|
1850 |
if (pd3dDevice == NULL || pd3dObject == NULL) {
|
|
1851 |
contextCaps = CAPS_EMPTY;
|
|
1852 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_EMPTY");
|
|
1853 |
return E_FAIL;
|
|
1854 |
}
|
|
1855 |
|
|
1856 |
contextCaps = CAPS_DEVICE_OK;
|
|
1857 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_DEVICE_OK");
|
2
|
1858 |
|
887
|
1859 |
if (IsAlphaRTSurfaceSupported()) {
|
|
1860 |
contextCaps |= CAPS_RT_PLAIN_ALPHA;
|
|
1861 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_RT_PLAIN_ALPHA");
|
|
1862 |
}
|
|
1863 |
if (IsAlphaRTTSupported()) {
|
|
1864 |
contextCaps |= CAPS_RT_TEXTURE_ALPHA;
|
|
1865 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_RT_TEXTURE_ALPHA");
|
|
1866 |
}
|
|
1867 |
if (IsOpaqueRTTSupported()) {
|
|
1868 |
contextCaps |= CAPS_RT_TEXTURE_OPAQUE;
|
|
1869 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_RT_TEXTURE_OPAQUE");
|
|
1870 |
}
|
|
1871 |
if (IsPixelShader20Supported()) {
|
|
1872 |
contextCaps |= CAPS_LCD_SHADER | CAPS_BIOP_SHADER | CAPS_PS20;
|
|
1873 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE,
|
|
1874 |
" | CAPS_LCD_SHADER | CAPS_BIOP_SHADER | CAPS_PS20");
|
|
1875 |
// Pre-PS3.0 video boards are very slow with the AA shader, so
|
|
1876 |
// we will require PS30 hw even though the shader is compiled for 2.0a
|
|
1877 |
// if (IsGradientInstructionExtensionSupported()) {
|
|
1878 |
// contextCaps |= CAPS_AA_SHADER;
|
|
1879 |
// J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_AA_SHADER");
|
|
1880 |
// }
|
2
|
1881 |
}
|
887
|
1882 |
if (IsPixelShader30Supported()) {
|
|
1883 |
if ((contextCaps & CAPS_AA_SHADER) == 0) {
|
|
1884 |
// This flag was not already mentioned above...
|
|
1885 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_AA_SHADER");
|
|
1886 |
}
|
|
1887 |
contextCaps |= CAPS_PS30 | CAPS_AA_SHADER;
|
|
1888 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_PS30");
|
|
1889 |
}
|
|
1890 |
if (IsMultiTexturingSupported()) {
|
|
1891 |
contextCaps |= CAPS_MULTITEXTURE;
|
|
1892 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_MULTITEXTURE");
|
|
1893 |
}
|
|
1894 |
if (!IsPow2TexturesOnly()) {
|
|
1895 |
contextCaps |= CAPS_TEXNONPOW2;
|
|
1896 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_TEXNONPOW2");
|
|
1897 |
}
|
|
1898 |
if (!IsSquareTexturesOnly()) {
|
|
1899 |
contextCaps |= CAPS_TEXNONSQUARE;
|
|
1900 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE, " | CAPS_TEXNONSQUARE");
|
|
1901 |
}
|
|
1902 |
return S_OK;
|
2
|
1903 |
}
|