1 // Copyright (C) 2002-2012 Nikolaus Gebhardt 2 // This file is part of the "Irrlicht Engine". 3 // For conditions of distribution and use, see copyright notice in irrlicht.h 4 5 #ifndef __I_GPU_PROGRAMMING_SERVICES_H_INCLUDED__ 6 #define __I_GPU_PROGRAMMING_SERVICES_H_INCLUDED__ 7 8 #include "EShaderTypes.h" 9 #include "EMaterialTypes.h" 10 #include "EPrimitiveTypes.h" 11 #include "path.h" 12 13 namespace irr 14 { 15 16 namespace io 17 { 18 class IReadFile; 19 } // end namespace io 20 21 namespace video 22 { 23 24 class IVideoDriver; 25 class IShaderConstantSetCallBack; 26 27 //! Enumeration for different types of shading languages 28 enum E_GPU_SHADING_LANGUAGE 29 { 30 //! The default language, so HLSL for Direct3D and GLSL for OpenGL. 31 EGSL_DEFAULT = 0, 32 33 //! Cg shading language.*/ 34 EGSL_CG 35 }; 36 37 //! Interface making it possible to create and use programs running on the GPU. 38 class IGPUProgrammingServices 39 { 40 public: 41 42 //! Destructor ~IGPUProgrammingServices()43 virtual ~IGPUProgrammingServices() {} 44 45 //! Adds a new high-level shading material renderer to the VideoDriver. 46 /** Currently only HLSL/D3D9 and GLSL/OpenGL are supported. 47 \param vertexShaderProgram String containing the source of the vertex 48 shader program. This can be 0 if no vertex program shall be used. 49 \param vertexShaderEntryPointName Name of the entry function of the 50 vertexShaderProgram (p.e. "main") 51 \param vsCompileTarget Vertex shader version the high level shader 52 shall be compiled to. 53 \param pixelShaderProgram String containing the source of the pixel 54 shader program. This can be 0 if no pixel shader shall be used. 55 \param pixelShaderEntryPointName Entry name of the function of the 56 pixelShaderProgram (p.e. "main") 57 \param psCompileTarget Pixel shader version the high level shader 58 shall be compiled to. 59 \param geometryShaderProgram String containing the source of the 60 geometry shader program. This can be 0 if no geometry shader shall be 61 used. 62 \param geometryShaderEntryPointName Entry name of the function of the 63 geometryShaderProgram (p.e. "main") 64 \param gsCompileTarget Geometry shader version the high level shader 65 shall be compiled to. 66 \param inType Type of vertices passed to geometry shader 67 \param outType Type of vertices created by geometry shader 68 \param verticesOut Maximal number of vertices created by geometry 69 shader. If 0, maximal number supported is assumed. 70 \param callback Pointer to an implementation of 71 IShaderConstantSetCallBack in which you can set the needed vertex, 72 pixel, and geometry shader program constants. Set this to 0 if you 73 don't need this. 74 \param baseMaterial Base material which renderstates will be used to 75 shade the material. 76 \param userData a user data int. This int can be set to any value and 77 will be set as parameter in the callback method when calling 78 OnSetConstants(). In this way it is easily possible to use the same 79 callback method for multiple materials and distinguish between them 80 during the call. 81 \param shaderLang a type of shading language used in current shader. 82 \return Number of the material type which can be set in 83 SMaterial::MaterialType to use the renderer. -1 is returned if an error 84 occured, e.g. if a shader program could not be compiled or a compile 85 target is not reachable. The error strings are then printed to the 86 error log and can be catched with a custom event receiver. */ 87 virtual s32 addHighLevelShaderMaterial( 88 const c8* vertexShaderProgram, 89 const c8* vertexShaderEntryPointName, 90 E_VERTEX_SHADER_TYPE vsCompileTarget, 91 const c8* pixelShaderProgram, 92 const c8* pixelShaderEntryPointName, 93 E_PIXEL_SHADER_TYPE psCompileTarget, 94 const c8* geometryShaderProgram, 95 const c8* geometryShaderEntryPointName = "main", 96 E_GEOMETRY_SHADER_TYPE gsCompileTarget = EGST_GS_4_0, 97 scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES, 98 scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP, 99 u32 verticesOut = 0, 100 IShaderConstantSetCallBack* callback = 0, 101 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 102 s32 userData = 0, 103 E_GPU_SHADING_LANGUAGE shadingLang = EGSL_DEFAULT) = 0; 104 105 //! convenience function for use without geometry shaders 106 s32 addHighLevelShaderMaterial( 107 const c8* vertexShaderProgram, 108 const c8* vertexShaderEntryPointName="main", 109 E_VERTEX_SHADER_TYPE vsCompileTarget=EVST_VS_1_1, 110 const c8* pixelShaderProgram=0, 111 const c8* pixelShaderEntryPointName="main", 112 E_PIXEL_SHADER_TYPE psCompileTarget=EPST_PS_1_1, 113 IShaderConstantSetCallBack* callback=0, 114 E_MATERIAL_TYPE baseMaterial=video::EMT_SOLID, 115 s32 userData=0, 116 E_GPU_SHADING_LANGUAGE shadingLang=EGSL_DEFAULT) 117 { 118 return addHighLevelShaderMaterial( 119 vertexShaderProgram, vertexShaderEntryPointName, 120 vsCompileTarget, pixelShaderProgram, 121 pixelShaderEntryPointName, psCompileTarget, 122 0, "main", EGST_GS_4_0, 123 scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, 124 callback, baseMaterial, userData, shadingLang); 125 } 126 127 //! convenience function for use with many defaults, without geometry shader 128 /** All shader names are set to "main" and compile targets are shader 129 type 1.1. 130 */ 131 s32 addHighLevelShaderMaterial( 132 const c8* vertexShaderProgram, 133 const c8* pixelShaderProgram=0, 134 IShaderConstantSetCallBack* callback=0, 135 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 136 s32 userData=0) 137 { 138 return addHighLevelShaderMaterial( 139 vertexShaderProgram, "main", 140 EVST_VS_1_1, pixelShaderProgram, 141 "main", EPST_PS_1_1, 142 0, "main", EGST_GS_4_0, 143 scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, 144 callback, baseMaterial, userData); 145 } 146 147 //! convenience function for use with many defaults, with geometry shader 148 /** All shader names are set to "main" and compile targets are shader 149 type 1.1 and geometry shader 4.0. 150 */ 151 s32 addHighLevelShaderMaterial( 152 const c8* vertexShaderProgram, 153 const c8* pixelShaderProgram = 0, 154 const c8* geometryShaderProgram = 0, 155 scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES, 156 scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP, 157 u32 verticesOut = 0, 158 IShaderConstantSetCallBack* callback = 0, 159 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 160 s32 userData = 0 ) 161 { 162 return addHighLevelShaderMaterial( 163 vertexShaderProgram, "main", 164 EVST_VS_1_1, pixelShaderProgram, 165 "main", EPST_PS_1_1, 166 geometryShaderProgram, "main", EGST_GS_4_0, 167 inType, outType, verticesOut, 168 callback, baseMaterial, userData); 169 } 170 171 //! Like IGPUProgrammingServices::addShaderMaterial(), but loads from files. 172 /** \param vertexShaderProgramFileName Text file containing the source 173 of the vertex shader program. Set to empty string if no vertex shader 174 shall be created. 175 \param vertexShaderEntryPointName Name of the entry function of the 176 vertexShaderProgram (p.e. "main") 177 \param vsCompileTarget Vertex shader version the high level shader 178 shall be compiled to. 179 \param pixelShaderProgramFileName Text file containing the source of 180 the pixel shader program. Set to empty string if no pixel shader shall 181 be created. 182 \param pixelShaderEntryPointName Entry name of the function of the 183 pixelShaderProgram (p.e. "main") 184 \param psCompileTarget Pixel shader version the high level shader 185 shall be compiled to. 186 \param geometryShaderProgramFileName Name of the source of 187 the geometry shader program. Set to empty string if no geometry shader 188 shall be created. 189 \param geometryShaderEntryPointName Entry name of the function of the 190 geometryShaderProgram (p.e. "main") 191 \param gsCompileTarget Geometry shader version the high level shader 192 shall be compiled to. 193 \param inType Type of vertices passed to geometry shader 194 \param outType Type of vertices created by geometry shader 195 \param verticesOut Maximal number of vertices created by geometry 196 shader. If 0, maximal number supported is assumed. 197 \param callback Pointer to an implementation of 198 IShaderConstantSetCallBack in which you can set the needed vertex, 199 pixel, and geometry shader program constants. Set this to 0 if you 200 don't need this. 201 \param baseMaterial Base material which renderstates will be used to 202 shade the material. 203 \param userData a user data int. This int can be set to any value and 204 will be set as parameter in the callback method when calling 205 OnSetConstants(). In this way it is easily possible to use the same 206 callback method for multiple materials and distinguish between them 207 during the call. 208 \param shaderLang a type of shading language used in current shader. 209 \return Number of the material type which can be set in 210 SMaterial::MaterialType to use the renderer. -1 is returned if an error 211 occured, e.g. if a shader program could not be compiled or a compile 212 target is not reachable. The error strings are then printed to the 213 error log and can be catched with a custom event receiver. */ 214 virtual s32 addHighLevelShaderMaterialFromFiles( 215 const io::path& vertexShaderProgramFileName, 216 const c8* vertexShaderEntryPointName, 217 E_VERTEX_SHADER_TYPE vsCompileTarget, 218 const io::path& pixelShaderProgramFileName, 219 const c8* pixelShaderEntryPointName, 220 E_PIXEL_SHADER_TYPE psCompileTarget, 221 const io::path& geometryShaderProgramFileName, 222 const c8* geometryShaderEntryPointName = "main", 223 E_GEOMETRY_SHADER_TYPE gsCompileTarget = EGST_GS_4_0, 224 scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES, 225 scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP, 226 u32 verticesOut = 0, 227 IShaderConstantSetCallBack* callback = 0, 228 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 229 s32 userData = 0, 230 E_GPU_SHADING_LANGUAGE shadingLang = EGSL_DEFAULT) = 0; 231 232 //! convenience function for use without geometry shaders 233 s32 addHighLevelShaderMaterialFromFiles( 234 const io::path& vertexShaderProgramFileName, 235 const c8* vertexShaderEntryPointName = "main", 236 E_VERTEX_SHADER_TYPE vsCompileTarget = EVST_VS_1_1, 237 const io::path& pixelShaderProgramFileName = "", 238 const c8* pixelShaderEntryPointName = "main", 239 E_PIXEL_SHADER_TYPE psCompileTarget = EPST_PS_1_1, 240 IShaderConstantSetCallBack* callback = 0, 241 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 242 s32 userData = 0, 243 E_GPU_SHADING_LANGUAGE shadingLang = EGSL_DEFAULT) 244 { 245 return addHighLevelShaderMaterialFromFiles( 246 vertexShaderProgramFileName, vertexShaderEntryPointName, 247 vsCompileTarget, pixelShaderProgramFileName, 248 pixelShaderEntryPointName, psCompileTarget, 249 "", "main", EGST_GS_4_0, 250 scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, 251 callback, baseMaterial, userData, shadingLang); 252 } 253 254 //! convenience function for use with many defaults, without geometry shader 255 /** All shader names are set to "main" and compile targets are shader 256 type 1.1. 257 */ 258 s32 addHighLevelShaderMaterialFromFiles( 259 const io::path& vertexShaderProgramFileName, 260 const io::path& pixelShaderProgramFileName = "", 261 IShaderConstantSetCallBack* callback = 0, 262 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 263 s32 userData = 0 ) 264 { 265 return addHighLevelShaderMaterialFromFiles( 266 vertexShaderProgramFileName, "main", 267 EVST_VS_1_1, pixelShaderProgramFileName, 268 "main", EPST_PS_1_1, 269 "", "main", EGST_GS_4_0, 270 scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, 271 callback, baseMaterial, userData); 272 } 273 274 //! convenience function for use with many defaults, with geometry shader 275 /** All shader names are set to "main" and compile targets are shader 276 type 1.1 and geometry shader 4.0. 277 */ 278 s32 addHighLevelShaderMaterialFromFiles( 279 const io::path& vertexShaderProgramFileName, 280 const io::path& pixelShaderProgramFileName = "", 281 const io::path& geometryShaderProgramFileName = "", 282 scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES, 283 scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP, 284 u32 verticesOut = 0, 285 IShaderConstantSetCallBack* callback = 0, 286 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 287 s32 userData = 0 ) 288 { 289 return addHighLevelShaderMaterialFromFiles( 290 vertexShaderProgramFileName, "main", 291 EVST_VS_1_1, pixelShaderProgramFileName, 292 "main", EPST_PS_1_1, 293 geometryShaderProgramFileName, "main", EGST_GS_4_0, 294 inType, outType, verticesOut, 295 callback, baseMaterial, userData); 296 } 297 298 //! Like IGPUProgrammingServices::addShaderMaterial(), but loads from files. 299 /** \param vertexShaderProgram Text file handle containing the source 300 of the vertex shader program. Set to 0 if no vertex shader shall be 301 created. 302 \param vertexShaderEntryPointName Name of the entry function of the 303 vertexShaderProgram 304 \param vsCompileTarget Vertex shader version the high level shader 305 shall be compiled to. 306 \param pixelShaderProgram Text file handle containing the source of 307 the pixel shader program. Set to 0 if no pixel shader shall be created. 308 \param pixelShaderEntryPointName Entry name of the function of the 309 pixelShaderProgram (p.e. "main") 310 \param psCompileTarget Pixel shader version the high level shader 311 shall be compiled to. 312 \param geometryShaderProgram Text file handle containing the source of 313 the geometry shader program. Set to 0 if no geometry shader shall be 314 created. 315 \param geometryShaderEntryPointName Entry name of the function of the 316 geometryShaderProgram (p.e. "main") 317 \param gsCompileTarget Geometry shader version the high level shader 318 shall be compiled to. 319 \param inType Type of vertices passed to geometry shader 320 \param outType Type of vertices created by geometry shader 321 \param verticesOut Maximal number of vertices created by geometry 322 shader. If 0, maximal number supported is assumed. 323 \param callback Pointer to an implementation of 324 IShaderConstantSetCallBack in which you can set the needed vertex and 325 pixel shader program constants. Set this to 0 if you don't need this. 326 \param baseMaterial Base material which renderstates will be used to 327 shade the material. 328 \param userData a user data int. This int can be set to any value and 329 will be set as parameter in the callback method when calling 330 OnSetConstants(). In this way it is easily possible to use the same 331 callback method for multiple materials and distinguish between them 332 during the call. 333 \param shaderLang a type of shading language used in current shader. 334 \return Number of the material type which can be set in 335 SMaterial::MaterialType to use the renderer. -1 is returned if an 336 error occured, e.g. if a shader program could not be compiled or a 337 compile target is not reachable. The error strings are then printed to 338 the error log and can be catched with a custom event receiver. */ 339 virtual s32 addHighLevelShaderMaterialFromFiles( 340 io::IReadFile* vertexShaderProgram, 341 const c8* vertexShaderEntryPointName, 342 E_VERTEX_SHADER_TYPE vsCompileTarget, 343 io::IReadFile* pixelShaderProgram, 344 const c8* pixelShaderEntryPointName, 345 E_PIXEL_SHADER_TYPE psCompileTarget, 346 io::IReadFile* geometryShaderProgram, 347 const c8* geometryShaderEntryPointName = "main", 348 E_GEOMETRY_SHADER_TYPE gsCompileTarget = EGST_GS_4_0, 349 scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES, 350 scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP, 351 u32 verticesOut = 0, 352 IShaderConstantSetCallBack* callback = 0, 353 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 354 s32 userData = 0, 355 E_GPU_SHADING_LANGUAGE shadingLang = EGSL_DEFAULT) = 0; 356 357 //! convenience function for use without geometry shaders 358 s32 addHighLevelShaderMaterialFromFiles( 359 io::IReadFile* vertexShaderProgram, 360 const c8* vertexShaderEntryPointName = "main", 361 E_VERTEX_SHADER_TYPE vsCompileTarget = EVST_VS_1_1, 362 io::IReadFile* pixelShaderProgram = 0, 363 const c8* pixelShaderEntryPointName = "main", 364 E_PIXEL_SHADER_TYPE psCompileTarget = EPST_PS_1_1, 365 IShaderConstantSetCallBack* callback = 0, 366 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 367 s32 userData = 0, 368 E_GPU_SHADING_LANGUAGE shadingLang = EGSL_DEFAULT) 369 { 370 return addHighLevelShaderMaterialFromFiles( 371 vertexShaderProgram, vertexShaderEntryPointName, 372 vsCompileTarget, pixelShaderProgram, 373 pixelShaderEntryPointName, psCompileTarget, 374 0, "main", EGST_GS_4_0, 375 scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, 376 callback, baseMaterial, userData, shadingLang); 377 } 378 379 //! Adds a new ASM shader material renderer to the VideoDriver 380 /** Note that it is a good idea to call IVideoDriver::queryFeature() in 381 advance to check if the IVideoDriver supports the vertex and/or pixel 382 shader version your are using. 383 384 The material is added to the VideoDriver like with 385 IVideoDriver::addMaterialRenderer() and can be used like it had been 386 added with that method. 387 \param vertexShaderProgram String containing the source of the vertex 388 shader program. This can be 0 if no vertex program shall be used. 389 390 For DX8 programs, the will always input registers look like this: v0: 391 position, v1: normal, v2: color, v3: texture cooridnates, v4: texture 392 coordinates 2 if available. 393 394 For DX9 programs, you can manually set the registers using the dcl_ 395 statements. 396 \param pixelShaderProgram String containing the source of the pixel 397 shader program. This can be 0 if you don't want to use a pixel shader. 398 \param callback Pointer to an implementation of 399 IShaderConstantSetCallBack in which you can set the needed vertex and 400 pixel shader program constants. Set this to 0 if you don't need this. 401 \param baseMaterial Base material which renderstates will be used to 402 shade the material. 403 \param userData a user data int. This int can be set to any value and 404 will be set as parameter in the callback method when calling 405 OnSetConstants(). In this way it is easily possible to use the same 406 callback method for multiple materials and distinguish between them 407 during the call. 408 \return Returns the number of the material type which can be set in 409 SMaterial::MaterialType to use the renderer. -1 is returned if an 410 error occured. -1 is returned for example if a vertex or pixel shader 411 program could not be compiled, the error strings are then printed out 412 into the error log, and can be catched with a custom event receiver. */ 413 virtual s32 addShaderMaterial(const c8* vertexShaderProgram = 0, 414 const c8* pixelShaderProgram = 0, 415 IShaderConstantSetCallBack* callback = 0, 416 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 417 s32 userData = 0) = 0; 418 419 //! Like IGPUProgrammingServices::addShaderMaterial(), but loads from files. 420 /** \param vertexShaderProgram Text file containing the source of the 421 vertex shader program. Set to 0 if no shader shall be created. 422 \param pixelShaderProgram Text file containing the source of the pixel 423 shader program. Set to 0 if no shader shall be created. 424 \param callback Pointer to an IShaderConstantSetCallback object to 425 which the OnSetConstants function is called. 426 \param baseMaterial baseMaterial 427 \param userData a user data int. This int can be set to any value and 428 will be set as parameter in the callback method when calling 429 OnSetConstants(). In this way it is easily possible to use the same 430 callback method for multiple materials and distinguish between them 431 during the call. 432 \return Returns the number of the material type which can be set in 433 SMaterial::MaterialType to use the renderer. -1 is returned if an 434 error occured. -1 is returned for example if a vertex or pixel shader 435 program could not be compiled, the error strings are then printed out 436 into the error log, and can be catched with a custom event receiver. */ 437 virtual s32 addShaderMaterialFromFiles(io::IReadFile* vertexShaderProgram, 438 io::IReadFile* pixelShaderProgram, 439 IShaderConstantSetCallBack* callback = 0, 440 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 441 s32 userData = 0) = 0; 442 443 //! Like IGPUProgrammingServices::addShaderMaterial(), but loads from files. 444 /** \param vertexShaderProgramFileName Text file name containing the 445 source of the vertex shader program. Set to 0 if no shader shall be 446 created. 447 \param pixelShaderProgramFileName Text file name containing the source 448 of the pixel shader program. Set to 0 if no shader shall be created. 449 \param callback Pointer to an IShaderConstantSetCallback object on 450 which the OnSetConstants function is called. 451 \param baseMaterial baseMaterial 452 \param userData a user data int. This int can be set to any value and 453 will be set as parameter in the callback method when calling 454 OnSetConstants(). In this way it is easily possible to use the same 455 callback method for multiple materials and distinguish between them 456 during the call. 457 \return Returns the number of the material type which can be set in 458 SMaterial::MaterialType to use the renderer. -1 is returned if an 459 error occured. -1 is returned for example if a vertex or pixel shader 460 program could not be compiled, the error strings are then printed out 461 into the error log, and can be catched with a custom event receiver. */ 462 virtual s32 addShaderMaterialFromFiles(const io::path& vertexShaderProgramFileName, 463 const io::path& pixelShaderProgramFileName, 464 IShaderConstantSetCallBack* callback = 0, 465 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID, 466 s32 userData = 0) = 0; 467 }; 468 469 470 } // end namespace video 471 } // end namespace irr 472 473 #endif 474 475