/[pcsx2_0.9.7]/trunk/plugins/zzogl-pg/opengl/zerogs.cpp
ViewVC logotype

Contents of /trunk/plugins/zzogl-pg/opengl/zerogs.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 6 months ago) by william
File size: 10355 byte(s)
re-commit (had local access denied errors when committing)
1 /* ZZ Open GL graphics plugin
2 * Copyright (c)2009-2010 zeydlitz@gmail.com, arcum42@gmail.com
3 * Based on Zerofrog's ZeroGS KOSMOS (c)2005-2008
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 //-------------------------- Includes
21 #include "Util.h"
22 #include "zerogs.h"
23 #include "ZZoglVB.h"
24
25 /////////////////////
26 // graphics resources
27
28 bool s_bTexFlush = false;
29 int s_nLastResolveReset = 0;
30 int s_nResolveCounts[30] = {0}; // resolve counts for last 30 frames
31 int s_nNewWidth = -1, s_nNewHeight = -1;
32
33 primInfo *prim;
34 ////////////////////
35 // State parameters
36 int g_nDepthUpdateCount = 0;
37
38 static ZeroGSInit s_ZeroGSInit;
39
40 // does one time only initializing/destruction
41
42 void HandleGLError()
43 {
44 FUNCLOG
45 // check the error status of this framebuffer */
46 GLenum error = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
47
48 // if error != GL_FRAMEBUFFER_COMPLETE_EXT, there's an error of some sort
49
50 if (error != 0)
51 {
52 int w = 0;
53 int h = 0;
54 GLint fmt;
55 glGetRenderbufferParameterivEXT(GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_INTERNAL_FORMAT_EXT, &fmt);
56 glGetRenderbufferParameterivEXT(GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_WIDTH_EXT, &w);
57 glGetRenderbufferParameterivEXT(GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_HEIGHT_EXT, &h);
58
59 switch (error)
60 {
61 case GL_FRAMEBUFFER_COMPLETE_EXT:
62 break;
63
64 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
65 ZZLog::Error_Log("Error! missing a required image/buffer attachment!");
66 break;
67
68 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
69 ZZLog::Error_Log("Error! has no images/buffers attached!");
70 break;
71
72 // case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
73 // ZZLog::Error_Log("Error! has an image/buffer attached in multiple locations!");
74 // break;
75
76 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
77 ZZLog::Error_Log("Error! has mismatched image/buffer dimensions!");
78 break;
79
80 case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
81 ZZLog::Error_Log("Error! colorbuffer attachments have different types!");
82 break;
83
84 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
85 ZZLog::Error_Log("Error! trying to draw to non-attached color buffer!");
86 break;
87
88 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
89 ZZLog::Error_Log("Error! trying to read from a non-attached color buffer!");
90 break;
91
92 case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
93 ZZLog::Error_Log("Error! format is not supported by current graphics card/driver!");
94 break;
95
96 default:
97 ZZLog::Error_Log("*UNKNOWN ERROR* reported from glCheckFramebufferStatusEXT(0x%x)!", error);
98 break;
99 }
100 }
101 }
102
103 void ZZGSStateReset()
104 {
105 FUNCLOG
106 icurctx = -1;
107
108 for (int i = 0; i < 2; ++i)
109 {
110 vb[i].Destroy();
111 memset(&vb[i], 0, sizeof(VB));
112
113 vb[i].tex0.tw = 1;
114 vb[i].tex0.th = 1;
115 vb[i].scissor.x1 = 639;
116 vb[i].scissor.y1 = 479;
117 vb[i].tex0.tbw = 64;
118 vb[i].Init(VB_BUFFERSIZE);
119 }
120
121 s_RangeMngr.Clear();
122
123 g_MemTargs.Destroy();
124 s_RTs.Destroy();
125 s_DepthRTs.Destroy();
126 s_BitwiseTextures.Destroy();
127
128 vb[0].ictx = 0;
129 vb[1].ictx = 1;
130 }
131
132 void SetDeviceSize(int nNewWidth, int nNewHeight)
133 {
134 FUNCLOG
135 s_nNewWidth = nNewWidth;
136 s_nNewHeight = nNewHeight;
137
138 if (!(conf.fullscreen()))
139 {
140 conf.width = nNewWidth;
141 conf.height = nNewHeight;
142 }
143 }
144
145 void ChangeDeviceSize(int nNewWidth, int nNewHeight)
146 {
147 FUNCLOG
148
149 Size oldSize = GLWin.backbuffer;
150
151 if (!ZZCreate(nNewWidth&~7, nNewHeight&~7))
152 {
153 ZZLog::Error_Log("Failed to recreate, changing to old device.");
154
155 if (!ZZCreate(oldSize.w, oldSize.h))
156 {
157 SysMessage("Failed to create device, exiting...");
158 exit(0);
159 }
160 }
161
162 for (int i = 0; i < 2; ++i)
163 {
164 vb[i].bNeedFrameCheck = vb[i].bNeedZCheck = 1;
165 vb[i].CheckFrame(0);
166 }
167
168 assert(vb[0].pBufferData != NULL && vb[1].pBufferData != NULL);
169 }
170
171 void SetAA(int mode)
172 {
173 FUNCLOG
174 float f = 1.0f;
175
176 // need to flush all targets
177 s_RTs.ResolveAll();
178 s_RTs.Destroy();
179 s_DepthRTs.ResolveAll();
180 s_DepthRTs.Destroy();
181
182 AA.x = AA.y = 0; // This is code for x0, x2, x4, x8 and x16 anti-aliasing.
183
184 if (mode > 0)
185 {
186 // ( 1, 0 ) ; ( 1, 1 ) ; ( 2, 1 ) ; ( 2, 2 )
187 // it's used as a binary shift, so x >> AA.x, y >> AA.y
188 AA.x = (mode + 1) / 2;
189 AA.y = mode / 2;
190 f = 2.0f;
191 }
192
193 memset(s_nResolveCounts, 0, sizeof(s_nResolveCounts));
194 s_nLastResolveReset = 0;
195
196 vb[0].prndr = NULL;
197 vb[0].pdepth = NULL;
198 vb[1].prndr = NULL;
199 vb[1].pdepth = NULL;
200
201 vb[0].bNeedFrameCheck = vb[0].bNeedZCheck = 1;
202 vb[1].bNeedFrameCheck = vb[1].bNeedZCheck = 1;
203
204 glPointSize(f);
205 }
206
207 //void RenderCustom(float fAlpha)
208 //{
209 // FUNCLOG
210 // GL_REPORT_ERROR();
211 //
212 // fAlpha = 1;
213 // glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // switch to the backbuffer
214 //
215 // DisableAllgl() ;
216 // SetShaderCaller("RenderCustom");
217 //
218 // glViewport(0, 0, GLWin.backbuffer.w, GLWin.backbuffer.h);
219 //
220 // // play custom animation
221 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
222 //
223 // // tex coords
224 // float4 v = float4(1 / 32767.0f, 1 / 32767.0f, 0, 0);
225 // ZZshSetParameter4fv(pvsBitBlt.prog, pvsBitBlt.sBitBltPos, v, "g_fBitBltPos");
226 // v.x = (float)nLogoWidth;
227 // v.y = (float)nLogoHeight;
228 // ZZshSetParameter4fv(pvsBitBlt.prog, pvsBitBlt.sBitBltTex, v, "g_fBitBltTex");
229 //
230 // v.x = v.y = v.z = v.w = fAlpha;
231 // ZZshSetParameter4fv(ppsBaseTexture.prog, ppsBaseTexture.sOneColor, v, "g_fOneColor");
232 //
233 // if (conf.wireframe()) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
234 //
235 // // inside vhDCb[0]'s target area, so render that region only
236 // ZZshGLSetTextureParameter(ppsBaseTexture.prog, ppsBaseTexture.sFinal, ptexLogo, "Logo");
237 // glBindBuffer(GL_ARRAY_BUFFER, vboRect);
238 //
239 // SET_STREAM();
240 //
241 // ZZshSetVertexShader(pvsBitBlt.prog);
242 // ZZshSetPixelShader(ppsBaseTexture.prog);
243 // DrawTriangleArray();
244 //
245 // // restore
246 // if (conf.wireframe()) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
247 //
248 // ProcessMessages();
249 //
250 // GLWin.SwapGLBuffers();
251 //
252 // glEnable(GL_SCISSOR_TEST);
253 // glEnable(GL_STENCIL_TEST);
254 //
255 // vb[0].bSyncVars = 0;
256 // vb[1].bSyncVars = 0;
257 //
258 // GL_REPORT_ERROR();
259 //}
260
261 //////////////////////////
262 // Internal Definitions //
263 //////////////////////////
264
265 __forceinline void SetFogColor(float4 v)
266 {
267 FUNCLOG
268
269 SetShaderCaller("SetFogColor");
270 ZZshSetParameter4fv(g_fparamFogColor, v, "g_fParamFogColor");
271 }
272
273 __forceinline void SetFogColor(u32 fog)
274 {
275 FUNCLOG
276
277 gs.fogcol = fog;
278
279 FlushBoth();
280
281 float4 v;
282
283 // set it immediately
284 v.SetColor(gs.fogcol);
285 SetFogColor(v);
286 }
287
288 __forceinline void SetFogColor(GIFRegFOGCOL* fog)
289 {
290 FUNCLOG
291
292 float4 v;
293
294 v.x = fog->FCR / 255.0f;
295 v.y = fog->FCG / 255.0f;
296 v.z = fog->FCB / 255.0f;
297 SetFogColor(v);
298 }
299
300 void ExtWrite()
301 {
302 FUNCLOG
303 ZZLog::Warn_Log("A hollow voice says 'EXTWRITE'! Nothing happens.");
304
305 // use local DISPFB, EXTDATA, EXTBUF, and PMODE
306 // int bpp, start, end;
307 // tex0Info texframe;
308
309 // bpp = 4;
310 // if( texframe.psm == PSMT16S ) bpp = 3;
311 // else if (PSMT_ISHALF(texframe.psm)) bpp = 2;
312 //
313 // // get the start and end addresses of the buffer
314 // GetRectMemAddress(start, end, texframe.psm, 0, 0, texframe.tw, texframe.th, texframe.tbp0, texframe.tbw);
315 }
316
317 ////////////
318 // Caches //
319 ////////////
320
321
322 // case 0: return false;
323 // case 1: break;
324 // case 2: m_CBP[0] = TEX0.CBP; break;
325 // case 3: m_CBP[1] = TEX0.CBP; break;
326 // case 4: if(m_CBP[0] == TEX0.CBP) return false; m_CBP[0] = TEX0.CBP; break;
327 // case 5: if(m_CBP[1] == TEX0.CBP) return false; m_CBP[1] = TEX0.CBP; break;
328 // case 6: ASSERT(0); return false; // ffx2 menu
329 // case 7: ASSERT(0); return false;
330 // default: __assume(0);
331
332 // cld state:
333 // 000 - clut data is not loaded; data in the temp buffer is stored
334 // 001 - clut data is always loaded.
335 // 010 - clut data is always loaded; cbp0 = cbp.
336 // 011 - clut data is always loadedl cbp1 = cbp.
337 // 100 - cbp0 is compared with cbp. if different, clut data is loaded.
338 // 101 - cbp1 is compared with cbp. if different, clut data is loaded.
339
340 // GSdx sets cbp0 & cbp1 when checking for clut changes. ZeroGS sets them in texClutWrite.
341 bool CheckChangeInClut(u32 highdword, u32 psm)
342 {
343 FUNCLOG
344 int cld = ZZOglGet_cld_TexBits(highdword);
345 int cbp = ZZOglGet_cbp_TexBits(highdword);
346
347 // processing the CLUT after tex0/2 are written
348 //ZZLog::Error_Log("high == 0x%x; cld == %d", highdword, cld);
349
350 switch (cld)
351 {
352 case 0:
353 return false;
354
355 case 1:
356 break;
357
358 case 2:
359 break;
360
361 case 3:
362 break;
363
364 case 4:
365 if (gs.cbp[0] == cbp) return false;
366 break;
367
368 case 5:
369 if (gs.cbp[1] == cbp) return false;
370 break;
371
372 default:
373 break;
374 }
375
376 // Compare the cache with current memory
377
378 // CSM2 is not supported
379 if (ZZOglGet_csm_TexBits(highdword))
380 return true;
381
382 int cpsm = ZZOglGet_cpsm_TexBits(highdword);
383 int csa = ZZOglGet_csa_TexBits(highdword);
384 int entries = PSMT_IS8CLUT(psm) ? 256 : 16;
385
386 u8* GSMem = g_pbyGSMemory + cbp * 256;
387
388 if (PSMT_IS32BIT(cpsm))
389 return Cmp_ClutBuffer_GSMem<u32>((u32*)GSMem, csa, entries*4);
390 else {
391 // Mana Khemia triggers this.
392 //ZZLog::Error_Log("16 bit clut not supported.");
393 return Cmp_ClutBuffer_GSMem<u16>((u16*)GSMem, csa, entries*2);
394 }
395 }
396
397 void texClutWrite(int ctx)
398 {
399 FUNCLOG
400 s_bTexFlush = false;
401
402 tex0Info& tex0 = vb[ctx].tex0;
403
404 assert(PSMT_ISCLUT(tex0.psm));
405
406 // processing the CLUT after tex0/2 are written
407 switch (tex0.cld)
408 {
409 case 0:
410 return;
411
412 case 1:
413 break; // tex0.cld is usually 1.
414
415 case 2:
416 gs.cbp[0] = tex0.cbp;
417 break;
418
419 case 3:
420 gs.cbp[1] = tex0.cbp;
421 break;
422
423 case 4:
424 if (gs.cbp[0] == tex0.cbp) return;
425 gs.cbp[0] = tex0.cbp;
426 break;
427
428 case 5:
429 if (gs.cbp[1] == tex0.cbp) return;
430 gs.cbp[1] = tex0.cbp;
431 break;
432
433 default: //ZZLog::Debug_Log("cld isn't 0-5!");
434 break;
435 }
436
437 Flush(!ctx);
438
439 // Write the memory to clut buffer
440 GSMem_to_ClutBuffer(tex0);
441 }
442

  ViewVC Help
Powered by ViewVC 1.1.22