gl_rasterizer.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. // SPDX-FileCopyrightText: 2015 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include <bitset>
  6. #include <memory>
  7. #include <string_view>
  8. #include <utility>
  9. #include <glad/glad.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/settings.h"
  16. #include "video_core/control/channel_state.h"
  17. #include "video_core/engines/kepler_compute.h"
  18. #include "video_core/engines/maxwell_3d.h"
  19. #include "video_core/memory_manager.h"
  20. #include "video_core/renderer_opengl/gl_device.h"
  21. #include "video_core/renderer_opengl/gl_query_cache.h"
  22. #include "video_core/renderer_opengl/gl_rasterizer.h"
  23. #include "video_core/renderer_opengl/gl_shader_cache.h"
  24. #include "video_core/renderer_opengl/gl_texture_cache.h"
  25. #include "video_core/renderer_opengl/maxwell_to_gl.h"
  26. #include "video_core/renderer_opengl/renderer_opengl.h"
  27. #include "video_core/shader_cache.h"
  28. #include "video_core/texture_cache/texture_cache_base.h"
  29. namespace OpenGL {
  30. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  31. using GLvec4 = std::array<GLfloat, 4>;
  32. using VideoCore::Surface::PixelFormat;
  33. using VideoCore::Surface::SurfaceTarget;
  34. using VideoCore::Surface::SurfaceType;
  35. MICROPROFILE_DEFINE(OpenGL_Drawing, "OpenGL", "Drawing", MP_RGB(128, 128, 192));
  36. MICROPROFILE_DEFINE(OpenGL_Clears, "OpenGL", "Clears", MP_RGB(128, 128, 192));
  37. MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(128, 128, 192));
  38. MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Management", MP_RGB(100, 255, 100));
  39. namespace {
  40. constexpr size_t NUM_SUPPORTED_VERTEX_ATTRIBUTES = 16;
  41. void oglEnable(GLenum cap, bool state) {
  42. (state ? glEnable : glDisable)(cap);
  43. }
  44. } // Anonymous namespace
  45. RasterizerOpenGL::RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  46. Core::Memory::Memory& cpu_memory_, const Device& device_,
  47. ScreenInfo& screen_info_, ProgramManager& program_manager_,
  48. StateTracker& state_tracker_)
  49. : RasterizerAccelerated(cpu_memory_), gpu(gpu_), device(device_), screen_info(screen_info_),
  50. program_manager(program_manager_), state_tracker(state_tracker_),
  51. texture_cache_runtime(device, program_manager, state_tracker),
  52. texture_cache(texture_cache_runtime, *this), buffer_cache_runtime(device),
  53. buffer_cache(*this, cpu_memory_, buffer_cache_runtime),
  54. shader_cache(*this, emu_window_, device, texture_cache, buffer_cache, program_manager,
  55. state_tracker, gpu.ShaderNotify()),
  56. query_cache(*this), accelerate_dma(buffer_cache),
  57. fence_manager(*this, gpu, texture_cache, buffer_cache, query_cache) {}
  58. RasterizerOpenGL::~RasterizerOpenGL() = default;
  59. void RasterizerOpenGL::SyncVertexFormats() {
  60. auto& flags = maxwell3d->dirty.flags;
  61. if (!flags[Dirty::VertexFormats]) {
  62. return;
  63. }
  64. flags[Dirty::VertexFormats] = false;
  65. // Use the vertex array as-is, assumes that the data is formatted correctly for OpenGL. Enables
  66. // the first 16 vertex attributes always, as we don't know which ones are actually used until
  67. // shader time. Note, Tegra technically supports 32, but we're capping this to 16 for now to
  68. // avoid OpenGL errors.
  69. // TODO(Subv): Analyze the shader to identify which attributes are actually used and don't
  70. // assume every shader uses them all.
  71. for (std::size_t index = 0; index < NUM_SUPPORTED_VERTEX_ATTRIBUTES; ++index) {
  72. if (!flags[Dirty::VertexFormat0 + index]) {
  73. continue;
  74. }
  75. flags[Dirty::VertexFormat0 + index] = false;
  76. const auto& attrib = maxwell3d->regs.vertex_attrib_format[index];
  77. const auto gl_index = static_cast<GLuint>(index);
  78. // Disable constant attributes.
  79. if (attrib.constant) {
  80. glDisableVertexAttribArray(gl_index);
  81. continue;
  82. }
  83. glEnableVertexAttribArray(gl_index);
  84. if (attrib.type == Maxwell::VertexAttribute::Type::SInt ||
  85. attrib.type == Maxwell::VertexAttribute::Type::UInt) {
  86. glVertexAttribIFormat(gl_index, attrib.ComponentCount(),
  87. MaxwellToGL::VertexFormat(attrib), attrib.offset);
  88. } else {
  89. glVertexAttribFormat(gl_index, attrib.ComponentCount(),
  90. MaxwellToGL::VertexFormat(attrib),
  91. attrib.IsNormalized() ? GL_TRUE : GL_FALSE, attrib.offset);
  92. }
  93. glVertexAttribBinding(gl_index, attrib.buffer);
  94. }
  95. }
  96. void RasterizerOpenGL::SyncVertexInstances() {
  97. auto& flags = maxwell3d->dirty.flags;
  98. if (!flags[Dirty::VertexInstances]) {
  99. return;
  100. }
  101. flags[Dirty::VertexInstances] = false;
  102. const auto& regs = maxwell3d->regs;
  103. for (std::size_t index = 0; index < NUM_SUPPORTED_VERTEX_ATTRIBUTES; ++index) {
  104. if (!flags[Dirty::VertexInstance0 + index]) {
  105. continue;
  106. }
  107. flags[Dirty::VertexInstance0 + index] = false;
  108. const auto gl_index = static_cast<GLuint>(index);
  109. const bool instancing_enabled = regs.vertex_stream_instances.IsInstancingEnabled(gl_index);
  110. const GLuint divisor = instancing_enabled ? regs.vertex_streams[index].frequency : 0;
  111. glVertexBindingDivisor(gl_index, divisor);
  112. }
  113. }
  114. void RasterizerOpenGL::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
  115. const VideoCore::DiskResourceLoadCallback& callback) {
  116. shader_cache.LoadDiskResources(title_id, stop_loading, callback);
  117. }
  118. void RasterizerOpenGL::Clear(u32 layer_count) {
  119. MICROPROFILE_SCOPE(OpenGL_Clears);
  120. if (!maxwell3d->ShouldExecute()) {
  121. return;
  122. }
  123. const auto& regs = maxwell3d->regs;
  124. bool use_color{};
  125. bool use_depth{};
  126. bool use_stencil{};
  127. if (regs.clear_surface.R || regs.clear_surface.G || regs.clear_surface.B ||
  128. regs.clear_surface.A) {
  129. use_color = true;
  130. const GLuint index = regs.clear_surface.RT;
  131. state_tracker.NotifyColorMask(index);
  132. glColorMaski(index, regs.clear_surface.R != 0, regs.clear_surface.G != 0,
  133. regs.clear_surface.B != 0, regs.clear_surface.A != 0);
  134. // TODO(Rodrigo): Determine if clamping is used on clears
  135. SyncFragmentColorClampState();
  136. SyncFramebufferSRGB();
  137. }
  138. if (regs.clear_surface.Z) {
  139. ASSERT_MSG(regs.zeta_enable != 0, "Tried to clear Z but buffer is not enabled!");
  140. use_depth = true;
  141. state_tracker.NotifyDepthMask();
  142. glDepthMask(GL_TRUE);
  143. }
  144. if (regs.clear_surface.S) {
  145. ASSERT_MSG(regs.zeta_enable, "Tried to clear stencil but buffer is not enabled!");
  146. use_stencil = true;
  147. }
  148. if (!use_color && !use_depth && !use_stencil) {
  149. // No color surface nor depth/stencil surface are enabled
  150. return;
  151. }
  152. SyncRasterizeEnable();
  153. SyncStencilTestState();
  154. std::scoped_lock lock{texture_cache.mutex};
  155. texture_cache.UpdateRenderTargets(true);
  156. state_tracker.BindFramebuffer(texture_cache.GetFramebuffer()->Handle());
  157. SyncViewport();
  158. if (regs.clear_control.use_scissor) {
  159. SyncScissorTest();
  160. } else {
  161. state_tracker.NotifyScissor0();
  162. glDisablei(GL_SCISSOR_TEST, 0);
  163. }
  164. UNIMPLEMENTED_IF(regs.clear_control.use_viewport_clip0);
  165. if (use_color) {
  166. glClearBufferfv(GL_COLOR, regs.clear_surface.RT, regs.clear_color.data());
  167. }
  168. if (use_depth && use_stencil) {
  169. glClearBufferfi(GL_DEPTH_STENCIL, 0, regs.clear_depth, regs.clear_stencil);
  170. } else if (use_depth) {
  171. glClearBufferfv(GL_DEPTH, 0, &regs.clear_depth);
  172. } else if (use_stencil) {
  173. glClearBufferiv(GL_STENCIL, 0, &regs.clear_stencil);
  174. }
  175. ++num_queued_commands;
  176. }
  177. void RasterizerOpenGL::Draw(bool is_indexed, u32 instance_count) {
  178. MICROPROFILE_SCOPE(OpenGL_Drawing);
  179. SCOPE_EXIT({ gpu.TickWork(); });
  180. query_cache.UpdateCounters();
  181. GraphicsPipeline* const pipeline{shader_cache.CurrentGraphicsPipeline()};
  182. if (!pipeline) {
  183. return;
  184. }
  185. gpu.TickWork();
  186. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  187. pipeline->SetEngine(maxwell3d, gpu_memory);
  188. pipeline->Configure(is_indexed);
  189. BindInlineIndexBuffer();
  190. SyncState();
  191. const GLenum primitive_mode = MaxwellToGL::PrimitiveTopology(maxwell3d->regs.draw.topology);
  192. BeginTransformFeedback(pipeline, primitive_mode);
  193. const GLuint base_instance = static_cast<GLuint>(maxwell3d->regs.global_base_instance_index);
  194. const GLsizei num_instances = static_cast<GLsizei>(instance_count);
  195. if (is_indexed) {
  196. const GLint base_vertex = static_cast<GLint>(maxwell3d->regs.global_base_vertex_index);
  197. const GLsizei num_vertices = static_cast<GLsizei>(maxwell3d->regs.index_buffer.count);
  198. const GLvoid* const offset = buffer_cache_runtime.IndexOffset();
  199. const GLenum format = MaxwellToGL::IndexFormat(maxwell3d->regs.index_buffer.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. }
  233. void RasterizerOpenGL::DispatchCompute() {
  234. ComputePipeline* const pipeline{shader_cache.CurrentComputePipeline()};
  235. if (!pipeline) {
  236. return;
  237. }
  238. pipeline->SetEngine(kepler_compute, gpu_memory);
  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::InvalidateGPUCache() {
  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(size_t as_id, GPUVAddr addr, u64 size) {
  335. {
  336. std::scoped_lock lock{texture_cache.mutex};
  337. texture_cache.UnmapGPUMemory(as_id, addr, size);
  338. }
  339. }
  340. void RasterizerOpenGL::SignalFence(std::function<void()>&& func) {
  341. fence_manager.SignalFence(std::move(func));
  342. }
  343. void RasterizerOpenGL::SyncOperation(std::function<void()>&& func) {
  344. fence_manager.SyncOperation(std::move(func));
  345. }
  346. void RasterizerOpenGL::SignalSyncPoint(u32 value) {
  347. fence_manager.SignalSyncPoint(value);
  348. }
  349. void RasterizerOpenGL::SignalReference() {
  350. fence_manager.SignalOrdering();
  351. }
  352. void RasterizerOpenGL::ReleaseFences() {
  353. fence_manager.WaitPendingFences();
  354. }
  355. void RasterizerOpenGL::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  356. if (Settings::IsGPULevelExtreme()) {
  357. FlushRegion(addr, size);
  358. }
  359. InvalidateRegion(addr, size);
  360. }
  361. void RasterizerOpenGL::WaitForIdle() {
  362. glMemoryBarrier(GL_ALL_BARRIER_BITS);
  363. SignalReference();
  364. }
  365. void RasterizerOpenGL::FragmentBarrier() {
  366. glTextureBarrier();
  367. glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT);
  368. }
  369. void RasterizerOpenGL::TiledCacheBarrier() {
  370. glTextureBarrier();
  371. }
  372. void RasterizerOpenGL::FlushCommands() {
  373. // Only flush when we have commands queued to OpenGL.
  374. if (num_queued_commands == 0) {
  375. return;
  376. }
  377. num_queued_commands = 0;
  378. // Make sure memory stored from the previous GL command stream is visible
  379. // This is only needed on assembly shaders where we write to GPU memory with raw pointers
  380. if (has_written_global_memory) {
  381. has_written_global_memory = false;
  382. glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
  383. }
  384. glFlush();
  385. }
  386. void RasterizerOpenGL::TickFrame() {
  387. // Ticking a frame means that buffers will be swapped, calling glFlush implicitly.
  388. num_queued_commands = 0;
  389. fence_manager.TickFrame();
  390. {
  391. std::scoped_lock lock{texture_cache.mutex};
  392. texture_cache.TickFrame();
  393. }
  394. {
  395. std::scoped_lock lock{buffer_cache.mutex};
  396. buffer_cache.TickFrame();
  397. }
  398. }
  399. bool RasterizerOpenGL::AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  400. const Tegra::Engines::Fermi2D::Surface& dst,
  401. const Tegra::Engines::Fermi2D::Config& copy_config) {
  402. MICROPROFILE_SCOPE(OpenGL_Blits);
  403. std::scoped_lock lock{texture_cache.mutex};
  404. texture_cache.BlitImage(dst, src, copy_config);
  405. return true;
  406. }
  407. Tegra::Engines::AccelerateDMAInterface& RasterizerOpenGL::AccessAccelerateDMA() {
  408. return accelerate_dma;
  409. }
  410. void RasterizerOpenGL::AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
  411. std::span<const u8> memory) {
  412. auto cpu_addr = gpu_memory->GpuToCpuAddress(address);
  413. if (!cpu_addr) [[unlikely]] {
  414. gpu_memory->WriteBlock(address, memory.data(), copy_size);
  415. return;
  416. }
  417. gpu_memory->WriteBlockUnsafe(address, memory.data(), copy_size);
  418. {
  419. std::unique_lock<std::mutex> lock{buffer_cache.mutex};
  420. if (!buffer_cache.InlineMemory(*cpu_addr, copy_size, memory)) {
  421. buffer_cache.WriteMemory(*cpu_addr, copy_size);
  422. }
  423. }
  424. {
  425. std::scoped_lock lock_texture{texture_cache.mutex};
  426. texture_cache.WriteMemory(*cpu_addr, copy_size);
  427. }
  428. shader_cache.InvalidateRegion(*cpu_addr, copy_size);
  429. query_cache.InvalidateRegion(*cpu_addr, copy_size);
  430. }
  431. bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& config,
  432. VAddr framebuffer_addr, u32 pixel_stride) {
  433. if (framebuffer_addr == 0) {
  434. return false;
  435. }
  436. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  437. std::scoped_lock lock{texture_cache.mutex};
  438. ImageView* const image_view{texture_cache.TryFindFramebufferImageView(framebuffer_addr)};
  439. if (!image_view) {
  440. return false;
  441. }
  442. // Verify that the cached surface is the same size and format as the requested framebuffer
  443. // ASSERT_MSG(image_view->size.width == config.width, "Framebuffer width is different");
  444. // ASSERT_MSG(image_view->size.height == config.height, "Framebuffer height is different");
  445. screen_info.texture.width = image_view->size.width;
  446. screen_info.texture.height = image_view->size.height;
  447. screen_info.display_texture = image_view->Handle(Shader::TextureType::Color2D);
  448. screen_info.display_srgb = VideoCore::Surface::IsPixelFormatSRGB(image_view->format);
  449. return true;
  450. }
  451. void RasterizerOpenGL::SyncState() {
  452. SyncViewport();
  453. SyncRasterizeEnable();
  454. SyncPolygonModes();
  455. SyncColorMask();
  456. SyncFragmentColorClampState();
  457. SyncMultiSampleState();
  458. SyncDepthTestState();
  459. SyncDepthClamp();
  460. SyncStencilTestState();
  461. SyncBlendState();
  462. SyncLogicOpState();
  463. SyncCullMode();
  464. SyncPrimitiveRestart();
  465. SyncScissorTest();
  466. SyncPointState();
  467. SyncLineState();
  468. SyncPolygonOffset();
  469. SyncAlphaTest();
  470. SyncFramebufferSRGB();
  471. SyncVertexFormats();
  472. SyncVertexInstances();
  473. }
  474. void RasterizerOpenGL::SyncViewport() {
  475. auto& flags = maxwell3d->dirty.flags;
  476. const auto& regs = maxwell3d->regs;
  477. const bool rescale_viewports = flags[VideoCommon::Dirty::RescaleViewports];
  478. const bool dirty_viewport = flags[Dirty::Viewports] || rescale_viewports;
  479. const bool dirty_clip_control = flags[Dirty::ClipControl];
  480. if (dirty_viewport || dirty_clip_control || flags[Dirty::FrontFace]) {
  481. flags[Dirty::FrontFace] = false;
  482. GLenum mode = MaxwellToGL::FrontFace(regs.gl_front_face);
  483. bool flip_faces = true;
  484. if (regs.window_origin.flip_y != 0) {
  485. flip_faces = !flip_faces;
  486. }
  487. if (regs.viewport_transform[0].scale_y < 0.0f) {
  488. flip_faces = !flip_faces;
  489. }
  490. if (flip_faces) {
  491. switch (mode) {
  492. case GL_CW:
  493. mode = GL_CCW;
  494. break;
  495. case GL_CCW:
  496. mode = GL_CW;
  497. break;
  498. }
  499. }
  500. glFrontFace(mode);
  501. }
  502. if (dirty_viewport || dirty_clip_control) {
  503. flags[Dirty::ClipControl] = false;
  504. bool flip_y = false;
  505. if (regs.viewport_transform[0].scale_y < 0.0f) {
  506. flip_y = !flip_y;
  507. }
  508. const bool lower_left{regs.window_origin.mode != Maxwell::WindowOrigin::Mode::UpperLeft};
  509. if (lower_left) {
  510. flip_y = !flip_y;
  511. }
  512. const bool is_zero_to_one = regs.depth_mode == Maxwell::DepthMode::ZeroToOne;
  513. const GLenum origin = flip_y ? GL_UPPER_LEFT : GL_LOWER_LEFT;
  514. const GLenum depth = is_zero_to_one ? GL_ZERO_TO_ONE : GL_NEGATIVE_ONE_TO_ONE;
  515. state_tracker.ClipControl(origin, depth);
  516. state_tracker.SetYNegate(lower_left);
  517. }
  518. const bool is_rescaling{texture_cache.IsRescaling()};
  519. const float scale = is_rescaling ? Settings::values.resolution_info.up_factor : 1.0f;
  520. const auto conv = [scale](float value) -> GLfloat {
  521. float new_value = value * scale;
  522. if (scale < 1.0f) {
  523. const bool sign = std::signbit(value);
  524. new_value = std::round(std::abs(new_value));
  525. new_value = sign ? -new_value : new_value;
  526. }
  527. return static_cast<GLfloat>(new_value);
  528. };
  529. if (dirty_viewport) {
  530. flags[Dirty::Viewports] = false;
  531. const bool force = flags[Dirty::ViewportTransform] || rescale_viewports;
  532. flags[Dirty::ViewportTransform] = false;
  533. flags[VideoCommon::Dirty::RescaleViewports] = false;
  534. for (size_t index = 0; index < Maxwell::NumViewports; ++index) {
  535. if (!force && !flags[Dirty::Viewport0 + index]) {
  536. continue;
  537. }
  538. flags[Dirty::Viewport0 + index] = false;
  539. if (!regs.viewport_scale_offset_enabled) {
  540. const auto x = static_cast<GLfloat>(regs.surface_clip.x);
  541. const auto y = static_cast<GLfloat>(regs.surface_clip.y);
  542. const auto width = static_cast<GLfloat>(regs.surface_clip.width);
  543. const auto height = static_cast<GLfloat>(regs.surface_clip.height);
  544. glViewportIndexedf(static_cast<GLuint>(index), x, y, width != 0.0f ? width : 1.0f,
  545. height != 0.0f ? height : 1.0f);
  546. continue;
  547. }
  548. const auto& src = regs.viewport_transform[index];
  549. GLfloat x = conv(src.translate_x - src.scale_x);
  550. GLfloat y = conv(src.translate_y - src.scale_y);
  551. GLfloat width = conv(src.scale_x * 2.0f);
  552. GLfloat height = conv(src.scale_y * 2.0f);
  553. if (height < 0) {
  554. y += height;
  555. height = -height;
  556. }
  557. glViewportIndexedf(static_cast<GLuint>(index), x, y, width != 0.0f ? width : 1.0f,
  558. height != 0.0f ? height : 1.0f);
  559. const GLdouble reduce_z = regs.depth_mode == Maxwell::DepthMode::MinusOneToOne;
  560. const GLdouble near_depth = src.translate_z - src.scale_z * reduce_z;
  561. const GLdouble far_depth = src.translate_z + src.scale_z;
  562. if (device.HasDepthBufferFloat()) {
  563. glDepthRangeIndexeddNV(static_cast<GLuint>(index), near_depth, far_depth);
  564. } else {
  565. glDepthRangeIndexed(static_cast<GLuint>(index), near_depth, far_depth);
  566. }
  567. if (!GLAD_GL_NV_viewport_swizzle) {
  568. continue;
  569. }
  570. glViewportSwizzleNV(static_cast<GLuint>(index),
  571. MaxwellToGL::ViewportSwizzle(src.swizzle.x),
  572. MaxwellToGL::ViewportSwizzle(src.swizzle.y),
  573. MaxwellToGL::ViewportSwizzle(src.swizzle.z),
  574. MaxwellToGL::ViewportSwizzle(src.swizzle.w));
  575. }
  576. }
  577. }
  578. void RasterizerOpenGL::SyncDepthClamp() {
  579. auto& flags = maxwell3d->dirty.flags;
  580. if (!flags[Dirty::DepthClampEnabled]) {
  581. return;
  582. }
  583. flags[Dirty::DepthClampEnabled] = false;
  584. bool depth_clamp_disabled{maxwell3d->regs.viewport_clip_control.geometry_clip ==
  585. Maxwell::ViewportClipControl::GeometryClip::Passthrough ||
  586. maxwell3d->regs.viewport_clip_control.geometry_clip ==
  587. Maxwell::ViewportClipControl::GeometryClip::FrustumXYZ ||
  588. maxwell3d->regs.viewport_clip_control.geometry_clip ==
  589. Maxwell::ViewportClipControl::GeometryClip::FrustumZ};
  590. oglEnable(GL_DEPTH_CLAMP, !depth_clamp_disabled);
  591. }
  592. void RasterizerOpenGL::SyncClipEnabled(u32 clip_mask) {
  593. auto& flags = maxwell3d->dirty.flags;
  594. if (!flags[Dirty::ClipDistances] && !flags[VideoCommon::Dirty::Shaders]) {
  595. return;
  596. }
  597. flags[Dirty::ClipDistances] = false;
  598. clip_mask &= maxwell3d->regs.user_clip_enable.raw;
  599. if (clip_mask == last_clip_distance_mask) {
  600. return;
  601. }
  602. last_clip_distance_mask = clip_mask;
  603. for (std::size_t i = 0; i < Maxwell::Regs::NumClipDistances; ++i) {
  604. oglEnable(static_cast<GLenum>(GL_CLIP_DISTANCE0 + i), (clip_mask >> i) & 1);
  605. }
  606. }
  607. void RasterizerOpenGL::SyncClipCoef() {
  608. UNIMPLEMENTED();
  609. }
  610. void RasterizerOpenGL::SyncCullMode() {
  611. auto& flags = maxwell3d->dirty.flags;
  612. const auto& regs = maxwell3d->regs;
  613. if (flags[Dirty::CullTest]) {
  614. flags[Dirty::CullTest] = false;
  615. if (regs.gl_cull_test_enabled) {
  616. glEnable(GL_CULL_FACE);
  617. glCullFace(MaxwellToGL::CullFace(regs.gl_cull_face));
  618. } else {
  619. glDisable(GL_CULL_FACE);
  620. }
  621. }
  622. }
  623. void RasterizerOpenGL::SyncPrimitiveRestart() {
  624. auto& flags = maxwell3d->dirty.flags;
  625. if (!flags[Dirty::PrimitiveRestart]) {
  626. return;
  627. }
  628. flags[Dirty::PrimitiveRestart] = false;
  629. if (maxwell3d->regs.primitive_restart.enabled) {
  630. glEnable(GL_PRIMITIVE_RESTART);
  631. glPrimitiveRestartIndex(maxwell3d->regs.primitive_restart.index);
  632. } else {
  633. glDisable(GL_PRIMITIVE_RESTART);
  634. }
  635. }
  636. void RasterizerOpenGL::SyncDepthTestState() {
  637. auto& flags = maxwell3d->dirty.flags;
  638. const auto& regs = maxwell3d->regs;
  639. if (flags[Dirty::DepthMask]) {
  640. flags[Dirty::DepthMask] = false;
  641. glDepthMask(regs.depth_write_enabled ? GL_TRUE : GL_FALSE);
  642. }
  643. if (flags[Dirty::DepthTest]) {
  644. flags[Dirty::DepthTest] = false;
  645. if (regs.depth_test_enable) {
  646. glEnable(GL_DEPTH_TEST);
  647. glDepthFunc(MaxwellToGL::ComparisonOp(regs.depth_test_func));
  648. } else {
  649. glDisable(GL_DEPTH_TEST);
  650. }
  651. }
  652. }
  653. void RasterizerOpenGL::SyncStencilTestState() {
  654. auto& flags = maxwell3d->dirty.flags;
  655. if (!flags[Dirty::StencilTest]) {
  656. return;
  657. }
  658. flags[Dirty::StencilTest] = false;
  659. const auto& regs = maxwell3d->regs;
  660. oglEnable(GL_STENCIL_TEST, regs.stencil_enable);
  661. glStencilFuncSeparate(GL_FRONT, MaxwellToGL::ComparisonOp(regs.stencil_front_op.func),
  662. regs.stencil_front_ref, regs.stencil_front_func_mask);
  663. glStencilOpSeparate(GL_FRONT, MaxwellToGL::StencilOp(regs.stencil_front_op.fail),
  664. MaxwellToGL::StencilOp(regs.stencil_front_op.zfail),
  665. MaxwellToGL::StencilOp(regs.stencil_front_op.zpass));
  666. glStencilMaskSeparate(GL_FRONT, regs.stencil_front_mask);
  667. if (regs.stencil_two_side_enable) {
  668. glStencilFuncSeparate(GL_BACK, MaxwellToGL::ComparisonOp(regs.stencil_back_op.func),
  669. regs.stencil_back_ref, regs.stencil_back_func_mask);
  670. glStencilOpSeparate(GL_BACK, MaxwellToGL::StencilOp(regs.stencil_back_op.fail),
  671. MaxwellToGL::StencilOp(regs.stencil_back_op.zfail),
  672. MaxwellToGL::StencilOp(regs.stencil_back_op.zpass));
  673. glStencilMaskSeparate(GL_BACK, regs.stencil_back_mask);
  674. } else {
  675. glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 0, 0xFFFFFFFF);
  676. glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_KEEP);
  677. glStencilMaskSeparate(GL_BACK, 0xFFFFFFFF);
  678. }
  679. }
  680. void RasterizerOpenGL::SyncRasterizeEnable() {
  681. auto& flags = maxwell3d->dirty.flags;
  682. if (!flags[Dirty::RasterizeEnable]) {
  683. return;
  684. }
  685. flags[Dirty::RasterizeEnable] = false;
  686. oglEnable(GL_RASTERIZER_DISCARD, maxwell3d->regs.rasterize_enable == 0);
  687. }
  688. void RasterizerOpenGL::SyncPolygonModes() {
  689. auto& flags = maxwell3d->dirty.flags;
  690. if (!flags[Dirty::PolygonModes]) {
  691. return;
  692. }
  693. flags[Dirty::PolygonModes] = false;
  694. const auto& regs = maxwell3d->regs;
  695. if (regs.fill_via_triangle_mode != Maxwell::FillViaTriangleMode::Disabled) {
  696. if (!GLAD_GL_NV_fill_rectangle) {
  697. LOG_ERROR(Render_OpenGL, "GL_NV_fill_rectangle used and not supported");
  698. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  699. return;
  700. }
  701. flags[Dirty::PolygonModeFront] = true;
  702. flags[Dirty::PolygonModeBack] = true;
  703. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL_RECTANGLE_NV);
  704. return;
  705. }
  706. if (regs.polygon_mode_front == regs.polygon_mode_back) {
  707. flags[Dirty::PolygonModeFront] = false;
  708. flags[Dirty::PolygonModeBack] = false;
  709. glPolygonMode(GL_FRONT_AND_BACK, MaxwellToGL::PolygonMode(regs.polygon_mode_front));
  710. return;
  711. }
  712. if (flags[Dirty::PolygonModeFront]) {
  713. flags[Dirty::PolygonModeFront] = false;
  714. glPolygonMode(GL_FRONT, MaxwellToGL::PolygonMode(regs.polygon_mode_front));
  715. }
  716. if (flags[Dirty::PolygonModeBack]) {
  717. flags[Dirty::PolygonModeBack] = false;
  718. glPolygonMode(GL_BACK, MaxwellToGL::PolygonMode(regs.polygon_mode_back));
  719. }
  720. }
  721. void RasterizerOpenGL::SyncColorMask() {
  722. auto& flags = maxwell3d->dirty.flags;
  723. if (!flags[Dirty::ColorMasks]) {
  724. return;
  725. }
  726. flags[Dirty::ColorMasks] = false;
  727. const bool force = flags[Dirty::ColorMaskCommon];
  728. flags[Dirty::ColorMaskCommon] = false;
  729. const auto& regs = maxwell3d->regs;
  730. if (regs.color_mask_common) {
  731. if (!force && !flags[Dirty::ColorMask0]) {
  732. return;
  733. }
  734. flags[Dirty::ColorMask0] = false;
  735. auto& mask = regs.color_mask[0];
  736. glColorMask(mask.R != 0, mask.B != 0, mask.G != 0, mask.A != 0);
  737. return;
  738. }
  739. // Path without color_mask_common set
  740. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  741. if (!force && !flags[Dirty::ColorMask0 + i]) {
  742. continue;
  743. }
  744. flags[Dirty::ColorMask0 + i] = false;
  745. const auto& mask = regs.color_mask[i];
  746. glColorMaski(static_cast<GLuint>(i), mask.R != 0, mask.G != 0, mask.B != 0, mask.A != 0);
  747. }
  748. }
  749. void RasterizerOpenGL::SyncMultiSampleState() {
  750. auto& flags = maxwell3d->dirty.flags;
  751. if (!flags[Dirty::MultisampleControl]) {
  752. return;
  753. }
  754. flags[Dirty::MultisampleControl] = false;
  755. const auto& regs = maxwell3d->regs;
  756. oglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE, regs.anti_alias_alpha_control.alpha_to_coverage);
  757. oglEnable(GL_SAMPLE_ALPHA_TO_ONE, regs.anti_alias_alpha_control.alpha_to_one);
  758. }
  759. void RasterizerOpenGL::SyncFragmentColorClampState() {
  760. auto& flags = maxwell3d->dirty.flags;
  761. if (!flags[Dirty::FragmentClampColor]) {
  762. return;
  763. }
  764. flags[Dirty::FragmentClampColor] = false;
  765. glClampColor(GL_CLAMP_FRAGMENT_COLOR,
  766. maxwell3d->regs.frag_color_clamp.AnyEnabled() ? GL_TRUE : GL_FALSE);
  767. }
  768. void RasterizerOpenGL::SyncBlendState() {
  769. auto& flags = maxwell3d->dirty.flags;
  770. const auto& regs = maxwell3d->regs;
  771. if (flags[Dirty::BlendColor]) {
  772. flags[Dirty::BlendColor] = false;
  773. glBlendColor(regs.blend_color.r, regs.blend_color.g, regs.blend_color.b,
  774. regs.blend_color.a);
  775. }
  776. // TODO(Rodrigo): Revisit blending, there are several registers we are not reading
  777. if (!flags[Dirty::BlendStates]) {
  778. return;
  779. }
  780. flags[Dirty::BlendStates] = false;
  781. if (!regs.blend_per_target_enabled) {
  782. if (!regs.blend.enable[0]) {
  783. glDisable(GL_BLEND);
  784. return;
  785. }
  786. glEnable(GL_BLEND);
  787. glBlendFuncSeparate(MaxwellToGL::BlendFunc(regs.blend.color_source),
  788. MaxwellToGL::BlendFunc(regs.blend.color_dest),
  789. MaxwellToGL::BlendFunc(regs.blend.alpha_source),
  790. MaxwellToGL::BlendFunc(regs.blend.alpha_dest));
  791. glBlendEquationSeparate(MaxwellToGL::BlendEquation(regs.blend.color_op),
  792. MaxwellToGL::BlendEquation(regs.blend.alpha_op));
  793. return;
  794. }
  795. const bool force = flags[Dirty::BlendIndependentEnabled];
  796. flags[Dirty::BlendIndependentEnabled] = false;
  797. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  798. if (!force && !flags[Dirty::BlendState0 + i]) {
  799. continue;
  800. }
  801. flags[Dirty::BlendState0 + i] = false;
  802. if (!regs.blend.enable[i]) {
  803. glDisablei(GL_BLEND, static_cast<GLuint>(i));
  804. continue;
  805. }
  806. glEnablei(GL_BLEND, static_cast<GLuint>(i));
  807. const auto& src = regs.blend_per_target[i];
  808. glBlendFuncSeparatei(static_cast<GLuint>(i), MaxwellToGL::BlendFunc(src.color_source),
  809. MaxwellToGL::BlendFunc(src.color_dest),
  810. MaxwellToGL::BlendFunc(src.alpha_source),
  811. MaxwellToGL::BlendFunc(src.alpha_dest));
  812. glBlendEquationSeparatei(static_cast<GLuint>(i), MaxwellToGL::BlendEquation(src.color_op),
  813. MaxwellToGL::BlendEquation(src.alpha_op));
  814. }
  815. }
  816. void RasterizerOpenGL::SyncLogicOpState() {
  817. auto& flags = maxwell3d->dirty.flags;
  818. if (!flags[Dirty::LogicOp]) {
  819. return;
  820. }
  821. flags[Dirty::LogicOp] = false;
  822. const auto& regs = maxwell3d->regs;
  823. if (regs.logic_op.enable) {
  824. glEnable(GL_COLOR_LOGIC_OP);
  825. glLogicOp(MaxwellToGL::LogicOp(regs.logic_op.op));
  826. } else {
  827. glDisable(GL_COLOR_LOGIC_OP);
  828. }
  829. }
  830. void RasterizerOpenGL::SyncScissorTest() {
  831. auto& flags = maxwell3d->dirty.flags;
  832. if (!flags[Dirty::Scissors] && !flags[VideoCommon::Dirty::RescaleScissors]) {
  833. return;
  834. }
  835. flags[Dirty::Scissors] = false;
  836. const bool force = flags[VideoCommon::Dirty::RescaleScissors];
  837. flags[VideoCommon::Dirty::RescaleScissors] = false;
  838. const auto& regs = maxwell3d->regs;
  839. const auto& resolution = Settings::values.resolution_info;
  840. const bool is_rescaling{texture_cache.IsRescaling()};
  841. const u32 up_scale = is_rescaling ? resolution.up_scale : 1U;
  842. const u32 down_shift = is_rescaling ? resolution.down_shift : 0U;
  843. const auto scale_up = [up_scale, down_shift](u32 value) -> u32 {
  844. if (value == 0) {
  845. return 0U;
  846. }
  847. const u32 upset = value * up_scale;
  848. u32 acumm{};
  849. if ((up_scale >> down_shift) == 0) {
  850. acumm = upset % 2;
  851. }
  852. const u32 converted_value = upset >> down_shift;
  853. return std::max<u32>(converted_value + acumm, 1U);
  854. };
  855. for (std::size_t index = 0; index < Maxwell::NumViewports; ++index) {
  856. if (!force && !flags[Dirty::Scissor0 + index]) {
  857. continue;
  858. }
  859. flags[Dirty::Scissor0 + index] = false;
  860. const auto& src = regs.scissor_test[index];
  861. if (src.enable) {
  862. glEnablei(GL_SCISSOR_TEST, static_cast<GLuint>(index));
  863. glScissorIndexed(static_cast<GLuint>(index), scale_up(src.min_x), scale_up(src.min_y),
  864. scale_up(src.max_x - src.min_x), scale_up(src.max_y - src.min_y));
  865. } else {
  866. glDisablei(GL_SCISSOR_TEST, static_cast<GLuint>(index));
  867. }
  868. }
  869. }
  870. void RasterizerOpenGL::SyncPointState() {
  871. auto& flags = maxwell3d->dirty.flags;
  872. if (!flags[Dirty::PointSize]) {
  873. return;
  874. }
  875. flags[Dirty::PointSize] = false;
  876. oglEnable(GL_POINT_SPRITE, maxwell3d->regs.point_sprite_enable);
  877. oglEnable(GL_PROGRAM_POINT_SIZE, maxwell3d->regs.point_size_attribute.enabled);
  878. const bool is_rescaling{texture_cache.IsRescaling()};
  879. const float scale = is_rescaling ? Settings::values.resolution_info.up_factor : 1.0f;
  880. glPointSize(std::max(1.0f, maxwell3d->regs.point_size * scale));
  881. }
  882. void RasterizerOpenGL::SyncLineState() {
  883. auto& flags = maxwell3d->dirty.flags;
  884. if (!flags[Dirty::LineWidth]) {
  885. return;
  886. }
  887. flags[Dirty::LineWidth] = false;
  888. const auto& regs = maxwell3d->regs;
  889. oglEnable(GL_LINE_SMOOTH, regs.line_anti_alias_enable);
  890. glLineWidth(regs.line_anti_alias_enable ? regs.line_width_smooth : regs.line_width_aliased);
  891. }
  892. void RasterizerOpenGL::SyncPolygonOffset() {
  893. auto& flags = maxwell3d->dirty.flags;
  894. if (!flags[Dirty::PolygonOffset]) {
  895. return;
  896. }
  897. flags[Dirty::PolygonOffset] = false;
  898. const auto& regs = maxwell3d->regs;
  899. oglEnable(GL_POLYGON_OFFSET_FILL, regs.polygon_offset_fill_enable);
  900. oglEnable(GL_POLYGON_OFFSET_LINE, regs.polygon_offset_line_enable);
  901. oglEnable(GL_POLYGON_OFFSET_POINT, regs.polygon_offset_point_enable);
  902. if (regs.polygon_offset_fill_enable || regs.polygon_offset_line_enable ||
  903. regs.polygon_offset_point_enable) {
  904. // Hardware divides polygon offset units by two
  905. glPolygonOffsetClamp(regs.slope_scale_depth_bias, regs.depth_bias / 2.0f,
  906. regs.depth_bias_clamp);
  907. }
  908. }
  909. void RasterizerOpenGL::SyncAlphaTest() {
  910. auto& flags = maxwell3d->dirty.flags;
  911. if (!flags[Dirty::AlphaTest]) {
  912. return;
  913. }
  914. flags[Dirty::AlphaTest] = false;
  915. const auto& regs = maxwell3d->regs;
  916. if (regs.alpha_test_enabled) {
  917. glEnable(GL_ALPHA_TEST);
  918. glAlphaFunc(MaxwellToGL::ComparisonOp(regs.alpha_test_func), regs.alpha_test_ref);
  919. } else {
  920. glDisable(GL_ALPHA_TEST);
  921. }
  922. }
  923. void RasterizerOpenGL::SyncFramebufferSRGB() {
  924. auto& flags = maxwell3d->dirty.flags;
  925. if (!flags[Dirty::FramebufferSRGB]) {
  926. return;
  927. }
  928. flags[Dirty::FramebufferSRGB] = false;
  929. oglEnable(GL_FRAMEBUFFER_SRGB, maxwell3d->regs.framebuffer_srgb);
  930. }
  931. void RasterizerOpenGL::BeginTransformFeedback(GraphicsPipeline* program, GLenum primitive_mode) {
  932. const auto& regs = maxwell3d->regs;
  933. if (regs.transform_feedback_enabled == 0) {
  934. return;
  935. }
  936. program->ConfigureTransformFeedback();
  937. UNIMPLEMENTED_IF(regs.IsShaderConfigEnabled(Maxwell::ShaderType::TessellationInit) ||
  938. regs.IsShaderConfigEnabled(Maxwell::ShaderType::Tessellation) ||
  939. regs.IsShaderConfigEnabled(Maxwell::ShaderType::Geometry));
  940. UNIMPLEMENTED_IF(primitive_mode != GL_POINTS);
  941. // We may have to call BeginTransformFeedbackNV here since they seem to call different
  942. // implementations on Nvidia's driver (the pointer is different) but we are using
  943. // ARB_transform_feedback3 features with NV_transform_feedback interactions and the ARB
  944. // extension doesn't define BeginTransformFeedback (without NV) interactions. It just works.
  945. glBeginTransformFeedback(GL_POINTS);
  946. }
  947. void RasterizerOpenGL::EndTransformFeedback() {
  948. if (maxwell3d->regs.transform_feedback_enabled != 0) {
  949. glEndTransformFeedback();
  950. }
  951. }
  952. void RasterizerOpenGL::InitializeChannel(Tegra::Control::ChannelState& channel) {
  953. CreateChannel(channel);
  954. {
  955. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  956. texture_cache.CreateChannel(channel);
  957. buffer_cache.CreateChannel(channel);
  958. }
  959. shader_cache.CreateChannel(channel);
  960. query_cache.CreateChannel(channel);
  961. state_tracker.SetupTables(channel);
  962. }
  963. void RasterizerOpenGL::BindChannel(Tegra::Control::ChannelState& channel) {
  964. const s32 channel_id = channel.bind_id;
  965. BindToChannel(channel_id);
  966. {
  967. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  968. texture_cache.BindToChannel(channel_id);
  969. buffer_cache.BindToChannel(channel_id);
  970. }
  971. shader_cache.BindToChannel(channel_id);
  972. query_cache.BindToChannel(channel_id);
  973. state_tracker.ChangeChannel(channel);
  974. state_tracker.InvalidateState();
  975. }
  976. void RasterizerOpenGL::ReleaseChannel(s32 channel_id) {
  977. EraseChannel(channel_id);
  978. {
  979. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  980. texture_cache.EraseChannel(channel_id);
  981. buffer_cache.EraseChannel(channel_id);
  982. }
  983. shader_cache.EraseChannel(channel_id);
  984. query_cache.EraseChannel(channel_id);
  985. }
  986. void RasterizerOpenGL::BindInlineIndexBuffer() {
  987. if (maxwell3d->inline_index_draw_indexes.empty()) {
  988. return;
  989. }
  990. const auto data_count = static_cast<u32>(maxwell3d->inline_index_draw_indexes.size());
  991. auto buffer = Buffer(buffer_cache_runtime, *this, 0, data_count);
  992. buffer.ImmediateUpload(0, maxwell3d->inline_index_draw_indexes);
  993. buffer_cache_runtime.BindIndexBuffer(buffer, 0, data_count);
  994. }
  995. AccelerateDMA::AccelerateDMA(BufferCache& buffer_cache_) : buffer_cache{buffer_cache_} {}
  996. bool AccelerateDMA::BufferCopy(GPUVAddr src_address, GPUVAddr dest_address, u64 amount) {
  997. std::scoped_lock lock{buffer_cache.mutex};
  998. return buffer_cache.DMACopy(src_address, dest_address, amount);
  999. }
  1000. bool AccelerateDMA::BufferClear(GPUVAddr src_address, u64 amount, u32 value) {
  1001. std::scoped_lock lock{buffer_cache.mutex};
  1002. return buffer_cache.DMAClear(src_address, amount, value);
  1003. }
  1004. } // namespace OpenGL