00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __SHADER_H__
00012 #define __SHADER_H__
00013
00014 #include "Render/RenderDll.h"
00015 #include "Common/Types.h"
00016 #include "Common/Bank.h"
00017 #include "Render/Texture.h"
00018 #include "Render/RenderDefs.h"
00019
00020 typedef KHANDLE KSHADER;
00021 #define KSHADER_NO ((KSHADER)KHANDLE_INVALID)
00022
00023 class KRender;
00024 class KShaderBank;
00025
00026
00027
00028 #define KSF_NONE 0x00000000
00029 #define KSF_LIGHTMAP 0x00000001
00030 #define KSF_RGBGEN 0x00000002
00031 #define KSF_TCMOD 0x00000004
00032
00033
00034 #define KSSP_NONE 0x00000000
00035 #define KSSP_ALPHASHADOW 0x00000001
00036 #define KSSP_AREAPORTAL 0x00000002
00037 #define KSSP_CLUSTERPORTAL 0x00000004
00038 #define KSSP_DONOTENTER 0x00000008
00039 #define KSSP_FLESH 0x00000010
00040 #define KSSP_FOG 0x00000020
00041 #define KSSP_LAVA 0x00000040
00042 #define KSSP_METALSTEPS 0x00000080
00043 #define KSSP_NODAMAGE 0x00000100
00044 #define KSSP_NODLIGHT 0x00000200
00045 #define KSSP_NODRAW 0x00000400
00046 #define KSSP_NODROP 0x00000800
00047 #define KSSP_NOIMPACT 0x00001000
00048 #define KSSP_NOMARKS 0x00002000
00049 #define KSSP_NOLIGHTMAP 0x00004000
00050 #define KSSP_NOSTEPS 0x00008000
00051 #define KSSP_NONSOLID 0x00010000
00052 #define KSSP_ORIGIN 0x00020000
00053 #define KSSP_PLAYERCLIP 0x00040000
00054 #define KSSP_SLICK 0x00080000
00055 #define KSSP_SLIME 0x00100000
00056 #define KSSP_STRUCTURAL 0x00200000
00057 #define KSSP_TRANS 0x00400000
00058 #define KSSP_WATER 0x00800000
00059 #define KSSP_SKY 0x01000000
00060
00061
00062 #define KSSF_TEXTURE 0x00000001
00063 #define KSSF_LIGHTMAP 0x00000002
00064 #define KSSF_MAP 0x00000004
00065 #define KSSF_CLAMPMAP 0x00000008
00066 #define KSSF_ANIMMAP 0x00000010
00067 #define KSSF_BLEND 0x00000020
00068 #define KSSF_DEPTHTEST 0x00000040
00069 #define KSSF_ALPHATEST 0x00000080
00070 #define KSSF_BUMPMAP 0x00000100
00071 #define KSSF_ENVMAP 0x00000200
00072
00073
00074 #define KSST_NO 0x00000000
00075 #define KSST_ROTATE 0x00000001
00076 #define KSST_SCALE 0x00000002
00077 #define KSST_SCROLL 0x00000003
00078 #define KSST_STRETCH 0x00000004
00079 #define KSST_TRANSFORM 0x00000005
00080 #define KSST_TURB 0x00000006
00081
00082
00083 #define KSSTF_NO 0x00000000
00084 #define KSSTF_SIN 0x00000001
00085 #define KSSTF_TRIANGLE 0x00000002
00086 #define KSSTF_SQUARE 0x00000003
00087 #define KSSTF_SAWTOOTH 0x00000004
00088 #define KSSTF_INVERSESAWTOOTH 0x00000005
00089
00090
00091 #define KSSR_NO 0x00000000
00092 #define KSSR_IDENTITYLIGHTING 0x00000001
00093 #define KSSR_IDENTITY 0x00000002
00094 #define KSSR_WAVE 0x00000003
00095 #define KSSR_ENTITY 0x00000004
00096 #define KSSR_ONEMINUSENTITY 0x00000005
00097 #define KSSR_VERTEX 0x00000006
00098 #define KSSR_ONEMINUSVERTEX 0x00000007
00099 #define KSSR_LIGHTINGDIFFUSE 0x00000008
00100
00101
00102 #define KSSTW_NO 0x00000000
00103 #define KSSTW_SIN 0x00000001
00104 #define KSSTW_TRIANGLE 0x00000002
00105 #define KSSTW_SQUARE 0x00000003
00106 #define KSSTW_SAWTOOTH 0x00000004
00107 #define KSSTW_INVERSESAWTOOTH 0x00000005
00108
00109
00110
00111
00112 #define KSGC_CW 0x00000001
00113 #define KSGC_CCW 0x00000002
00114 #define KSGC_NONE 0x00000004
00115
00116
00117 class RENDER_API KShaderTcMod
00118 {
00119 public:
00120 u8 m_TcMod;
00121 u8 m_TcModFunc;
00122 float m_TcModParam1;
00123 float m_TcModParam2;
00124 float m_TcModParam3;
00125 float m_TcModParam4;
00126 float m_TcModParam5;
00127 float m_TcModParam6;
00128
00129 KShaderTcMod()
00130 {
00131 m_TcMod = KSST_NO;
00132 m_TcModFunc = KSSTF_NO;
00133 m_TcModParam1 = 0.0f;
00134 m_TcModParam2 = 0.0f;
00135 m_TcModParam3 = 0.0f;
00136 m_TcModParam4 = 0.0f;
00137 m_TcModParam5 = 0.0f;
00138 m_TcModParam6 = 0.0f;
00139 }
00140 };
00141
00142
00143 class RENDER_API KShaderStage
00144 {
00145 public:
00146 char* m_pMapName;
00147 KTEXTURE* m_pTextureList;
00148 u32 m_nTextures;
00149 u32 m_StageFlags;
00150 float m_Frequency;
00151 KRBLEND m_BlendSrc;
00152 KRBLEND m_BlendDst;
00153 u32 m_AlphaRef;
00154 KRCMPFUNC m_AlphaFunc;
00155 bool m_bDepthWrite;
00156 KRCMPFUNC m_DepthFunc;
00157 KTable<KShaderTcMod*> m_TcModList;
00158 u32 m_RgbGen;
00159 u32 m_RgbGenWaveFunc;
00160 float m_RgbGenWaveParam1;
00161 float m_RgbGenWaveParam2;
00162 float m_RgbGenWaveParam3;
00163 float m_RgbGenWaveParam4;
00164 u32 m_TexCoordIndex;
00165
00166 KShaderStage()
00167 {
00168 m_AlphaRef = 128;
00169 m_AlphaFunc = KRCMP_ALWAYS;
00170 m_DepthFunc = KRCMP_LESSEQUAL;
00171 m_pMapName = NULL;
00172 m_pTextureList = NULL;
00173 m_nTextures = 0;
00174 m_Frequency = 0.0f;
00175 m_BlendSrc = KRBLEND_ONE;
00176 m_BlendDst = KRBLEND_ZERO;
00177 m_StageFlags = KSSF_TEXTURE;
00178 m_RgbGen = KSSR_NO;
00179 m_RgbGenWaveFunc = KSSTW_NO;
00180 m_RgbGenWaveParam1 = 0.0f;
00181 m_RgbGenWaveParam2 = 0.0f;
00182 m_RgbGenWaveParam3 = 0.0f;
00183 m_RgbGenWaveParam4 = 0.0f;
00184 m_TexCoordIndex = 0;
00185 m_bDepthWrite = true;
00186 }
00187
00188 ~KShaderStage()
00189 {
00190 if( m_pTextureList ) Deletev( m_pTextureList );
00191 if( m_pMapName ) free( m_pMapName );
00192
00193 for( u32 t = 0; t < m_TcModList.GetSize(); t ++ )
00194 {
00195 if( m_TcModList[t] )
00196 Deletep( m_TcModList[t] );
00197 }
00198 m_TcModList.Clear();
00199 }
00200 };
00201
00202
00203 class RENDER_API KShader
00204 {
00205 public:
00206 KRender* m_pRender;
00207 KShaderBank* m_pShaderBank;
00208 u32 m_Flags;
00209 char* m_pName;
00210 u32 m_SurfaceParm;
00211 u32 m_Cull;
00212 KTable<KShaderStage*> m_StageList;
00213 bool m_bMipMap;
00214 u16 m_SortValue;
00215 KSHADER m_hShader;
00216 KTEXTURE m_hTexture;
00217 bool m_bAlpha;
00218 s32 m_nInstances;
00219
00220 KShader( KRender* pRender, KShaderBank* pShaderBank )
00221 {
00222 m_pRender = pRender;
00223 m_pShaderBank = pShaderBank;
00224 m_Flags = KSF_NONE;
00225 m_hShader = KSHADER_NO;
00226 m_hTexture = KTEXTURE_NO;
00227 m_pName = NULL;
00228 m_SurfaceParm = KSSP_NONE;
00229 m_Cull = KSGC_CCW;
00230 m_bMipMap = true;
00231 m_SortValue = 0;
00232 m_bAlpha = false;
00233 m_nInstances = 0;
00234 }
00235
00236 ~KShader()
00237 {
00238 if( m_pName )
00239 free( m_pName );
00240
00241 FlushAllStages();
00242 }
00243
00244 void FlushAllStages();
00245 void FlushAllTextures();
00246 void LoadTextures( u32 Size );
00247 void AddStage( char* pMapName, KRBLEND BlendSrc = KRBLEND_ONE, KRBLEND BlendDst = KRBLEND_ONE );
00248 void SetStageBlend( u32 Stage, KRBLEND BlendSrc = KRBLEND_ONE, KRBLEND BlendDst = KRBLEND_ONE );
00249
00250 KShaderBank* GetpShaderBank() { return m_pShaderBank; }
00251 u32 GetnStages() { return m_StageList.GetSize(); }
00252
00253 void AddInstance() { m_nInstances ++; }
00254 void RemoveInstance() { KASSERT( m_nInstances > 0); m_nInstances --; }
00255 s32 GetnInstances() { return m_nInstances; }
00256 };
00257
00258
00259 class RENDER_API KShaderBank : public KBank< KShader, KSHADER >
00260 {
00261 protected:
00262 KRender* m_pRender;
00263
00264 bool LoadFile( char* pFileName );
00265 static KRBLEND GetBlendByText( char* pBlendText );
00266
00267 public:
00268 KShaderBank( KRender* pRender );
00269 virtual ~KShaderBank();
00270
00271 bool PreLoad();
00272 KSHADER LoadShader( char* pShaderName, u32 Size = 1 );
00273 void UnloadShader( KSHADER& hShader );
00274 KSHADER CreateShader( char* pShaderName );
00275 KTEXTURE GetTexture( char* pFileName, bool GenerateMipMap = true, u32 Size = 1 );
00276 void FlushShader( KSHADER ShaderId );
00277 KShader* GetpShader( KSHADER Handle ) { return GetpData( Handle ); }
00278 void DumpUsed();
00279 void UnloadTexture( KTEXTURE hTexture );
00280 };
00281
00282 #endif __SHADER_H__