gl_rasterizer.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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/assert.h"
  14. #include "common/logging/log.h"
  15. #include "common/math_util.h"
  16. #include "common/microprofile.h"
  17. #include "common/settings.h"
  18. #include "core/memory.h"
  19. #include "video_core/engines/kepler_compute.h"
  20. #include "video_core/engines/maxwell_3d.h"
  21. #include "video_core/memory_manager.h"
  22. #include "video_core/renderer_opengl/gl_device.h"
  23. #include "video_core/renderer_opengl/gl_query_cache.h"
  24. #include "video_core/renderer_opengl/gl_rasterizer.h"
  25. #include "video_core/renderer_opengl/gl_shader_cache.h"
  26. #include "video_core/renderer_opengl/gl_texture_cache.h"
  27. #include "video_core/renderer_opengl/maxwell_to_gl.h"
  28. #include "video_core/renderer_opengl/renderer_opengl.h"
  29. #include "video_core/shader_cache.h"
  30. #include "video_core/texture_cache/texture_cache_base.h"
  31. namespace OpenGL {
  32. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  33. using GLvec4 = std::array<GLfloat, 4>;
  34. using VideoCore::Surface::PixelFormat;
  35. using VideoCore::Surface::SurfaceTarget;
  36. using VideoCore::Surface::SurfaceType;
  37. MICROPROFILE_DEFINE(OpenGL_Drawing, "OpenGL", "Drawing", MP_RGB(128, 128, 192));
  38. MICROPROFILE_DEFINE(OpenGL_Clears, "OpenGL", "Clears", MP_RGB(128, 128, 192));
  39. MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(128, 128, 192));
  40. MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Management", MP_RGB(100, 255, 100));
  41. namespace {
  42. constexpr size_t NUM_SUPPORTED_VERTEX_ATTRIBUTES = 16;
  43. void oglEnable(GLenum cap, bool state) {
  44. (state ? glEnable : glDisable)(cap);
  45. }
  46. } // Anonymous namespace
  47. RasterizerOpenGL::RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  48. Core::Memory::Memory& cpu_memory_, const Device& device_,
  49. ScreenInfo& screen_info_, ProgramManager& program_manager_,
  50. StateTracker& state_tracker_)
  51. : RasterizerAccelerated(cpu_memory_), gpu(gpu_), maxwell3d(gpu.Maxwell3D()),
  52. kepler_compute(gpu.KeplerCompute()), gpu_memory(gpu.MemoryManager()), device(device_),
  53. screen_info(screen_info_), program_manager(program_manager_), state_tracker(state_tracker_),
  54. texture_cache_runtime(device, program_manager, state_tracker),
  55. texture_cache(texture_cache_runtime, *this, maxwell3d, kepler_compute, gpu_memory),
  56. buffer_cache_runtime(device),
  57. buffer_cache(*this, maxwell3d, kepler_compute, gpu_memory, cpu_memory_, buffer_cache_runtime),
  58. shader_cache(*this, emu_window_, maxwell3d, kepler_compute, gpu_memory, device, texture_cache,
  59. buffer_cache, program_manager, state_tracker, gpu.ShaderNotify()),
  60. query_cache(*this, maxwell3d, gpu_memory), accelerate_dma(buffer_cache),
  61. fence_manager(*this, gpu, texture_cache, buffer_cache, query_cache) {}
  62. RasterizerOpenGL::~RasterizerOpenGL() = default;
  63. void RasterizerOpenGL::SyncVertexFormats() {
  64. auto& flags = maxwell3d.dirty.flags;
  65. if (!flags[Dirty::VertexFormats]) {
  66. return;
  67. }
  68. flags[Dirty::VertexFormats] = false;
  69. // Use the vertex array as-is, assumes that the data is formatted correctly for OpenGL. Enables
  70. // the first 16 vertex attributes always, as we don't know which ones are actually used until
  71. // shader time. Note, Tegra technically supports 32, but we're capping this to 16 for now to
  72. // avoid OpenGL errors.
  73. // TODO(Subv): Analyze the shader to identify which attributes are actually used and don't
  74. // assume every shader uses them all.
  75. for (std::size_t index = 0; index < NUM_SUPPORTED_VERTEX_ATTRIBUTES; ++index) {
  76. if (!flags[Dirty::VertexFormat0 + index]) {
  77. continue;
  78. }
  79. flags[Dirty::VertexFormat0 + index] = false;
  80. const auto attrib = maxwell3d.regs.vertex_attrib_format[index];
  81. const auto gl_index = static_cast<GLuint>(index);
  82. // Disable constant attributes.
  83. if (attrib.constant) {
  84. glDisableVertexAttribArray(gl_index);
  85. continue;
  86. }
  87. glEnableVertexAttribArray(gl_index);
  88. if (attrib.type == Maxwell::VertexAttribute::Type::SignedInt ||
  89. attrib.type == Maxwell::VertexAttribute::Type::UnsignedInt) {
  90. glVertexAttribIFormat(gl_index, attrib.ComponentCount(),
  91. MaxwellToGL::VertexFormat(attrib), attrib.offset);
  92. } else {
  93. glVertexAttribFormat(gl_index, attrib.ComponentCount(),
  94. MaxwellToGL::VertexFormat(attrib),
  95. attrib.IsNormalized() ? GL_TRUE : GL_FALSE, attrib.offset);
  96. }
  97. glVertexAttribBinding(gl_index, attrib.buffer);
  98. }
  99. }
  100. void RasterizerOpenGL::SyncVertexInstances() {
  101. auto& flags = maxwell3d.dirty.flags;
  102. if (!flags[Dirty::VertexInstances]) {
  103. return;
  104. }
  105. flags[Dirty::VertexInstances] = false;
  106. const auto& regs = maxwell3d.regs;
  107. for (std::size_t index = 0; index < NUM_SUPPORTED_VERTEX_ATTRIBUTES; ++index) {
  108. if (!flags[Dirty::VertexInstance0 + index]) {
  109. continue;
  110. }
  111. flags[Dirty::VertexInstance0 + index] = false;
  112. const auto gl_index = static_cast<GLuint>(index);
  113. const bool instancing_enabled = regs.instanced_arrays.IsInstancingEnabled(gl_index);
  114. const GLuint divisor = instancing_enabled ? regs.vertex_array[index].divisor : 0;
  115. glVertexBindingDivisor(gl_index, divisor);
  116. }
  117. }
  118. void RasterizerOpenGL::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
  119. const VideoCore::DiskResourceLoadCallback& callback) {
  120. shader_cache.LoadDiskResources(title_id, stop_loading, callback);
  121. }
  122. void RasterizerOpenGL::Clear() {
  123. MICROPROFILE_SCOPE(OpenGL_Clears);
  124. if (!maxwell3d.ShouldExecute()) {
  125. return;
  126. }
  127. const auto& regs = maxwell3d.regs;
  128. bool use_color{};
  129. bool use_depth{};
  130. bool use_stencil{};
  131. if (regs.clear_buffers.R || regs.clear_buffers.G || regs.clear_buffers.B ||
  132. regs.clear_buffers.A) {
  133. use_color = true;
  134. const GLuint index = regs.clear_buffers.RT;
  135. state_tracker.NotifyColorMask(index);
  136. glColorMaski(index, regs.clear_buffers.R != 0, regs.clear_buffers.G != 0,
  137. regs.clear_buffers.B != 0, regs.clear_buffers.A != 0);
  138. // TODO(Rodrigo): Determine if clamping is used on clears
  139. SyncFragmentColorClampState();
  140. SyncFramebufferSRGB();
  141. }
  142. if (regs.clear_buffers.Z) {
  143. ASSERT_MSG(regs.zeta_enable != 0, "Tried to clear Z but buffer is not enabled!");
  144. use_depth = true;
  145. state_tracker.NotifyDepthMask();
  146. glDepthMask(GL_TRUE);
  147. }
  148. if (regs.clear_buffers.S) {
  149. ASSERT_MSG(regs.zeta_enable, "Tried to clear stencil but buffer is not enabled!");
  150. use_stencil = true;
  151. }
  152. if (!use_color && !use_depth && !use_stencil) {
  153. // No color surface nor depth/stencil surface are enabled
  154. return;
  155. }
  156. SyncRasterizeEnable();
  157. SyncStencilTestState();
  158. if (regs.clear_flags.scissor) {
  159. SyncScissorTest();
  160. } else {
  161. state_tracker.NotifyScissor0();
  162. glDisablei(GL_SCISSOR_TEST, 0);
  163. }
  164. UNIMPLEMENTED_IF(regs.clear_flags.viewport);
  165. std::scoped_lock lock{texture_cache.mutex};
  166. texture_cache.UpdateRenderTargets(true);
  167. state_tracker.BindFramebuffer(texture_cache.GetFramebuffer()->Handle());
  168. if (use_color) {
  169. glClearBufferfv(GL_COLOR, regs.clear_buffers.RT, regs.clear_color);
  170. }
  171. if (use_depth && use_stencil) {
  172. glClearBufferfi(GL_DEPTH_STENCIL, 0, regs.clear_depth, regs.clear_stencil);
  173. } else if (use_depth) {
  174. glClearBufferfv(GL_DEPTH, 0, &regs.clear_depth);
  175. } else if (use_stencil) {
  176. glClearBufferiv(GL_STENCIL, 0, &regs.clear_stencil);
  177. }
  178. ++num_queued_commands;
  179. }
  180. void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) {
  181. MICROPROFILE_SCOPE(OpenGL_Drawing);
  182. query_cache.UpdateCounters();
  183. SyncState();
  184. GraphicsPipeline* const pipeline{shader_cache.CurrentGraphicsPipeline()};
  185. if (!pipeline) {
  186. return;
  187. }
  188. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  189. pipeline->Configure(is_indexed);
  190. const GLenum primitive_mode = MaxwellToGL::PrimitiveTopology(maxwell3d.regs.draw.topology);
  191. BeginTransformFeedback(pipeline, primitive_mode);
  192. const GLuint base_instance = static_cast<GLuint>(maxwell3d.regs.vb_base_instance);
  193. const GLsizei num_instances =
  194. static_cast<GLsizei>(is_instanced ? maxwell3d.mme_draw.instance_count : 1);
  195. if (is_indexed) {
  196. const GLint base_vertex = static_cast<GLint>(maxwell3d.regs.vb_element_base);
  197. const GLsizei num_vertices = static_cast<GLsizei>(maxwell3d.regs.index_array.count);
  198. const GLvoid* const offset = buffer_cache_runtime.IndexOffset();
  199. const GLenum format = MaxwellToGL::IndexFormat(maxwell3d.regs.index_array.format);
  200. if (num_instances == 1 && base_instance == 0 && base_vertex == 0) {
  201. glDrawElements(primitive_mode, num_vertices, format, offset);
  202. } else if (num_instances == 1 && base_instance == 0) {
  203. glDrawElementsBaseVertex(primitive_mode, num_vertices, format, offset, base_vertex);
  204. } else if (base_vertex == 0 && base_instance == 0) {
  205. glDrawElementsInstanced(primitive_mode, num_vertices, format, offset, num_instances);
  206. } else if (base_vertex == 0) {
  207. glDrawElementsInstancedBaseInstance(primitive_mode, num_vertices, format, offset,
  208. num_instances, base_instance);
  209. } else if (base_instance == 0) {
  210. glDrawElementsInstancedBaseVertex(primitive_mode, num_vertices, format, offset,
  211. num_instances, base_vertex);
  212. } else {
  213. glDrawElementsInstancedBaseVertexBaseInstance(primitive_mode, num_vertices, format,
  214. offset, num_instances, base_vertex,
  215. base_instance);
  216. }
  217. } else {
  218. const GLint base_vertex = static_cast<GLint>(maxwell3d.regs.vertex_buffer.first);
  219. const GLsizei num_vertices = static_cast<GLsizei>(maxwell3d.regs.vertex_buffer.count);
  220. if (num_instances == 1 && base_instance == 0) {
  221. glDrawArrays(primitive_mode, base_vertex, num_vertices);
  222. } else if (base_instance == 0) {
  223. glDrawArraysInstanced(primitive_mode, base_vertex, num_vertices, num_instances);
  224. } else {
  225. glDrawArraysInstancedBaseInstance(primitive_mode, base_vertex, num_vertices,
  226. num_instances, base_instance);
  227. }
  228. }
  229. EndTransformFeedback();
  230. ++num_queued_commands;
  231. has_written_global_memory |= pipeline->WritesGlobalMemory();
  232. gpu.TickWork();
  233. }
  234. void RasterizerOpenGL::DispatchCompute() {
  235. ComputePipeline* const pipeline{shader_cache.CurrentComputePipeline()};
  236. if (!pipeline) {
  237. return;
  238. }
  239. pipeline->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. has_written_global_memory |= pipeline->WritesGlobalMemory();
  244. }
  245. void RasterizerOpenGL::ResetCounter(VideoCore::QueryType type) {
  246. query_cache.ResetCounter(type);
  247. }
  248. void RasterizerOpenGL::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
  249. std::optional<u64> timestamp) {
  250. query_cache.Query(gpu_addr, type, timestamp);
  251. }
  252. void RasterizerOpenGL::BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr,
  253. u32 size) {
  254. std::scoped_lock lock{buffer_cache.mutex};
  255. buffer_cache.BindGraphicsUniformBuffer(stage, index, gpu_addr, size);
  256. }
  257. void RasterizerOpenGL::DisableGraphicsUniformBuffer(size_t stage, u32 index) {
  258. buffer_cache.DisableGraphicsUniformBuffer(stage, index);
  259. }
  260. void RasterizerOpenGL::FlushAll() {}
  261. void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
  262. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  263. if (addr == 0 || size == 0) {
  264. return;
  265. }
  266. {
  267. std::scoped_lock lock{texture_cache.mutex};
  268. texture_cache.DownloadMemory(addr, size);
  269. }
  270. {
  271. std::scoped_lock lock{buffer_cache.mutex};
  272. buffer_cache.DownloadMemory(addr, size);
  273. }
  274. query_cache.FlushRegion(addr, size);
  275. }
  276. bool RasterizerOpenGL::MustFlushRegion(VAddr addr, u64 size) {
  277. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  278. if (!Settings::IsGPULevelHigh()) {
  279. return buffer_cache.IsRegionGpuModified(addr, size);
  280. }
  281. return texture_cache.IsRegionGpuModified(addr, size) ||
  282. buffer_cache.IsRegionGpuModified(addr, size);
  283. }
  284. void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
  285. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  286. if (addr == 0 || size == 0) {
  287. return;
  288. }
  289. {
  290. std::scoped_lock lock{texture_cache.mutex};
  291. texture_cache.WriteMemory(addr, size);
  292. }
  293. {
  294. std::scoped_lock lock{buffer_cache.mutex};
  295. buffer_cache.WriteMemory(addr, size);
  296. }
  297. shader_cache.InvalidateRegion(addr, size);
  298. query_cache.InvalidateRegion(addr, size);
  299. }
  300. void RasterizerOpenGL::OnCPUWrite(VAddr addr, u64 size) {
  301. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  302. if (addr == 0 || size == 0) {
  303. return;
  304. }
  305. shader_cache.OnCPUWrite(addr, size);
  306. {
  307. std::scoped_lock lock{texture_cache.mutex};
  308. texture_cache.WriteMemory(addr, size);
  309. }
  310. {
  311. std::scoped_lock lock{buffer_cache.mutex};
  312. buffer_cache.CachedWriteMemory(addr, size);
  313. }
  314. }
  315. void RasterizerOpenGL::SyncGuestHost() {
  316. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  317. shader_cache.SyncGuestHost();
  318. {
  319. std::scoped_lock lock{buffer_cache.mutex};
  320. buffer_cache.FlushCachedWrites();
  321. }
  322. }
  323. void RasterizerOpenGL::UnmapMemory(VAddr addr, u64 size) {
  324. {
  325. std::scoped_lock lock{texture_cache.mutex};
  326. texture_cache.UnmapMemory(addr, size);
  327. }
  328. {
  329. std::scoped_lock lock{buffer_cache.mutex};
  330. buffer_cache.WriteMemory(addr, size);
  331. }
  332. shader_cache.OnCPUWrite(addr, size);
  333. }
  334. void RasterizerOpenGL::ModifyGPUMemory(GPUVAddr addr, u64 size) {
  335. {
  336. std::scoped_lock lock{texture_cache.mutex};
  337. texture_cache.UnmapGPUMemory(addr, size);
  338. }
  339. }
  340. void RasterizerOpenGL::SignalSemaphore(GPUVAddr addr, u32 value) {
  341. if (!gpu.IsAsync()) {
  342. gpu_memory.Write<u32>(addr, value);
  343. return;
  344. }
  345. fence_manager.SignalSemaphore(addr, value);
  346. }
  347. void RasterizerOpenGL::SignalSyncPoint(u32 value) {
  348. if (!gpu.IsAsync()) {
  349. gpu.IncrementSyncPoint(value);
  350. return;
  351. }
  352. fence_manager.SignalSyncPoint(value);
  353. }
  354. void RasterizerOpenGL::SignalReference() {
  355. if (!gpu.IsAsync()) {
  356. return;
  357. }
  358. fence_manager.SignalOrdering();
  359. }
  360. void RasterizerOpenGL::ReleaseFences() {
  361. if (!gpu.IsAsync()) {
  362. return;
  363. }
  364. fence_manager.WaitPendingFences();
  365. }
  366. void RasterizerOpenGL::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  367. if (Settings::IsGPULevelExtreme()) {
  368. FlushRegion(addr, size);
  369. }
  370. InvalidateRegion(addr, size);
  371. }
  372. void RasterizerOpenGL::WaitForIdle() {
  373. glMemoryBarrier(GL_ALL_BARRIER_BITS);
  374. SignalReference();
  375. }
  376. void RasterizerOpenGL::FragmentBarrier() {
  377. glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT);
  378. }
  379. void RasterizerOpenGL::TiledCacheBarrier() {
  380. glTextureBarrier();
  381. }
  382. void RasterizerOpenGL::FlushCommands() {
  383. // Only flush when we have commands queued to OpenGL.
  384. if (num_queued_commands == 0) {
  385. return;
  386. }
  387. num_queued_commands = 0;
  388. // Make sure memory stored from the previous GL command stream is visible
  389. // This is only needed on assembly shaders where we write to GPU memory with raw pointers
  390. if (has_written_global_memory) {
  391. has_written_global_memory = false;
  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(GraphicsPipeline* 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