Back to index

nux  3.0.0
GLRenderStates.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010-2012 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "GLResource.h"
00024 #include "GLRenderStates.h"
00025 
00026 namespace nux
00027 {
00028 
00029   const struct StateLookUpTable
00030   {
00031     StateLookUpTable()
00032     {
00033       // setup render state map
00034 #define UL_MAP(state__, default_value__, checked__)                         \
00035         default_render_state[GFXRS_##state__].iValue     = default_value__;      \
00036         default_render_state[GFXRS_##state__].Checked   = checked__;
00037 
00038 #define UL_MAP_FLOAT(state__, default_value__, checked__)                         \
00039     default_render_state[GFXRS_##state__].fValue     = default_value__;      \
00040     default_render_state[GFXRS_##state__].Checked   = checked__;            \
00041  
00042 
00043 #ifndef NUX_OPENGLES_20
00044       UL_MAP(FRONT_POLYGONMODE            ,           GL_FILL         ,       1);
00045       UL_MAP(BACK_POLYGONMODE             ,           GL_FILL         ,       1);
00046 #endif
00047       UL_MAP(CULLFACEENABLE               ,           GL_FALSE        ,       1);
00048       UL_MAP(CULLFACE                     ,           GL_BACK         ,       1);
00049       UL_MAP(FRONTFACE                    ,           GL_CCW          ,       1);
00050 
00051       UL_MAP(SCISSORTESTENABLE            ,           GL_FALSE        ,       1);
00052       UL_MAP(FOGENABLE                    ,           GL_FALSE        ,       1);
00053 
00054       UL_MAP(ZTESTENABLE                  ,           GL_FALSE        ,       1);
00055       UL_MAP(ZWRITEENABLE                 ,           GL_TRUE         ,       1);
00056       UL_MAP(ZFUNC                        ,           GL_LESS         ,       1);
00057       UL_MAP_FLOAT(ZNEAR                  ,           static_cast<unsigned int> (0.0f)            ,       1);
00058       UL_MAP_FLOAT(ZFAR                   ,           static_cast<unsigned int> (1.0f)            ,       1);
00059 
00060       UL_MAP(ALPHABLENDENABLE             ,           GL_FALSE        ,       1);
00061       UL_MAP(BLENDOP                      ,           GL_FUNC_ADD     ,       1);
00062       UL_MAP(BLENDOPALPHA                 ,           GL_FUNC_ADD     ,       1);
00063       UL_MAP(SRCBLEND                     ,           GL_ONE          ,       1);
00064       UL_MAP(DESTBLEND                    ,           GL_ZERO         ,       1);
00065       UL_MAP(SRCBLENDALPHA                ,           GL_ONE          ,       1);
00066       UL_MAP(DESTBLENDALPHA               ,           GL_ZERO         ,       1);
00067 
00068       UL_MAP(ALPHATESTENABLE              ,           GL_FALSE        ,       1);
00069       UL_MAP(ALPHATESTREF                 ,           0x0             ,       1);
00070       UL_MAP(ALPHATESTFUNC                ,           GL_ALWAYS       ,       1);
00071 
00072       UL_MAP(STENCILENABLE                ,           GL_FALSE        ,       1);
00073       UL_MAP(TWOSIDEDSTENCILENABLE        ,           GL_FALSE        ,       1);
00074       UL_MAP(FRONT_STENCILWRITEMASK       ,           0xFFFFFFFF      ,       1);
00075       UL_MAP(BACK_STENCILWRITEMASK        ,           0xFFFFFFFF      ,       1);
00076 
00077       UL_MAP(FRONT_STENCILFUNC            ,           GL_ALWAYS       ,       1);
00078       UL_MAP(FRONT_STENCILREF             ,           0x0             ,       1);
00079       UL_MAP(FRONT_STENCILMASK            ,           0xFF            ,       1);
00080       UL_MAP(FRONT_STENCILFAIL            ,           GL_KEEP         ,       1);
00081       UL_MAP(FRONT_STENCILZFAIL           ,           GL_KEEP         ,       1);
00082       UL_MAP(FRONT_STENCILZPASS           ,           GL_KEEP         ,       1);
00083 
00084       UL_MAP(BACK_STENCILFUNC             ,           GL_ALWAYS       ,       1);
00085       UL_MAP(BACK_STENCILREF              ,           0x0             ,       1);
00086       UL_MAP(BACK_STENCILMASK             ,           0xFF            ,       1);
00087       UL_MAP(BACK_STENCILFAIL             ,           GL_KEEP         ,       1);
00088       UL_MAP(BACK_STENCILZFAIL            ,           GL_KEEP         ,       1);
00089       UL_MAP(BACK_STENCILZPASS            ,           GL_KEEP         ,       1);
00090 
00091 
00092       UL_MAP(POINTSMOOTHENABLE            ,           GL_FALSE        ,       1);
00093       UL_MAP(LINESMOOTHENABLE             ,           GL_FALSE        ,       1);
00094       UL_MAP(POINTSIZE                    ,           1               ,       1);
00095       UL_MAP(LINEWIDTH                    ,           1               ,       1);
00096       UL_MAP(POINTHINT                    ,           GL_FASTEST      ,       1);
00097       UL_MAP(LINEHINT                     ,           GL_FASTEST      ,       1);
00098 
00099       UL_MAP(COLORWRITEENABLE_R           ,           GL_TRUE         ,       1);
00100       UL_MAP(COLORWRITEENABLE_G           ,           GL_TRUE         ,       1);
00101       UL_MAP(COLORWRITEENABLE_B           ,           GL_TRUE         ,       1);
00102       UL_MAP(COLORWRITEENABLE_A           ,           GL_TRUE         ,       1);
00103 
00104 #undef UL_MAP
00105 #undef UL_MAP_FLOAT
00106     };
00107 
00108     RenderStateMap default_render_state[GFXRS_MAX_RENDERSTATES];
00109     RenderStateMap sampler_state_map[GFXSS_MAX_SAMPLERSTATES];
00110 
00111   } s_StateLUT;
00112 
00113 
00114   GpuRenderStates::GpuRenderStates(GpuBrand board, GpuInfo* info)
00115   {
00116     _gpu_brand = board;
00117     _gpu_info = info;
00118     Memcpy(&render_state_changes_, &s_StateLUT.default_render_state, sizeof(render_state_changes_));
00119   }
00120 
00121   GpuRenderStates::~GpuRenderStates()
00122   {
00123 
00124   }
00125 
00126   void GpuRenderStates::ResetDefault()
00127   {
00128     HW__EnableCulling( s_StateLUT.default_render_state[GFXRS_CULLFACEENABLE].iValue );
00129     HW__SetFrontFace( s_StateLUT.default_render_state[GFXRS_FRONTFACE].iValue );
00130     HW__SetCullFace( s_StateLUT.default_render_state[GFXRS_CULLFACE].iValue );
00131 
00132     HW__SetDepthMask( s_StateLUT.default_render_state[GFXRS_ZWRITEENABLE].iValue );
00133     HW__SetDepthFunc( s_StateLUT.default_render_state[GFXRS_ZFUNC].iValue );
00134     HW__SetEnableDepthTest( s_StateLUT.default_render_state[GFXRS_ZTESTENABLE].iValue );
00135     HW__SetDepthRange( s_StateLUT.default_render_state[GFXRS_ZNEAR].fValue, s_StateLUT.default_render_state[GFXRS_ZFAR].fValue );
00136 
00137     HW__EnableAlphaBlend( s_StateLUT.default_render_state[GFXRS_ALPHABLENDENABLE].iValue );
00138     HW__SetAlphaBlendOp( s_StateLUT.default_render_state[GFXRS_BLENDOP].iValue, s_StateLUT.default_render_state[GFXRS_BLENDOPALPHA].iValue );
00139     HW__SetSeparateAlphaBlendFactors(
00140       s_StateLUT.default_render_state[GFXRS_SRCBLEND].iValue, s_StateLUT.default_render_state[GFXRS_DESTBLEND].iValue,
00141       s_StateLUT.default_render_state[GFXRS_SRCBLENDALPHA].iValue, s_StateLUT.default_render_state[GFXRS_DESTBLENDALPHA ].iValue );
00142 
00143 
00144     HW__EnableAlphaTest( s_StateLUT.default_render_state[GFXRS_ALPHATESTENABLE].iValue );
00145     HW__SetAlphaTestFunc( s_StateLUT.default_render_state[GFXRS_ALPHATESTFUNC].iValue, s_StateLUT.default_render_state[GFXRS_ALPHATESTREF].iValue );
00146 
00147 
00148     HW__EnableStencil( s_StateLUT.default_render_state[GFXRS_STENCILENABLE].iValue );
00149     HW__SetStencilFunc(
00150       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILFUNC].iValue,
00151       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILREF].iValue,
00152       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILMASK].iValue);
00153     HW__SetStencilOp(
00154       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILFAIL].iValue,
00155       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILZFAIL].iValue,
00156       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILZPASS].iValue);
00157 
00158     HW__EnableLineSmooth(s_StateLUT.default_render_state[GFXRS_LINESMOOTHENABLE].iValue);
00159     HW__SetLineWidth(s_StateLUT.default_render_state[GFXRS_LINEWIDTH].iValue, s_StateLUT.default_render_state[GFXRS_LINEHINT].iValue);
00160 
00161     HW__EnablePointSmooth(s_StateLUT.default_render_state[GFXRS_POINTSMOOTHENABLE].iValue);
00162     HW__SetPointSize(s_StateLUT.default_render_state[GFXRS_POINTSIZE].iValue, s_StateLUT.default_render_state[GFXRS_POINTHINT].iValue);
00163 
00164 #if 0
00165     HW__EnableTwoSidedStencil( s_StateLUT.default_render_state[GFXRS_TWOSIDEDSTENCILENABLE].iValue );
00166 
00167     HW__SetFrontFaceStencilFunc(
00168       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILFUNC].iValue,
00169       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILREF].iValue,
00170       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILMASK].iValue );
00171     HW__SetFrontFaceStencilOp(
00172       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILFAIL].iValue,
00173       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILZFAIL].iValue,
00174       s_StateLUT.default_render_state[GFXRS_FRONT_STENCILZPASS].iValue );
00175 
00176     HW__SetBackFaceStencilFunc(
00177       s_StateLUT.default_render_state[GFXRS_BACK_STENCILFUNC].iValue,
00178       s_StateLUT.default_render_state[GFXRS_BACK_STENCILREF].iValue,
00179       s_StateLUT.default_render_state[GFXRS_BACK_STENCILMASK].iValue );
00180     HW__SetBackFaceStencilOp(
00181       s_StateLUT.default_render_state[GFXRS_BACK_STENCILFAIL].iValue,
00182       s_StateLUT.default_render_state[GFXRS_BACK_STENCILZFAIL].iValue,
00183       s_StateLUT.default_render_state[GFXRS_BACK_STENCILZPASS].iValue );
00184 #endif
00185 
00186     HW__EnableScissor( s_StateLUT.default_render_state[GFXRS_SCISSORTESTENABLE].iValue );
00187 
00188     HW__EnableFog( s_StateLUT.default_render_state[GFXRS_FOGENABLE].iValue );
00189   }
00190 
00191   void GpuRenderStates::SubmitChangeStates()
00192   {
00193     HW__EnableCulling( render_state_changes_[GFXRS_CULLFACEENABLE].iValue );
00194     HW__SetFrontFace( render_state_changes_[GFXRS_FRONTFACE].iValue );
00195     HW__SetCullFace( render_state_changes_[GFXRS_CULLFACE].iValue );
00196 
00197     HW__SetDepthMask( render_state_changes_[GFXRS_ZWRITEENABLE].iValue );
00198     HW__SetDepthFunc( render_state_changes_[GFXRS_ZFUNC].iValue );
00199     HW__SetEnableDepthTest( render_state_changes_[GFXRS_ZTESTENABLE].iValue );
00200     HW__SetDepthRange( render_state_changes_[GFXRS_ZNEAR].fValue, render_state_changes_[GFXRS_ZFAR].fValue );
00201 
00202     HW__EnableAlphaBlend( render_state_changes_[GFXRS_ALPHABLENDENABLE].iValue );
00203     HW__SetAlphaBlendOp( render_state_changes_[GFXRS_BLENDOP].iValue, render_state_changes_[GFXRS_BLENDOPALPHA].iValue );
00204     HW__SetSeparateAlphaBlendFactors(
00205       render_state_changes_[GFXRS_SRCBLEND].iValue, render_state_changes_[GFXRS_DESTBLEND].iValue,
00206       render_state_changes_[GFXRS_SRCBLENDALPHA].iValue, render_state_changes_[GFXRS_DESTBLENDALPHA ].iValue );
00207 
00208 
00209     HW__EnableAlphaTest( render_state_changes_[GFXRS_ALPHATESTENABLE].iValue );
00210     HW__SetAlphaTestFunc( render_state_changes_[GFXRS_ALPHATESTFUNC].iValue, render_state_changes_[GFXRS_ALPHATESTREF].iValue );
00211 
00212 
00213     HW__EnableStencil( render_state_changes_[GFXRS_STENCILENABLE].iValue );
00214 
00215     HW__SetStencilFunc(
00216       render_state_changes_[GFXRS_FRONT_STENCILFUNC].iValue,
00217       render_state_changes_[GFXRS_FRONT_STENCILREF].iValue,
00218       render_state_changes_[GFXRS_FRONT_STENCILMASK].iValue);
00219     HW__SetStencilOp(
00220       render_state_changes_[GFXRS_FRONT_STENCILFAIL].iValue,
00221       render_state_changes_[GFXRS_FRONT_STENCILZFAIL].iValue,
00222       render_state_changes_[GFXRS_FRONT_STENCILZPASS].iValue);
00223 
00224     HW__EnableLineSmooth(render_state_changes_[GFXRS_LINESMOOTHENABLE].iValue);
00225     HW__SetLineWidth(render_state_changes_[GFXRS_LINEWIDTH].iValue, render_state_changes_[GFXRS_LINEHINT].iValue);
00226 
00227     HW__EnablePointSmooth(render_state_changes_[GFXRS_POINTSMOOTHENABLE].iValue);
00228     HW__SetPointSize(render_state_changes_[GFXRS_POINTSIZE].iValue, render_state_changes_[GFXRS_POINTHINT].iValue);
00229         
00230 #if 0
00231     HW__EnableTwoSidedStencil( s_StateLUT.default_render_state[GFXRS_TWOSIDEDSTENCILENABLE].iValue );
00232 
00233     HW__SetFrontFaceStencilFunc(
00234       render_state_changes_[GFXRS_FRONT_STENCILFUNC].iValue,
00235       render_state_changes_[GFXRS_FRONT_STENCILREF].iValue,
00236       render_state_changes_[GFXRS_FRONT_STENCILMASK].iValue );
00237     HW__SetFrontFaceStencilOp(
00238       render_state_changes_[GFXRS_FRONT_STENCILFAIL].iValue,
00239       render_state_changes_[GFXRS_FRONT_STENCILZFAIL].iValue,
00240       render_state_changes_[GFXRS_FRONT_STENCILZPASS].iValue );
00241 
00242     HW__SetBackFaceStencilFunc(
00243       render_state_changes_[GFXRS_BACK_STENCILFUNC].iValue,
00244       render_state_changes_[GFXRS_BACK_STENCILREF].iValue,
00245       render_state_changes_[GFXRS_BACK_STENCILMASK].iValue );
00246     HW__SetBackFaceStencilOp(
00247       render_state_changes_[GFXRS_BACK_STENCILFAIL].iValue,
00248       render_state_changes_[GFXRS_BACK_STENCILZFAIL].iValue,
00249       render_state_changes_[GFXRS_BACK_STENCILZPASS].iValue );
00250 #endif
00251 
00252     HW__EnableScissor( render_state_changes_[GFXRS_SCISSORTESTENABLE].iValue );
00253 
00254     HW__EnableFog( render_state_changes_[GFXRS_FOGENABLE].iValue );
00255 
00256   }
00257 
00258   void GpuRenderStates::ResetStateChangeToDefault()
00259   {
00260     for (unsigned int i = 0; i < GFXRS_MAX_RENDERSTATES; i++)
00261     {
00262       if (render_state_changes_[i].Checked &&
00263           ((render_state_changes_[i].iValue != s_StateLUT.default_render_state[i].iValue) ||
00264             (render_state_changes_[i].fValue != s_StateLUT.default_render_state[i].fValue)))
00265       {
00266         render_state_changes_[i].iValue = s_StateLUT.default_render_state[i].iValue;
00267         render_state_changes_[i].fValue = s_StateLUT.default_render_state[i].fValue;
00268       }
00269     }
00270   }
00271 
00272   void GpuRenderStates::CheckStateChange()
00273   {
00274     for (unsigned int i = 0; i < GFXRS_MAX_RENDERSTATES; i++)
00275     {
00276       if (render_state_changes_[i].Checked &&
00277           ((render_state_changes_[i].iValue != s_StateLUT.default_render_state[i].iValue) ||
00278             (render_state_changes_[i].fValue != s_StateLUT.default_render_state[i].fValue)))
00279       {
00280         render_state_changes_[i].iValue = s_StateLUT.default_render_state[i].iValue;
00281         render_state_changes_[i].fValue = s_StateLUT.default_render_state[i].fValue;
00282         nuxError("[GpuRenderStates::Check] Render state doesn't have default value");
00283       }
00284     }
00285   }
00286 }
00287