gl_rasterizer.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <bitset>
  7. #include <memory>
  8. #include <string>
  9. #include <string_view>
  10. #include <tuple>
  11. #include <utility>
  12. #include <glad/glad.h>
  13. #include "common/alignment.h"
  14. #include "common/assert.h"
  15. #include "common/logging/log.h"
  16. #include "common/math_util.h"
  17. #include "common/microprofile.h"
  18. #include "common/scope_exit.h"
  19. #include "common/settings.h"
  20. #include "core/core.h"
  21. #include "core/hle/kernel/k_process.h"
  22. #include "core/memory.h"
  23. #include "video_core/engines/kepler_compute.h"
  24. #include "video_core/engines/maxwell_3d.h"
  25. #include "video_core/engines/shader_type.h"
  26. #include "video_core/memory_manager.h"
  27. #include "video_core/renderer_opengl/gl_device.h"
  28. #include "video_core/renderer_opengl/gl_query_cache.h"
  29. #include "video_core/renderer_opengl/gl_rasterizer.h"
  30. #include "video_core/renderer_opengl/gl_shader_cache.h"
  31. #include "video_core/renderer_opengl/gl_texture_cache.h"
  32. #include "video_core/renderer_opengl/maxwell_to_gl.h"
  33. #include "video_core/renderer_opengl/renderer_opengl.h"
  34. #include "video_core/shader_cache.h"
  35. #include "video_core/texture_cache/texture_cache.h"
  36. namespace OpenGL {
  37. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  38. using GLvec4 = std::array<GLfloat, 4>;
  39. using Tegra::Engines::ShaderType;
  40. using VideoCore::Surface::PixelFormat;
  41. using VideoCore::Surface::SurfaceTarget;
  42. using VideoCore::Surface::SurfaceType;
  43. MICROPROFILE_DEFINE(OpenGL_Drawing, "OpenGL", "Drawing", MP_RGB(128, 128, 192));
  44. MICROPROFILE_DEFINE(OpenGL_Clears, "OpenGL", "Clears", MP_RGB(128, 128, 192));
  45. MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(128, 128, 192));
  46. MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Management", MP_RGB(100, 255, 100));
  47. namespace {
  48. constexpr size_t NUM_SUPPORTED_VERTEX_ATTRIBUTES = 16;
  49. void oglEnable(GLenum cap, bool state) {
  50. (state ? glEnable : glDisable)(cap);
  51. }
  52. } // Anonymous namespace
  53. RasterizerOpenGL::RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  54. Core::Memory::Memory& cpu_memory_, const Device& device_,
  55. ScreenInfo& screen_info_, ProgramManager& program_manager_,
  56. StateTracker& state_tracker_)
  57. : RasterizerAccelerated(cpu_memory_), gpu(gpu_), maxwell3d(gpu.Maxwell3D()),
  58. kepler_compute(gpu.KeplerCompute()), gpu_memory(gpu.MemoryManager()), device(device_),
  59. screen_info(screen_info_), program_manager(program_manager_), state_tracker(state_tracker_),
  60. texture_cache_runtime(device, program_manager, state_tracker),
  61. texture_cache(texture_cache_runtime, *this, maxwell3d, kepler_compute, gpu_memory),
  62. buffer_cache_runtime(device),
  63. buffer_cache(*this, maxwell3d, kepler_compute, gpu_memory, cpu_memory_, buffer_cache_runtime),
  64. shader_cache(*this, emu_window_, maxwell3d, kepler_compute, gpu_memory, device, texture_cache,
  65. buffer_cache, program_manager, state_tracker),
  66. query_cache(*this, maxwell3d, gpu_memory), accelerate_dma(buffer_cache),
  67. fence_manager(*this, gpu, texture_cache, buffer_cache, query_cache) {}
  68. RasterizerOpenGL::~RasterizerOpenGL() = default;
  69. void RasterizerOpenGL::SyncVertexFormats() {
  70. auto& flags = maxwell3d.dirty.flags;
  71. if (!flags[Dirty::VertexFormats]) {
  72. return;
  73. }
  74. flags[Dirty::VertexFormats] = false;
  75. // Use the vertex array as-is, assumes that the data is formatted correctly for OpenGL. Enables
  76. // the first 16 vertex attributes always, as we don't know which ones are actually used until
  77. // shader time. Note, Tegra technically supports 32, but we're capping this to 16 for now to
  78. // avoid OpenGL errors.
  79. // TODO(Subv): Analyze the shader to identify which attributes are actually used and don't
  80. // assume every shader uses them all.
  81. for (std::size_t index = 0; index < NUM_SUPPORTED_VERTEX_ATTRIBUTES; ++index) {
  82. if (!flags[Dirty::VertexFormat0 + index]) {
  83. continue;
  84. }
  85. flags[Dirty::VertexFormat0 + index] = false;
  86. const auto attrib = maxwell3d.regs.vertex_attrib_format[index];
  87. const auto gl_index = static_cast<GLuint>(index);
  88. // Disable constant attributes.
  89. if (attrib.IsConstant()) {
  90. glDisableVertexAttribArray(gl_index);
  91. continue;
  92. }
  93. glEnableVertexAttribArray(gl_index);
  94. if (attrib.type == Maxwell::VertexAttribute::Type::SignedInt ||
  95. attrib.type == Maxwell::VertexAttribute::Type::UnsignedInt) {
  96. glVertexAttribIFormat(gl_index, attrib.ComponentCount(),
  97. MaxwellToGL::VertexFormat(attrib), attrib.offset);
  98. } else {
  99. glVertexAttribFormat(gl_index, attrib.ComponentCount(),
  100. MaxwellToGL::VertexFormat(attrib),
  101. attrib.IsNormalized() ? GL_TRUE : GL_FALSE, attrib.offset);
  102. }
  103. glVertexAttribBinding(gl_index, attrib.buffer);
  104. }
  105. }
  106. void RasterizerOpenGL::SyncVertexInstances() {
  107. auto& flags = maxwell3d.dirty.flags;
  108. if (!flags[Dirty::VertexInstances]) {
  109. return;
  110. }
  111. flags[Dirty::VertexInstances] = false;
  112. const auto& regs = maxwell3d.regs;
  113. for (std::size_t index = 0; index < NUM_SUPPORTED_VERTEX_ATTRIBUTES; ++index) {
  114. if (!flags[Dirty::VertexInstance0 + index]) {
  115. continue;
  116. }
  117. flags[Dirty::VertexInstance0 + index] = false;
  118. const auto gl_index = static_cast<GLuint>(index);
  119. const bool instancing_enabled = regs.instanced_arrays.IsInstancingEnabled(gl_index);
  120. const GLuint divisor = instancing_enabled ? regs.vertex_array[index].divisor : 0;
  121. glVertexBindingDivisor(gl_index, divisor);
  122. }
  123. }
  124. void RasterizerOpenGL::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
  125. const VideoCore::DiskResourceLoadCallback& callback) {}
  126. void RasterizerOpenGL::Clear() {
  127. MICROPROFILE_SCOPE(OpenGL_Clears);
  128. if (!maxwell3d.ShouldExecute()) {
  129. return;
  130. }
  131. const auto& regs = maxwell3d.regs;
  132. bool use_color{};
  133. bool use_depth{};
  134. bool use_stencil{};
  135. if (regs.clear_buffers.R || regs.clear_buffers.G || regs.clear_buffers.B ||
  136. regs.clear_buffers.A) {
  137. use_color = true;
  138. const GLuint index = regs.clear_buffers.RT;
  139. state_tracker.NotifyColorMask(index);
  140. glColorMaski(index, regs.clear_buffers.R != 0, regs.clear_buffers.G != 0,
  141. regs.clear_buffers.B != 0, regs.clear_buffers.A != 0);
  142. // TODO(Rodrigo): Determine if clamping is used on clears
  143. SyncFragmentColorClampState();
  144. SyncFramebufferSRGB();
  145. }
  146. if (regs.clear_buffers.Z) {
  147. ASSERT_MSG(regs.zeta_enable != 0, "Tried to clear Z but buffer is not enabled!");
  148. use_depth = true;
  149. state_tracker.NotifyDepthMask();
  150. glDepthMask(GL_TRUE);
  151. }
  152. if (regs.clear_buffers.S) {
  153. ASSERT_MSG(regs.zeta_enable, "Tried to clear stencil but buffer is not enabled!");
  154. use_stencil = true;
  155. }
  156. if (!use_color && !use_depth && !use_stencil) {
  157. // No color surface nor depth/stencil surface are enabled
  158. return;
  159. }
  160. SyncRasterizeEnable();
  161. SyncStencilTestState();
  162. if (regs.clear_flags.scissor) {
  163. SyncScissorTest();
  164. } else {
  165. state_tracker.NotifyScissor0();
  166. glDisablei(GL_SCISSOR_TEST, 0);
  167. }
  168. UNIMPLEMENTED_IF(regs.clear_flags.viewport);
  169. std::scoped_lock lock{texture_cache.mutex};
  170. texture_cache.UpdateRenderTargets(true);
  171. state_tracker.BindFramebuffer(texture_cache.GetFramebuffer()->Handle());
  172. if (use_color) {
  173. glClearBufferfv(GL_COLOR, regs.clear_buffers.RT, regs.clear_color);
  174. }
  175. if (use_depth && use_stencil) {
  176. glClearBufferfi(GL_DEPTH_STENCIL, 0, regs.clear_depth, regs.clear_stencil);
  177. } else if (use_depth) {
  178. glClearBufferfv(GL_DEPTH, 0, &regs.clear_depth);
  179. } else if (use_stencil) {
  180. glClearBufferiv(GL_STENCIL, 0, &regs.clear_stencil);
  181. }
  182. ++num_queued_commands;
  183. }
  184. void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) {
  185. MICROPROFILE_SCOPE(OpenGL_Drawing);
  186. query_cache.UpdateCounters();
  187. SyncState();
  188. GraphicsProgram* const program{shader_cache.CurrentGraphicsProgram()};
  189. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  190. program->Configure(is_indexed);
  191. const GLenum primitive_mode = MaxwellToGL::PrimitiveTopology(maxwell3d.regs.draw.topology);
  192. BeginTransformFeedback(program, primitive_mode);
  193. const GLuint base_instance = static_cast<GLuint>(maxwell3d.regs.vb_base_instance);
  194. const GLsizei num_instances =
  195. static_cast<GLsizei>(is_instanced ? maxwell3d.mme_draw.instance_count : 1);
  196. if (is_indexed) {
  197. const GLint base_vertex = static_cast<GLint>(maxwell3d.regs.vb_element_base);
  198. const GLsizei num_vertices = static_cast<GLsizei>(maxwell3d.regs.index_array.count);
  199. const GLvoid* const offset = buffer_cache_runtime.IndexOffset();
  200. const GLenum format = MaxwellToGL::IndexFormat(maxwell3d.regs.index_array.format);
  201. if (num_instances == 1 && base_instance == 0 && base_vertex == 0) {
  202. glDrawElements(primitive_mode, num_vertices, format, offset);
  203. } else if (num_instances == 1 && base_instance == 0) {
  204. glDrawElementsBaseVertex(primitive_mode, num_vertices, format, offset, base_vertex);
  205. } else if (base_vertex == 0 && base_instance == 0) {
  206. glDrawElementsInstanced(primitive_mode, num_vertices, format, offset, num_instances);
  207. } else if (base_vertex == 0) {
  208. glDrawElementsInstancedBaseInstance(primitive_mode, num_vertices, format, offset,
  209. num_instances, base_instance);
  210. } else if (base_instance == 0) {
  211. glDrawElementsInstancedBaseVertex(primitive_mode, num_vertices, format, offset,
  212. num_instances, base_vertex);
  213. } else {
  214. glDrawElementsInstancedBaseVertexBaseInstance(primitive_mode, num_vertices, format,
  215. offset, num_instances, base_vertex,
  216. base_instance);
  217. }
  218. } else {
  219. const GLint base_vertex = static_cast<GLint>(maxwell3d.regs.vertex_buffer.first);
  220. const GLsizei num_vertices = static_cast<GLsizei>(maxwell3d.regs.vertex_buffer.count);
  221. if (num_instances == 1 && base_instance == 0) {
  222. glDrawArrays(primitive_mode, base_vertex, num_vertices);
  223. } else if (base_instance == 0) {
  224. glDrawArraysInstanced(primitive_mode, base_vertex, num_vertices, num_instances);
  225. } else {
  226. glDrawArraysInstancedBaseInstance(primitive_mode, base_vertex, num_vertices,
  227. num_instances, base_instance);
  228. }
  229. }
  230. EndTransformFeedback();
  231. ++num_queued_commands;
  232. gpu.TickWork();
  233. }
  234. void RasterizerOpenGL::DispatchCompute() {
  235. ComputeProgram* const program{shader_cache.CurrentComputeProgram()};
  236. if (!program) {
  237. return;
  238. }
  239. program->Configure();
  240. const auto& qmd{kepler_compute.launch_description};
  241. glDispatchCompute(qmd.grid_dim_x, qmd.grid_dim_y, qmd.grid_dim_z);
  242. ++num_queued_commands;
  243. }
  244. void RasterizerOpenGL::ResetCounter(VideoCore::QueryType type) {
  245. query_cache.ResetCounter(type);
  246. }
  247. void RasterizerOpenGL::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
  248. std::optional<u64> timestamp) {
  249. query_cache.Query(gpu_addr, type, timestamp);
  250. }
  251. void RasterizerOpenGL::BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr,
  252. u32 size) {
  253. std::scoped_lock lock{buffer_cache.mutex};
  254. buffer_cache.BindGraphicsUniformBuffer(stage, index, gpu_addr, size);
  255. }
  256. void RasterizerOpenGL::DisableGraphicsUniformBuffer(size_t stage, u32 index) {
  257. buffer_cache.DisableGraphicsUniformBuffer(stage, index);
  258. }
  259. void RasterizerOpenGL::FlushAll() {}
  260. void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
  261. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  262. if (addr == 0 || size == 0) {
  263. return;
  264. }
  265. {
  266. std::scoped_lock lock{texture_cache.mutex};
  267. texture_cache.DownloadMemory(addr, size);
  268. }
  269. {
  270. std::scoped_lock lock{buffer_cache.mutex};
  271. buffer_cache.DownloadMemory(addr, size);
  272. }
  273. query_cache.FlushRegion(addr, size);
  274. }
  275. bool RasterizerOpenGL::MustFlushRegion(VAddr addr, u64 size) {
  276. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  277. if (!Settings::IsGPULevelHigh()) {
  278. return buffer_cache.IsRegionGpuModified(addr, size);
  279. }
  280. return texture_cache.IsRegionGpuModified(addr, size) ||
  281. buffer_cache.IsRegionGpuModified(addr, size);
  282. }
  283. void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
  284. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  285. if (addr == 0 || size == 0) {
  286. return;
  287. }
  288. {
  289. std::scoped_lock lock{texture_cache.mutex};
  290. texture_cache.WriteMemory(addr, size);
  291. }
  292. {
  293. std::scoped_lock lock{buffer_cache.mutex};
  294. buffer_cache.WriteMemory(addr, size);
  295. }
  296. shader_cache.InvalidateRegion(addr, size);
  297. query_cache.InvalidateRegion(addr, size);
  298. }
  299. void RasterizerOpenGL::OnCPUWrite(VAddr addr, u64 size) {
  300. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  301. if (addr == 0 || size == 0) {
  302. return;
  303. }
  304. shader_cache.OnCPUWrite(addr, size);
  305. {
  306. std::scoped_lock lock{texture_cache.mutex};
  307. texture_cache.WriteMemory(addr, size);
  308. }
  309. {
  310. std::scoped_lock lock{buffer_cache.mutex};
  311. buffer_cache.CachedWriteMemory(addr, size);
  312. }
  313. }
  314. void RasterizerOpenGL::SyncGuestHost() {
  315. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  316. shader_cache.SyncGuestHost();
  317. {
  318. std::scoped_lock lock{buffer_cache.mutex};
  319. buffer_cache.FlushCachedWrites();
  320. }
  321. }
  322. void RasterizerOpenGL::UnmapMemory(VAddr addr, u64 size) {
  323. {
  324. std::scoped_lock lock{texture_cache.mutex};
  325. texture_cache.UnmapMemory(addr, size);
  326. }
  327. {
  328. std::scoped_lock lock{buffer_cache.mutex};
  329. buffer_cache.WriteMemory(addr, size);
  330. }
  331. shader_cache.OnCPUWrite(addr, size);
  332. }
  333. void RasterizerOpenGL::ModifyGPUMemory(GPUVAddr addr, u64 size) {
  334. {
  335. std::scoped_lock lock{texture_cache.mutex};
  336. texture_cache.UnmapGPUMemory(addr, size);
  337. }
  338. }
  339. void RasterizerOpenGL::SignalSemaphore(GPUVAddr addr, u32 value) {
  340. if (!gpu.IsAsync()) {
  341. gpu_memory.Write<u32>(addr, value);
  342. return;
  343. }
  344. fence_manager.SignalSemaphore(addr, value);
  345. }
  346. void RasterizerOpenGL::SignalSyncPoint(u32 value) {
  347. if (!gpu.IsAsync()) {
  348. gpu.IncrementSyncPoint(value);
  349. return;
  350. }
  351. fence_manager.SignalSyncPoint(value);
  352. }
  353. void RasterizerOpenGL::SignalReference() {
  354. if (!gpu.IsAsync()) {
  355. return;
  356. }
  357. fence_manager.SignalOrdering();
  358. }
  359. void RasterizerOpenGL::ReleaseFences() {
  360. if (!gpu.IsAsync()) {
  361. return;
  362. }
  363. fence_manager.WaitPendingFences();
  364. }
  365. void RasterizerOpenGL::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  366. if (Settings::IsGPULevelExtreme()) {
  367. FlushRegion(addr, size);
  368. }
  369. InvalidateRegion(addr, size);
  370. }
  371. void RasterizerOpenGL::WaitForIdle() {
  372. glMemoryBarrier(GL_ALL_BARRIER_BITS);
  373. SignalReference();
  374. }
  375. void RasterizerOpenGL::FragmentBarrier() {
  376. glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT);
  377. }
  378. void RasterizerOpenGL::TiledCacheBarrier() {
  379. glTextureBarrier();
  380. }
  381. void RasterizerOpenGL::FlushCommands() {
  382. // Only flush when we have commands queued to OpenGL.
  383. if (num_queued_commands == 0) {
  384. return;
  385. }
  386. num_queued_commands = 0;
  387. // Make sure memory stored from the previous GL command stream is visible
  388. // This is only needed on assembly shaders where we write to GPU memory with raw pointers
  389. // TODO: Call this only when NV_shader_buffer_load or NV_shader_buffer_store have been used
  390. // and prefer using NV_shader_storage_buffer_object when possible
  391. if (Settings::values.use_assembly_shaders.GetValue()) {
  392. glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
  393. }
  394. glFlush();
  395. }
  396. void RasterizerOpenGL::TickFrame() {
  397. // Ticking a frame means that buffers will be swapped, calling glFlush implicitly.
  398. num_queued_commands = 0;
  399. fence_manager.TickFrame();
  400. {
  401. std::scoped_lock lock{texture_cache.mutex};
  402. texture_cache.TickFrame();
  403. }
  404. {
  405. std::scoped_lock lock{buffer_cache.mutex};
  406. buffer_cache.TickFrame();
  407. }
  408. }
  409. bool RasterizerOpenGL::AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  410. const Tegra::Engines::Fermi2D::Surface& dst,
  411. const Tegra::Engines::Fermi2D::Config& copy_config) {
  412. MICROPROFILE_SCOPE(OpenGL_Blits);
  413. std::scoped_lock lock{texture_cache.mutex};
  414. texture_cache.BlitImage(dst, src, copy_config);
  415. return true;
  416. }
  417. Tegra::Engines::AccelerateDMAInterface& RasterizerOpenGL::AccessAccelerateDMA() {
  418. return accelerate_dma;
  419. }
  420. bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& config,
  421. VAddr framebuffer_addr, u32 pixel_stride) {
  422. if (framebuffer_addr == 0) {
  423. return false;
  424. }
  425. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  426. std::scoped_lock lock{texture_cache.mutex};
  427. ImageView* const image_view{texture_cache.TryFindFramebufferImageView(framebuffer_addr)};
  428. if (!image_view) {
  429. return false;
  430. }
  431. // Verify that the cached surface is the same size and format as the requested framebuffer
  432. // ASSERT_MSG(image_view->size.width == config.width, "Framebuffer width is different");
  433. // ASSERT_MSG(image_view->size.height == config.height, "Framebuffer height is different");
  434. screen_info.display_texture = image_view->Handle(Shader::TextureType::Color2D);
  435. screen_info.display_srgb = VideoCore::Surface::IsPixelFormatSRGB(image_view->format);
  436. return true;
  437. }
  438. void RasterizerOpenGL::SyncState() {
  439. SyncViewport();
  440. SyncRasterizeEnable();
  441. SyncPolygonModes();
  442. SyncColorMask();
  443. SyncFragmentColorClampState();
  444. SyncMultiSampleState();
  445. SyncDepthTestState();
  446. SyncDepthClamp();
  447. SyncStencilTestState();
  448. SyncBlendState();
  449. SyncLogicOpState();
  450. SyncCullMode();
  451. SyncPrimitiveRestart();
  452. SyncScissorTest();
  453. SyncPointState();
  454. SyncLineState();
  455. SyncPolygonOffset();
  456. SyncAlphaTest();
  457. SyncFramebufferSRGB();
  458. SyncVertexFormats();
  459. SyncVertexInstances();
  460. }
  461. void RasterizerOpenGL::SyncViewport() {
  462. auto& flags = maxwell3d.dirty.flags;
  463. const auto& regs = maxwell3d.regs;
  464. const bool dirty_viewport = flags[Dirty::Viewports];
  465. const bool dirty_clip_control = flags[Dirty::ClipControl];
  466. if (dirty_clip_control || flags[Dirty::FrontFace]) {
  467. flags[Dirty::FrontFace] = false;
  468. GLenum mode = MaxwellToGL::FrontFace(regs.front_face);
  469. if (regs.screen_y_control.triangle_rast_flip != 0 &&
  470. regs.viewport_transform[0].scale_y < 0.0f) {
  471. switch (mode) {
  472. case GL_CW:
  473. mode = GL_CCW;
  474. break;
  475. case GL_CCW:
  476. mode = GL_CW;
  477. break;
  478. }
  479. }
  480. glFrontFace(mode);
  481. }
  482. if (dirty_viewport || flags[Dirty::ClipControl]) {
  483. flags[Dirty::ClipControl] = false;
  484. bool flip_y = false;
  485. if (regs.viewport_transform[0].scale_y < 0.0f) {
  486. flip_y = !flip_y;
  487. }
  488. if (regs.screen_y_control.y_negate != 0) {
  489. flip_y = !flip_y;
  490. }
  491. const bool is_zero_to_one = regs.depth_mode == Maxwell::DepthMode::ZeroToOne;
  492. const GLenum origin = flip_y ? GL_UPPER_LEFT : GL_LOWER_LEFT;
  493. const GLenum depth = is_zero_to_one ? GL_ZERO_TO_ONE : GL_NEGATIVE_ONE_TO_ONE;
  494. state_tracker.ClipControl(origin, depth);
  495. state_tracker.SetYNegate(regs.screen_y_control.y_negate != 0);
  496. }
  497. if (dirty_viewport) {
  498. flags[Dirty::Viewports] = false;
  499. const bool force = flags[Dirty::ViewportTransform];
  500. flags[Dirty::ViewportTransform] = false;
  501. for (std::size_t i = 0; i < Maxwell::NumViewports; ++i) {
  502. if (!force && !flags[Dirty::Viewport0 + i]) {
  503. continue;
  504. }
  505. flags[Dirty::Viewport0 + i] = false;
  506. const auto& src = regs.viewport_transform[i];
  507. const Common::Rectangle<f32> rect{src.GetRect()};
  508. glViewportIndexedf(static_cast<GLuint>(i), rect.left, rect.bottom, rect.GetWidth(),
  509. rect.GetHeight());
  510. const GLdouble reduce_z = regs.depth_mode == Maxwell::DepthMode::MinusOneToOne;
  511. const GLdouble near_depth = src.translate_z - src.scale_z * reduce_z;
  512. const GLdouble far_depth = src.translate_z + src.scale_z;
  513. if (device.HasDepthBufferFloat()) {
  514. glDepthRangeIndexeddNV(static_cast<GLuint>(i), near_depth, far_depth);
  515. } else {
  516. glDepthRangeIndexed(static_cast<GLuint>(i), near_depth, far_depth);
  517. }
  518. if (!GLAD_GL_NV_viewport_swizzle) {
  519. continue;
  520. }
  521. glViewportSwizzleNV(static_cast<GLuint>(i), MaxwellToGL::ViewportSwizzle(src.swizzle.x),
  522. MaxwellToGL::ViewportSwizzle(src.swizzle.y),
  523. MaxwellToGL::ViewportSwizzle(src.swizzle.z),
  524. MaxwellToGL::ViewportSwizzle(src.swizzle.w));
  525. }
  526. }
  527. }
  528. void RasterizerOpenGL::SyncDepthClamp() {
  529. auto& flags = maxwell3d.dirty.flags;
  530. if (!flags[Dirty::DepthClampEnabled]) {
  531. return;
  532. }
  533. flags[Dirty::DepthClampEnabled] = false;
  534. oglEnable(GL_DEPTH_CLAMP, maxwell3d.regs.view_volume_clip_control.depth_clamp_disabled == 0);
  535. }
  536. void RasterizerOpenGL::SyncClipEnabled(u32 clip_mask) {
  537. auto& flags = maxwell3d.dirty.flags;
  538. if (!flags[Dirty::ClipDistances] && !flags[VideoCommon::Dirty::Shaders]) {
  539. return;
  540. }
  541. flags[Dirty::ClipDistances] = false;
  542. clip_mask &= maxwell3d.regs.clip_distance_enabled;
  543. if (clip_mask == last_clip_distance_mask) {
  544. return;
  545. }
  546. last_clip_distance_mask = clip_mask;
  547. for (std::size_t i = 0; i < Maxwell::Regs::NumClipDistances; ++i) {
  548. oglEnable(static_cast<GLenum>(GL_CLIP_DISTANCE0 + i), (clip_mask >> i) & 1);
  549. }
  550. }
  551. void RasterizerOpenGL::SyncClipCoef() {
  552. UNIMPLEMENTED();
  553. }
  554. void RasterizerOpenGL::SyncCullMode() {
  555. auto& flags = maxwell3d.dirty.flags;
  556. const auto& regs = maxwell3d.regs;
  557. if (flags[Dirty::CullTest]) {
  558. flags[Dirty::CullTest] = false;
  559. if (regs.cull_test_enabled) {
  560. glEnable(GL_CULL_FACE);
  561. glCullFace(MaxwellToGL::CullFace(regs.cull_face));
  562. } else {
  563. glDisable(GL_CULL_FACE);
  564. }
  565. }
  566. }
  567. void RasterizerOpenGL::SyncPrimitiveRestart() {
  568. auto& flags = maxwell3d.dirty.flags;
  569. if (!flags[Dirty::PrimitiveRestart]) {
  570. return;
  571. }
  572. flags[Dirty::PrimitiveRestart] = false;
  573. if (maxwell3d.regs.primitive_restart.enabled) {
  574. glEnable(GL_PRIMITIVE_RESTART);
  575. glPrimitiveRestartIndex(maxwell3d.regs.primitive_restart.index);
  576. } else {
  577. glDisable(GL_PRIMITIVE_RESTART);
  578. }
  579. }
  580. void RasterizerOpenGL::SyncDepthTestState() {
  581. auto& flags = maxwell3d.dirty.flags;
  582. const auto& regs = maxwell3d.regs;
  583. if (flags[Dirty::DepthMask]) {
  584. flags[Dirty::DepthMask] = false;
  585. glDepthMask(regs.depth_write_enabled ? GL_TRUE : GL_FALSE);
  586. }
  587. if (flags[Dirty::DepthTest]) {
  588. flags[Dirty::DepthTest] = false;
  589. if (regs.depth_test_enable) {
  590. glEnable(GL_DEPTH_TEST);
  591. glDepthFunc(MaxwellToGL::ComparisonOp(regs.depth_test_func));
  592. } else {
  593. glDisable(GL_DEPTH_TEST);
  594. }
  595. }
  596. }
  597. void RasterizerOpenGL::SyncStencilTestState() {
  598. auto& flags = maxwell3d.dirty.flags;
  599. if (!flags[Dirty::StencilTest]) {
  600. return;
  601. }
  602. flags[Dirty::StencilTest] = false;
  603. const auto& regs = maxwell3d.regs;
  604. oglEnable(GL_STENCIL_TEST, regs.stencil_enable);
  605. glStencilFuncSeparate(GL_FRONT, MaxwellToGL::ComparisonOp(regs.stencil_front_func_func),
  606. regs.stencil_front_func_ref, regs.stencil_front_func_mask);
  607. glStencilOpSeparate(GL_FRONT, MaxwellToGL::StencilOp(regs.stencil_front_op_fail),
  608. MaxwellToGL::StencilOp(regs.stencil_front_op_zfail),
  609. MaxwellToGL::StencilOp(regs.stencil_front_op_zpass));
  610. glStencilMaskSeparate(GL_FRONT, regs.stencil_front_mask);
  611. if (regs.stencil_two_side_enable) {
  612. glStencilFuncSeparate(GL_BACK, MaxwellToGL::ComparisonOp(regs.stencil_back_func_func),
  613. regs.stencil_back_func_ref, regs.stencil_back_func_mask);
  614. glStencilOpSeparate(GL_BACK, MaxwellToGL::StencilOp(regs.stencil_back_op_fail),
  615. MaxwellToGL::StencilOp(regs.stencil_back_op_zfail),
  616. MaxwellToGL::StencilOp(regs.stencil_back_op_zpass));
  617. glStencilMaskSeparate(GL_BACK, regs.stencil_back_mask);
  618. } else {
  619. glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 0, 0xFFFFFFFF);
  620. glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_KEEP);
  621. glStencilMaskSeparate(GL_BACK, 0xFFFFFFFF);
  622. }
  623. }
  624. void RasterizerOpenGL::SyncRasterizeEnable() {
  625. auto& flags = maxwell3d.dirty.flags;
  626. if (!flags[Dirty::RasterizeEnable]) {
  627. return;
  628. }
  629. flags[Dirty::RasterizeEnable] = false;
  630. oglEnable(GL_RASTERIZER_DISCARD, maxwell3d.regs.rasterize_enable == 0);
  631. }
  632. void RasterizerOpenGL::SyncPolygonModes() {
  633. auto& flags = maxwell3d.dirty.flags;
  634. if (!flags[Dirty::PolygonModes]) {
  635. return;
  636. }
  637. flags[Dirty::PolygonModes] = false;
  638. const auto& regs = maxwell3d.regs;
  639. if (regs.fill_rectangle) {
  640. if (!GLAD_GL_NV_fill_rectangle) {
  641. LOG_ERROR(Render_OpenGL, "GL_NV_fill_rectangle used and not supported");
  642. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  643. return;
  644. }
  645. flags[Dirty::PolygonModeFront] = true;
  646. flags[Dirty::PolygonModeBack] = true;
  647. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL_RECTANGLE_NV);
  648. return;
  649. }
  650. if (regs.polygon_mode_front == regs.polygon_mode_back) {
  651. flags[Dirty::PolygonModeFront] = false;
  652. flags[Dirty::PolygonModeBack] = false;
  653. glPolygonMode(GL_FRONT_AND_BACK, MaxwellToGL::PolygonMode(regs.polygon_mode_front));
  654. return;
  655. }
  656. if (flags[Dirty::PolygonModeFront]) {
  657. flags[Dirty::PolygonModeFront] = false;
  658. glPolygonMode(GL_FRONT, MaxwellToGL::PolygonMode(regs.polygon_mode_front));
  659. }
  660. if (flags[Dirty::PolygonModeBack]) {
  661. flags[Dirty::PolygonModeBack] = false;
  662. glPolygonMode(GL_BACK, MaxwellToGL::PolygonMode(regs.polygon_mode_back));
  663. }
  664. }
  665. void RasterizerOpenGL::SyncColorMask() {
  666. auto& flags = maxwell3d.dirty.flags;
  667. if (!flags[Dirty::ColorMasks]) {
  668. return;
  669. }
  670. flags[Dirty::ColorMasks] = false;
  671. const bool force = flags[Dirty::ColorMaskCommon];
  672. flags[Dirty::ColorMaskCommon] = false;
  673. const auto& regs = maxwell3d.regs;
  674. if (regs.color_mask_common) {
  675. if (!force && !flags[Dirty::ColorMask0]) {
  676. return;
  677. }
  678. flags[Dirty::ColorMask0] = false;
  679. auto& mask = regs.color_mask[0];
  680. glColorMask(mask.R != 0, mask.B != 0, mask.G != 0, mask.A != 0);
  681. return;
  682. }
  683. // Path without color_mask_common set
  684. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  685. if (!force && !flags[Dirty::ColorMask0 + i]) {
  686. continue;
  687. }
  688. flags[Dirty::ColorMask0 + i] = false;
  689. const auto& mask = regs.color_mask[i];
  690. glColorMaski(static_cast<GLuint>(i), mask.R != 0, mask.G != 0, mask.B != 0, mask.A != 0);
  691. }
  692. }
  693. void RasterizerOpenGL::SyncMultiSampleState() {
  694. auto& flags = maxwell3d.dirty.flags;
  695. if (!flags[Dirty::MultisampleControl]) {
  696. return;
  697. }
  698. flags[Dirty::MultisampleControl] = false;
  699. const auto& regs = maxwell3d.regs;
  700. oglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE, regs.multisample_control.alpha_to_coverage);
  701. oglEnable(GL_SAMPLE_ALPHA_TO_ONE, regs.multisample_control.alpha_to_one);
  702. }
  703. void RasterizerOpenGL::SyncFragmentColorClampState() {
  704. auto& flags = maxwell3d.dirty.flags;
  705. if (!flags[Dirty::FragmentClampColor]) {
  706. return;
  707. }
  708. flags[Dirty::FragmentClampColor] = false;
  709. glClampColor(GL_CLAMP_FRAGMENT_COLOR, maxwell3d.regs.frag_color_clamp ? GL_TRUE : GL_FALSE);
  710. }
  711. void RasterizerOpenGL::SyncBlendState() {
  712. auto& flags = maxwell3d.dirty.flags;
  713. const auto& regs = maxwell3d.regs;
  714. if (flags[Dirty::BlendColor]) {
  715. flags[Dirty::BlendColor] = false;
  716. glBlendColor(regs.blend_color.r, regs.blend_color.g, regs.blend_color.b,
  717. regs.blend_color.a);
  718. }
  719. // TODO(Rodrigo): Revisit blending, there are several registers we are not reading
  720. if (!flags[Dirty::BlendStates]) {
  721. return;
  722. }
  723. flags[Dirty::BlendStates] = false;
  724. if (!regs.independent_blend_enable) {
  725. if (!regs.blend.enable[0]) {
  726. glDisable(GL_BLEND);
  727. return;
  728. }
  729. glEnable(GL_BLEND);
  730. glBlendFuncSeparate(MaxwellToGL::BlendFunc(regs.blend.factor_source_rgb),
  731. MaxwellToGL::BlendFunc(regs.blend.factor_dest_rgb),
  732. MaxwellToGL::BlendFunc(regs.blend.factor_source_a),
  733. MaxwellToGL::BlendFunc(regs.blend.factor_dest_a));
  734. glBlendEquationSeparate(MaxwellToGL::BlendEquation(regs.blend.equation_rgb),
  735. MaxwellToGL::BlendEquation(regs.blend.equation_a));
  736. return;
  737. }
  738. const bool force = flags[Dirty::BlendIndependentEnabled];
  739. flags[Dirty::BlendIndependentEnabled] = false;
  740. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  741. if (!force && !flags[Dirty::BlendState0 + i]) {
  742. continue;
  743. }
  744. flags[Dirty::BlendState0 + i] = false;
  745. if (!regs.blend.enable[i]) {
  746. glDisablei(GL_BLEND, static_cast<GLuint>(i));
  747. continue;
  748. }
  749. glEnablei(GL_BLEND, static_cast<GLuint>(i));
  750. const auto& src = regs.independent_blend[i];
  751. glBlendFuncSeparatei(static_cast<GLuint>(i), MaxwellToGL::BlendFunc(src.factor_source_rgb),
  752. MaxwellToGL::BlendFunc(src.factor_dest_rgb),
  753. MaxwellToGL::BlendFunc(src.factor_source_a),
  754. MaxwellToGL::BlendFunc(src.factor_dest_a));
  755. glBlendEquationSeparatei(static_cast<GLuint>(i),
  756. MaxwellToGL::BlendEquation(src.equation_rgb),
  757. MaxwellToGL::BlendEquation(src.equation_a));
  758. }
  759. }
  760. void RasterizerOpenGL::SyncLogicOpState() {
  761. auto& flags = maxwell3d.dirty.flags;
  762. if (!flags[Dirty::LogicOp]) {
  763. return;
  764. }
  765. flags[Dirty::LogicOp] = false;
  766. const auto& regs = maxwell3d.regs;
  767. if (regs.logic_op.enable) {
  768. glEnable(GL_COLOR_LOGIC_OP);
  769. glLogicOp(MaxwellToGL::LogicOp(regs.logic_op.operation));
  770. } else {
  771. glDisable(GL_COLOR_LOGIC_OP);
  772. }
  773. }
  774. void RasterizerOpenGL::SyncScissorTest() {
  775. auto& flags = maxwell3d.dirty.flags;
  776. if (!flags[Dirty::Scissors]) {
  777. return;
  778. }
  779. flags[Dirty::Scissors] = false;
  780. const auto& regs = maxwell3d.regs;
  781. for (std::size_t index = 0; index < Maxwell::NumViewports; ++index) {
  782. if (!flags[Dirty::Scissor0 + index]) {
  783. continue;
  784. }
  785. flags[Dirty::Scissor0 + index] = false;
  786. const auto& src = regs.scissor_test[index];
  787. if (src.enable) {
  788. glEnablei(GL_SCISSOR_TEST, static_cast<GLuint>(index));
  789. glScissorIndexed(static_cast<GLuint>(index), src.min_x, src.min_y,
  790. src.max_x - src.min_x, src.max_y - src.min_y);
  791. } else {
  792. glDisablei(GL_SCISSOR_TEST, static_cast<GLuint>(index));
  793. }
  794. }
  795. }
  796. void RasterizerOpenGL::SyncPointState() {
  797. auto& flags = maxwell3d.dirty.flags;
  798. if (!flags[Dirty::PointSize]) {
  799. return;
  800. }
  801. flags[Dirty::PointSize] = false;
  802. oglEnable(GL_POINT_SPRITE, maxwell3d.regs.point_sprite_enable);
  803. oglEnable(GL_PROGRAM_POINT_SIZE, maxwell3d.regs.vp_point_size.enable);
  804. glPointSize(std::max(1.0f, maxwell3d.regs.point_size));
  805. }
  806. void RasterizerOpenGL::SyncLineState() {
  807. auto& flags = maxwell3d.dirty.flags;
  808. if (!flags[Dirty::LineWidth]) {
  809. return;
  810. }
  811. flags[Dirty::LineWidth] = false;
  812. const auto& regs = maxwell3d.regs;
  813. oglEnable(GL_LINE_SMOOTH, regs.line_smooth_enable);
  814. glLineWidth(regs.line_smooth_enable ? regs.line_width_smooth : regs.line_width_aliased);
  815. }
  816. void RasterizerOpenGL::SyncPolygonOffset() {
  817. auto& flags = maxwell3d.dirty.flags;
  818. if (!flags[Dirty::PolygonOffset]) {
  819. return;
  820. }
  821. flags[Dirty::PolygonOffset] = false;
  822. const auto& regs = maxwell3d.regs;
  823. oglEnable(GL_POLYGON_OFFSET_FILL, regs.polygon_offset_fill_enable);
  824. oglEnable(GL_POLYGON_OFFSET_LINE, regs.polygon_offset_line_enable);
  825. oglEnable(GL_POLYGON_OFFSET_POINT, regs.polygon_offset_point_enable);
  826. if (regs.polygon_offset_fill_enable || regs.polygon_offset_line_enable ||
  827. regs.polygon_offset_point_enable) {
  828. // Hardware divides polygon offset units by two
  829. glPolygonOffsetClamp(regs.polygon_offset_factor, regs.polygon_offset_units / 2.0f,
  830. regs.polygon_offset_clamp);
  831. }
  832. }
  833. void RasterizerOpenGL::SyncAlphaTest() {
  834. auto& flags = maxwell3d.dirty.flags;
  835. if (!flags[Dirty::AlphaTest]) {
  836. return;
  837. }
  838. flags[Dirty::AlphaTest] = false;
  839. const auto& regs = maxwell3d.regs;
  840. if (regs.alpha_test_enabled) {
  841. glEnable(GL_ALPHA_TEST);
  842. glAlphaFunc(MaxwellToGL::ComparisonOp(regs.alpha_test_func), regs.alpha_test_ref);
  843. } else {
  844. glDisable(GL_ALPHA_TEST);
  845. }
  846. }
  847. void RasterizerOpenGL::SyncFramebufferSRGB() {
  848. auto& flags = maxwell3d.dirty.flags;
  849. if (!flags[Dirty::FramebufferSRGB]) {
  850. return;
  851. }
  852. flags[Dirty::FramebufferSRGB] = false;
  853. oglEnable(GL_FRAMEBUFFER_SRGB, maxwell3d.regs.framebuffer_srgb);
  854. }
  855. void RasterizerOpenGL::BeginTransformFeedback(GraphicsProgram* program, GLenum primitive_mode) {
  856. const auto& regs = maxwell3d.regs;
  857. if (regs.tfb_enabled == 0) {
  858. return;
  859. }
  860. program->ConfigureTransformFeedback();
  861. UNIMPLEMENTED_IF(regs.IsShaderConfigEnabled(Maxwell::ShaderProgram::TesselationControl) ||
  862. regs.IsShaderConfigEnabled(Maxwell::ShaderProgram::TesselationEval) ||
  863. regs.IsShaderConfigEnabled(Maxwell::ShaderProgram::Geometry));
  864. UNIMPLEMENTED_IF(primitive_mode != GL_POINTS);
  865. // We may have to call BeginTransformFeedbackNV here since they seem to call different
  866. // implementations on Nvidia's driver (the pointer is different) but we are using
  867. // ARB_transform_feedback3 features with NV_transform_feedback interactions and the ARB
  868. // extension doesn't define BeginTransformFeedback (without NV) interactions. It just works.
  869. glBeginTransformFeedback(GL_POINTS);
  870. }
  871. void RasterizerOpenGL::EndTransformFeedback() {
  872. if (maxwell3d.regs.tfb_enabled != 0) {
  873. glEndTransformFeedback();
  874. }
  875. }
  876. AccelerateDMA::AccelerateDMA(BufferCache& buffer_cache_) : buffer_cache{buffer_cache_} {}
  877. bool AccelerateDMA::BufferCopy(GPUVAddr src_address, GPUVAddr dest_address, u64 amount) {
  878. std::scoped_lock lock{buffer_cache.mutex};
  879. return buffer_cache.DMACopy(src_address, dest_address, amount);
  880. }
  881. bool AccelerateDMA::BufferClear(GPUVAddr src_address, u64 amount, u32 value) {
  882. std::scoped_lock lock{buffer_cache.mutex};
  883. return buffer_cache.DMAClear(src_address, amount, value);
  884. }
  885. } // namespace OpenGL