COGLES2Driver.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // Copyright (C) 2013 Patryk Nadrowski
  2. // Heavily based on the OpenGL driver implemented by Nikolaus Gebhardt
  3. // OpenGL ES driver implemented by Christian Stehno and first OpenGL ES 2.0
  4. // driver implemented by Amundis.
  5. // This file is part of the "Irrlicht Engine".
  6. // For conditions of distribution and use, see copyright notice in Irrlicht.h
  7. #ifndef __C_OGLES2_DRIVER_H_INCLUDED__
  8. #define __C_OGLES2_DRIVER_H_INCLUDED__
  9. #include "IrrCompileConfig.h"
  10. #if defined(_IRR_WINDOWS_API_)
  11. // include windows headers for HWND
  12. #ifndef WIN32_LEAN_AND_MEAN
  13. #define WIN32_LEAN_AND_MEAN
  14. #endif
  15. #include <windows.h>
  16. #elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
  17. #include "MacOSX/CIrrDeviceMacOSX.h"
  18. #elif defined(_IRR_COMPILE_WITH_IPHONE_DEVICE_)
  19. #include "iOS/CIrrDeviceiOS.h"
  20. #endif
  21. #include "SIrrCreationParameters.h"
  22. #ifdef _IRR_COMPILE_WITH_OGLES2_
  23. #if defined(_IRR_COMPILE_WITH_IPHONE_DEVICE_)
  24. #include <OpenGLES/ES2/gl.h>
  25. #include <OpenGLES/ES2/glext.h>
  26. #elif defined(_IRR_COMPILE_WITH_ANDROID_DEVICE_)
  27. #include <EGL/egl.h>
  28. #include <GLES2/gl2.h>
  29. #include "android_native_app_glue.h"
  30. #else
  31. #include <EGL/eglplatform.h>
  32. #endif
  33. #include "CNullDriver.h"
  34. #include "IMaterialRendererServices.h"
  35. #include "EDriverFeatures.h"
  36. #include "fast_atof.h"
  37. #ifdef _MSC_VER
  38. #pragma comment(lib, "libEGL.lib")
  39. #pragma comment(lib, "libGLESv2.lib")
  40. #endif
  41. #include "COGLES2ExtensionHandler.h"
  42. namespace irr
  43. {
  44. namespace video
  45. {
  46. class COGLES2CallBridge;
  47. class COGLES2Texture;
  48. class COGLES2FixedPipelineRenderer;
  49. class COGLES2Renderer2D;
  50. class COGLES2NormalMapRenderer;
  51. class COGLES2ParallaxMapRenderer;
  52. class COGLES2Driver : public CNullDriver, public IMaterialRendererServices, public COGLES2ExtensionHandler
  53. {
  54. friend class COGLES2CallBridge;
  55. friend class COGLES2Texture;
  56. public:
  57. #if defined(_IRR_COMPILE_WITH_X11_DEVICE_) || defined(_IRR_COMPILE_WITH_SDL_DEVICE_) || defined(_IRR_WINDOWS_API_) || defined(_IRR_COMPILE_WITH_CONSOLE_DEVICE_)
  58. COGLES2Driver(const SIrrlichtCreationParameters& params,
  59. const SExposedVideoData& data,
  60. io::IFileSystem* io);
  61. #endif
  62. #ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
  63. COGLES2Driver(const SIrrlichtCreationParameters& params,
  64. io::IFileSystem* io, CIrrDeviceMacOSX *device);
  65. #endif
  66. #if defined(_IRR_COMPILE_WITH_IPHONE_DEVICE_)
  67. COGLES2Driver(const SIrrlichtCreationParameters& params,
  68. const SExposedVideoData& data,
  69. io::IFileSystem* io, CIrrDeviceIPhone* device);
  70. #endif
  71. //! destructor
  72. virtual ~COGLES2Driver();
  73. //! clears the zbuffer
  74. virtual bool beginScene(bool backBuffer=true, bool zBuffer=true,
  75. SColor color=SColor(255, 0, 0, 0),
  76. const SExposedVideoData& videoData=SExposedVideoData(),
  77. core::rect<s32>* sourceRect=0);
  78. //! presents the rendered scene on the screen, returns false if failed
  79. virtual bool endScene();
  80. //! sets transformation
  81. virtual void setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat);
  82. struct SHWBufferLink_opengl : public SHWBufferLink
  83. {
  84. SHWBufferLink_opengl(const scene::IMeshBuffer *meshBuffer): SHWBufferLink(meshBuffer), vbo_verticesID(0), vbo_indicesID(0) {}
  85. u32 vbo_verticesID; //tmp
  86. u32 vbo_indicesID; //tmp
  87. u32 vbo_verticesSize; //tmp
  88. u32 vbo_indicesSize; //tmp
  89. };
  90. bool updateVertexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);
  91. bool updateIndexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);
  92. //! updates hardware buffer if needed
  93. virtual bool updateHardwareBuffer(SHWBufferLink *HWBuffer);
  94. //! Create hardware buffer from mesh
  95. virtual SHWBufferLink *createHardwareBuffer(const scene::IMeshBuffer* mb);
  96. //! Delete hardware buffer (only some drivers can)
  97. virtual void deleteHardwareBuffer(SHWBufferLink *HWBuffer);
  98. //! Draw hardware buffer
  99. virtual void drawHardwareBuffer(SHWBufferLink *HWBuffer);
  100. //! draws a vertex primitive list
  101. virtual void drawVertexPrimitiveList(const void* vertices, u32 vertexCount,
  102. const void* indexList, u32 primitiveCount,
  103. E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType);
  104. void drawVertexPrimitiveList2d3d(const void* vertices, u32 vertexCount,
  105. const void* indexList, u32 primitiveCount,
  106. E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType,
  107. E_INDEX_TYPE iType = EIT_16BIT, bool threed = true);
  108. //! queries the features of the driver, returns true if feature is available
  109. virtual bool queryFeature(E_VIDEO_DRIVER_FEATURE feature) const
  110. {
  111. return FeatureEnabled[feature] && COGLES2ExtensionHandler::queryFeature(feature);
  112. }
  113. //! Sets a material.
  114. virtual void setMaterial(const SMaterial& material);
  115. //! draws an 2d image, using a color (if color is other then Color(255,255,255,255)) and the alpha channel of the texture if wanted.
  116. virtual void draw2DImage(const video::ITexture* texture,
  117. const core::position2d<s32>& destPos,
  118. const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
  119. SColor color = SColor(255, 255, 255, 255), bool useAlphaChannelOfTexture = false);
  120. //! draws a set of 2d images
  121. virtual void draw2DImageBatch(const video::ITexture* texture,
  122. const core::position2d<s32>& pos,
  123. const core::array<core::rect<s32> >& sourceRects,
  124. const core::array<s32>& indices, s32 kerningWidth = 0,
  125. const core::rect<s32>* clipRect = 0,
  126. SColor color = SColor(255, 255, 255, 255),
  127. bool useAlphaChannelOfTexture = false);
  128. //! Draws a part of the texture into the rectangle.
  129. virtual void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
  130. const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,
  131. const video::SColor* const colors = 0, bool useAlphaChannelOfTexture = false);
  132. void draw2DImageBatch(const video::ITexture* texture,
  133. const core::array<core::position2d<s32> >& positions,
  134. const core::array<core::rect<s32> >& sourceRects,
  135. const core::rect<s32>* clipRect,
  136. SColor color,
  137. bool useAlphaChannelOfTexture);
  138. //! draw an 2d rectangle
  139. virtual void draw2DRectangle(SColor color, const core::rect<s32>& pos,
  140. const core::rect<s32>* clip = 0);
  141. //!Draws an 2d rectangle with a gradient.
  142. virtual void draw2DRectangle(const core::rect<s32>& pos,
  143. SColor colorLeftUp, SColor colorRightUp, SColor colorLeftDown, SColor colorRightDown,
  144. const core::rect<s32>* clip = 0);
  145. //! Draws a 2d line.
  146. virtual void draw2DLine(const core::position2d<s32>& start,
  147. const core::position2d<s32>& end,
  148. SColor color = SColor(255, 255, 255, 255));
  149. //! Draws a single pixel
  150. virtual void drawPixel(u32 x, u32 y, const SColor & color);
  151. //! Draws a 3d line.
  152. virtual void draw3DLine(const core::vector3df& start,
  153. const core::vector3df& end,
  154. SColor color = SColor(255, 255, 255, 255));
  155. //! Draws a pixel
  156. // virtual void drawPixel(u32 x, u32 y, const SColor & color);
  157. //! Returns the name of the video driver.
  158. virtual const wchar_t* getName() const;
  159. //! deletes all dynamic lights there are
  160. virtual void deleteAllDynamicLights();
  161. //! adds a dynamic light
  162. virtual s32 addDynamicLight(const SLight& light);
  163. //! Turns a dynamic light on or off
  164. /** \param lightIndex: the index returned by addDynamicLight
  165. \param turnOn: true to turn the light on, false to turn it off */
  166. virtual void turnLightOn(s32 lightIndex, bool turnOn);
  167. //! returns the maximal amount of dynamic lights the device can handle
  168. virtual u32 getMaximalDynamicLightAmount() const;
  169. //! Sets the dynamic ambient light color.
  170. virtual void setAmbientLight(const SColorf& color);
  171. //! return the dynamic ambient light color.
  172. const SColorf& getAmbientLight() const;
  173. //! Returns the maximum texture size supported.
  174. virtual core::dimension2du getMaxTextureSize() const;
  175. //! Draws a shadow volume into the stencil buffer.
  176. virtual void drawStencilShadowVolume(const core::vector3df* triangles, s32 count, bool zfail);
  177. //! Fills the stencil shadow with color.
  178. virtual void drawStencilShadow(bool clearStencilBuffer = false,
  179. video::SColor leftUpEdge = video::SColor(0, 0, 0, 0),
  180. video::SColor rightUpEdge = video::SColor(0, 0, 0, 0),
  181. video::SColor leftDownEdge = video::SColor(0, 0, 0, 0),
  182. video::SColor rightDownEdge = video::SColor(0, 0, 0, 0));
  183. //! sets a viewport
  184. virtual void setViewPort(const core::rect<s32>& area);
  185. //! Only used internally by the engine
  186. virtual void OnResize(const core::dimension2d<u32>& size);
  187. //! Returns type of video driver
  188. virtual E_DRIVER_TYPE getDriverType() const;
  189. //! get color format of the current color buffer
  190. virtual ECOLOR_FORMAT getColorFormat() const;
  191. //! Returns the transformation set by setTransform
  192. virtual const core::matrix4& getTransform(E_TRANSFORMATION_STATE state) const;
  193. //! Can be called by an IMaterialRenderer to make its work easier.
  194. virtual void setBasicRenderStates(const SMaterial& material, const SMaterial& lastmaterial, bool resetAllRenderstates);
  195. //! Compare in SMaterial doesn't check texture parameters, so we should call this on each OnRender call.
  196. virtual void setTextureRenderStates(const SMaterial& material, bool resetAllRenderstates);
  197. //! Get a vertex shader constant index.
  198. virtual s32 getVertexShaderConstantID(const c8* name);
  199. //! Get a pixel shader constant index.
  200. virtual s32 getPixelShaderConstantID(const c8* name);
  201. //! Sets a vertex shader constant.
  202. virtual void setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount = 1);
  203. //! Sets a pixel shader constant.
  204. virtual void setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount = 1);
  205. //! Sets a constant for the vertex shader based on an index.
  206. virtual bool setVertexShaderConstant(s32 index, const f32* floats, int count);
  207. //! Int interface for the above.
  208. virtual bool setVertexShaderConstant(s32 index, const s32* ints, int count);
  209. //! Sets a constant for the pixel shader based on an index.
  210. virtual bool setPixelShaderConstant(s32 index, const f32* floats, int count);
  211. //! Int interface for the above.
  212. virtual bool setPixelShaderConstant(s32 index, const s32* ints, int count);
  213. //! sets the current Texture
  214. bool setActiveTexture(u32 stage, const video::ITexture* texture);
  215. //! check if active texture is not equal null.
  216. bool isActiveTexture(u32 stage);
  217. //! disables all textures beginning with fromStage.
  218. bool disableTextures(u32 fromStage = 0);
  219. //! Adds a new material renderer to the VideoDriver
  220. virtual s32 addShaderMaterial(const c8* vertexShaderProgram, const c8* pixelShaderProgram,
  221. IShaderConstantSetCallBack* callback, E_MATERIAL_TYPE baseMaterial, s32 userData);
  222. //! Adds a new material renderer to the VideoDriver
  223. virtual s32 addHighLevelShaderMaterial(
  224. const c8* vertexShaderProgram,
  225. const c8* vertexShaderEntryPointName = 0,
  226. E_VERTEX_SHADER_TYPE vsCompileTarget = EVST_VS_1_1,
  227. const c8* pixelShaderProgram = 0,
  228. const c8* pixelShaderEntryPointName = 0,
  229. E_PIXEL_SHADER_TYPE psCompileTarget = EPST_PS_1_1,
  230. const c8* geometryShaderProgram = 0,
  231. const c8* geometryShaderEntryPointName = "main",
  232. E_GEOMETRY_SHADER_TYPE gsCompileTarget = EGST_GS_4_0,
  233. scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES,
  234. scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP,
  235. u32 verticesOut = 0,
  236. IShaderConstantSetCallBack* callback = 0,
  237. E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID,
  238. s32 userData=0,
  239. E_GPU_SHADING_LANGUAGE shadingLang = EGSL_DEFAULT);
  240. //! Returns pointer to the IGPUProgrammingServices interface.
  241. virtual IGPUProgrammingServices* getGPUProgrammingServices();
  242. //! Returns a pointer to the IVideoDriver interface.
  243. virtual IVideoDriver* getVideoDriver();
  244. //! Returns the maximum amount of primitives
  245. virtual u32 getMaximalPrimitiveCount() const;
  246. virtual ITexture* addRenderTargetTexture(const core::dimension2d<u32>& size,
  247. const io::path& name, const ECOLOR_FORMAT format = ECF_UNKNOWN,
  248. const bool useStencil = false);
  249. virtual bool setRenderTarget(video::ITexture* texture, bool clearBackBuffer,
  250. bool clearZBuffer, SColor color);
  251. //! set or reset special render targets
  252. // virtual bool setRenderTarget(video::E_RENDER_TARGET target, bool clearTarget,
  253. // bool clearZBuffer, SColor color);
  254. //! Sets multiple render targets
  255. // virtual bool setRenderTarget(const core::array<video::IRenderTarget>& texture,
  256. // bool clearBackBuffer=true, bool clearZBuffer=true, SColor color=SColor(0,0,0,0));
  257. //! Clears the ZBuffer.
  258. virtual void clearZBuffer();
  259. //! Returns an image created from the last rendered frame.
  260. virtual IImage* createScreenShot(video::ECOLOR_FORMAT format=video::ECF_UNKNOWN, video::E_RENDER_TARGET target=video::ERT_FRAME_BUFFER);
  261. //! checks if an OpenGL error has happend and prints it
  262. bool testGLError();
  263. //! checks if an OGLES1 error has happend and prints it
  264. bool testEGLError();
  265. //! Set/unset a clipping plane.
  266. virtual bool setClipPlane(u32 index, const core::plane3df& plane, bool enable = false);
  267. //! returns the current amount of user clip planes set.
  268. u32 getClipPlaneCount() const;
  269. //! returns the 0 indexed Plane
  270. const core::plane3df& getClipPlane(u32 index) const;
  271. //! Enable/disable a clipping plane.
  272. virtual void enableClipPlane(u32 index, bool enable);
  273. //! Returns the graphics card vendor name.
  274. virtual core::stringc getVendorInfo()
  275. {
  276. return vendorName;
  277. };
  278. ITexture* createDepthTexture(ITexture* texture, bool shared = true);
  279. void removeDepthTexture(ITexture* texture);
  280. void deleteFramebuffers(s32 n, const u32 *framebuffers);
  281. void deleteRenderbuffers(s32 n, const u32 *renderbuffers);
  282. // returns the current size of the screen or rendertarget
  283. virtual const core::dimension2d<u32>& getCurrentRenderTargetSize() const;
  284. //! Convert E_BLEND_FACTOR to OpenGL equivalent
  285. GLenum getGLBlend(E_BLEND_FACTOR factor) const;
  286. //! Get ZBuffer bits.
  287. GLenum getZBufferBits() const;
  288. //! Get current material.
  289. const SMaterial& getCurrentMaterial() const;
  290. //! Get bridge calls.
  291. COGLES2CallBridge* getBridgeCalls() const;
  292. void reloadEGLSurface(void* window);
  293. private:
  294. // Bridge calls.
  295. COGLES2CallBridge* BridgeCalls;
  296. void uploadClipPlane(u32 index);
  297. //! inits the opengl-es driver
  298. bool genericDriverInit(const core::dimension2d<u32>& screenSize, bool stencilBuffer);
  299. //! returns a device dependent texture from a software surface (IImage)
  300. virtual video::ITexture* createDeviceDependentTexture(IImage* surface, const io::path& name, void* mipmapData);
  301. //! creates a transposed matrix in supplied GLfloat array to pass to OGLES1
  302. inline void createGLMatrix(float gl_matrix[16], const core::matrix4& m);
  303. inline void createGLTextureMatrix(float gl_matrix[16], const core::matrix4& m);
  304. //! Map Irrlicht wrap mode to OpenGL enum
  305. GLint getTextureWrapMode(u8 clamp) const;
  306. //! sets the needed renderstates
  307. void setRenderStates3DMode();
  308. //! sets the needed renderstates
  309. void setRenderStates2DMode(bool alpha, bool texture, bool alphaChannel);
  310. void createMaterialRenderers();
  311. core::stringw Name;
  312. core::matrix4 Matrices[ETS_COUNT];
  313. //! enumeration for rendering modes such as 2d and 3d for minizing the switching of renderStates.
  314. enum E_RENDER_MODE
  315. {
  316. ERM_NONE = 0, // no render state has been set yet.
  317. ERM_2D, // 2d drawing rendermode
  318. ERM_3D // 3d rendering mode
  319. };
  320. E_RENDER_MODE CurrentRenderMode;
  321. //! bool to make all renderstates reset if set to true.
  322. bool ResetRenderStates;
  323. bool Transformation3DChanged;
  324. u8 AntiAlias;
  325. SMaterial Material, LastMaterial;
  326. COGLES2Texture* RenderTargetTexture;
  327. const ITexture* CurrentTexture[MATERIAL_MAX_TEXTURES];
  328. core::array<ITexture*> DepthTextures;
  329. struct SUserClipPlane
  330. {
  331. core::plane3df Plane;
  332. bool Enabled;
  333. };
  334. core::array<SUserClipPlane> UserClipPlane;
  335. core::dimension2d<u32> CurrentRendertargetSize;
  336. core::stringc vendorName;
  337. core::matrix4 TextureFlipMatrix;
  338. //! Color buffer format
  339. ECOLOR_FORMAT ColorFormat;
  340. //! All the lights that have been requested; a hardware limited
  341. //! number of them will be used at once.
  342. struct RequestedLight
  343. {
  344. RequestedLight(SLight const & lightData)
  345. : LightData(lightData), DesireToBeOn(true) { }
  346. SLight LightData;
  347. bool DesireToBeOn;
  348. };
  349. core::array<RequestedLight> RequestedLights;
  350. SColorf AmbientLight;
  351. COGLES2Renderer2D* MaterialRenderer2D;
  352. #ifdef _IRR_COMPILE_WITH_WINDOWS_DEVICE_
  353. HDC HDc;
  354. #endif
  355. #if defined(_IRR_COMPILE_WITH_IPHONE_DEVICE_)
  356. CIrrDeviceIPhone* Device;
  357. GLuint ViewFramebuffer;
  358. GLuint ViewRenderbuffer;
  359. GLuint ViewDepthRenderbuffer;
  360. #else
  361. NativeWindowType EglWindow;
  362. void* EglDisplay;
  363. void* EglSurface;
  364. void* EglContext;
  365. EGLConfig EglConfig;
  366. #endif
  367. SIrrlichtCreationParameters Params;
  368. };
  369. //! This bridge between Irlicht pseudo OpenGL calls
  370. //! and true OpenGL calls.
  371. class COGLES2CallBridge
  372. {
  373. public:
  374. COGLES2CallBridge(COGLES2Driver* driver);
  375. // Blending calls.
  376. void setBlendFunc(GLenum source, GLenum destination);
  377. void setBlend(bool enable);
  378. // Cull face calls.
  379. void setCullFaceFunc(GLenum mode);
  380. void setCullFace(bool enable);
  381. // Depth calls.
  382. void setDepthFunc(GLenum mode);
  383. void setDepthMask(bool enable);
  384. void setDepthTest(bool enable);
  385. // Program calls.
  386. void setProgram(GLuint program);
  387. // Texture calls.
  388. void setActiveTexture(GLenum texture);
  389. void setTexture(u32 stage);
  390. // Viewport calls.
  391. void setViewport(const core::rect<s32>& viewport);
  392. private:
  393. COGLES2Driver* Driver;
  394. GLenum BlendSource;
  395. GLenum BlendDestination;
  396. bool Blend;
  397. GLenum CullFaceMode;
  398. bool CullFace;
  399. GLenum DepthFunc;
  400. bool DepthMask;
  401. bool DepthTest;
  402. GLuint Program;
  403. GLenum ActiveTexture;
  404. const ITexture* Texture[MATERIAL_MAX_TEXTURES];
  405. core::rect<s32> Viewport;
  406. };
  407. } // end namespace video
  408. } // end namespace irr
  409. #endif // _IRR_COMPILE_WITH_OPENGL_
  410. #endif