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