gl_rasterizer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. RasterizerOpenGL::RasterizerOpenGL() {
  34. has_ARB_buffer_storage = false;
  35. has_ARB_direct_state_access = false;
  36. has_ARB_separate_shader_objects = false;
  37. has_ARB_vertex_attrib_binding = false;
  38. // Create sampler objects
  39. for (size_t i = 0; i < texture_samplers.size(); ++i) {
  40. texture_samplers[i].Create();
  41. state.texture_units[i].sampler = texture_samplers[i].sampler.handle;
  42. }
  43. // Create SSBOs
  44. for (size_t stage = 0; stage < ssbos.size(); ++stage) {
  45. for (size_t buffer = 0; buffer < ssbos[stage].size(); ++buffer) {
  46. ssbos[stage][buffer].Create();
  47. state.draw.const_buffers[stage][buffer].ssbo = ssbos[stage][buffer].handle;
  48. }
  49. }
  50. GLint ext_num;
  51. glGetIntegerv(GL_NUM_EXTENSIONS, &ext_num);
  52. for (GLint i = 0; i < ext_num; i++) {
  53. std::string extension{reinterpret_cast<const char*>(glGetStringi(GL_EXTENSIONS, i))};
  54. if (extension == "GL_ARB_buffer_storage") {
  55. has_ARB_buffer_storage = true;
  56. } else if (extension == "GL_ARB_direct_state_access") {
  57. has_ARB_direct_state_access = true;
  58. } else if (extension == "GL_ARB_separate_shader_objects") {
  59. has_ARB_separate_shader_objects = true;
  60. } else if (extension == "GL_ARB_vertex_attrib_binding") {
  61. has_ARB_vertex_attrib_binding = true;
  62. }
  63. }
  64. ASSERT_MSG(has_ARB_separate_shader_objects, "has_ARB_separate_shader_objects is unsupported");
  65. // Clipping plane 0 is always enabled for PICA fixed clip plane z <= 0
  66. state.clip_distance[0] = true;
  67. // Generate VBO, VAO and UBO
  68. vertex_buffer = OGLStreamBuffer::MakeBuffer(GLAD_GL_ARB_buffer_storage, GL_ARRAY_BUFFER);
  69. vertex_buffer->Create(VERTEX_BUFFER_SIZE, VERTEX_BUFFER_SIZE / 2);
  70. sw_vao.Create();
  71. uniform_buffer.Create();
  72. state.draw.vertex_array = sw_vao.handle;
  73. state.draw.vertex_buffer = vertex_buffer->GetHandle();
  74. state.draw.uniform_buffer = uniform_buffer.handle;
  75. state.Apply();
  76. // Create render framebuffer
  77. framebuffer.Create();
  78. hw_vao.Create();
  79. hw_vao_enabled_attributes.fill(false);
  80. stream_buffer = OGLStreamBuffer::MakeBuffer(has_ARB_buffer_storage, GL_ARRAY_BUFFER);
  81. stream_buffer->Create(STREAM_BUFFER_SIZE, STREAM_BUFFER_SIZE / 2);
  82. state.draw.vertex_buffer = stream_buffer->GetHandle();
  83. shader_program_manager = std::make_unique<GLShader::ProgramManager>();
  84. state.draw.shader_program = 0;
  85. state.draw.vertex_array = hw_vao.handle;
  86. state.Apply();
  87. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, stream_buffer->GetHandle());
  88. for (unsigned index = 0; index < uniform_buffers.size(); ++index) {
  89. auto& buffer = uniform_buffers[index];
  90. buffer.Create();
  91. glBindBuffer(GL_UNIFORM_BUFFER, buffer.handle);
  92. glBufferData(GL_UNIFORM_BUFFER, sizeof(GLShader::MaxwellUniformData), nullptr,
  93. GL_STREAM_COPY);
  94. glBindBufferBase(GL_UNIFORM_BUFFER, index, buffer.handle);
  95. }
  96. accelerate_draw = AccelDraw::Disabled;
  97. glEnable(GL_BLEND);
  98. LOG_CRITICAL(Render_OpenGL, "Sync fixed function OpenGL state here!");
  99. }
  100. RasterizerOpenGL::~RasterizerOpenGL() {
  101. if (stream_buffer != nullptr) {
  102. state.draw.vertex_buffer = stream_buffer->GetHandle();
  103. state.Apply();
  104. stream_buffer->Release();
  105. }
  106. }
  107. void RasterizerOpenGL::AnalyzeVertexArray(bool is_indexed) {
  108. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  109. if (is_indexed) {
  110. UNREACHABLE();
  111. }
  112. // TODO(bunnei): Add support for 1+ vertex arrays
  113. vs_input_size = regs.vertex_buffer.count * regs.vertex_array[0].stride;
  114. }
  115. void RasterizerOpenGL::SetupVertexArray(u8* array_ptr, GLintptr buffer_offset) {
  116. MICROPROFILE_SCOPE(OpenGL_VAO);
  117. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  118. const auto& memory_manager = Core::System().GetInstance().GPU().memory_manager;
  119. state.draw.vertex_array = hw_vao.handle;
  120. state.draw.vertex_buffer = stream_buffer->GetHandle();
  121. state.Apply();
  122. // TODO(bunnei): Add support for 1+ vertex arrays
  123. const auto& vertex_array{regs.vertex_array[0]};
  124. ASSERT_MSG(vertex_array.enable, "vertex array 0 is disabled?");
  125. ASSERT_MSG(!vertex_array.divisor, "vertex array 0 divisor is unimplemented!");
  126. for (unsigned index = 1; index < Maxwell::NumVertexArrays; ++index) {
  127. ASSERT_MSG(!regs.vertex_array[index].enable, "vertex array %d is unimplemented!", index);
  128. }
  129. // Use the vertex array as-is, assumes that the data is formatted correctly for OpenGL.
  130. // Enables the first 16 vertex attributes always, as we don't know which ones are actually used
  131. // until shader time. Note, Tegra technically supports 32, but we're cappinig this to 16 for now
  132. // to avoid OpenGL errors.
  133. for (unsigned index = 0; index < 16; ++index) {
  134. auto& attrib = regs.vertex_attrib_format[index];
  135. glVertexAttribPointer(index, attrib.ComponentCount(), MaxwellToGL::VertexType(attrib),
  136. attrib.IsNormalized() ? GL_TRUE : GL_FALSE, vertex_array.stride,
  137. reinterpret_cast<GLvoid*>(buffer_offset + attrib.offset));
  138. glEnableVertexAttribArray(index);
  139. hw_vao_enabled_attributes[index] = true;
  140. }
  141. // Copy vertex array data
  142. const u32 data_size{vertex_array.stride * regs.vertex_buffer.count};
  143. const VAddr data_addr{memory_manager->PhysicalToVirtualAddress(vertex_array.StartAddress())};
  144. res_cache.FlushRegion(data_addr, data_size, nullptr);
  145. Memory::ReadBlock(data_addr, array_ptr, data_size);
  146. array_ptr += data_size;
  147. buffer_offset += data_size;
  148. }
  149. void RasterizerOpenGL::SetupShaders(u8* buffer_ptr, GLintptr buffer_offset, size_t ptr_pos) {
  150. // Helper function for uploading uniform data
  151. const auto copy_buffer = [&](GLuint handle, GLintptr offset, GLsizeiptr size) {
  152. if (has_ARB_direct_state_access) {
  153. glCopyNamedBufferSubData(stream_buffer->GetHandle(), handle, offset, 0, size);
  154. } else {
  155. glBindBuffer(GL_COPY_WRITE_BUFFER, handle);
  156. glCopyBufferSubData(GL_ARRAY_BUFFER, GL_COPY_WRITE_BUFFER, offset, 0, size);
  157. }
  158. };
  159. auto& gpu = Core::System().GetInstance().GPU().Maxwell3D();
  160. ASSERT_MSG(!gpu.regs.shader_config[0].enable, "VertexA is unsupported!");
  161. for (unsigned index = 1; index < Maxwell::MaxShaderProgram; ++index) {
  162. ptr_pos += sizeof(GLShader::MaxwellUniformData);
  163. auto& shader_config = gpu.regs.shader_config[index];
  164. const Maxwell::ShaderProgram program{static_cast<Maxwell::ShaderProgram>(index)};
  165. const auto& stage = index - 1; // Stage indices are 0 - 5
  166. const bool is_enabled = gpu.IsShaderStageEnabled(static_cast<Maxwell::ShaderStage>(stage));
  167. // Skip stages that are not enabled
  168. if (!is_enabled) {
  169. continue;
  170. }
  171. // Upload uniform data as one UBO per stage
  172. const GLintptr ubo_offset = buffer_offset + static_cast<GLintptr>(ptr_pos);
  173. copy_buffer(uniform_buffers[stage].handle, ubo_offset,
  174. sizeof(GLShader::MaxwellUniformData));
  175. GLShader::MaxwellUniformData* ub_ptr =
  176. reinterpret_cast<GLShader::MaxwellUniformData*>(&buffer_ptr[ptr_pos]);
  177. ub_ptr->SetFromRegs(gpu.state.shader_stages[stage]);
  178. // Fetch program code from memory
  179. GLShader::ProgramCode program_code;
  180. const u64 gpu_address{gpu.regs.code_address.CodeAddress() + shader_config.offset};
  181. const VAddr cpu_address{gpu.memory_manager.PhysicalToVirtualAddress(gpu_address)};
  182. Memory::ReadBlock(cpu_address, program_code.data(), program_code.size() * sizeof(u64));
  183. GLShader::ShaderSetup setup{std::move(program_code)};
  184. GLShader::ShaderEntries shader_resources;
  185. switch (program) {
  186. case Maxwell::ShaderProgram::VertexB: {
  187. GLShader::MaxwellVSConfig vs_config{setup};
  188. shader_resources =
  189. shader_program_manager->UseProgrammableVertexShader(vs_config, setup);
  190. break;
  191. }
  192. case Maxwell::ShaderProgram::Fragment: {
  193. GLShader::MaxwellFSConfig fs_config{setup};
  194. shader_resources =
  195. shader_program_manager->UseProgrammableFragmentShader(fs_config, setup);
  196. break;
  197. }
  198. default:
  199. LOG_CRITICAL(HW_GPU, "Unimplemented shader index=%d, enable=%d, offset=0x%08X", index,
  200. shader_config.enable.Value(), shader_config.offset);
  201. UNREACHABLE();
  202. }
  203. // Configure the const buffers for this shader stage.
  204. SetupConstBuffers(static_cast<Maxwell::ShaderStage>(stage),
  205. shader_resources.const_buffer_entries);
  206. }
  207. shader_program_manager->UseTrivialGeometryShader();
  208. }
  209. bool RasterizerOpenGL::AccelerateDrawBatch(bool is_indexed) {
  210. accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays;
  211. DrawArrays();
  212. return true;
  213. }
  214. void RasterizerOpenGL::DrawArrays() {
  215. if (accelerate_draw == AccelDraw::Disabled)
  216. return;
  217. MICROPROFILE_SCOPE(OpenGL_Drawing);
  218. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  219. // TODO(bunnei): Implement these
  220. const bool has_stencil = false;
  221. const bool using_color_fb = true;
  222. const bool using_depth_fb = false;
  223. const MathUtil::Rectangle<s32> viewport_rect{regs.viewport[0].GetRect()};
  224. const bool write_color_fb =
  225. state.color_mask.red_enabled == GL_TRUE || state.color_mask.green_enabled == GL_TRUE ||
  226. state.color_mask.blue_enabled == GL_TRUE || state.color_mask.alpha_enabled == GL_TRUE;
  227. const bool write_depth_fb =
  228. (state.depth.test_enabled && state.depth.write_mask == GL_TRUE) ||
  229. (has_stencil && state.stencil.test_enabled && state.stencil.write_mask != 0);
  230. Surface color_surface;
  231. Surface depth_surface;
  232. MathUtil::Rectangle<u32> surfaces_rect;
  233. std::tie(color_surface, depth_surface, surfaces_rect) =
  234. res_cache.GetFramebufferSurfaces(using_color_fb, using_depth_fb, viewport_rect);
  235. const u16 res_scale = color_surface != nullptr
  236. ? color_surface->res_scale
  237. : (depth_surface == nullptr ? 1u : depth_surface->res_scale);
  238. MathUtil::Rectangle<u32> draw_rect{
  239. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) +
  240. viewport_rect.left * res_scale,
  241. surfaces_rect.left, surfaces_rect.right)), // Left
  242. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
  243. viewport_rect.top * res_scale,
  244. surfaces_rect.bottom, surfaces_rect.top)), // Top
  245. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) +
  246. viewport_rect.right * res_scale,
  247. surfaces_rect.left, surfaces_rect.right)), // Right
  248. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
  249. viewport_rect.bottom * res_scale,
  250. surfaces_rect.bottom, surfaces_rect.top))}; // Bottom
  251. // Bind the framebuffer surfaces
  252. BindFramebufferSurfaces(color_surface, depth_surface, has_stencil);
  253. // Sync the viewport
  254. SyncViewport(surfaces_rect, res_scale);
  255. // TODO(bunnei): Sync framebuffer_scale uniform here
  256. // TODO(bunnei): Sync scissorbox uniform(s) here
  257. // Sync and bind the texture surfaces
  258. BindTextures();
  259. // Viewport can have negative offsets or larger dimensions than our framebuffer sub-rect. Enable
  260. // scissor test to prevent drawing outside of the framebuffer region
  261. state.scissor.enabled = true;
  262. state.scissor.x = draw_rect.left;
  263. state.scissor.y = draw_rect.bottom;
  264. state.scissor.width = draw_rect.GetWidth();
  265. state.scissor.height = draw_rect.GetHeight();
  266. state.Apply();
  267. // Draw the vertex batch
  268. const bool is_indexed = accelerate_draw == AccelDraw::Indexed;
  269. AnalyzeVertexArray(is_indexed);
  270. state.draw.vertex_buffer = stream_buffer->GetHandle();
  271. state.Apply();
  272. size_t buffer_size = static_cast<size_t>(vs_input_size);
  273. if (is_indexed) {
  274. UNREACHABLE();
  275. }
  276. // Uniform space for the 5 shader stages
  277. buffer_size += sizeof(GLShader::MaxwellUniformData) * Maxwell::MaxShaderStage;
  278. size_t ptr_pos = 0;
  279. u8* buffer_ptr;
  280. GLintptr buffer_offset;
  281. std::tie(buffer_ptr, buffer_offset) =
  282. stream_buffer->Map(static_cast<GLsizeiptr>(buffer_size), 4);
  283. SetupVertexArray(buffer_ptr, buffer_offset);
  284. ptr_pos += vs_input_size;
  285. GLintptr index_buffer_offset = 0;
  286. if (is_indexed) {
  287. UNREACHABLE();
  288. }
  289. SetupShaders(buffer_ptr, buffer_offset, ptr_pos);
  290. stream_buffer->Unmap();
  291. shader_program_manager->ApplyTo(state);
  292. state.Apply();
  293. if (is_indexed) {
  294. UNREACHABLE();
  295. } else {
  296. glDrawArrays(MaxwellToGL::PrimitiveTopology(regs.draw.topology), 0,
  297. regs.vertex_buffer.count);
  298. }
  299. // Disable scissor test
  300. state.scissor.enabled = false;
  301. accelerate_draw = AccelDraw::Disabled;
  302. // Unbind textures for potential future use as framebuffer attachments
  303. for (auto& texture_unit : state.texture_units) {
  304. texture_unit.texture_2d = 0;
  305. }
  306. state.Apply();
  307. // Mark framebuffer surfaces as dirty
  308. MathUtil::Rectangle<u32> draw_rect_unscaled{
  309. draw_rect.left / res_scale, draw_rect.top / res_scale, draw_rect.right / res_scale,
  310. draw_rect.bottom / res_scale};
  311. if (color_surface != nullptr && write_color_fb) {
  312. auto interval = color_surface->GetSubRectInterval(draw_rect_unscaled);
  313. res_cache.InvalidateRegion(boost::icl::first(interval), boost::icl::length(interval),
  314. color_surface);
  315. }
  316. if (depth_surface != nullptr && write_depth_fb) {
  317. auto interval = depth_surface->GetSubRectInterval(draw_rect_unscaled);
  318. res_cache.InvalidateRegion(boost::icl::first(interval), boost::icl::length(interval),
  319. depth_surface);
  320. }
  321. }
  322. void RasterizerOpenGL::BindTextures() {
  323. using Regs = Tegra::Engines::Maxwell3D::Regs;
  324. auto& maxwell3d = Core::System::GetInstance().GPU().Get3DEngine();
  325. // Each Maxwell shader stage can have an arbitrary number of textures, but we're limited to a
  326. // certain number in OpenGL. We try to only use the minimum amount of host textures by not
  327. // keeping a 1:1 relation between guest texture ids and host texture ids, ie, guest texture id 8
  328. // can be host texture id 0 if it's the only texture used in the guest shader program.
  329. u32 host_texture_index = 0;
  330. for (u32 stage = 0; stage < Regs::MaxShaderStage; ++stage) {
  331. ASSERT(host_texture_index < texture_samplers.size());
  332. const auto textures = maxwell3d.GetStageTextures(static_cast<Regs::ShaderStage>(stage));
  333. for (unsigned texture_index = 0; texture_index < textures.size(); ++texture_index) {
  334. const auto& texture = textures[texture_index];
  335. if (texture.enabled) {
  336. texture_samplers[host_texture_index].SyncWithConfig(texture.tsc);
  337. Surface surface = res_cache.GetTextureSurface(texture);
  338. if (surface != nullptr) {
  339. state.texture_units[host_texture_index].texture_2d = surface->texture.handle;
  340. } else {
  341. // Can occur when texture addr is null or its memory is unmapped/invalid
  342. state.texture_units[texture_index].texture_2d = 0;
  343. }
  344. ++host_texture_index;
  345. } else {
  346. state.texture_units[texture_index].texture_2d = 0;
  347. }
  348. }
  349. }
  350. }
  351. void RasterizerOpenGL::NotifyMaxwellRegisterChanged(u32 id) {}
  352. void RasterizerOpenGL::FlushAll() {
  353. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  354. res_cache.FlushAll();
  355. }
  356. void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
  357. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  358. res_cache.FlushRegion(addr, size);
  359. }
  360. void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
  361. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  362. res_cache.InvalidateRegion(addr, size, nullptr);
  363. }
  364. void RasterizerOpenGL::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  365. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  366. res_cache.FlushRegion(addr, size);
  367. res_cache.InvalidateRegion(addr, size, nullptr);
  368. }
  369. bool RasterizerOpenGL::AccelerateDisplayTransfer(const void* config) {
  370. MICROPROFILE_SCOPE(OpenGL_Blits);
  371. UNREACHABLE();
  372. return true;
  373. }
  374. bool RasterizerOpenGL::AccelerateTextureCopy(const void* config) {
  375. UNREACHABLE();
  376. return true;
  377. }
  378. bool RasterizerOpenGL::AccelerateFill(const void* config) {
  379. UNREACHABLE();
  380. return true;
  381. }
  382. bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& framebuffer,
  383. VAddr framebuffer_addr, u32 pixel_stride,
  384. ScreenInfo& screen_info) {
  385. if (framebuffer_addr == 0) {
  386. return false;
  387. }
  388. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  389. SurfaceParams src_params;
  390. src_params.addr = framebuffer_addr;
  391. src_params.width = std::min(framebuffer.width, pixel_stride);
  392. src_params.height = framebuffer.height;
  393. src_params.stride = pixel_stride;
  394. src_params.is_tiled = false;
  395. src_params.pixel_format =
  396. SurfaceParams::PixelFormatFromGPUPixelFormat(framebuffer.pixel_format);
  397. src_params.UpdateParams();
  398. MathUtil::Rectangle<u32> src_rect;
  399. Surface src_surface;
  400. std::tie(src_surface, src_rect) =
  401. res_cache.GetSurfaceSubRect(src_params, ScaleMatch::Ignore, true);
  402. if (src_surface == nullptr) {
  403. return false;
  404. }
  405. u32 scaled_width = src_surface->GetScaledWidth();
  406. u32 scaled_height = src_surface->GetScaledHeight();
  407. screen_info.display_texcoords = MathUtil::Rectangle<float>(
  408. (float)src_rect.bottom / (float)scaled_height, (float)src_rect.left / (float)scaled_width,
  409. (float)src_rect.top / (float)scaled_height, (float)src_rect.right / (float)scaled_width);
  410. screen_info.display_texture = src_surface->texture.handle;
  411. return true;
  412. }
  413. void RasterizerOpenGL::SamplerInfo::Create() {
  414. sampler.Create();
  415. mag_filter = min_filter = Tegra::Texture::TextureFilter::Linear;
  416. wrap_u = wrap_v = Tegra::Texture::WrapMode::Wrap;
  417. border_color_r = border_color_g = border_color_b = border_color_a = 0;
  418. // default is GL_LINEAR_MIPMAP_LINEAR
  419. glSamplerParameteri(sampler.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  420. // Other attributes have correct defaults
  421. }
  422. void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Tegra::Texture::TSCEntry& config) {
  423. GLuint s = sampler.handle;
  424. if (mag_filter != config.mag_filter) {
  425. mag_filter = config.mag_filter;
  426. glSamplerParameteri(s, GL_TEXTURE_MAG_FILTER, MaxwellToGL::TextureFilterMode(mag_filter));
  427. }
  428. if (min_filter != config.min_filter) {
  429. min_filter = config.min_filter;
  430. glSamplerParameteri(s, GL_TEXTURE_MIN_FILTER, MaxwellToGL::TextureFilterMode(min_filter));
  431. }
  432. if (wrap_u != config.wrap_u) {
  433. wrap_u = config.wrap_u;
  434. glSamplerParameteri(s, GL_TEXTURE_WRAP_S, MaxwellToGL::WrapMode(wrap_u));
  435. }
  436. if (wrap_v != config.wrap_v) {
  437. wrap_v = config.wrap_v;
  438. glSamplerParameteri(s, GL_TEXTURE_WRAP_T, MaxwellToGL::WrapMode(wrap_v));
  439. }
  440. if (wrap_u == Tegra::Texture::WrapMode::Border || wrap_v == Tegra::Texture::WrapMode::Border) {
  441. // TODO(Subv): Implement border color
  442. ASSERT(false);
  443. }
  444. }
  445. void RasterizerOpenGL::SetupConstBuffers(Maxwell::ShaderStage stage,
  446. const std::vector<GLShader::ConstBufferEntry>& entries) {
  447. auto& gpu = Core::System::GetInstance().GPU();
  448. auto& maxwell3d = gpu.Get3DEngine();
  449. ASSERT_MSG(maxwell3d.IsShaderStageEnabled(stage),
  450. "Attempted to upload constbuffer of disabled shader stage");
  451. // Reset all buffer draw state for this stage.
  452. for (auto& buffer : state.draw.const_buffers[static_cast<size_t>(stage)]) {
  453. buffer.bindpoint = 0;
  454. buffer.enabled = false;
  455. }
  456. // Upload only the enabled buffers from the 16 constbuffers of each shader stage
  457. auto& shader_stage = maxwell3d.state.shader_stages[static_cast<size_t>(stage)];
  458. for (u32 bindpoint = 0; bindpoint < entries.size(); ++bindpoint) {
  459. const auto& used_buffer = entries[bindpoint];
  460. const auto& buffer = shader_stage.const_buffers[used_buffer.GetIndex()];
  461. auto& buffer_draw_state =
  462. state.draw.const_buffers[static_cast<size_t>(stage)][used_buffer.GetIndex()];
  463. ASSERT_MSG(buffer.enabled, "Attempted to upload disabled constbuffer");
  464. buffer_draw_state.enabled = true;
  465. buffer_draw_state.bindpoint = bindpoint;
  466. VAddr addr = gpu.memory_manager->PhysicalToVirtualAddress(buffer.address);
  467. std::vector<u8> data(used_buffer.GetSize() * sizeof(float));
  468. Memory::ReadBlock(addr, data.data(), data.size());
  469. glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer_draw_state.ssbo);
  470. glBufferData(GL_SHADER_STORAGE_BUFFER, data.size(), data.data(), GL_DYNAMIC_DRAW);
  471. glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
  472. }
  473. state.Apply();
  474. }
  475. void RasterizerOpenGL::BindFramebufferSurfaces(const Surface& color_surface,
  476. const Surface& depth_surface, bool has_stencil) {
  477. state.draw.draw_framebuffer = framebuffer.handle;
  478. state.Apply();
  479. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  480. color_surface != nullptr ? color_surface->texture.handle : 0, 0);
  481. if (depth_surface != nullptr) {
  482. if (has_stencil) {
  483. // attach both depth and stencil
  484. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
  485. depth_surface->texture.handle, 0);
  486. } else {
  487. // attach depth
  488. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
  489. depth_surface->texture.handle, 0);
  490. // clear stencil attachment
  491. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  492. }
  493. } else {
  494. // clear both depth and stencil attachment
  495. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0,
  496. 0);
  497. }
  498. }
  499. void RasterizerOpenGL::SyncViewport(const MathUtil::Rectangle<u32>& surfaces_rect, u16 res_scale) {
  500. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  501. const MathUtil::Rectangle<s32> viewport_rect{regs.viewport[0].GetRect()};
  502. state.viewport.x = static_cast<GLint>(surfaces_rect.left) + viewport_rect.left * res_scale;
  503. state.viewport.y = static_cast<GLint>(surfaces_rect.bottom) + viewport_rect.bottom * res_scale;
  504. state.viewport.width = static_cast<GLsizei>(viewport_rect.GetWidth() * res_scale);
  505. state.viewport.height = static_cast<GLsizei>(viewport_rect.GetHeight() * res_scale);
  506. }
  507. void RasterizerOpenGL::SyncClipEnabled() {
  508. UNREACHABLE();
  509. }
  510. void RasterizerOpenGL::SyncClipCoef() {
  511. UNREACHABLE();
  512. }
  513. void RasterizerOpenGL::SyncCullMode() {
  514. UNREACHABLE();
  515. }
  516. void RasterizerOpenGL::SyncDepthScale() {
  517. UNREACHABLE();
  518. }
  519. void RasterizerOpenGL::SyncDepthOffset() {
  520. UNREACHABLE();
  521. }
  522. void RasterizerOpenGL::SyncBlendEnabled() {
  523. UNREACHABLE();
  524. }
  525. void RasterizerOpenGL::SyncBlendFuncs() {
  526. UNREACHABLE();
  527. }
  528. void RasterizerOpenGL::SyncBlendColor() {
  529. UNREACHABLE();
  530. }