PolyRenderer.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. Copyright (C) 2015 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or se ll
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "polycode/core/PolyString.h"
  21. #include "polycode/core/PolyGlobals.h"
  22. #include "polycode/core/PolyMatrix4.h"
  23. #include "polycode/core/PolyVector2.h"
  24. #include "polycode/core/PolyShader.h"
  25. #include "polycode/core/PolyImage.h"
  26. #include "polycode/core/PolyRectangle.h"
  27. #include "polycode/core/PolyShader.h"
  28. #include "polycode/core/PolyRenderDataArray.h"
  29. #include "polycode/core/PolyThreaded.h"
  30. #include "polycode/core/PolyGPUDrawBuffer.h"
  31. #include <stack>
  32. #include <queue>
  33. #include <mutex>
  34. #define RENDERER_MAX_LIGHTS 8
  35. #define RENDERER_MAX_LIGHT_SHADOWS 2
  36. #define MAX_QUEUED_FRAMES 2
  37. namespace Polycode {
  38. class Texture;
  39. class RenderBuffer;
  40. class _PolyExport GraphicsInterface : public PolyBase {
  41. public:
  42. GraphicsInterface();
  43. virtual void setParamInShader(Shader *shader, ProgramParam *param, LocalShaderParam *localParam) = 0;
  44. virtual void setAttributeInShader(Shader *shader, ProgramAttribute *attribute, AttributeBinding *attributeBinding) = 0;
  45. virtual void disableAttribute(Shader *shader, const ProgramAttribute &attribute) = 0;
  46. virtual void createTexture(Texture *texture) = 0;
  47. virtual void setViewport(unsigned int x,unsigned int y,unsigned int width, unsigned height) = 0;
  48. virtual void clearBuffers(const Color &clearColor, bool colorBuffer, bool depthBuffer, bool stencilBuffer) = 0;
  49. virtual void createProgram(ShaderProgram *program) = 0;
  50. virtual void createShader(Shader *shader) = 0;
  51. virtual void useShader(Shader *shader) = 0;
  52. virtual void createSubmeshBuffers(MeshGeometry *submesh) = 0;
  53. virtual void drawSubmeshBuffers(MeshGeometry *submesh, Shader *shader) = 0;
  54. virtual void destroySubmeshBufferData(void *platformData) = 0;
  55. virtual void destroyProgramData(void *platformData) = 0;
  56. virtual void destroyShaderData(void *platformData) = 0;
  57. virtual void destroyTextureData(void *platformData) = 0;
  58. virtual void destroyRenderBufferData(void *platformData) = 0;
  59. virtual void enableDepthTest(bool val) = 0;
  60. virtual void enableDepthWrite(bool val) = 0;
  61. virtual void setBlendingMode(unsigned int blendingMode) = 0;
  62. virtual void enableBackfaceCulling(bool val) = 0;
  63. virtual void setLineSize(Number lineSize) = 0;
  64. virtual void enableScissor(bool val) = 0;
  65. virtual void setScissorBox(const Polycode::Rectangle &box) = 0;
  66. virtual void setWireframeMode(bool val) = 0;
  67. virtual void createRenderBuffer(RenderBuffer *renderBuffer) = 0;
  68. virtual void bindRenderBuffer(RenderBuffer *buffer) = 0;
  69. virtual void beginDrawCall() = 0;
  70. virtual void endDrawCall() = 0;
  71. };
  72. class _PolyExport RendererThreadJob {
  73. public:
  74. int jobType;
  75. void *data;
  76. void *data2;
  77. };
  78. class RenderThreadDebugInfo {
  79. public:
  80. unsigned int buffersProcessed;
  81. unsigned int drawCallsProcessed;
  82. unsigned int timeTaken;
  83. };
  84. class LightInfoBinding {
  85. public:
  86. std::shared_ptr<LocalShaderParam> position;
  87. std::shared_ptr<LocalShaderParam> direction;
  88. std::shared_ptr<LocalShaderParam> specular;
  89. std::shared_ptr<LocalShaderParam> diffuse;
  90. std::shared_ptr<LocalShaderParam> spotExponent;
  91. std::shared_ptr<LocalShaderParam> spotCosCutoff;
  92. std::shared_ptr<LocalShaderParam> constantAttenuation;
  93. std::shared_ptr<LocalShaderParam> linearAttenuation;
  94. std::shared_ptr<LocalShaderParam> quadraticAttenuation;
  95. std::shared_ptr<LocalShaderParam> shadowEnabled;
  96. };
  97. class LightShadowInfoBinding {
  98. public:
  99. std::shared_ptr<LocalShaderParam> shadowMatrix;
  100. std::shared_ptr<LocalShaderParam> shadowBuffer;
  101. };
  102. class _PolyExport RenderFrame : public PolyBase {
  103. public:
  104. RenderFrame(const Polycode::Rectangle &viewport);
  105. ~RenderFrame();
  106. void addDrawBuffer(GPUDrawBuffer *buffer);
  107. std::queue<GPUDrawBuffer*> drawBuffers;
  108. Polycode::Rectangle viewport;
  109. void *customFrameFinalizerData;
  110. void(*customFrameFinalizer)(void*);
  111. };
  112. class _PolyExport RenderThread : public Threaded {
  113. public:
  114. RenderThread();
  115. void setGraphicsInterface(Core *core, GraphicsInterface *graphicsInterface);
  116. ~RenderThread();
  117. virtual void runThread();
  118. void beginFrame();
  119. void endFrame();
  120. void updateRenderThread();
  121. void enqueueFrame(RenderFrame *frame);
  122. void enqueueJob(int jobType, void *data, void *data2=NULL);
  123. void processJob(const RendererThreadJob &job);
  124. void clearFrameQueue();
  125. ShaderBinding *getShaderBinding();
  126. void processDrawBufferLights(GPUDrawBuffer *buffer);
  127. void processDrawBuffer(GPUDrawBuffer *buffer);
  128. RenderThreadDebugInfo getFrameInfo();
  129. void initGlobals();
  130. void lockRenderMutex();
  131. void unlockRenderMutex();
  132. static const int JOB_REQUEST_CONTEXT_CHANGE = 0;
  133. static const int JOB_DESTROY_TEXTURE = 8;
  134. static const int JOB_DESTROY_SHADER = 9;
  135. static const int JOB_DESTROY_PROGRAM = 10;
  136. static const int JOB_DESTROY_SUBMESH_BUFFER = 11;
  137. static const int JOB_DESTROY_RENDER_BUFFER = 13;
  138. protected:
  139. unsigned int frameStart;
  140. RenderThreadDebugInfo lastFrameDebugInfo;
  141. RenderThreadDebugInfo currentDebugFrameInfo;
  142. std::vector<RenderFrame*> framesToDelete;
  143. Core *core;
  144. std::mutex jobQueueMutex;
  145. std::mutex renderMutex;
  146. std::queue<RendererThreadJob> jobQueue;
  147. std::queue<RenderFrame*> frameQueue;
  148. GraphicsInterface *graphicsInterface;
  149. ShaderBinding *rendererShaderBinding;
  150. std::shared_ptr<LocalShaderParam> projectionMatrixParam;
  151. std::shared_ptr<LocalShaderParam> viewMatrixParam;
  152. std::shared_ptr<LocalShaderParam> modelMatrixParam;
  153. LightInfoBinding lights[RENDERER_MAX_LIGHTS];
  154. LightShadowInfoBinding lightShadows[RENDERER_MAX_LIGHT_SHADOWS];
  155. };
  156. class _PolyExport Renderer : public PolyBase {
  157. public:
  158. Renderer(Core *core, RenderThread *customThread=NULL);
  159. virtual ~Renderer();
  160. void setGraphicsInterface(Core *core, GraphicsInterface *graphicsInterface);
  161. RenderThread *getRenderThread();
  162. void setBackingResolutionScale(Number xScale, Number yScale);
  163. Number getBackingResolutionScaleX();
  164. Number getBackingResolutionScaleY();
  165. void destroyRenderBufferPlatformData(void *platformData);
  166. void destroyTexturePlatformData(void *platformData);
  167. void destroyProgramPlatformData(void *platformData);
  168. void destroyShaderPlatformData(void *platformData);
  169. void destroySubmeshPlatformData(void *platformData);
  170. void setAnisotropyAmount(Number amount);
  171. Number getAnisotropyAmount();
  172. static Vector3 unProject(const Vector3 &position, const Matrix4 &modelMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport);
  173. static Vector3 project(const Vector3 &position, const Matrix4 &modelMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport);
  174. void submitRenderFrame(RenderFrame *newFrame);
  175. static const int BLEND_MODE_NONE = 0;
  176. static const int BLEND_MODE_NORMAL = 1;
  177. static const int BLEND_MODE_LIGHTEN = 2;
  178. static const int BLEND_MODE_COLOR = 3;
  179. static const int BLEND_MODE_PREMULTIPLIED = 4;
  180. static const int BLEND_MODE_MULTIPLY = 5;
  181. static const int BLEND_MODE_MATERIAL = 6;
  182. static const int DEPTH_FUNCTION_GREATER = 0;
  183. static const int DEPTH_FUNCTION_LEQUAL = 1;
  184. protected:
  185. Number backingResolutionScaleX;
  186. Number backingResolutionScaleY;
  187. Number anisotropy;
  188. int cpuBufferIndex;
  189. int gpuBufferIndex;
  190. RenderThread *renderThread;
  191. };
  192. }