]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - include/ishaders.h
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / include / ishaders.h
1 /*\r
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \r
7 GtkRadiant is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 GtkRadiant is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with GtkRadiant; if not, write to the Free Software\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
20 */\r
21 \r
22 //-----------------------------------------------------------------------------\r
23 //\r
24 //\r
25 // DESCRIPTION:\r
26 // a set of functions to manipulate textures in Radiant\r
27 // \r
28 \r
29 #ifndef __ISHADERS_H_\r
30 #define __ISHADERS_H_\r
31 \r
32 #define SHADERS_MAJOR "shaders"\r
33 // define a GUID for this interface so plugins can access and reference it\r
34 // {D42F798A-DF57-11d3-A3EE-0004AC96D4C3}\r
35 static const GUID QERShadersTable_GUID = \r
36 { 0xd42f798a, 0xdf57, 0x11d3, { 0xa3, 0xee, 0x0, 0x4, 0xac, 0x96, 0xd4, 0xc3 } };\r
37 \r
38 // NOTES ABOUT SYNTAX:\r
39 // if a function starts by 'Try' it means that if the requested thing could not be found / loaded it will return nothing / NULL\r
40 // otherwise a default object will be created\r
41 // the _QERShadersTable is also used by shader code inside Radiant. but for speed and "keep it simple" consideration you\r
42 // can get the static equivalent of the func pointers by adding 'QERApp_' (access to _QERShadersTable is better thought ..\r
43 // see the note to move all the shader language out of Radiant below)\r
44 \r
45 /*!\r
46 \todo FIXME TTimo\r
47 fix the reference count strategy\r
48 - define the policy. It seems the initial policy of doing an inc ref when you create the shader is not good\r
49 (it doesn't work, and it's not being used right)\r
50 so, when you request an IShader and store it, incref it yourself\r
51 as a debugging safe check: push the created increfed objects into a list, and scan them at next idle loop\r
52 to make sure they have been decref'ed ? (sounds easy, may not be that much).\r
53 */\r
54 \r
55 class IShader\r
56 {\r
57 public:\r
58   // Increment the number of references to this object\r
59   virtual void IncRef () = 0;\r
60   // Decrement the reference count\r
61   virtual void DecRef () = 0;\r
62   // get/set the qtexture_t* Radiant uses to represent this shader object\r
63   virtual qtexture_t* getTexture() const = 0;\r
64   virtual void setTexture(qtexture_t *pTex) = 0;\r
65   // get shader name\r
66   virtual const char* getName() const = 0;\r
67   // is this shader in use?\r
68   // NOTE: this flag can mean this shader has been in use at least once since the last rescan of in-use stuff\r
69   // (rescan of in-use happens in several cases, user command or during a texture directory load)\r
70   // NOTE: this is used to draw the green outline in the texture window\r
71   // NOTE: when does Radiant set the InUse flag? Whenever Select_SetTexture is called (well that doesn't necessarily means the texture actually gets in use, but that's close enough)\r
72   virtual bool IsInUse() const = 0;\r
73   virtual void SetInUse(bool) = 0;\r
74   // is this shader displayed in the texture browser?\r
75   // NOTE: if IsInUse() == true, the shader will always be displayed in the texture window and this flag ingored\r
76   virtual bool IsDisplayed() const = 0;\r
77   virtual void SetDisplayed(bool) = 0;\r
78   // get the editor flags (QER_NOCARVE QER_TRANS)\r
79   virtual int getFlags() = 0;\r
80   // get the transparency value\r
81   virtual float getTrans() = 0;\r
82   // test if it's a true shader, or a default shader created to wrap around a texture\r
83   virtual bool IsDefault() = 0;\r
84   // test if it's a plain color shader, i.e. a shader we use on plain color stuff (like info_playerstart)\r
85   virtual bool IsColor() = 0;\r
86   // get the related color then!\r
87   virtual void getColor(vec3_t v) = 0;\r
88   // get the alphaFunc\r
89   virtual void getAlphaFunc(int *func, float *ref) = 0;\r
90   // get the cull type\r
91   virtual int getCull() = 0;\r
92   // get shader file name (ie the file where this one is defined)\r
93   virtual const char* getShaderFileName() const = 0;\r
94 };\r
95 \r
96 // NOTE: how to move all the shader language out of Radiant in a plugin?\r
97 // -> change this _QERShadersTable into an IShadersManager\r
98 // -> let the plugin create an instance of IShadersManager\r
99 // -> make sure Radiant uses this IShadersManager to load / query the shaders\r
100 \r
101 // NOTE: shader and texture names used must be full path, ie. most often with "textures/" prefix\r
102 // (since shaders are defined in .shader files with textures/)\r
103 \r
104 // free all shaders\r
105 // free the shaders, will not free the qtexture_t*\r
106 typedef void            (WINAPI* PFN_FREESHADERS)               ();\r
107 // reload all the shaders\r
108 // this will free everything (shaders and their textures), then reload all in use stuff\r
109 typedef void            (WINAPI* PFN_RELOADSHADERS)             ();\r
110 // load all shaders in a given directory\r
111 // this will scan the list of in-memory shaders, and load the related qtexture_t if needed\r
112 typedef int             (WINAPI* PFN_LOADSHADERSFROMDIR)(const char* path);\r
113 // load a shader file (ie a set of shaders)\r
114 // after LoadShaderFile shaders will be in memory, next step is to load the qtexture_t Radiant uses to represent them\r
115 // if a shader with the same name exists, new one will not be loaded - don't use this to refresh the shaders!\r
116 typedef void            (WINAPI* PFN_LOADSHADERFILE)    (const char* filename);\r
117 // tell if a given shader exists in our shader table\r
118 // NOTE: this doesn't tell wether it's corresponding qtexture is loaded \r
119 typedef int                     (WINAPI* PFN_HASSHADER)                 (const char* name);\r
120 // return the shader for a given name\r
121 // if the qtexture is not already in memory, will try loading it\r
122 // if the qtexture could not be found, will use default\r
123 // will return NULL on shader not found\r
124 typedef IShader*        (WINAPI* PFN_TRYSHADERFORNAME)  (const char* name);\r
125 // return the shader for a given name\r
126 // if the qtexture is not already in memory, will try loading it\r
127 // will create a default shader if not found (will use a default texture)\r
128 typedef IShader*        (WINAPI* PFN_SHADERFORNAME)             (const char* name);\r
129 // query / load a texture\r
130 // will not try loading a shader, will look for the actual image file ..\r
131 // returns NULL on file not found\r
132 // NOTE: strategy for file lookup:\r
133 //   paths must be relative, ie. textures/me/myfile\r
134 //   if a 3-letters filename extension (such as .jpg or .tga) is provided, it will get loaded first\r
135 //   if not found or no extension, will try loading after adding .tga and .jpg (in this order)\r
136 typedef qtexture_t* (WINAPI* PFN_TRYTEXTUREFORNAME) (const char* filename);\r
137 // query / load a texture\r
138 // will not try loading a shader, will look for the actual image file ..\r
139 // on file not found will use the "texture not found"\r
140 typedef qtexture_t* (WINAPI* PFN_TEXTUREFORNAME)        (const char* filename);\r
141 // get the number of active shaders\r
142 // these are the shaders currently loaded, that have an associated qtexture_t*\r
143 typedef int                     (WINAPI* PFN_GETACTIVESHADERCOUNT)      ();\r
144 // for stuff that needs to be represented by a plain texture\r
145 // the shader will get a "color" name, use GetColor to get the actual color\r
146 typedef IShader*        (WINAPI* PFN_COLORSHADERFORNAME)        (const char* name);\r
147 // reload a shaderfile - update shaders and their display properties/qtexture_t if needed\r
148 // will not reload the texture files\r
149 // will switch to "show in use" atfer use\r
150 // filename must be reletive path of the shader, ex. scripts/gothic_wall.shader\r
151 typedef void            (WINAPI* PFN_RELOADSHADERFILE)(const char* filename);\r
152 // retrieve a shader if exists, without loading the textures for it etc.\r
153 // use this function if you want special info on a shader\r
154 typedef IShader* (WINAPI* PFN_SHADERFORNAMENOLOAD) (const char* name);\r
155 // force the "in use" flag on all active shaders\r
156 typedef void (WINAPI* PFN_ACTIVESHADERSSETINUSE) (bool b);\r
157 // sort the shaders in alphabetical order, we use the order in the texture inspector\r
158 typedef void (WINAPI* PFN_SORTACTIVESHADERS) ();\r
159 // check if there exists an active shader with the given texture name (loaded or not, doesn't matter)\r
160 // (used to detect the textures we need to create a default shader for .. while scanning a directory)\r
161 typedef IShader* (WINAPI* PFN_ACTIVESHADERFORTEXTURENAME) (char *);\r
162 // create a shader to wrap around a texture name, we use this when loading a texture directory and some textures\r
163 // are not present as shaders\r
164 typedef IShader* (WINAPI* PFN_CREATESHADERFORTEXTURENAME) (const char* name);\r
165 // switch the IsDisplayed flag on all the active shaders\r
166 typedef void (WINAPI* PFN_ACTIVESHADERSSETDISPLAYED) (bool b);\r
167 // retrieve an active shader based on index\r
168 typedef IShader* (WINAPI* PFN_ACTIVESHADERFORINDEX) (int i);\r
169 // will cleanup a texture name and force it to the right format\r
170 // the debug version is painfully slow, but will detect more problems\r
171 // the idea being to avoid loading the same file several time because of uppercase/lowercase etc.\r
172 typedef const char* (WINAPI* PFN_CLEANTEXTURENAME) (const char* name, bool bAddTexture);\r
173 \r
174 struct _QERShadersTable\r
175 {\r
176   int           m_nSize;\r
177   PFN_FREESHADERS                                     m_pfnFreeShaders;\r
178   PFN_RELOADSHADERS                           m_pfnReloadShaders;\r
179   PFN_LOADSHADERSFROMDIR            m_pfnLoadShadersFromDir;\r
180   PFN_LOADSHADERFILE                        m_pfnLoadShaderFile;\r
181   PFN_RELOADSHADERFILE              m_pfnReloadShaderFile;\r
182   PFN_HASSHADER                                 m_pfnHasShader;\r
183   PFN_TRYSHADERFORNAME              m_pfnTry_Shader_ForName;\r
184   PFN_SHADERFORNAME                           m_pfnShader_ForName;\r
185   PFN_TRYTEXTUREFORNAME             m_pfnTry_Texture_ForName;\r
186   PFN_TEXTUREFORNAME                        m_pfnTexture_ForName;\r
187   PFN_GETACTIVESHADERCOUNT                          m_pfnGetActiveShaderCount;\r
188   PFN_COLORSHADERFORNAME                                                m_pfnColorShader_ForName;\r
189   PFN_SHADERFORNAMENOLOAD                                               m_pfnShader_ForName_NoLoad;\r
190   PFN_ACTIVESHADERSSETINUSE         m_pfnActiveShaders_SetInUse;\r
191   PFN_SORTACTIVESHADERS             m_pfnSortActiveShaders;\r
192   PFN_ACTIVESHADERFORTEXTURENAME    m_pfnActiveShader_ForTextureName;\r
193   PFN_CREATESHADERFORTEXTURENAME    m_pfnCreateShader_ForTextureName;\r
194   PFN_ACTIVESHADERSSETDISPLAYED     m_pfnActiveShaders_SetDisplayed;\r
195   PFN_ACTIVESHADERFORINDEX          m_pfnActiveShader_ForIndex;\r
196   PFN_CLEANTEXTURENAME              m_pfnCleanTextureName;\r
197 };\r
198 \r
199 /*!\r
200 \todo FIXME fix the QERApp_ prototyping on shaders module\r
201 make it homogeneous with other modules, should be straight calls\r
202 */\r
203 \r
204 #ifdef USE_SHADERSTABLE_DEFINE\r
205   #ifndef __SHADERSTABLENAME\r
206     #define __SHADERSTABLENAME g_ShadersTable\r
207   #endif\r
208 #define QERApp_Shader_ForName __SHADERSTABLENAME.m_pfnShader_ForName\r
209 #define QERApp_Texture_ForName2 __SHADERSTABLENAME.m_pfnTexture_ForName\r
210 #define QERApp_FreeShaders __SHADERSTABLENAME.m_pfnFreeShaders\r
211 #define QERApp_ReloadShaders __SHADERSTABLENAME.m_pfnReloadShaders\r
212 #define QERApp_SortActiveShaders __SHADERSTABLENAME.m_pfnSortActiveShaders\r
213 #define QERApp_ReloadShaderFile __SHADERSTABLENAME.m_pfnReloadShaderFile\r
214 #define QERApp_LoadShaderFile __SHADERSTABLENAME.m_pfnLoadShaderFile\r
215 #define QERApp_HasShader __SHADERSTABLENAME.m_pfnHasShader\r
216 #define QERApp_Try_Shader_ForName __SHADERSTABLENAME.m_pfnTry_Shader_ForName\r
217 #define QERApp_Try_Texture_ForName __SHADERSTABLENAME.m_pfnTry_Texture_ForName\r
218 #define QERApp_ColorShader_ForName __SHADERSTABLENAME.m_pfnColorShader_ForName\r
219 #define QERApp_Shader_ForName_NoLoad __SHADERSTABLENAME.m_pfnShader_ForName_NoLoad\r
220 #define QERApp_LoadShadersFromDir __SHADERSTABLENAME.m_pfnLoadShadersFromDir\r
221 #define QERApp_LoadShadersFromDir __SHADERSTABLENAME.m_pfnLoadShadersFromDir\r
222 #define QERApp_CreateShader_ForTextureName __SHADERSTABLENAME.m_pfnCreateShader_ForTextureName\r
223 #define QERApp_GetActiveShaderCount __SHADERSTABLENAME.m_pfnGetActiveShaderCount\r
224 #define QERApp_ActiveShaders_SetDisplayed __SHADERSTABLENAME.m_pfnActiveShaders_SetDisplayed\r
225 #define QERApp_ActiveShader_ForIndex __SHADERSTABLENAME.m_pfnActiveShader_ForIndex\r
226 #define QERApp_ActiveShaders_SetInUse __SHADERSTABLENAME.m_pfnActiveShaders_SetInUse\r
227 #define QERApp_ActiveShader_ForTextureName __SHADERSTABLENAME.m_pfnActiveShader_ForTextureName\r
228 #define QERApp_ActiveShader_ForIndex __SHADERSTABLENAME.m_pfnActiveShader_ForIndex\r
229 #define QERApp_CleanTextureName __SHADERSTABLENAME.m_pfnCleanTextureName\r
230 #endif\r
231 \r
232 #define APPSHADERS_MAJOR "appshaders"\r
233 // FIXME: remove\r
234 static const GUID QERAppShadersTable_GUID = \r
235 { 0xec3008a8, 0xbd0b, 0x11d4, { 0x82, 0x51, 0x20, 0x4c, 0x4f, 0x4f, 0x50, 0x20 } };\r
236 \r
237 // g_qeglobals.d_qtextures is used internally by the editor for actual camera drawing\r
238 typedef qtexture_t** (WINAPI* PFN_QTEXTURES)();\r
239 // g_qeglobals.d_qtexmap is a map for fast access\r
240 typedef GHashTable* (WINAPI* PFN_QTEXMAP)();\r
241 // d_texturewin\r
242 //++timo NOTE: this same function is also in isurface.h table, we would eventually have to merge some stuff\r
243 typedef texturewin_t* (* PFN_QEGLOBALSTEXTUREWIN)();\r
244 // Texture_SetTexture\r
245 //++timo NOTE: this one may have to be reorganized too .. putting it here is a bit clumsy\r
246 // NOTE: the C++ function used internally has a lot of default values\r
247 typedef void (WINAPI* PFN_TEXTURESETTEXTURE)(texdef_t *texdef, brushprimit_texdef_t *brushprimit_texdef);\r
248 // Texture_ShowInuse\r
249 typedef void (WINAPI* PFN_TEXTURESHOWINUSE)();\r
250 // BuildShaderList\r
251 typedef void (* PFN_BUILDSHADERLIST)();\r
252 // PreloadShaders\r
253 typedef void (* PFN_PRELOADSHADERS)();\r
254 \r
255 // a table that Radiant makes available to the shader module in return\r
256 struct _QERAppShadersTable\r
257 {\r
258   int m_nSize;\r
259   PFN_QTEXTURES           m_pfnQTextures;\r
260   PFN_QTEXMAP             m_pfnQTexmap;\r
261         PFN_QEGLOBALSTEXTUREWIN m_pfnQeglobalsTexturewin;\r
262         PFN_TEXTURESETTEXTURE   m_pfnTexture_SetTexture;\r
263         PFN_TEXTURESHOWINUSE    m_pfnTexture_ShowInuse;\r
264   PFN_BUILDSHADERLIST     m_pfnBuildShaderList;\r
265   PFN_PRELOADSHADERS      m_pfnPreloadShaders;\r
266 };\r
267 \r
268 #ifdef USE_APPSHADERSTABLE_DEFINE\r
269   #ifndef __APPSHADERTABLENAME\r
270     #define __APPSHADERTABLENAME g_AppShadersTable\r
271   #endif\r
272 #define Texture_ShowInuse __APPSHADERTABLENAME.m_pfnTexture_ShowInuse\r
273 #endif\r
274 \r
275 /*!\r
276 NOTE TTimo: there is an important distinction between SHADER_NOT_FOUND and SHADER_NOTEX:\r
277 SHADER_NOT_FOUND means we didn't find the raw texture or the shader for this\r
278 SHADER_NOTEX means we recognize this as a shader script, but we are missing the texture to represent it\r
279 this was in the initial design of the shader code since early GtkRadiant alpha, and got sort of foxed in 1.2 and put back in\r
280 */\r
281 #define SHADER_NOT_FOUND "textures/radiant/notex"\r
282 #define SHADER_NOTEX "textures/radiant/shadernotex" ///< Q3 tech specific\r
283 \r
284 #endif\r