gl_rasterizer.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <string>
  6. #include <tuple>
  7. #include <utility>
  8. #include <glad/glad.h>
  9. #include "common/alignment.h"
  10. #include "common/assert.h"
  11. #include "common/logging/log.h"
  12. #include "common/math_util.h"
  13. #include "common/microprofile.h"
  14. #include "common/scope_exit.h"
  15. #include "common/vector_math.h"
  16. #include "core/core.h"
  17. #include "core/hle/kernel/process.h"
  18. #include "core/settings.h"
  19. #include "video_core/engines/maxwell_3d.h"
  20. #include "video_core/renderer_opengl/gl_rasterizer.h"
  21. #include "video_core/renderer_opengl/gl_shader_gen.h"
  22. #include "video_core/renderer_opengl/maxwell_to_gl.h"
  23. #include "video_core/renderer_opengl/renderer_opengl.h"
  24. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  25. using PixelFormat = SurfaceParams::PixelFormat;
  26. using SurfaceType = SurfaceParams::SurfaceType;
  27. MICROPROFILE_DEFINE(OpenGL_VAO, "OpenGL", "Vertex Array Setup", MP_RGB(128, 128, 192));
  28. MICROPROFILE_DEFINE(OpenGL_VS, "OpenGL", "Vertex Shader Setup", MP_RGB(128, 128, 192));
  29. MICROPROFILE_DEFINE(OpenGL_FS, "OpenGL", "Fragment Shader Setup", MP_RGB(128, 128, 192));
  30. MICROPROFILE_DEFINE(OpenGL_Drawing, "OpenGL", "Drawing", MP_RGB(128, 128, 192));
  31. MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(100, 100, 255));
  32. MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Mgmt", MP_RGB(100, 255, 100));
  33. enum class UniformBindings : GLuint { Common, VS, FS };
  34. static void SetShaderUniformBlockBinding(GLuint shader, const char* name, UniformBindings binding,
  35. size_t expected_size) {
  36. GLuint ub_index = glGetUniformBlockIndex(shader, name);
  37. if (ub_index != GL_INVALID_INDEX) {
  38. GLint ub_size = 0;
  39. glGetActiveUniformBlockiv(shader, ub_index, GL_UNIFORM_BLOCK_DATA_SIZE, &ub_size);
  40. ASSERT_MSG(ub_size == expected_size,
  41. "Uniform block size did not match! Got %d, expected %zu",
  42. static_cast<int>(ub_size), expected_size);
  43. glUniformBlockBinding(shader, ub_index, static_cast<GLuint>(binding));
  44. }
  45. }
  46. static void SetShaderUniformBlockBindings(GLuint shader) {
  47. SetShaderUniformBlockBinding(shader, "shader_data", UniformBindings::Common,
  48. sizeof(RasterizerOpenGL::UniformData));
  49. SetShaderUniformBlockBinding(shader, "vs_config", UniformBindings::VS,
  50. sizeof(RasterizerOpenGL::VSUniformData));
  51. SetShaderUniformBlockBinding(shader, "fs_config", UniformBindings::FS,
  52. sizeof(RasterizerOpenGL::FSUniformData));
  53. }
  54. RasterizerOpenGL::RasterizerOpenGL() {
  55. shader_dirty = true;
  56. has_ARB_buffer_storage = false;
  57. has_ARB_direct_state_access = false;
  58. has_ARB_separate_shader_objects = false;
  59. has_ARB_vertex_attrib_binding = false;
  60. GLint ext_num;
  61. glGetIntegerv(GL_NUM_EXTENSIONS, &ext_num);
  62. for (GLint i = 0; i < ext_num; i++) {
  63. std::string extension{reinterpret_cast<const char*>(glGetStringi(GL_EXTENSIONS, i))};
  64. if (extension == "GL_ARB_buffer_storage") {
  65. has_ARB_buffer_storage = true;
  66. } else if (extension == "GL_ARB_direct_state_access") {
  67. has_ARB_direct_state_access = true;
  68. } else if (extension == "GL_ARB_separate_shader_objects") {
  69. has_ARB_separate_shader_objects = true;
  70. } else if (extension == "GL_ARB_vertex_attrib_binding") {
  71. has_ARB_vertex_attrib_binding = true;
  72. }
  73. }
  74. // Clipping plane 0 is always enabled for PICA fixed clip plane z <= 0
  75. state.clip_distance[0] = true;
  76. // Generate VBO, VAO and UBO
  77. vertex_buffer = OGLStreamBuffer::MakeBuffer(GLAD_GL_ARB_buffer_storage, GL_ARRAY_BUFFER);
  78. vertex_buffer->Create(VERTEX_BUFFER_SIZE, VERTEX_BUFFER_SIZE / 2);
  79. sw_vao.Create();
  80. uniform_buffer.Create();
  81. state.draw.vertex_array = sw_vao.handle;
  82. state.draw.vertex_buffer = vertex_buffer->GetHandle();
  83. state.draw.uniform_buffer = uniform_buffer.handle;
  84. state.Apply();
  85. glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformData), nullptr, GL_STATIC_DRAW);
  86. glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer.handle);
  87. uniform_block_data.dirty = true;
  88. // Create render framebuffer
  89. framebuffer.Create();
  90. if (has_ARB_separate_shader_objects) {
  91. hw_vao.Create();
  92. hw_vao_enabled_attributes.fill(false);
  93. stream_buffer = OGLStreamBuffer::MakeBuffer(has_ARB_buffer_storage, GL_ARRAY_BUFFER);
  94. stream_buffer->Create(STREAM_BUFFER_SIZE, STREAM_BUFFER_SIZE / 2);
  95. state.draw.vertex_buffer = stream_buffer->GetHandle();
  96. pipeline.Create();
  97. state.draw.program_pipeline = pipeline.handle;
  98. state.draw.shader_program = 0;
  99. state.draw.vertex_array = hw_vao.handle;
  100. state.Apply();
  101. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, stream_buffer->GetHandle());
  102. vs_uniform_buffer.Create();
  103. glBindBuffer(GL_UNIFORM_BUFFER, vs_uniform_buffer.handle);
  104. glBufferData(GL_UNIFORM_BUFFER, sizeof(VSUniformData), nullptr, GL_STREAM_COPY);
  105. glBindBufferBase(GL_UNIFORM_BUFFER, 1, vs_uniform_buffer.handle);
  106. } else {
  107. UNREACHABLE();
  108. }
  109. accelerate_draw = AccelDraw::Disabled;
  110. glEnable(GL_BLEND);
  111. LOG_CRITICAL(Render_OpenGL, "Sync fixed function OpenGL state here!");
  112. }
  113. RasterizerOpenGL::~RasterizerOpenGL() {
  114. if (stream_buffer != nullptr) {
  115. state.draw.vertex_buffer = stream_buffer->GetHandle();
  116. state.Apply();
  117. stream_buffer->Release();
  118. }
  119. }
  120. void RasterizerOpenGL::AnalyzeVertexArray(bool is_indexed) {
  121. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  122. if (is_indexed) {
  123. UNREACHABLE();
  124. }
  125. // TODO(bunnei): Add support for 1+ vertex arrays
  126. vs_input_size = regs.vertex_buffer.count * regs.vertex_array[0].stride;
  127. }
  128. void RasterizerOpenGL::SetupVertexArray(u8* array_ptr, GLintptr buffer_offset) {
  129. MICROPROFILE_SCOPE(OpenGL_VAO);
  130. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  131. const auto& memory_manager = Core::System().GetInstance().GPU().memory_manager;
  132. state.draw.vertex_array = hw_vao.handle;
  133. state.draw.vertex_buffer = stream_buffer->GetHandle();
  134. state.Apply();
  135. // TODO(bunnei): Add support for 1+ vertex arrays
  136. const auto& vertex_array{regs.vertex_array[0]};
  137. ASSERT_MSG(vertex_array.enable, "vertex array 0 is disabled?");
  138. ASSERT_MSG(!vertex_array.divisor, "vertex array 0 divisor is unimplemented!");
  139. for (unsigned index = 1; index < Maxwell::NumVertexArrays; ++index) {
  140. ASSERT_MSG(!regs.vertex_array[index].enable, "vertex array %d is unimplemented!", index);
  141. }
  142. // Use the vertex array as-is, assumes that the data is formatted correctly for OpenGL.
  143. // Enables the first 16 vertex attributes always, as we don't know which ones are actually used
  144. // until shader time. Note, Tegra technically supports 32, but we're cappinig this to 16 for now
  145. // to avoid OpenGL errors.
  146. for (unsigned index = 0; index < 16; ++index) {
  147. auto& attrib = regs.vertex_attrib_format[index];
  148. glVertexAttribPointer(index, attrib.ComponentCount(), MaxwellToGL::VertexType(attrib),
  149. attrib.IsNormalized() ? GL_TRUE : GL_FALSE, vertex_array.stride,
  150. reinterpret_cast<GLvoid*>(buffer_offset + attrib.offset));
  151. glEnableVertexAttribArray(index);
  152. hw_vao_enabled_attributes[index] = true;
  153. }
  154. // Copy vertex array data
  155. const u32 data_size{vertex_array.stride * regs.vertex_buffer.count};
  156. const VAddr data_addr{memory_manager->PhysicalToVirtualAddress(vertex_array.StartAddress())};
  157. res_cache.FlushRegion(data_addr, data_size, nullptr);
  158. Memory::ReadBlock(data_addr, array_ptr, data_size);
  159. array_ptr += data_size;
  160. buffer_offset += data_size;
  161. }
  162. void RasterizerOpenGL::SetupVertexShader(VSUniformData* ub_ptr, GLintptr buffer_offset) {
  163. MICROPROFILE_SCOPE(OpenGL_VS);
  164. LOG_CRITICAL(Render_OpenGL, "Emulated shaders are not supported! Using a passthrough shader.");
  165. glUseProgramStages(pipeline.handle, GL_VERTEX_SHADER_BIT, current_shader->shader.handle);
  166. }
  167. void RasterizerOpenGL::SetupFragmentShader(FSUniformData* ub_ptr, GLintptr buffer_offset) {
  168. MICROPROFILE_SCOPE(OpenGL_FS);
  169. UNREACHABLE();
  170. }
  171. bool RasterizerOpenGL::AccelerateDrawBatch(bool is_indexed) {
  172. if (!has_ARB_separate_shader_objects) {
  173. UNREACHABLE();
  174. return false;
  175. }
  176. accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays;
  177. DrawArrays();
  178. return true;
  179. }
  180. void RasterizerOpenGL::DrawArrays() {
  181. if (accelerate_draw == AccelDraw::Disabled)
  182. return;
  183. MICROPROFILE_SCOPE(OpenGL_Drawing);
  184. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  185. // TODO(bunnei): Implement these
  186. const bool has_stencil = false;
  187. const bool using_color_fb = true;
  188. const bool using_depth_fb = false;
  189. MathUtil::Rectangle<s32> viewport_rect_unscaled{
  190. static_cast<s32>(regs.viewport[0].x), // left
  191. static_cast<s32>(regs.viewport[0].y + regs.viewport[0].height), // top
  192. static_cast<s32>(regs.viewport[0].x + regs.viewport[0].width), // right
  193. static_cast<s32>(regs.viewport[0].y) // bottom
  194. };
  195. const bool write_color_fb =
  196. state.color_mask.red_enabled == GL_TRUE || state.color_mask.green_enabled == GL_TRUE ||
  197. state.color_mask.blue_enabled == GL_TRUE || state.color_mask.alpha_enabled == GL_TRUE;
  198. const bool write_depth_fb =
  199. (state.depth.test_enabled && state.depth.write_mask == GL_TRUE) ||
  200. (has_stencil && state.stencil.test_enabled && state.stencil.write_mask != 0);
  201. Surface color_surface;
  202. Surface depth_surface;
  203. MathUtil::Rectangle<u32> surfaces_rect;
  204. std::tie(color_surface, depth_surface, surfaces_rect) =
  205. res_cache.GetFramebufferSurfaces(using_color_fb, using_depth_fb, viewport_rect_unscaled);
  206. const u16 res_scale = color_surface != nullptr
  207. ? color_surface->res_scale
  208. : (depth_surface == nullptr ? 1u : depth_surface->res_scale);
  209. MathUtil::Rectangle<u32> draw_rect{
  210. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) +
  211. viewport_rect_unscaled.left * res_scale,
  212. surfaces_rect.left, surfaces_rect.right)), // Left
  213. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
  214. viewport_rect_unscaled.top * res_scale,
  215. surfaces_rect.bottom, surfaces_rect.top)), // Top
  216. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) +
  217. viewport_rect_unscaled.right * res_scale,
  218. surfaces_rect.left, surfaces_rect.right)), // Right
  219. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
  220. viewport_rect_unscaled.bottom * res_scale,
  221. surfaces_rect.bottom, surfaces_rect.top))}; // Bottom
  222. // Bind the framebuffer surfaces
  223. state.draw.draw_framebuffer = framebuffer.handle;
  224. state.Apply();
  225. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  226. color_surface != nullptr ? color_surface->texture.handle : 0, 0);
  227. if (depth_surface != nullptr) {
  228. if (has_stencil) {
  229. // attach both depth and stencil
  230. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
  231. depth_surface->texture.handle, 0);
  232. } else {
  233. // attach depth
  234. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
  235. depth_surface->texture.handle, 0);
  236. // clear stencil attachment
  237. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  238. }
  239. } else {
  240. // clear both depth and stencil attachment
  241. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0,
  242. 0);
  243. }
  244. // Sync the viewport
  245. state.viewport.x =
  246. static_cast<GLint>(surfaces_rect.left) + viewport_rect_unscaled.left * res_scale;
  247. state.viewport.y =
  248. static_cast<GLint>(surfaces_rect.bottom) + viewport_rect_unscaled.bottom * res_scale;
  249. state.viewport.width = static_cast<GLsizei>(viewport_rect_unscaled.GetWidth() * res_scale);
  250. state.viewport.height = static_cast<GLsizei>(viewport_rect_unscaled.GetHeight() * res_scale);
  251. // TODO(bunnei): Sync framebuffer_scale uniform here
  252. // TODO(bunnei): Sync scissorbox uniform(s) here
  253. // TODO(bunnei): Sync and bind the texture surfaces
  254. // Sync and bind the shader
  255. if (shader_dirty) {
  256. SetShader();
  257. shader_dirty = false;
  258. }
  259. // Sync the uniform data
  260. if (uniform_block_data.dirty) {
  261. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(UniformData), &uniform_block_data.data);
  262. uniform_block_data.dirty = false;
  263. }
  264. // Viewport can have negative offsets or larger dimensions than our framebuffer sub-rect. Enable
  265. // scissor test to prevent drawing outside of the framebuffer region
  266. state.scissor.enabled = true;
  267. state.scissor.x = draw_rect.left;
  268. state.scissor.y = draw_rect.bottom;
  269. state.scissor.width = draw_rect.GetWidth();
  270. state.scissor.height = draw_rect.GetHeight();
  271. state.Apply();
  272. // Draw the vertex batch
  273. GLenum primitive_mode;
  274. switch (regs.draw.topology) {
  275. case Maxwell::PrimitiveTopology::TriangleStrip:
  276. primitive_mode = GL_TRIANGLE_STRIP;
  277. break;
  278. default:
  279. UNREACHABLE();
  280. }
  281. const bool is_indexed = accelerate_draw == AccelDraw::Indexed;
  282. AnalyzeVertexArray(is_indexed);
  283. state.draw.vertex_buffer = stream_buffer->GetHandle();
  284. state.Apply();
  285. size_t buffer_size = static_cast<size_t>(vs_input_size);
  286. if (is_indexed) {
  287. UNREACHABLE();
  288. }
  289. buffer_size += sizeof(VSUniformData);
  290. size_t ptr_pos = 0;
  291. u8* buffer_ptr;
  292. GLintptr buffer_offset;
  293. std::tie(buffer_ptr, buffer_offset) =
  294. stream_buffer->Map(static_cast<GLsizeiptr>(buffer_size), 4);
  295. SetupVertexArray(buffer_ptr, buffer_offset);
  296. ptr_pos += vs_input_size;
  297. GLintptr index_buffer_offset = 0;
  298. if (is_indexed) {
  299. UNREACHABLE();
  300. }
  301. SetupVertexShader(reinterpret_cast<VSUniformData*>(&buffer_ptr[ptr_pos]),
  302. buffer_offset + static_cast<GLintptr>(ptr_pos));
  303. const GLintptr vs_ubo_offset = buffer_offset + static_cast<GLintptr>(ptr_pos);
  304. ptr_pos += sizeof(VSUniformData);
  305. stream_buffer->Unmap();
  306. const auto copy_buffer = [&](GLuint handle, GLintptr offset, GLsizeiptr size) {
  307. if (has_ARB_direct_state_access) {
  308. glCopyNamedBufferSubData(stream_buffer->GetHandle(), handle, offset, 0, size);
  309. } else {
  310. glBindBuffer(GL_COPY_WRITE_BUFFER, handle);
  311. glCopyBufferSubData(GL_ARRAY_BUFFER, GL_COPY_WRITE_BUFFER, offset, 0, size);
  312. }
  313. };
  314. copy_buffer(vs_uniform_buffer.handle, vs_ubo_offset, sizeof(VSUniformData));
  315. glUseProgramStages(pipeline.handle, GL_FRAGMENT_SHADER_BIT, current_shader->shader.handle);
  316. if (is_indexed) {
  317. UNREACHABLE();
  318. } else {
  319. glDrawArrays(primitive_mode, 0, regs.vertex_buffer.count);
  320. }
  321. // Disable scissor test
  322. state.scissor.enabled = false;
  323. accelerate_draw = AccelDraw::Disabled;
  324. // Unbind textures for potential future use as framebuffer attachments
  325. for (auto& texture_unit : state.texture_units) {
  326. texture_unit.texture_2d = 0;
  327. }
  328. state.Apply();
  329. // Mark framebuffer surfaces as dirty
  330. MathUtil::Rectangle<u32> draw_rect_unscaled{
  331. draw_rect.left / res_scale, draw_rect.top / res_scale, draw_rect.right / res_scale,
  332. draw_rect.bottom / res_scale};
  333. if (color_surface != nullptr && write_color_fb) {
  334. auto interval = color_surface->GetSubRectInterval(draw_rect_unscaled);
  335. res_cache.InvalidateRegion(boost::icl::first(interval), boost::icl::length(interval),
  336. color_surface);
  337. }
  338. if (depth_surface != nullptr && write_depth_fb) {
  339. auto interval = depth_surface->GetSubRectInterval(draw_rect_unscaled);
  340. res_cache.InvalidateRegion(boost::icl::first(interval), boost::icl::length(interval),
  341. depth_surface);
  342. }
  343. }
  344. void RasterizerOpenGL::NotifyMaxwellRegisterChanged(u32 id) {}
  345. void RasterizerOpenGL::FlushAll() {
  346. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  347. res_cache.FlushAll();
  348. }
  349. void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
  350. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  351. res_cache.FlushRegion(addr, size);
  352. }
  353. void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
  354. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  355. res_cache.InvalidateRegion(addr, size, nullptr);
  356. }
  357. void RasterizerOpenGL::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  358. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  359. res_cache.FlushRegion(addr, size);
  360. res_cache.InvalidateRegion(addr, size, nullptr);
  361. }
  362. bool RasterizerOpenGL::AccelerateDisplayTransfer(const void* config) {
  363. MICROPROFILE_SCOPE(OpenGL_Blits);
  364. UNREACHABLE();
  365. return true;
  366. }
  367. bool RasterizerOpenGL::AccelerateTextureCopy(const void* config) {
  368. UNREACHABLE();
  369. return true;
  370. }
  371. bool RasterizerOpenGL::AccelerateFill(const void* config) {
  372. UNREACHABLE();
  373. return true;
  374. }
  375. bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& framebuffer,
  376. VAddr framebuffer_addr, u32 pixel_stride,
  377. ScreenInfo& screen_info) {
  378. if (framebuffer_addr == 0) {
  379. return false;
  380. }
  381. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  382. SurfaceParams src_params;
  383. src_params.addr = framebuffer_addr;
  384. src_params.width = std::min(framebuffer.width, pixel_stride);
  385. src_params.height = framebuffer.height;
  386. src_params.stride = pixel_stride;
  387. src_params.is_tiled = false;
  388. src_params.pixel_format =
  389. SurfaceParams::PixelFormatFromGPUPixelFormat(framebuffer.pixel_format);
  390. src_params.UpdateParams();
  391. MathUtil::Rectangle<u32> src_rect;
  392. Surface src_surface;
  393. std::tie(src_surface, src_rect) =
  394. res_cache.GetSurfaceSubRect(src_params, ScaleMatch::Ignore, true);
  395. if (src_surface == nullptr) {
  396. return false;
  397. }
  398. u32 scaled_width = src_surface->GetScaledWidth();
  399. u32 scaled_height = src_surface->GetScaledHeight();
  400. screen_info.display_texcoords = MathUtil::Rectangle<float>(
  401. (float)src_rect.bottom / (float)scaled_height, (float)src_rect.left / (float)scaled_width,
  402. (float)src_rect.top / (float)scaled_height, (float)src_rect.right / (float)scaled_width);
  403. screen_info.display_texture = src_surface->texture.handle;
  404. return true;
  405. }
  406. void RasterizerOpenGL::SetShader() {
  407. // TODO(bunnei): The below sets up a static test shader for passing untransformed vertices to
  408. // OpenGL for rendering. This should be removed/replaced when we start emulating Maxwell
  409. // shaders.
  410. static constexpr char vertex_shader[] = R"(
  411. #version 150 core
  412. in vec2 vert_position;
  413. in vec2 vert_tex_coord;
  414. out vec2 frag_tex_coord;
  415. void main() {
  416. // Multiply input position by the rotscale part of the matrix and then manually translate by
  417. // the last column. This is equivalent to using a full 3x3 matrix and expanding the vector
  418. // to `vec3(vert_position.xy, 1.0)`
  419. gl_Position = vec4(mat2(mat3x2(0.0015625f, 0.0, 0.0, -0.0027778, -1.0, 1.0)) * vert_position + mat3x2(0.0015625f, 0.0, 0.0, -0.0027778, -1.0, 1.0)[2], 0.0, 1.0);
  420. frag_tex_coord = vert_tex_coord;
  421. }
  422. )";
  423. static constexpr char fragment_shader[] = R"(
  424. #version 150 core
  425. in vec2 frag_tex_coord;
  426. out vec4 color;
  427. uniform sampler2D color_texture;
  428. void main() {
  429. color = vec4(1.0, 0.0, 1.0, 0.0);
  430. }
  431. )";
  432. if (current_shader) {
  433. return;
  434. }
  435. LOG_CRITICAL(Render_OpenGL, "Emulated shaders are not supported! Using a passthrough shader.");
  436. current_shader = &test_shader;
  437. if (has_ARB_separate_shader_objects) {
  438. test_shader.shader.Create(vertex_shader, nullptr, fragment_shader, {}, true);
  439. glActiveShaderProgram(pipeline.handle, test_shader.shader.handle);
  440. } else {
  441. UNREACHABLE();
  442. }
  443. state.draw.shader_program = test_shader.shader.handle;
  444. state.Apply();
  445. if (has_ARB_separate_shader_objects) {
  446. state.draw.shader_program = 0;
  447. state.Apply();
  448. }
  449. }
  450. void RasterizerOpenGL::SyncClipEnabled() {
  451. UNREACHABLE();
  452. }
  453. void RasterizerOpenGL::SyncClipCoef() {
  454. UNREACHABLE();
  455. }
  456. void RasterizerOpenGL::SyncCullMode() {
  457. UNREACHABLE();
  458. }
  459. void RasterizerOpenGL::SyncDepthScale() {
  460. UNREACHABLE();
  461. }
  462. void RasterizerOpenGL::SyncDepthOffset() {
  463. UNREACHABLE();
  464. }
  465. void RasterizerOpenGL::SyncBlendEnabled() {
  466. UNREACHABLE();
  467. }
  468. void RasterizerOpenGL::SyncBlendFuncs() {
  469. UNREACHABLE();
  470. }
  471. void RasterizerOpenGL::SyncBlendColor() {
  472. UNREACHABLE();
  473. }