gl_rasterizer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. switch (program) {
  185. case Maxwell::ShaderProgram::VertexB: {
  186. GLShader::MaxwellVSConfig vs_config{setup};
  187. shader_program_manager->UseProgrammableVertexShader(vs_config, setup);
  188. break;
  189. }
  190. case Maxwell::ShaderProgram::Fragment: {
  191. GLShader::MaxwellFSConfig fs_config{setup};
  192. shader_program_manager->UseProgrammableFragmentShader(fs_config, setup);
  193. break;
  194. }
  195. default:
  196. LOG_CRITICAL(HW_GPU, "Unimplemented shader index=%d, enable=%d, offset=0x%08X", index,
  197. shader_config.enable.Value(), shader_config.offset);
  198. UNREACHABLE();
  199. }
  200. }
  201. shader_program_manager->UseTrivialGeometryShader();
  202. }
  203. bool RasterizerOpenGL::AccelerateDrawBatch(bool is_indexed) {
  204. accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays;
  205. DrawArrays();
  206. return true;
  207. }
  208. void RasterizerOpenGL::DrawArrays() {
  209. if (accelerate_draw == AccelDraw::Disabled)
  210. return;
  211. MICROPROFILE_SCOPE(OpenGL_Drawing);
  212. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  213. // TODO(bunnei): Implement these
  214. const bool has_stencil = false;
  215. const bool using_color_fb = true;
  216. const bool using_depth_fb = false;
  217. const MathUtil::Rectangle<s32> viewport_rect{regs.viewport[0].GetRect()};
  218. const bool write_color_fb =
  219. state.color_mask.red_enabled == GL_TRUE || state.color_mask.green_enabled == GL_TRUE ||
  220. state.color_mask.blue_enabled == GL_TRUE || state.color_mask.alpha_enabled == GL_TRUE;
  221. const bool write_depth_fb =
  222. (state.depth.test_enabled && state.depth.write_mask == GL_TRUE) ||
  223. (has_stencil && state.stencil.test_enabled && state.stencil.write_mask != 0);
  224. Surface color_surface;
  225. Surface depth_surface;
  226. MathUtil::Rectangle<u32> surfaces_rect;
  227. std::tie(color_surface, depth_surface, surfaces_rect) =
  228. res_cache.GetFramebufferSurfaces(using_color_fb, using_depth_fb, viewport_rect);
  229. const u16 res_scale = color_surface != nullptr
  230. ? color_surface->res_scale
  231. : (depth_surface == nullptr ? 1u : depth_surface->res_scale);
  232. MathUtil::Rectangle<u32> draw_rect{
  233. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) +
  234. viewport_rect.left * res_scale,
  235. surfaces_rect.left, surfaces_rect.right)), // Left
  236. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
  237. viewport_rect.top * res_scale,
  238. surfaces_rect.bottom, surfaces_rect.top)), // Top
  239. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) +
  240. viewport_rect.right * res_scale,
  241. surfaces_rect.left, surfaces_rect.right)), // Right
  242. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
  243. viewport_rect.bottom * res_scale,
  244. surfaces_rect.bottom, surfaces_rect.top))}; // Bottom
  245. // Bind the framebuffer surfaces
  246. BindFramebufferSurfaces(color_surface, depth_surface, has_stencil);
  247. // Sync the viewport
  248. SyncViewport(surfaces_rect, res_scale);
  249. // TODO(bunnei): Sync framebuffer_scale uniform here
  250. // TODO(bunnei): Sync scissorbox uniform(s) here
  251. // Sync and bind the texture surfaces
  252. BindTextures();
  253. // Configure the constant buffer objects
  254. SetupConstBuffers();
  255. // Viewport can have negative offsets or larger dimensions than our framebuffer sub-rect. Enable
  256. // scissor test to prevent drawing outside of the framebuffer region
  257. state.scissor.enabled = true;
  258. state.scissor.x = draw_rect.left;
  259. state.scissor.y = draw_rect.bottom;
  260. state.scissor.width = draw_rect.GetWidth();
  261. state.scissor.height = draw_rect.GetHeight();
  262. state.Apply();
  263. // Draw the vertex batch
  264. const bool is_indexed = accelerate_draw == AccelDraw::Indexed;
  265. AnalyzeVertexArray(is_indexed);
  266. state.draw.vertex_buffer = stream_buffer->GetHandle();
  267. state.Apply();
  268. size_t buffer_size = static_cast<size_t>(vs_input_size);
  269. if (is_indexed) {
  270. UNREACHABLE();
  271. }
  272. // Uniform space for the 5 shader stages
  273. buffer_size += sizeof(GLShader::MaxwellUniformData) * Maxwell::MaxShaderStage;
  274. size_t ptr_pos = 0;
  275. u8* buffer_ptr;
  276. GLintptr buffer_offset;
  277. std::tie(buffer_ptr, buffer_offset) =
  278. stream_buffer->Map(static_cast<GLsizeiptr>(buffer_size), 4);
  279. SetupVertexArray(buffer_ptr, buffer_offset);
  280. ptr_pos += vs_input_size;
  281. GLintptr index_buffer_offset = 0;
  282. if (is_indexed) {
  283. UNREACHABLE();
  284. }
  285. SetupShaders(buffer_ptr, buffer_offset, ptr_pos);
  286. stream_buffer->Unmap();
  287. shader_program_manager->ApplyTo(state);
  288. state.Apply();
  289. if (is_indexed) {
  290. UNREACHABLE();
  291. } else {
  292. glDrawArrays(MaxwellToGL::PrimitiveTopology(regs.draw.topology), 0,
  293. regs.vertex_buffer.count);
  294. }
  295. // Disable scissor test
  296. state.scissor.enabled = false;
  297. accelerate_draw = AccelDraw::Disabled;
  298. // Unbind textures for potential future use as framebuffer attachments
  299. for (auto& texture_unit : state.texture_units) {
  300. texture_unit.texture_2d = 0;
  301. }
  302. state.Apply();
  303. // Mark framebuffer surfaces as dirty
  304. MathUtil::Rectangle<u32> draw_rect_unscaled{
  305. draw_rect.left / res_scale, draw_rect.top / res_scale, draw_rect.right / res_scale,
  306. draw_rect.bottom / res_scale};
  307. if (color_surface != nullptr && write_color_fb) {
  308. auto interval = color_surface->GetSubRectInterval(draw_rect_unscaled);
  309. res_cache.InvalidateRegion(boost::icl::first(interval), boost::icl::length(interval),
  310. color_surface);
  311. }
  312. if (depth_surface != nullptr && write_depth_fb) {
  313. auto interval = depth_surface->GetSubRectInterval(draw_rect_unscaled);
  314. res_cache.InvalidateRegion(boost::icl::first(interval), boost::icl::length(interval),
  315. depth_surface);
  316. }
  317. }
  318. void RasterizerOpenGL::BindTextures() {
  319. using Regs = Tegra::Engines::Maxwell3D::Regs;
  320. auto& maxwell3d = Core::System::GetInstance().GPU().Get3DEngine();
  321. // Each Maxwell shader stage can have an arbitrary number of textures, but we're limited to a
  322. // certain number in OpenGL. We try to only use the minimum amount of host textures by not
  323. // keeping a 1:1 relation between guest texture ids and host texture ids, ie, guest texture id 8
  324. // can be host texture id 0 if it's the only texture used in the guest shader program.
  325. u32 host_texture_index = 0;
  326. for (u32 stage = 0; stage < Regs::MaxShaderStage; ++stage) {
  327. ASSERT(host_texture_index < texture_samplers.size());
  328. const auto textures = maxwell3d.GetStageTextures(static_cast<Regs::ShaderStage>(stage));
  329. for (unsigned texture_index = 0; texture_index < textures.size(); ++texture_index) {
  330. const auto& texture = textures[texture_index];
  331. if (texture.enabled) {
  332. texture_samplers[host_texture_index].SyncWithConfig(texture.tsc);
  333. Surface surface = res_cache.GetTextureSurface(texture);
  334. if (surface != nullptr) {
  335. state.texture_units[host_texture_index].texture_2d = surface->texture.handle;
  336. } else {
  337. // Can occur when texture addr is null or its memory is unmapped/invalid
  338. state.texture_units[texture_index].texture_2d = 0;
  339. }
  340. ++host_texture_index;
  341. } else {
  342. state.texture_units[texture_index].texture_2d = 0;
  343. }
  344. }
  345. }
  346. }
  347. void RasterizerOpenGL::NotifyMaxwellRegisterChanged(u32 id) {}
  348. void RasterizerOpenGL::FlushAll() {
  349. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  350. res_cache.FlushAll();
  351. }
  352. void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
  353. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  354. res_cache.FlushRegion(addr, size);
  355. }
  356. void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
  357. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  358. res_cache.InvalidateRegion(addr, size, nullptr);
  359. }
  360. void RasterizerOpenGL::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  361. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  362. res_cache.FlushRegion(addr, size);
  363. res_cache.InvalidateRegion(addr, size, nullptr);
  364. }
  365. bool RasterizerOpenGL::AccelerateDisplayTransfer(const void* config) {
  366. MICROPROFILE_SCOPE(OpenGL_Blits);
  367. UNREACHABLE();
  368. return true;
  369. }
  370. bool RasterizerOpenGL::AccelerateTextureCopy(const void* config) {
  371. UNREACHABLE();
  372. return true;
  373. }
  374. bool RasterizerOpenGL::AccelerateFill(const void* config) {
  375. UNREACHABLE();
  376. return true;
  377. }
  378. bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& framebuffer,
  379. VAddr framebuffer_addr, u32 pixel_stride,
  380. ScreenInfo& screen_info) {
  381. if (framebuffer_addr == 0) {
  382. return false;
  383. }
  384. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  385. SurfaceParams src_params;
  386. src_params.addr = framebuffer_addr;
  387. src_params.width = std::min(framebuffer.width, pixel_stride);
  388. src_params.height = framebuffer.height;
  389. src_params.stride = pixel_stride;
  390. src_params.is_tiled = false;
  391. src_params.pixel_format =
  392. SurfaceParams::PixelFormatFromGPUPixelFormat(framebuffer.pixel_format);
  393. src_params.UpdateParams();
  394. MathUtil::Rectangle<u32> src_rect;
  395. Surface src_surface;
  396. std::tie(src_surface, src_rect) =
  397. res_cache.GetSurfaceSubRect(src_params, ScaleMatch::Ignore, true);
  398. if (src_surface == nullptr) {
  399. return false;
  400. }
  401. u32 scaled_width = src_surface->GetScaledWidth();
  402. u32 scaled_height = src_surface->GetScaledHeight();
  403. screen_info.display_texcoords = MathUtil::Rectangle<float>(
  404. (float)src_rect.bottom / (float)scaled_height, (float)src_rect.left / (float)scaled_width,
  405. (float)src_rect.top / (float)scaled_height, (float)src_rect.right / (float)scaled_width);
  406. screen_info.display_texture = src_surface->texture.handle;
  407. return true;
  408. }
  409. void RasterizerOpenGL::SamplerInfo::Create() {
  410. sampler.Create();
  411. mag_filter = min_filter = Tegra::Texture::TextureFilter::Linear;
  412. wrap_u = wrap_v = Tegra::Texture::WrapMode::Wrap;
  413. border_color_r = border_color_g = border_color_b = border_color_a = 0;
  414. // default is GL_LINEAR_MIPMAP_LINEAR
  415. glSamplerParameteri(sampler.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  416. // Other attributes have correct defaults
  417. }
  418. void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Tegra::Texture::TSCEntry& config) {
  419. GLuint s = sampler.handle;
  420. if (mag_filter != config.mag_filter) {
  421. mag_filter = config.mag_filter;
  422. glSamplerParameteri(s, GL_TEXTURE_MAG_FILTER, MaxwellToGL::TextureFilterMode(mag_filter));
  423. }
  424. if (min_filter != config.min_filter) {
  425. min_filter = config.min_filter;
  426. glSamplerParameteri(s, GL_TEXTURE_MIN_FILTER, MaxwellToGL::TextureFilterMode(min_filter));
  427. }
  428. if (wrap_u != config.wrap_u) {
  429. wrap_u = config.wrap_u;
  430. glSamplerParameteri(s, GL_TEXTURE_WRAP_S, MaxwellToGL::WrapMode(wrap_u));
  431. }
  432. if (wrap_v != config.wrap_v) {
  433. wrap_v = config.wrap_v;
  434. glSamplerParameteri(s, GL_TEXTURE_WRAP_T, MaxwellToGL::WrapMode(wrap_v));
  435. }
  436. if (wrap_u == Tegra::Texture::WrapMode::Border || wrap_v == Tegra::Texture::WrapMode::Border) {
  437. // TODO(Subv): Implement border color
  438. ASSERT(false);
  439. }
  440. }
  441. void RasterizerOpenGL::SetupConstBuffers() {
  442. using Regs = Tegra::Engines::Maxwell3D::Regs;
  443. auto& gpu = Core::System::GetInstance().GPU();
  444. auto& maxwell3d = gpu.Get3DEngine();
  445. // Upload only the enabled buffers from the 16 constbuffers of each shader stage
  446. u32 current_bindpoint = 0;
  447. for (u32 stage = 0; stage < Regs::MaxShaderStage; ++stage) {
  448. auto& shader_stage = maxwell3d.state.shader_stages[stage];
  449. bool stage_enabled = maxwell3d.IsShaderStageEnabled(static_cast<Regs::ShaderStage>(stage));
  450. for (u32 buffer_id = 0; buffer_id < Regs::MaxConstBuffers; ++buffer_id) {
  451. const auto& buffer = shader_stage.const_buffers[buffer_id];
  452. state.draw.const_buffers[stage][buffer_id].enabled = buffer.enabled && stage_enabled;
  453. if (buffer.enabled && stage_enabled) {
  454. state.draw.const_buffers[stage][buffer_id].bindpoint = current_bindpoint;
  455. current_bindpoint++;
  456. VAddr addr = gpu.memory_manager->PhysicalToVirtualAddress(buffer.address);
  457. const u8* data = Memory::GetPointer(addr);
  458. glBindBuffer(GL_SHADER_STORAGE_BUFFER,
  459. state.draw.const_buffers[stage][buffer_id].ssbo);
  460. glBufferData(GL_SHADER_STORAGE_BUFFER, buffer.size, data, GL_DYNAMIC_DRAW);
  461. glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
  462. } else {
  463. state.draw.const_buffers[stage][buffer_id].bindpoint = -1;
  464. }
  465. }
  466. }
  467. state.Apply();
  468. }
  469. void RasterizerOpenGL::BindFramebufferSurfaces(const Surface& color_surface,
  470. const Surface& depth_surface, bool has_stencil) {
  471. state.draw.draw_framebuffer = framebuffer.handle;
  472. state.Apply();
  473. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  474. color_surface != nullptr ? color_surface->texture.handle : 0, 0);
  475. if (depth_surface != nullptr) {
  476. if (has_stencil) {
  477. // attach both depth and stencil
  478. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
  479. depth_surface->texture.handle, 0);
  480. } else {
  481. // attach depth
  482. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
  483. depth_surface->texture.handle, 0);
  484. // clear stencil attachment
  485. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  486. }
  487. } else {
  488. // clear both depth and stencil attachment
  489. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0,
  490. 0);
  491. }
  492. }
  493. void RasterizerOpenGL::SyncViewport(const MathUtil::Rectangle<u32>& surfaces_rect, u16 res_scale) {
  494. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  495. const MathUtil::Rectangle<s32> viewport_rect{regs.viewport[0].GetRect()};
  496. state.viewport.x = static_cast<GLint>(surfaces_rect.left) + viewport_rect.left * res_scale;
  497. state.viewport.y = static_cast<GLint>(surfaces_rect.bottom) + viewport_rect.bottom * res_scale;
  498. state.viewport.width = static_cast<GLsizei>(viewport_rect.GetWidth() * res_scale);
  499. state.viewport.height = static_cast<GLsizei>(viewport_rect.GetHeight() * res_scale);
  500. }
  501. void RasterizerOpenGL::SyncClipEnabled() {
  502. UNREACHABLE();
  503. }
  504. void RasterizerOpenGL::SyncClipCoef() {
  505. UNREACHABLE();
  506. }
  507. void RasterizerOpenGL::SyncCullMode() {
  508. UNREACHABLE();
  509. }
  510. void RasterizerOpenGL::SyncDepthScale() {
  511. UNREACHABLE();
  512. }
  513. void RasterizerOpenGL::SyncDepthOffset() {
  514. UNREACHABLE();
  515. }
  516. void RasterizerOpenGL::SyncBlendEnabled() {
  517. UNREACHABLE();
  518. }
  519. void RasterizerOpenGL::SyncBlendFuncs() {
  520. UNREACHABLE();
  521. }
  522. void RasterizerOpenGL::SyncBlendColor() {
  523. UNREACHABLE();
  524. }