2 Copyright (C) 2003 Rice1964
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include "osal_opengl.h"
22 #include "OGLExtensions.h"
23 #elif SDL_VIDEO_OPENGL_ES2
24 #include "OGLES2FragmentShaders.h"
27 #include "OGLRender.h"
28 #include "OGLGraphicsContext.h"
29 #include "OGLTexture.h"
30 #include "TextureManager.h"
33 //include "ae_bridge.h"
34 //static int hardwareType = HARDWARE_TYPE_UNKNOWN;
37 // FIXME: Use OGL internal L/T and matrix stack
38 // FIXME: Use OGL lookupAt function
39 // FIXME: Use OGL DisplayList
41 UVFlagMap OGLXUVFlagMaps[] =
43 {TEXTURE_UV_FLAG_WRAP, GL_REPEAT},
44 {TEXTURE_UV_FLAG_MIRROR, GL_MIRRORED_REPEAT_ARB},
45 {TEXTURE_UV_FLAG_CLAMP, GL_CLAMP},
48 //===================================================================
49 OGLRender::OGLRender()
51 COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext);
52 m_bSupportFogCoordExt = pcontext->m_bSupportFogCoord;
53 m_bMultiTexture = pcontext->m_bSupportMultiTexture;
54 m_bSupportClampToEdge = false;
55 for( int i=0; i<8; i++ )
58 m_texUnitEnabled[i]=FALSE;
61 m_bEnableMultiTexture = false;
65 OGLRender::~OGLRender()
70 bool OGLRender::InitDeviceObjects()
72 // enable Z-buffer by default
77 bool OGLRender::ClearDeviceObjects()
82 void OGLRender::Initialize(void)
84 glMatrixMode(GL_MODELVIEW);
89 glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
93 COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext);
94 if( pcontext->IsExtensionSupported("GL_IBM_texture_mirrored_repeat") )
96 OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT_IBM;
98 else if( pcontext->IsExtensionSupported("ARB_texture_mirrored_repeat") )
100 OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT_ARB;
104 OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_REPEAT;
107 if( pcontext->IsExtensionSupported("GL_ARB_texture_border_clamp") || pcontext->IsExtensionSupported("GL_EXT_texture_edge_clamp") )
109 m_bSupportClampToEdge = true;
110 OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP_TO_EDGE;
114 m_bSupportClampToEdge = false;
115 OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP;
118 glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
120 glEnableClientState( GL_VERTEX_ARRAY );
123 if( m_bMultiTexture )
125 pglClientActiveTextureARB( GL_TEXTURE0_ARB );
127 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
129 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
132 pglClientActiveTextureARB( GL_TEXTURE1_ARB );
134 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u) );
136 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
141 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
143 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
147 if (m_bSupportFogCoordExt)
149 pglFogCoordPointerEXT( GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][4]) );
151 glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT );
153 glFogi( GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT );
155 glFogi(GL_FOG_MODE, GL_LINEAR); // Fog Mode
157 glFogf(GL_FOG_DENSITY, 1.0f); // How Dense Will The Fog Be
159 glHint(GL_FOG_HINT, GL_FASTEST); // Fog Hint Value
161 glFogi( GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT );
163 glFogf( GL_FOG_START, 0.0f );
165 glFogf( GL_FOG_END, 1.0f );
169 //glColorPointer( 1, GL_UNSIGNED_BYTE, sizeof(TLITVERTEX), &g_vtxBuffer[0].r);
170 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
172 glEnableClientState( GL_COLOR_ARRAY );
175 if( pcontext->IsExtensionSupported("GL_NV_depth_clamp") )
177 glEnable(GL_DEPTH_CLAMP_NV);
181 #elif SDL_VIDEO_OPENGL_ES2
182 OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT;
183 m_bSupportClampToEdge = true;
184 OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP_TO_EDGE;
186 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
189 if( m_bMultiTexture )
191 glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u));
193 glVertexAttribPointer(VS_TEXCOORD1,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u));
198 glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u));
202 if (m_bSupportFogCoordExt)
204 glVertexAttribPointer(VS_FOG,1,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][4]));
208 glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
213 // hardwareType = Android_JNI_GetHardwareType();
216 //===================================================================
217 TextureFilterMap OglTexFilterMap[2]=
219 {FILTER_POINT, GL_NEAREST},
220 {FILTER_LINEAR, GL_LINEAR},
223 void OGLRender::ApplyTextureFilter()
225 static uint32 minflag=0xFFFF, magflag=0xFFFF;
228 if( m_texUnitEnabled[0] )
230 if( mtex != m_curBoundTex[0] )
232 mtex = m_curBoundTex[0];
233 minflag = m_dwMinFilter;
234 magflag = m_dwMagFilter;
235 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, OglTexFilterMap[m_dwMinFilter].realFilter);
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, OglTexFilterMap[m_dwMagFilter].realFilter);
242 if( minflag != (unsigned int)m_dwMinFilter )
244 minflag = m_dwMinFilter;
245 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, OglTexFilterMap[m_dwMinFilter].realFilter);
248 if( magflag != (unsigned int)m_dwMagFilter )
250 magflag = m_dwMagFilter;
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, OglTexFilterMap[m_dwMagFilter].realFilter);
258 void OGLRender::SetShadeMode(RenderShadeMode mode)
261 if( mode == SHADE_SMOOTH )
262 glShadeModel(GL_SMOOTH);
264 glShadeModel(GL_FLAT);
269 void OGLRender::ZBufferEnable(BOOL bZBuffer)
271 gRSP.bZBufferEnabled = bZBuffer;
272 if( g_curRomInfo.bForceDepthBuffer )
276 glDepthMask(GL_TRUE);
278 //glEnable(GL_DEPTH_TEST);
279 glDepthFunc( GL_LEQUAL );
284 glDepthMask(GL_FALSE);
286 //glDisable(GL_DEPTH_TEST);
287 glDepthFunc( GL_ALWAYS );
292 void OGLRender::ClearBuffer(bool cbuffer, bool zbuffer)
295 if( cbuffer ) flag |= GL_COLOR_BUFFER_BIT;
296 if( zbuffer ) flag |= GL_DEPTH_BUFFER_BIT;
297 float depth = ((gRDP.originalFillColor&0xFFFF)>>2)/(float)0x3FFF;
304 void OGLRender::ClearZBuffer(float depth)
306 uint32 flag=GL_DEPTH_BUFFER_BIT;
313 void OGLRender::SetZCompare(BOOL bZCompare)
315 if( g_curRomInfo.bForceDepthBuffer )
318 gRSP.bZBufferEnabled = bZCompare;
319 if( bZCompare == TRUE )
321 //glEnable(GL_DEPTH_TEST);
322 glDepthFunc( GL_LEQUAL );
327 //glDisable(GL_DEPTH_TEST);
328 glDepthFunc( GL_ALWAYS );
333 void OGLRender::SetZUpdate(BOOL bZUpdate)
335 if( g_curRomInfo.bForceDepthBuffer )
340 //glEnable(GL_DEPTH_TEST);
341 glDepthMask(GL_TRUE);
346 glDepthMask(GL_FALSE);
351 void OGLRender::ApplyZBias(int bias)
353 float f1 = bias > 0 ? -3.0f : 0.0f; // z offset = -3.0 * max(abs(dz/dx),abs(dz/dy)) per pixel delta z slope
354 float f2 = bias > 0 ? -3.0f : 0.0f; // z offset += -3.0 * 1 bit
357 // Android_JNI_GetPolygonOffset(hardwareType, bias, &f1, &f2);
358 // glPolygonOffset(0.2f, 0.2f);
363 glEnable(GL_POLYGON_OFFSET_FILL); // enable z offsets
368 glDisable(GL_POLYGON_OFFSET_FILL); // disable z offsets
371 glPolygonOffset(f1, f2); // set bias functions
375 void OGLRender::SetZBias(int bias)
377 #if defined(DEBUGGER)
378 if( pauseAtNext == true )
379 DebuggerAppendMsg("Set zbias = %d", bias);
381 // set member variable and apply the setting in opengl
386 void OGLRender::SetAlphaRef(uint32 dwAlpha)
388 if (m_dwAlpha != dwAlpha)
392 glAlphaFunc(GL_GEQUAL, (float)dwAlpha);
398 void OGLRender::ForceAlphaRef(uint32 dwAlpha)
401 float ref = dwAlpha/255.0f;
402 glAlphaFunc(GL_GEQUAL, ref);
404 #elif SDL_VIDEO_OPENGL_ES2
409 void OGLRender::SetFillMode(FillMode mode)
412 if( mode == RICE_FILLMODE_WINFRAME )
414 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
419 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
425 void OGLRender::SetCullMode(bool bCullFront, bool bCullBack)
427 CRender::SetCullMode(bCullFront, bCullBack);
428 if( bCullFront && bCullBack )
430 glCullFace(GL_FRONT_AND_BACK);
432 glEnable(GL_CULL_FACE);
435 else if( bCullFront )
437 glCullFace(GL_FRONT);
439 glEnable(GL_CULL_FACE);
446 glEnable(GL_CULL_FACE);
451 glDisable(GL_CULL_FACE);
456 bool OGLRender::SetCurrentTexture(int tile, CTexture *handler,uint32 dwTileWidth, uint32 dwTileHeight, TxtrCacheEntry *pTextureEntry)
458 RenderTexture &texture = g_textures[tile];
459 texture.pTextureEntry = pTextureEntry;
461 if( handler!= NULL && texture.m_lpsTexturePtr != handler->GetTexture() )
463 texture.m_pCTexture = handler;
464 texture.m_lpsTexturePtr = handler->GetTexture();
466 texture.m_dwTileWidth = dwTileWidth;
467 texture.m_dwTileHeight = dwTileHeight;
469 if( handler->m_bIsEnhancedTexture )
471 texture.m_fTexWidth = (float)pTextureEntry->pTexture->m_dwCreatedTextureWidth;
472 texture.m_fTexHeight = (float)pTextureEntry->pTexture->m_dwCreatedTextureHeight;
476 texture.m_fTexWidth = (float)handler->m_dwCreatedTextureWidth;
477 texture.m_fTexHeight = (float)handler->m_dwCreatedTextureHeight;
484 bool OGLRender::SetCurrentTexture(int tile, TxtrCacheEntry *pEntry)
486 if (pEntry != NULL && pEntry->pTexture != NULL)
488 SetCurrentTexture( tile, pEntry->pTexture, pEntry->ti.WidthToCreate, pEntry->ti.HeightToCreate, pEntry);
493 SetCurrentTexture( tile, NULL, 64, 64, NULL );
499 void OGLRender::SetAddressUAllStages(uint32 dwTile, TextureUVFlag dwFlag)
501 SetTextureUFlag(dwFlag, dwTile);
504 void OGLRender::SetAddressVAllStages(uint32 dwTile, TextureUVFlag dwFlag)
506 SetTextureVFlag(dwFlag, dwTile);
509 void OGLRender::SetTexWrapS(int unitno,GLuint flag)
516 DebuggerAppendMsg("Check me, unitno != 0 in base ogl");
519 if( m_curBoundTex[0] != mtex || mflag != flag )
521 mtex = m_curBoundTex[0];
523 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, flag);
527 void OGLRender::SetTexWrapT(int unitno,GLuint flag)
531 if( m_curBoundTex[0] != mtex || mflag != flag )
533 mtex = m_curBoundTex[0];
535 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, flag);
540 void OGLRender::SetTextureUFlag(TextureUVFlag dwFlag, uint32 dwTile)
542 TileUFlags[dwTile] = dwFlag;
543 if( dwTile == gRSP.curTile ) // For basic OGL, only support the 1st texel
545 COGLTexture* pTexture = g_textures[gRSP.curTile].m_pCOGLTexture;
548 EnableTexUnit(0,TRUE);
549 BindTexture(pTexture->m_dwTextureName, 0);
551 SetTexWrapS(0, OGLXUVFlagMaps[dwFlag].realFlag);
554 void OGLRender::SetTextureVFlag(TextureUVFlag dwFlag, uint32 dwTile)
556 TileVFlags[dwTile] = dwFlag;
557 if( dwTile == gRSP.curTile ) // For basic OGL, only support the 1st texel
559 COGLTexture* pTexture = g_textures[gRSP.curTile].m_pCOGLTexture;
562 EnableTexUnit(0,TRUE);
563 BindTexture(pTexture->m_dwTextureName, 0);
565 SetTexWrapT(0, OGLXUVFlagMaps[dwFlag].realFlag);
569 // Basic render drawing functions
571 bool OGLRender::RenderTexRect()
573 glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
576 GLboolean cullface = glIsEnabled(GL_CULL_FACE);
577 glDisable(GL_CULL_FACE);
580 float depth = -(g_texRectTVtx[3].z*2-1);
584 glBegin(GL_TRIANGLE_FAN);
586 glColor4f(g_texRectTVtx[3].r, g_texRectTVtx[3].g, g_texRectTVtx[3].b, g_texRectTVtx[3].a);
587 TexCoord(g_texRectTVtx[3]);
588 glVertex3f(g_texRectTVtx[3].x, g_texRectTVtx[3].y, depth);
590 glColor4f(g_texRectTVtx[2].r, g_texRectTVtx[2].g, g_texRectTVtx[2].b, g_texRectTVtx[2].a);
591 TexCoord(g_texRectTVtx[2]);
592 glVertex3f(g_texRectTVtx[2].x, g_texRectTVtx[2].y, depth);
594 glColor4f(g_texRectTVtx[1].r, g_texRectTVtx[1].g, g_texRectTVtx[1].b, g_texRectTVtx[1].a);
595 TexCoord(g_texRectTVtx[1]);
596 glVertex3f(g_texRectTVtx[1].x, g_texRectTVtx[1].y, depth);
598 glColor4f(g_texRectTVtx[0].r, g_texRectTVtx[0].g, g_texRectTVtx[0].b, g_texRectTVtx[0].a);
599 TexCoord(g_texRectTVtx[0]);
600 glVertex3f(g_texRectTVtx[0].x, g_texRectTVtx[0].y, depth);
605 #elif SDL_VIDEO_OPENGL_ES2
608 g_texRectTVtx[3].r, g_texRectTVtx[3].g, g_texRectTVtx[3].b, g_texRectTVtx[3].a,
609 g_texRectTVtx[2].r, g_texRectTVtx[2].g, g_texRectTVtx[2].b, g_texRectTVtx[2].a,
610 g_texRectTVtx[1].r, g_texRectTVtx[1].g, g_texRectTVtx[1].b, g_texRectTVtx[1].a,
611 g_texRectTVtx[0].r, g_texRectTVtx[0].g, g_texRectTVtx[0].b, g_texRectTVtx[0].a
615 g_texRectTVtx[3].tcord[0].u,g_texRectTVtx[3].tcord[0].v,
616 g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
617 g_texRectTVtx[1].tcord[0].u,g_texRectTVtx[1].tcord[0].v,
618 g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v
622 g_texRectTVtx[3].tcord[1].u,g_texRectTVtx[3].tcord[1].v,
623 g_texRectTVtx[2].tcord[1].u,g_texRectTVtx[2].tcord[1].v,
624 g_texRectTVtx[1].tcord[1].u,g_texRectTVtx[1].tcord[1].v,
625 g_texRectTVtx[0].tcord[1].u,g_texRectTVtx[0].tcord[1].v
628 float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
630 GLfloat vertices[] = {
631 -inv + g_texRectTVtx[3].x / w, inv - g_texRectTVtx[3].y / h, depth, 1,
632 -inv + g_texRectTVtx[2].x / w, inv - g_texRectTVtx[2].y / h, depth, 1,
633 -inv + g_texRectTVtx[1].x / w, inv - g_texRectTVtx[1].y / h, depth, 1,
634 -inv + g_texRectTVtx[0].x / w, inv - g_texRectTVtx[0].y / h, depth, 1
637 glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_TRUE, 0, &colour );
638 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
639 glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, 0, &tex);
640 glVertexAttribPointer(VS_TEXCOORD1,2,GL_FLOAT,GL_FALSE, 0, &tex2);
642 glDrawArrays(GL_TRIANGLE_FAN,0,4);
645 //Restore old pointers
646 glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
647 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
648 glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u));
649 glVertexAttribPointer(VS_TEXCOORD1,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u));
652 if( cullface ) glEnable(GL_CULL_FACE);
658 bool OGLRender::RenderFillRect(uint32 dwColor, float depth)
660 float a = (dwColor>>24)/255.0f;
661 float r = ((dwColor>>16)&0xFF)/255.0f;
662 float g = ((dwColor>>8)&0xFF)/255.0f;
663 float b = (dwColor&0xFF)/255.0f;
664 glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
667 GLboolean cullface = glIsEnabled(GL_CULL_FACE);
668 glDisable(GL_CULL_FACE);
673 glBegin(GL_TRIANGLE_FAN);
675 glVertex4f(m_fillRectVtx[0].x, m_fillRectVtx[1].y, depth, 1);
676 glVertex4f(m_fillRectVtx[1].x, m_fillRectVtx[1].y, depth, 1);
677 glVertex4f(m_fillRectVtx[1].x, m_fillRectVtx[0].y, depth, 1);
678 glVertex4f(m_fillRectVtx[0].x, m_fillRectVtx[0].y, depth, 1);
682 #elif SDL_VIDEO_OPENGL_ES2
690 float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
692 GLfloat vertices[] = {
693 -inv + m_fillRectVtx[0].x / w, inv - m_fillRectVtx[1].y / h, depth, 1,
694 -inv + m_fillRectVtx[1].x / w, inv - m_fillRectVtx[1].y / h, depth, 1,
695 -inv + m_fillRectVtx[1].x / w, inv - m_fillRectVtx[0].y / h, depth, 1,
696 -inv + m_fillRectVtx[0].x / w, inv - m_fillRectVtx[0].y / h, depth, 1
699 glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_FALSE, 0, &colour );
700 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
701 glDisableVertexAttribArray(VS_TEXCOORD0);
702 glDisableVertexAttribArray(VS_TEXCOORD1);
704 glDrawArrays(GL_TRIANGLE_FAN,0,4);
707 //Restore old pointers
708 glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
709 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
710 glEnableVertexAttribArray(VS_TEXCOORD0);
711 glEnableVertexAttribArray(VS_TEXCOORD1);
715 if( cullface ) glEnable(GL_CULL_FACE);
721 bool OGLRender::RenderLine3D()
724 ApplyZBias(0); // disable z offsets
726 glBegin(GL_TRIANGLE_FAN);
728 glColor4f(m_line3DVtx[1].r, m_line3DVtx[1].g, m_line3DVtx[1].b, m_line3DVtx[1].a);
729 glVertex3f(m_line3DVector[3].x, m_line3DVector[3].y, -m_line3DVtx[1].z);
730 glVertex3f(m_line3DVector[2].x, m_line3DVector[2].y, -m_line3DVtx[0].z);
732 glColor4ub(m_line3DVtx[0].r, m_line3DVtx[0].g, m_line3DVtx[0].b, m_line3DVtx[0].a);
733 glVertex3f(m_line3DVector[1].x, m_line3DVector[1].y, -m_line3DVtx[1].z);
734 glVertex3f(m_line3DVector[0].x, m_line3DVector[0].y, -m_line3DVtx[0].z);
739 ApplyZBias(m_dwZBias); // set Z offset back to previous value
745 extern FiddledVtx * g_pVtxBase;
747 // This is so weired that I can not do vertex transform by myself. I have to use
748 // OpenGL internal transform
749 bool OGLRender::RenderFlushTris()
751 if( !m_bSupportFogCoordExt )
752 SetFogFlagForNegativeW();
755 if( !gRDP.bFogEnableInBlender && gRSP.bFogEnabled )
761 ApplyZBias(m_dwZBias); // set the bias factors
763 glViewportWrapper(windowSetting.vpLeftW, windowSetting.uDisplayHeight-windowSetting.vpTopW-windowSetting.vpHeightW+windowSetting.statusBarHeightToUse, windowSetting.vpWidthW, windowSetting.vpHeightW, false);
766 //if options.bOGLVertexClipper == FALSE )
768 glDrawElements( GL_TRIANGLES, gRSP.numVertices, GL_UNSIGNED_SHORT, g_vtxIndex );
773 //ClipVertexesOpenGL();
776 glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5Clipped[0][0]) );
777 glEnableClientState( GL_VERTEX_ARRAY );
779 pglClientActiveTextureARB( GL_TEXTURE0_ARB );
780 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_clippedVtxBuffer[0].tcord[0].u) );
781 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
783 pglClientActiveTextureARB( GL_TEXTURE1_ARB );
784 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_clippedVtxBuffer[0].tcord[1].u) );
785 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
787 glDrawElements( GL_TRIANGLES, gRSP.numVertices, GL_UNSIGNED_INT, g_vtxIndex );
790 pglClientActiveTextureARB( GL_TEXTURE0_ARB );
791 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) );
792 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
794 pglClientActiveTextureARB( GL_TEXTURE1_ARB );
795 glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u) );
796 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
798 glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) );
799 glEnableClientState( GL_VERTEX_ARRAY );
803 if( !m_bSupportFogCoordExt )
807 if( !gRDP.bFogEnableInBlender && gRSP.bFogEnabled )
815 void OGLRender::DrawSimple2DTexture(float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, COLOR dif, COLOR spe, float z, float rhw)
817 if( status.bVIOriginIsUpdated == true && currentRomOptions.screenUpdateSetting==SCREEN_UPDATE_AT_1ST_PRIMITIVE )
819 status.bVIOriginIsUpdated=false;
820 CGraphicsContext::Get()->UpdateFrame();
821 DEBUGGER_PAUSE_AND_DUMP_NO_UPDATE(NEXT_SET_CIMG,{DebuggerAppendMsg("Screen Update at 1st Simple2DTexture");});
824 StartDrawSimple2DTexture(x0, y0, x1, y1, u0, v0, u1, v1, dif, spe, z, rhw);
826 GLboolean cullface = glIsEnabled(GL_CULL_FACE);
827 glDisable(GL_CULL_FACE);
829 glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight);
832 float a = (g_texRectTVtx[0].dcDiffuse >>24)/255.0f;
833 float r = ((g_texRectTVtx[0].dcDiffuse>>16)&0xFF)/255.0f;
834 float g = ((g_texRectTVtx[0].dcDiffuse>>8)&0xFF)/255.0f;
835 float b = (g_texRectTVtx[0].dcDiffuse&0xFF)/255.0f;
839 glBegin(GL_TRIANGLES);
843 OGLRender::TexCoord(g_texRectTVtx[0]);
844 glVertex3f(g_texRectTVtx[0].x, g_texRectTVtx[0].y, -g_texRectTVtx[0].z);
846 OGLRender::TexCoord(g_texRectTVtx[1]);
847 glVertex3f(g_texRectTVtx[1].x, g_texRectTVtx[1].y, -g_texRectTVtx[1].z);
849 OGLRender::TexCoord(g_texRectTVtx[2]);
850 glVertex3f(g_texRectTVtx[2].x, g_texRectTVtx[2].y, -g_texRectTVtx[2].z);
852 OGLRender::TexCoord(g_texRectTVtx[0]);
853 glVertex3f(g_texRectTVtx[0].x, g_texRectTVtx[0].y, -g_texRectTVtx[0].z);
855 OGLRender::TexCoord(g_texRectTVtx[2]);
856 glVertex3f(g_texRectTVtx[2].x, g_texRectTVtx[2].y, -g_texRectTVtx[2].z);
858 OGLRender::TexCoord(g_texRectTVtx[3]);
859 glVertex3f(g_texRectTVtx[3].x, g_texRectTVtx[3].y, -g_texRectTVtx[3].z);
864 #elif SDL_VIDEO_OPENGL_ES2
876 g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v,
877 g_texRectTVtx[1].tcord[0].u,g_texRectTVtx[1].tcord[0].v,
878 g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
880 g_texRectTVtx[0].tcord[0].u,g_texRectTVtx[0].tcord[0].v,
881 g_texRectTVtx[2].tcord[0].u,g_texRectTVtx[2].tcord[0].v,
882 g_texRectTVtx[3].tcord[0].u,g_texRectTVtx[3].tcord[0].v,
886 g_texRectTVtx[0].tcord[1].u,g_texRectTVtx[0].tcord[1].v,
887 g_texRectTVtx[1].tcord[1].u,g_texRectTVtx[1].tcord[1].v,
888 g_texRectTVtx[2].tcord[1].u,g_texRectTVtx[2].tcord[1].v,
890 g_texRectTVtx[0].tcord[1].u,g_texRectTVtx[0].tcord[1].v,
891 g_texRectTVtx[2].tcord[1].u,g_texRectTVtx[2].tcord[1].v,
892 g_texRectTVtx[3].tcord[1].u,g_texRectTVtx[3].tcord[1].v,
895 float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
897 GLfloat vertices[] = {
898 -inv + g_texRectTVtx[0].x/ w, inv - g_texRectTVtx[0].y/ h, -g_texRectTVtx[0].z,1,
899 -inv + g_texRectTVtx[1].x/ w, inv - g_texRectTVtx[1].y/ h, -g_texRectTVtx[1].z,1,
900 -inv + g_texRectTVtx[2].x/ w, inv - g_texRectTVtx[2].y/ h, -g_texRectTVtx[2].z,1,
902 -inv + g_texRectTVtx[0].x/ w, inv - g_texRectTVtx[0].y/ h, -g_texRectTVtx[0].z,1,
903 -inv + g_texRectTVtx[2].x/ w, inv - g_texRectTVtx[2].y/ h, -g_texRectTVtx[2].z,1,
904 -inv + g_texRectTVtx[3].x/ w, inv - g_texRectTVtx[3].y/ h, -g_texRectTVtx[3].z,1
907 glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_FALSE, 0, &colour );
908 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
909 glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, 0, &tex);
910 glVertexAttribPointer(VS_TEXCOORD1,2,GL_FLOAT,GL_FALSE, 0, &tex2);
912 glDrawArrays(GL_TRIANGLES,0,6);
915 //Restore old pointers
916 glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
917 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
918 glVertexAttribPointer(VS_TEXCOORD0,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u));
919 glVertexAttribPointer(VS_TEXCOORD1,2,GL_FLOAT,GL_FALSE, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u));
923 if( cullface ) glEnable(GL_CULL_FACE);
927 void OGLRender::DrawSimpleRect(int nX0, int nY0, int nX1, int nY1, uint32 dwColor, float depth, float rhw)
929 StartDrawSimpleRect(nX0, nY0, nX1, nY1, dwColor, depth, rhw);
931 GLboolean cullface = glIsEnabled(GL_CULL_FACE);
932 glDisable(GL_CULL_FACE);
935 float a = (dwColor>>24)/255.0f;
936 float r = ((dwColor>>16)&0xFF)/255.0f;
937 float g = ((dwColor>>8)&0xFF)/255.0f;
938 float b = (dwColor&0xFF)/255.0f;
942 glBegin(GL_TRIANGLE_FAN);
945 glVertex3f(m_simpleRectVtx[1].x, m_simpleRectVtx[0].y, -depth);
946 glVertex3f(m_simpleRectVtx[1].x, m_simpleRectVtx[1].y, -depth);
947 glVertex3f(m_simpleRectVtx[0].x, m_simpleRectVtx[1].y, -depth);
948 glVertex3f(m_simpleRectVtx[0].x, m_simpleRectVtx[0].y, -depth);
953 #elif SDL_VIDEO_OPENGL_ES2
960 float w = windowSetting.uDisplayWidth / 2.0f, h = windowSetting.uDisplayHeight / 2.0f, inv = 1.0f;
962 GLfloat vertices[] = {
963 -inv + m_simpleRectVtx[1].x / w, inv - m_simpleRectVtx[0].y / h, -depth, 1,
964 -inv + m_simpleRectVtx[1].x / w, inv - m_simpleRectVtx[1].y / h, -depth, 1,
965 -inv + m_simpleRectVtx[0].x / w, inv - m_simpleRectVtx[1].y / h, -depth, 1,
966 -inv + m_simpleRectVtx[0].x / w, inv - m_simpleRectVtx[0].y / h, -depth, 1
969 glVertexAttribPointer(VS_COLOR, 4, GL_FLOAT,GL_FALSE, 0, &colour );
970 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,0,&vertices);
971 glDisableVertexAttribArray(VS_TEXCOORD0);
972 glDisableVertexAttribArray(VS_TEXCOORD1);
974 glDrawArrays(GL_TRIANGLE_FAN,0,4);
977 //Restore old pointers
978 glVertexAttribPointer(VS_COLOR, 4, GL_UNSIGNED_BYTE,GL_TRUE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) );
979 glVertexAttribPointer(VS_POSITION,4,GL_FLOAT,GL_FALSE,sizeof(float)*5,&(g_vtxProjected5[0][0]));
980 glEnableVertexAttribArray(VS_TEXCOORD0);
981 glEnableVertexAttribArray(VS_TEXCOORD1);
985 if( cullface ) glEnable(GL_CULL_FACE);
989 void OGLRender::InitCombinerBlenderForSimpleRectDraw(uint32 tile)
991 //glEnable(GL_CULL_FACE);
992 EnableTexUnit(0,FALSE);
996 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
998 //glEnable(GL_ALPHA_TEST);
1001 COLOR OGLRender::PostProcessDiffuseColor(COLOR curDiffuseColor)
1003 uint32 color = curDiffuseColor;
1004 uint32 colorflag = m_pColorCombiner->m_pDecodedMux->m_dwShadeColorChannelFlag;
1005 uint32 alphaflag = m_pColorCombiner->m_pDecodedMux->m_dwShadeAlphaChannelFlag;
1006 if( colorflag+alphaflag != MUX_0 )
1008 if( (colorflag & 0xFFFFFF00) == 0 && (alphaflag & 0xFFFFFF00) == 0 )
1010 color = (m_pColorCombiner->GetConstFactor(colorflag, alphaflag, curDiffuseColor));
1013 color = (CalculateConstFactor(colorflag, alphaflag, curDiffuseColor));
1016 //return (color<<8)|(color>>24);
1020 COLOR OGLRender::PostProcessSpecularColor()
1025 void OGLRender::SetViewportRender()
1027 glViewportWrapper(windowSetting.vpLeftW, windowSetting.uDisplayHeight-windowSetting.vpTopW-windowSetting.vpHeightW+windowSetting.statusBarHeightToUse, windowSetting.vpWidthW, windowSetting.vpHeightW);
1028 OPENGL_CHECK_ERRORS;
1031 void OGLRender::RenderReset()
1033 CRender::RenderReset();
1035 glMatrixMode(GL_PROJECTION);
1036 OPENGL_CHECK_ERRORS;
1038 OPENGL_CHECK_ERRORS;
1039 glOrtho(0, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, 0, -1, 1);
1040 OPENGL_CHECK_ERRORS;
1043 glMatrixMode(GL_MODELVIEW);
1044 OPENGL_CHECK_ERRORS;
1046 OPENGL_CHECK_ERRORS;
1049 void OGLRender::SetAlphaTestEnable(BOOL bAlphaTestEnable)
1052 if( bAlphaTestEnable && debuggerEnableAlphaTest )
1055 #if SDL_VIDEO_OPENGL
1056 if( bAlphaTestEnable )
1057 glEnable(GL_ALPHA_TEST);
1059 glDisable(GL_ALPHA_TEST);
1060 OPENGL_CHECK_ERRORS;
1061 #elif SDL_VIDEO_OPENGL_ES2
1062 COGL_FragmentProgramCombiner* frag = (COGL_FragmentProgramCombiner*)m_pColorCombiner;
1063 frag->SetAlphaTestState(bAlphaTestEnable);
1069 void OGLRender::BindTexture(GLuint texture, int unitno)
1074 DebuggerAppendMsg("Check me, base ogl bind texture, unit no != 0");
1077 if( m_curBoundTex[0] != texture )
1079 glBindTexture(GL_TEXTURE_2D,texture);
1080 OPENGL_CHECK_ERRORS;
1081 m_curBoundTex[0] = texture;
1085 void OGLRender::DisBindTexture(GLuint texture, int unitno)
1087 //EnableTexUnit(0,FALSE);
1088 //glBindTexture(GL_TEXTURE_2D, 0); //Not to bind any texture
1091 void OGLRender::EnableTexUnit(int unitno, BOOL flag)
1096 DebuggerAppendMsg("Check me, in the base ogl render, unitno!=0");
1099 if( m_texUnitEnabled[0] != flag )
1101 m_texUnitEnabled[0] = flag;
1104 glEnable(GL_TEXTURE_2D);
1106 glDisable(GL_TEXTURE_2D);
1110 void OGLRender::TexCoord2f(float u, float v)
1115 void OGLRender::TexCoord(TLITVERTEX &vtxInfo)
1117 glTexCoord2f(vtxInfo.tcord[0].u, vtxInfo.tcord[0].v);
1120 void OGLRender::UpdateScissor()
1122 if( options.bEnableHacks && g_CI.dwWidth == 0x200 && gRDP.scissor.right == 0x200 && g_CI.dwWidth>(*g_GraphicsInfo.VI_WIDTH_REG & 0xFFF) )
1125 uint32 width = *g_GraphicsInfo.VI_WIDTH_REG & 0xFFF;
1126 uint32 height = (gRDP.scissor.right*gRDP.scissor.bottom)/width;
1127 glEnable(GL_SCISSOR_TEST);
1128 OPENGL_CHECK_ERRORS;
1129 glScissor(windowSetting.uDisplayX, windowSetting.uDisplayY+int(height*windowSetting.fMultY+windowSetting.statusBarHeightToUse),
1130 int(width*windowSetting.fMultX), int(height*windowSetting.fMultY) );
1131 OPENGL_CHECK_ERRORS;
1135 UpdateScissorWithClipRatio();
1139 void OGLRender::ApplyRDPScissor(bool force)
1141 if( !force && status.curScissor == RDP_SCISSOR ) return;
1143 if( options.bEnableHacks && g_CI.dwWidth == 0x200 && gRDP.scissor.right == 0x200 && g_CI.dwWidth>(*g_GraphicsInfo.VI_WIDTH_REG & 0xFFF) )
1146 uint32 width = *g_GraphicsInfo.VI_WIDTH_REG & 0xFFF;
1147 uint32 height = (gRDP.scissor.right*gRDP.scissor.bottom)/width;
1148 glEnable(GL_SCISSOR_TEST);
1149 OPENGL_CHECK_ERRORS;
1150 glScissor(windowSetting.uDisplayX, windowSetting.uDisplayY+int(height*windowSetting.fMultY+windowSetting.statusBarHeightToUse),
1151 int(width*windowSetting.fMultX), int(height*windowSetting.fMultY) );
1152 OPENGL_CHECK_ERRORS;
1156 glScissor(windowSetting.uDisplayX+int(gRDP.scissor.left*windowSetting.fMultX), windowSetting.uDisplayY+int((windowSetting.uViHeight-gRDP.scissor.bottom)*windowSetting.fMultY+windowSetting.statusBarHeightToUse),
1157 int((gRDP.scissor.right-gRDP.scissor.left)*windowSetting.fMultX), int((gRDP.scissor.bottom-gRDP.scissor.top)*windowSetting.fMultY ));
1158 OPENGL_CHECK_ERRORS;
1161 status.curScissor = RDP_SCISSOR;
1164 void OGLRender::ApplyScissorWithClipRatio(bool force)
1166 if( !force && status.curScissor == RSP_SCISSOR ) return;
1168 glEnable(GL_SCISSOR_TEST);
1169 OPENGL_CHECK_ERRORS;
1170 glScissor(windowSetting.uDisplayX+windowSetting.clipping.left, windowSetting.uDisplayY+int((windowSetting.uViHeight-gRSP.real_clip_scissor_bottom)*windowSetting.fMultY)+windowSetting.statusBarHeightToUse,
1171 windowSetting.clipping.width, windowSetting.clipping.height);
1172 OPENGL_CHECK_ERRORS;
1174 status.curScissor = RSP_SCISSOR;
1177 void OGLRender::SetFogMinMax(float fMin, float fMax)
1179 #if SDL_VIDEO_OPENGL
1180 glFogf(GL_FOG_START, gRSPfFogMin); // Fog Start Depth
1181 OPENGL_CHECK_ERRORS;
1182 glFogf(GL_FOG_END, gRSPfFogMax); // Fog End Depth
1183 OPENGL_CHECK_ERRORS;
1187 void OGLRender::TurnFogOnOff(bool flag)
1189 #if SDL_VIDEO_OPENGL
1194 OPENGL_CHECK_ERRORS;
1195 #elif SDL_VIDEO_OPENGL_ES2
1196 ((COGL_FragmentProgramCombiner*)m_pColorCombiner)->SetFogState(flag);
1197 OPENGL_CHECK_ERRORS;
1201 void OGLRender::SetFogEnable(bool bEnable)
1203 DEBUGGER_IF_DUMP( (gRSP.bFogEnabled != (bEnable==TRUE) && logFog ), TRACE1("Set Fog %s", bEnable? "enable":"disable"));
1205 gRSP.bFogEnabled = bEnable&&(options.fogMethod == 1);
1208 if(options.fogMethod == 2)
1210 gRSP.bFogEnabled = true;
1213 #if SDL_VIDEO_OPENGL
1214 if( gRSP.bFogEnabled )
1216 //TRACE2("Enable fog, min=%f, max=%f",gRSPfFogMin,gRSPfFogMax );
1217 glFogfv(GL_FOG_COLOR, gRDP.fvFogColor); // Set Fog Color
1218 OPENGL_CHECK_ERRORS;
1219 glFogf(GL_FOG_START, gRSPfFogMin); // Fog Start Depth
1220 OPENGL_CHECK_ERRORS;
1221 glFogf(GL_FOG_END, gRSPfFogMax); // Fog End Depth
1222 OPENGL_CHECK_ERRORS;
1224 OPENGL_CHECK_ERRORS;
1229 OPENGL_CHECK_ERRORS;
1231 #elif SDL_VIDEO_OPENGL_ES2
1232 ((COGL_FragmentProgramCombiner*)m_pColorCombiner)->SetFogState(gRSP.bFogEnabled);
1233 OPENGL_CHECK_ERRORS;
1237 void OGLRender::SetFogColor(uint32 r, uint32 g, uint32 b, uint32 a)
1239 gRDP.fogColor = COLOR_RGBA(r, g, b, a);
1240 gRDP.fvFogColor[0] = r/255.0f; //r
1241 gRDP.fvFogColor[1] = g/255.0f; //g
1242 gRDP.fvFogColor[2] = b/255.0f; //b
1243 gRDP.fvFogColor[3] = a/255.0f; //a
1244 #if SDL_VIDEO_OPENGL
1245 glFogfv(GL_FOG_COLOR, gRDP.fvFogColor); // Set Fog Color
1247 OPENGL_CHECK_ERRORS;
1250 void OGLRender::DisableMultiTexture()
1252 pglActiveTexture(GL_TEXTURE1_ARB);
1253 OPENGL_CHECK_ERRORS;
1254 EnableTexUnit(1,FALSE);
1255 pglActiveTexture(GL_TEXTURE0_ARB);
1256 OPENGL_CHECK_ERRORS;
1257 EnableTexUnit(0,FALSE);
1258 pglActiveTexture(GL_TEXTURE0_ARB);
1259 OPENGL_CHECK_ERRORS;
1260 EnableTexUnit(0,TRUE);
1263 void OGLRender::EndRendering(void)
1265 #if SDL_VIDEO_OPENGL
1267 OPENGL_CHECK_ERRORS;
1269 if( CRender::gRenderReferenceCount > 0 )
1270 CRender::gRenderReferenceCount--;
1273 void OGLRender::glViewportWrapper(GLint x, GLint y, GLsizei width, GLsizei height, bool flag)
1275 static GLint mx=0,my=0;
1276 static GLsizei m_width=0, m_height=0;
1277 static bool mflag=true;
1279 x+=windowSetting.uDisplayX;
1280 y+=windowSetting.uDisplayY;
1282 if( x!=mx || y!=my || width!=m_width || height!=m_height || mflag!=flag)
1289 glMatrixMode(GL_PROJECTION);
1290 OPENGL_CHECK_ERRORS;
1292 OPENGL_CHECK_ERRORS;
1293 if( flag ) glOrtho(0, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight, 0, -1, 1);
1294 OPENGL_CHECK_ERRORS;
1295 glViewport(x,y,width,height);
1296 OPENGL_CHECK_ERRORS;