gl_rasterizer.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <vector>
  6. #include "common/common_types.h"
  7. #include "video_core/hwrasterizer_base.h"
  8. #include "video_core/shader/shader_interpreter.h"
  9. #include "gl_state.h"
  10. #include "gl_rasterizer_cache.h"
  11. class RasterizerOpenGL : public HWRasterizer {
  12. public:
  13. RasterizerOpenGL();
  14. ~RasterizerOpenGL() override;
  15. /// Initialize API-specific GPU objects
  16. void InitObjects() override;
  17. /// Reset the rasterizer, such as flushing all caches and updating all state
  18. void Reset() override;
  19. /// Queues the primitive formed by the given vertices for rendering
  20. void AddTriangle(const Pica::Shader::OutputVertex& v0,
  21. const Pica::Shader::OutputVertex& v1,
  22. const Pica::Shader::OutputVertex& v2) override;
  23. /// Draw the current batch of triangles
  24. void DrawTriangles() override;
  25. /// Commit the rasterizer's framebuffer contents immediately to the current 3DS memory framebuffer
  26. void CommitFramebuffer() override;
  27. /// Notify rasterizer that the specified PICA register has been changed
  28. void NotifyPicaRegisterChanged(u32 id) override;
  29. /// Notify rasterizer that the specified 3DS memory region will be read from after this notification
  30. void NotifyPreRead(PAddr addr, u32 size) override;
  31. /// Notify rasterizer that a 3DS memory region has been changed
  32. void NotifyFlush(PAddr addr, u32 size) override;
  33. private:
  34. /// Structure used for managing texture environment states
  35. struct TEVConfigUniforms {
  36. GLuint enabled;
  37. GLuint color_sources;
  38. GLuint alpha_sources;
  39. GLuint color_modifiers;
  40. GLuint alpha_modifiers;
  41. GLuint color_alpha_op;
  42. GLuint color_alpha_multiplier;
  43. GLuint const_color;
  44. GLuint updates_combiner_buffer_color_alpha;
  45. };
  46. /// Structure used for storing information about color textures
  47. struct TextureInfo {
  48. OGLTexture texture;
  49. GLsizei width;
  50. GLsizei height;
  51. Pica::Regs::ColorFormat format;
  52. GLenum gl_format;
  53. GLenum gl_type;
  54. };
  55. /// Structure used for storing information about depth textures
  56. struct DepthTextureInfo {
  57. OGLTexture texture;
  58. GLsizei width;
  59. GLsizei height;
  60. Pica::Regs::DepthFormat format;
  61. GLenum gl_format;
  62. GLenum gl_type;
  63. };
  64. /// Structure that the hardware rendered vertices are composed of
  65. struct HardwareVertex {
  66. HardwareVertex(const Pica::Shader::OutputVertex& v) {
  67. position[0] = v.pos.x.ToFloat32();
  68. position[1] = v.pos.y.ToFloat32();
  69. position[2] = v.pos.z.ToFloat32();
  70. position[3] = v.pos.w.ToFloat32();
  71. color[0] = v.color.x.ToFloat32();
  72. color[1] = v.color.y.ToFloat32();
  73. color[2] = v.color.z.ToFloat32();
  74. color[3] = v.color.w.ToFloat32();
  75. tex_coord0[0] = v.tc0.x.ToFloat32();
  76. tex_coord0[1] = v.tc0.y.ToFloat32();
  77. tex_coord1[0] = v.tc1.x.ToFloat32();
  78. tex_coord1[1] = v.tc1.y.ToFloat32();
  79. tex_coord2[0] = v.tc2.x.ToFloat32();
  80. tex_coord2[1] = v.tc2.y.ToFloat32();
  81. }
  82. GLfloat position[4];
  83. GLfloat color[4];
  84. GLfloat tex_coord0[2];
  85. GLfloat tex_coord1[2];
  86. GLfloat tex_coord2[2];
  87. };
  88. /// Reconfigure the OpenGL color texture to use the given format and dimensions
  89. void ReconfigureColorTexture(TextureInfo& texture, Pica::Regs::ColorFormat format, u32 width, u32 height);
  90. /// Reconfigure the OpenGL depth texture to use the given format and dimensions
  91. void ReconfigureDepthTexture(DepthTextureInfo& texture, Pica::Regs::DepthFormat format, u32 width, u32 height);
  92. /// Syncs the state and contents of the OpenGL framebuffer to match the current PICA framebuffer
  93. void SyncFramebuffer();
  94. /// Syncs the cull mode to match the PICA register
  95. void SyncCullMode();
  96. /// Syncs the blend enabled status to match the PICA register
  97. void SyncBlendEnabled();
  98. /// Syncs the blend functions to match the PICA register
  99. void SyncBlendFuncs();
  100. /// Syncs the blend color to match the PICA register
  101. void SyncBlendColor();
  102. /// Syncs the alpha test states to match the PICA register
  103. void SyncAlphaTest();
  104. /// Syncs the logic op states to match the PICA register
  105. void SyncLogicOp();
  106. /// Syncs the stencil test states to match the PICA register
  107. void SyncStencilTest();
  108. /// Syncs the depth test states to match the PICA register
  109. void SyncDepthTest();
  110. /// Syncs the specified TEV stage's color and alpha sources to match the PICA register
  111. void SyncTevSources(unsigned stage_index, const Pica::Regs::TevStageConfig& config);
  112. /// Syncs the specified TEV stage's color and alpha modifiers to match the PICA register
  113. void SyncTevModifiers(unsigned stage_index, const Pica::Regs::TevStageConfig& config);
  114. /// Syncs the specified TEV stage's color and alpha combiner operations to match the PICA register
  115. void SyncTevOps(unsigned stage_index, const Pica::Regs::TevStageConfig& config);
  116. /// Syncs the specified TEV stage's constant color to match the PICA register
  117. void SyncTevColor(unsigned stage_index, const Pica::Regs::TevStageConfig& config);
  118. /// Syncs the specified TEV stage's color and alpha multipliers to match the PICA register
  119. void SyncTevMultipliers(unsigned stage_index, const Pica::Regs::TevStageConfig& config);
  120. /// Syncs the TEV combiner color buffer to match the PICA register
  121. void SyncCombinerColor();
  122. /// Syncs the TEV combiner write flags to match the PICA register
  123. void SyncCombinerWriteFlags();
  124. /// Syncs the remaining OpenGL drawing state to match the current PICA state
  125. void SyncDrawState();
  126. /// Copies the 3DS color framebuffer into the OpenGL color framebuffer texture
  127. void ReloadColorBuffer();
  128. /// Copies the 3DS depth framebuffer into the OpenGL depth framebuffer texture
  129. void ReloadDepthBuffer();
  130. /**
  131. * Save the current OpenGL color framebuffer to the current PICA framebuffer in 3DS memory
  132. * Loads the OpenGL framebuffer textures into temporary buffers
  133. * Then copies into the 3DS framebuffer using proper Morton order
  134. */
  135. void CommitColorBuffer();
  136. /**
  137. * Save the current OpenGL depth framebuffer to the current PICA framebuffer in 3DS memory
  138. * Loads the OpenGL framebuffer textures into temporary buffers
  139. * Then copies into the 3DS framebuffer using proper Morton order
  140. */
  141. void CommitDepthBuffer();
  142. RasterizerCacheOpenGL res_cache;
  143. std::vector<HardwareVertex> vertex_batch;
  144. OpenGLState state;
  145. PAddr last_fb_color_addr;
  146. PAddr last_fb_depth_addr;
  147. // Hardware rasterizer
  148. TextureInfo fb_color_texture;
  149. DepthTextureInfo fb_depth_texture;
  150. OGLShader shader;
  151. OGLVertexArray vertex_array;
  152. OGLBuffer vertex_buffer;
  153. OGLFramebuffer framebuffer;
  154. // Hardware vertex shader
  155. GLuint attrib_position;
  156. GLuint attrib_color;
  157. GLuint attrib_texcoords;
  158. // Hardware fragment shader
  159. GLuint uniform_alphatest_enabled;
  160. GLuint uniform_alphatest_func;
  161. GLuint uniform_alphatest_ref;
  162. GLuint uniform_tex;
  163. GLuint uniform_tev_combiner_buffer_color;
  164. TEVConfigUniforms uniform_tev_cfgs[6];
  165. };