PolyRenderer.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 sell
  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 "PolyString.h"
  21. #include "PolyGlobals.h"
  22. #include "PolyMatrix4.h"
  23. #include "PolyVector2.h"
  24. #include "PolyShader.h"
  25. #include "PolyImage.h"
  26. #include "PolyRectangle.h"
  27. #include "PolyShader.h"
  28. #include "PolyRenderDataArray.h"
  29. #include "PolyThreaded.h"
  30. #include "PolyGPUDrawBuffer.h"
  31. #include <stack>
  32. #include <queue>
  33. namespace Polycode {
  34. class _PolyExport GraphicsInterface : public PolyBase {
  35. public:
  36. GraphicsInterface();
  37. virtual void setParamInShader(Shader *shader, ProgramParam *param, LocalShaderParam *localParam) = 0;
  38. virtual void setAttributeInShader(Shader *shader, ProgramAttribute *attribute, AttributeBinding *attributeBinding) = 0;
  39. virtual void disableAttribute(Shader *shader, const ProgramAttribute &attribute) = 0;
  40. virtual void createTexture(Texture *texture) = 0;
  41. virtual void setViewport(unsigned int x,unsigned int y,unsigned int width, unsigned height) = 0;
  42. virtual void clearBuffers(const Color &clearColor, bool colorBuffer, bool depthBuffer, bool stencilBuffer) = 0;
  43. virtual void createProgram(ShaderProgram *program) = 0;
  44. virtual void createShader(Shader *shader) = 0;
  45. virtual void useShader(Shader *shader) = 0;
  46. virtual void createVertexBuffer(VertexDataArray *dataArray) = 0;
  47. virtual void createIndexBuffer(IndexDataArray *dataArray) = 0;
  48. virtual void bindFramebuffer(Texture *framebufferTexture) = 0;
  49. virtual void drawIndices(int type, IndexDataArray *indexArray) = 0;
  50. virtual void drawArrays(int type, unsigned int vertexCount) = 0;
  51. virtual void enableDepthTest(bool val) = 0;
  52. virtual void enableDepthWrite(bool val) = 0;
  53. virtual void setBlendingMode(unsigned int blendingMode) = 0;
  54. virtual void enableBackfaceCulling(bool val) = 0;
  55. virtual void beginDrawCall() = 0;
  56. virtual void endDrawCall() = 0;
  57. };
  58. class _PolyExport RendererThreadJob {
  59. public:
  60. int jobType;
  61. void *data;
  62. };
  63. class RenderThreadDebugInfo {
  64. public:
  65. unsigned int buffersProcessed;
  66. unsigned int drawCallsProcessed;
  67. unsigned int timeTaken;
  68. };
  69. class _PolyExport RenderThread : public Threaded {
  70. public:
  71. RenderThread();
  72. void setGraphicsInterface(Core *core, GraphicsInterface *interface);
  73. virtual void runThread();
  74. void enqueueJob(int jobType, void *data);
  75. void processJob(const RendererThreadJob &job);
  76. ShaderBinding *getShaderBinding();
  77. void processDrawBuffer(GPUDrawBuffer *buffer);
  78. RenderThreadDebugInfo getFrameInfo();
  79. static const int JOB_REQUEST_CONTEXT_CHANGE = 0;
  80. static const int JOB_CREATE_TEXTURE = 1;
  81. static const int JOB_PROCESS_DRAW_BUFFER = 2;
  82. static const int JOB_END_FRAME = 3;
  83. static const int JOB_CREATE_PROGRAM = 4;
  84. static const int JOB_CREATE_SHADER = 5;
  85. static const int JOB_BEGIN_FRAME = 6;
  86. static const int JOB_CREATE_VERTEX_BUFFERS = 7;
  87. protected:
  88. unsigned int frameStart;
  89. RenderThreadDebugInfo lastFrameDebugInfo;
  90. RenderThreadDebugInfo currentDebugFrameInfo;
  91. Core *core;
  92. CoreMutex *jobQueueMutex;
  93. std::queue<RendererThreadJob> jobQueue;
  94. GraphicsInterface *interface;
  95. ShaderBinding *rendererShaderBinding;
  96. LocalShaderParam *projectionMatrixParam;
  97. LocalShaderParam *viewMatrixParam;
  98. LocalShaderParam *modelMatrixParam;
  99. };
  100. class _PolyExport Renderer : public PolyBase {
  101. public:
  102. Renderer();
  103. virtual ~Renderer();
  104. void setGraphicsInterface(Core *core, GraphicsInterface *interface);
  105. RenderThread *getRenderThread();
  106. Cubemap *createCubemap(Texture *t0, Texture *t1, Texture *t2, Texture *t3, Texture *t4, Texture *t5);
  107. Texture *createTexture(unsigned int width, unsigned int height, char *textureData, bool clamp, bool createMipmaps, int type, unsigned int filteringMode, unsigned int anisotropy, bool framebufferTexture);
  108. void destroyTexture(Texture *texture);
  109. void processDrawBuffer(GPUDrawBuffer *buffer);
  110. void setBackingResolutionScale(Number xScale, Number yScale);
  111. Number getBackingResolutionScaleX();
  112. Number getBackingResolutionScaleY();
  113. ShaderProgram *createProgram(const String &fileName);
  114. Shader *createShader(ShaderProgram *vertexProgram, ShaderProgram *fragmentProgram);
  115. void createVertexBuffers(Mesh *mesh);
  116. void setAnisotropyAmount(Number amount);
  117. Number getAnisotropyAmount();
  118. static Vector3 unProject(const Vector3 &position, const Matrix4 &modelMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport);
  119. static Vector3 project(const Vector3 &position, const Matrix4 &modelMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport);
  120. void beginFrame();
  121. void endFrame();
  122. static const int BLEND_MODE_NONE = 0;
  123. static const int BLEND_MODE_NORMAL = 1;
  124. static const int BLEND_MODE_LIGHTEN = 2;
  125. static const int BLEND_MODE_COLOR = 3;
  126. static const int BLEND_MODE_PREMULTIPLIED = 4;
  127. static const int BLEND_MODE_MULTIPLY = 5;
  128. static const int DEPTH_FUNCTION_GREATER = 0;
  129. static const int DEPTH_FUNCTION_LEQUAL = 1;
  130. protected:
  131. Number backingResolutionScaleX;
  132. Number backingResolutionScaleY;
  133. Number anisotropy;
  134. int cpuBufferIndex;
  135. int gpuBufferIndex;
  136. RenderThread *renderThread;
  137. };
  138. }