gl_rasterizer.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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::SignedInt ||
  85. attrib.type == Maxwell::VertexAttribute::Type::UnsignedInt) {
  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.instanced_arrays.IsInstancingEnabled(gl_index);
  110. const GLuint divisor = instancing_enabled ? regs.vertex_array[index].divisor : 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() {
  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_buffers.R || regs.clear_buffers.G || regs.clear_buffers.B ||
  128. regs.clear_buffers.A) {
  129. use_color = true;
  130. const GLuint index = regs.clear_buffers.RT;
  131. state_tracker.NotifyColorMask(index);
  132. glColorMaski(index, regs.clear_buffers.R != 0, regs.clear_buffers.G != 0,
  133. regs.clear_buffers.B != 0, regs.clear_buffers.A != 0);
  134. // TODO(Rodrigo): Determine if clamping is used on clears
  135. SyncFragmentColorClampState();
  136. SyncFramebufferSRGB();
  137. }
  138. if (regs.clear_buffers.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_buffers.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_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. if (use_color) {
  166. glClearBufferfv(GL_COLOR, regs.clear_buffers.RT, regs.clear_color);
  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, bool is_instanced) {
  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. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  186. pipeline->SetEngine(maxwell3d, gpu_memory);
  187. pipeline->Configure(is_indexed);
  188. SyncState();
  189. const GLenum primitive_mode = MaxwellToGL::PrimitiveTopology(maxwell3d->regs.draw.topology);
  190. BeginTransformFeedback(pipeline, primitive_mode);
  191. const GLuint base_instance = static_cast<GLuint>(maxwell3d->regs.vb_base_instance);
  192. const GLsizei num_instances =
  193. static_cast<GLsizei>(is_instanced ? maxwell3d->mme_draw.instance_count : 1);
  194. if (is_indexed) {
  195. const GLint base_vertex = static_cast<GLint>(maxwell3d->regs.vb_element_base);
  196. const GLsizei num_vertices = static_cast<GLsizei>(maxwell3d->regs.index_array.count);
  197. const GLvoid* const offset = buffer_cache_runtime.IndexOffset();
  198. const GLenum format = MaxwellToGL::IndexFormat(maxwell3d->regs.index_array.format);
  199. if (num_instances == 1 && base_instance == 0 && base_vertex == 0) {
  200. glDrawElements(primitive_mode, num_vertices, format, offset);
  201. } else if (num_instances == 1 && base_instance == 0) {
  202. glDrawElementsBaseVertex(primitive_mode, num_vertices, format, offset, base_vertex);
  203. } else if (base_vertex == 0 && base_instance == 0) {
  204. glDrawElementsInstanced(primitive_mode, num_vertices, format, offset, num_instances);
  205. } else if (base_vertex == 0) {
  206. glDrawElementsInstancedBaseInstance(primitive_mode, num_vertices, format, offset,
  207. num_instances, base_instance);
  208. } else if (base_instance == 0) {
  209. glDrawElementsInstancedBaseVertex(primitive_mode, num_vertices, format, offset,
  210. num_instances, base_vertex);
  211. } else {
  212. glDrawElementsInstancedBaseVertexBaseInstance(primitive_mode, num_vertices, format,
  213. offset, num_instances, base_vertex,
  214. base_instance);
  215. }
  216. } else {
  217. const GLint base_vertex = static_cast<GLint>(maxwell3d->regs.vertex_buffer.first);
  218. const GLsizei num_vertices = static_cast<GLsizei>(maxwell3d->regs.vertex_buffer.count);
  219. if (num_instances == 1 && base_instance == 0) {
  220. glDrawArrays(primitive_mode, base_vertex, num_vertices);
  221. } else if (base_instance == 0) {
  222. glDrawArraysInstanced(primitive_mode, base_vertex, num_vertices, num_instances);
  223. } else {
  224. glDrawArraysInstancedBaseInstance(primitive_mode, base_vertex, num_vertices,
  225. num_instances, base_instance);
  226. }
  227. }
  228. EndTransformFeedback();
  229. ++num_queued_commands;
  230. has_written_global_memory |= pipeline->WritesGlobalMemory();
  231. }
  232. void RasterizerOpenGL::DispatchCompute() {
  233. ComputePipeline* const pipeline{shader_cache.CurrentComputePipeline()};
  234. if (!pipeline) {
  235. return;
  236. }
  237. pipeline->Configure();
  238. const auto& qmd{kepler_compute->launch_description};
  239. glDispatchCompute(qmd.grid_dim_x, qmd.grid_dim_y, qmd.grid_dim_z);
  240. ++num_queued_commands;
  241. has_written_global_memory |= pipeline->WritesGlobalMemory();
  242. }
  243. void RasterizerOpenGL::ResetCounter(VideoCore::QueryType type) {
  244. query_cache.ResetCounter(type);
  245. }
  246. void RasterizerOpenGL::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
  247. std::optional<u64> timestamp) {
  248. query_cache.Query(gpu_addr, type, timestamp);
  249. }
  250. void RasterizerOpenGL::BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr,
  251. u32 size) {
  252. std::scoped_lock lock{buffer_cache.mutex};
  253. buffer_cache.BindGraphicsUniformBuffer(stage, index, gpu_addr, size);
  254. }
  255. void RasterizerOpenGL::DisableGraphicsUniformBuffer(size_t stage, u32 index) {
  256. buffer_cache.DisableGraphicsUniformBuffer(stage, index);
  257. }
  258. void RasterizerOpenGL::FlushAll() {}
  259. void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
  260. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  261. if (addr == 0 || size == 0) {
  262. return;
  263. }
  264. {
  265. std::scoped_lock lock{texture_cache.mutex};
  266. texture_cache.DownloadMemory(addr, size);
  267. }
  268. {
  269. std::scoped_lock lock{buffer_cache.mutex};
  270. buffer_cache.DownloadMemory(addr, size);
  271. }
  272. query_cache.FlushRegion(addr, size);
  273. }
  274. bool RasterizerOpenGL::MustFlushRegion(VAddr addr, u64 size) {
  275. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  276. if (!Settings::IsGPULevelHigh()) {
  277. return buffer_cache.IsRegionGpuModified(addr, size);
  278. }
  279. return texture_cache.IsRegionGpuModified(addr, size) ||
  280. buffer_cache.IsRegionGpuModified(addr, size);
  281. }
  282. void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
  283. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  284. if (addr == 0 || size == 0) {
  285. return;
  286. }
  287. {
  288. std::scoped_lock lock{texture_cache.mutex};
  289. texture_cache.WriteMemory(addr, size);
  290. }
  291. {
  292. std::scoped_lock lock{buffer_cache.mutex};
  293. buffer_cache.WriteMemory(addr, size);
  294. }
  295. shader_cache.InvalidateRegion(addr, size);
  296. query_cache.InvalidateRegion(addr, size);
  297. }
  298. void RasterizerOpenGL::OnCPUWrite(VAddr addr, u64 size) {
  299. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  300. if (addr == 0 || size == 0) {
  301. return;
  302. }
  303. shader_cache.OnCPUWrite(addr, size);
  304. {
  305. std::scoped_lock lock{texture_cache.mutex};
  306. texture_cache.WriteMemory(addr, size);
  307. }
  308. {
  309. std::scoped_lock lock{buffer_cache.mutex};
  310. buffer_cache.CachedWriteMemory(addr, size);
  311. }
  312. }
  313. void RasterizerOpenGL::SyncGuestHost() {
  314. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  315. shader_cache.SyncGuestHost();
  316. {
  317. std::scoped_lock lock{buffer_cache.mutex};
  318. buffer_cache.FlushCachedWrites();
  319. }
  320. }
  321. void RasterizerOpenGL::UnmapMemory(VAddr addr, u64 size) {
  322. {
  323. std::scoped_lock lock{texture_cache.mutex};
  324. texture_cache.UnmapMemory(addr, size);
  325. }
  326. {
  327. std::scoped_lock lock{buffer_cache.mutex};
  328. buffer_cache.WriteMemory(addr, size);
  329. }
  330. shader_cache.OnCPUWrite(addr, size);
  331. }
  332. void RasterizerOpenGL::ModifyGPUMemory(GPUVAddr addr, u64 size) {
  333. {
  334. std::scoped_lock lock{texture_cache.mutex};
  335. texture_cache.UnmapGPUMemory(addr, size);
  336. }
  337. }
  338. void RasterizerOpenGL::SignalSemaphore(GPUVAddr addr, u32 value) {
  339. if (!gpu.IsAsync()) {
  340. gpu_memory->Write<u32>(addr, value);
  341. return;
  342. }
  343. auto paddr = gpu_memory->GetPointer(addr);
  344. fence_manager.SignalSemaphore(paddr, 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 | GL_TEXTURE_FETCH_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. if (has_written_global_memory) {
  390. has_written_global_memory = false;
  391. glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
  392. }
  393. glFlush();
  394. }
  395. void RasterizerOpenGL::TickFrame() {
  396. // Ticking a frame means that buffers will be swapped, calling glFlush implicitly.
  397. num_queued_commands = 0;
  398. fence_manager.TickFrame();
  399. {
  400. std::scoped_lock lock{texture_cache.mutex};
  401. texture_cache.TickFrame();
  402. }
  403. {
  404. std::scoped_lock lock{buffer_cache.mutex};
  405. buffer_cache.TickFrame();
  406. }
  407. }
  408. bool RasterizerOpenGL::AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  409. const Tegra::Engines::Fermi2D::Surface& dst,
  410. const Tegra::Engines::Fermi2D::Config& copy_config) {
  411. MICROPROFILE_SCOPE(OpenGL_Blits);
  412. std::scoped_lock lock{texture_cache.mutex};
  413. texture_cache.BlitImage(dst, src, copy_config);
  414. return true;
  415. }
  416. Tegra::Engines::AccelerateDMAInterface& RasterizerOpenGL::AccessAccelerateDMA() {
  417. return accelerate_dma;
  418. }
  419. void RasterizerOpenGL::AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
  420. std::span<u8> memory) {
  421. auto cpu_addr = gpu_memory->GpuToCpuAddress(address);
  422. if (!cpu_addr) [[unlikely]] {
  423. gpu_memory->WriteBlock(address, memory.data(), copy_size);
  424. return;
  425. }
  426. gpu_memory->WriteBlockUnsafe(address, memory.data(), copy_size);
  427. {
  428. std::unique_lock<std::mutex> lock{buffer_cache.mutex};
  429. if (!buffer_cache.InlineMemory(*cpu_addr, copy_size, memory)) {
  430. buffer_cache.WriteMemory(*cpu_addr, copy_size);
  431. }
  432. }
  433. {
  434. std::scoped_lock lock_texture{texture_cache.mutex};
  435. texture_cache.WriteMemory(*cpu_addr, copy_size);
  436. }
  437. shader_cache.InvalidateRegion(*cpu_addr, copy_size);
  438. query_cache.InvalidateRegion(*cpu_addr, copy_size);
  439. }
  440. bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& config,
  441. VAddr framebuffer_addr, u32 pixel_stride) {
  442. if (framebuffer_addr == 0) {
  443. return false;
  444. }
  445. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  446. std::scoped_lock lock{texture_cache.mutex};
  447. ImageView* const image_view{texture_cache.TryFindFramebufferImageView(framebuffer_addr)};
  448. if (!image_view) {
  449. return false;
  450. }
  451. // Verify that the cached surface is the same size and format as the requested framebuffer
  452. // ASSERT_MSG(image_view->size.width == config.width, "Framebuffer width is different");
  453. // ASSERT_MSG(image_view->size.height == config.height, "Framebuffer height is different");
  454. screen_info.texture.width = image_view->size.width;
  455. screen_info.texture.height = image_view->size.height;
  456. screen_info.display_texture = image_view->Handle(Shader::TextureType::Color2D);
  457. screen_info.display_srgb = VideoCore::Surface::IsPixelFormatSRGB(image_view->format);
  458. return true;
  459. }
  460. void RasterizerOpenGL::SyncState() {
  461. SyncViewport();
  462. SyncRasterizeEnable();
  463. SyncPolygonModes();
  464. SyncColorMask();
  465. SyncFragmentColorClampState();
  466. SyncMultiSampleState();
  467. SyncDepthTestState();
  468. SyncDepthClamp();
  469. SyncStencilTestState();
  470. SyncBlendState();
  471. SyncLogicOpState();
  472. SyncCullMode();
  473. SyncPrimitiveRestart();
  474. SyncScissorTest();
  475. SyncPointState();
  476. SyncLineState();
  477. SyncPolygonOffset();
  478. SyncAlphaTest();
  479. SyncFramebufferSRGB();
  480. SyncVertexFormats();
  481. SyncVertexInstances();
  482. }
  483. void RasterizerOpenGL::SyncViewport() {
  484. auto& flags = maxwell3d->dirty.flags;
  485. const auto& regs = maxwell3d->regs;
  486. const bool rescale_viewports = flags[VideoCommon::Dirty::RescaleViewports];
  487. const bool dirty_viewport = flags[Dirty::Viewports] || rescale_viewports;
  488. const bool dirty_clip_control = flags[Dirty::ClipControl];
  489. if (dirty_viewport || dirty_clip_control || flags[Dirty::FrontFace]) {
  490. flags[Dirty::FrontFace] = false;
  491. GLenum mode = MaxwellToGL::FrontFace(regs.front_face);
  492. bool flip_faces = true;
  493. if (regs.screen_y_control.triangle_rast_flip != 0) {
  494. flip_faces = !flip_faces;
  495. }
  496. if (regs.viewport_transform[0].scale_y < 0.0f) {
  497. flip_faces = !flip_faces;
  498. }
  499. if (flip_faces) {
  500. switch (mode) {
  501. case GL_CW:
  502. mode = GL_CCW;
  503. break;
  504. case GL_CCW:
  505. mode = GL_CW;
  506. break;
  507. }
  508. }
  509. glFrontFace(mode);
  510. }
  511. if (dirty_viewport || dirty_clip_control) {
  512. flags[Dirty::ClipControl] = false;
  513. bool flip_y = false;
  514. if (regs.viewport_transform[0].scale_y < 0.0f) {
  515. flip_y = !flip_y;
  516. }
  517. if (regs.screen_y_control.y_negate != 0) {
  518. flip_y = !flip_y;
  519. }
  520. const bool is_zero_to_one = regs.depth_mode == Maxwell::DepthMode::ZeroToOne;
  521. const GLenum origin = flip_y ? GL_UPPER_LEFT : GL_LOWER_LEFT;
  522. const GLenum depth = is_zero_to_one ? GL_ZERO_TO_ONE : GL_NEGATIVE_ONE_TO_ONE;
  523. state_tracker.ClipControl(origin, depth);
  524. state_tracker.SetYNegate(regs.screen_y_control.y_negate != 0);
  525. }
  526. const bool is_rescaling{texture_cache.IsRescaling()};
  527. const float scale = is_rescaling ? Settings::values.resolution_info.up_factor : 1.0f;
  528. const auto conv = [scale](float value) -> GLfloat {
  529. float new_value = value * scale;
  530. if (scale < 1.0f) {
  531. const bool sign = std::signbit(value);
  532. new_value = std::round(std::abs(new_value));
  533. new_value = sign ? -new_value : new_value;
  534. }
  535. return static_cast<GLfloat>(new_value);
  536. };
  537. if (dirty_viewport) {
  538. flags[Dirty::Viewports] = false;
  539. const bool force = flags[Dirty::ViewportTransform] || rescale_viewports;
  540. flags[Dirty::ViewportTransform] = false;
  541. flags[VideoCommon::Dirty::RescaleViewports] = false;
  542. for (size_t index = 0; index < Maxwell::NumViewports; ++index) {
  543. if (!force && !flags[Dirty::Viewport0 + index]) {
  544. continue;
  545. }
  546. flags[Dirty::Viewport0 + index] = false;
  547. const auto& src = regs.viewport_transform[index];
  548. GLfloat x = conv(src.translate_x - src.scale_x);
  549. GLfloat y = conv(src.translate_y - src.scale_y);
  550. GLfloat width = conv(src.scale_x * 2.0f);
  551. GLfloat height = conv(src.scale_y * 2.0f);
  552. if (height < 0) {
  553. y += height;
  554. height = -height;
  555. }
  556. glViewportIndexedf(static_cast<GLuint>(index), x, y, width != 0.0f ? width : 1.0f,
  557. height != 0.0f ? height : 1.0f);
  558. const GLdouble reduce_z = regs.depth_mode == Maxwell::DepthMode::MinusOneToOne;
  559. const GLdouble near_depth = src.translate_z - src.scale_z * reduce_z;
  560. const GLdouble far_depth = src.translate_z + src.scale_z;
  561. if (device.HasDepthBufferFloat()) {
  562. glDepthRangeIndexeddNV(static_cast<GLuint>(index), near_depth, far_depth);
  563. } else {
  564. glDepthRangeIndexed(static_cast<GLuint>(index), near_depth, far_depth);
  565. }
  566. if (!GLAD_GL_NV_viewport_swizzle) {
  567. continue;
  568. }
  569. glViewportSwizzleNV(static_cast<GLuint>(index),
  570. MaxwellToGL::ViewportSwizzle(src.swizzle.x),
  571. MaxwellToGL::ViewportSwizzle(src.swizzle.y),
  572. MaxwellToGL::ViewportSwizzle(src.swizzle.z),
  573. MaxwellToGL::ViewportSwizzle(src.swizzle.w));
  574. }
  575. }
  576. }
  577. void RasterizerOpenGL::SyncDepthClamp() {
  578. auto& flags = maxwell3d->dirty.flags;
  579. if (!flags[Dirty::DepthClampEnabled]) {
  580. return;
  581. }
  582. flags[Dirty::DepthClampEnabled] = false;
  583. oglEnable(GL_DEPTH_CLAMP, maxwell3d->regs.view_volume_clip_control.depth_clamp_disabled == 0);
  584. }
  585. void RasterizerOpenGL::SyncClipEnabled(u32 clip_mask) {
  586. auto& flags = maxwell3d->dirty.flags;
  587. if (!flags[Dirty::ClipDistances] && !flags[VideoCommon::Dirty::Shaders]) {
  588. return;
  589. }
  590. flags[Dirty::ClipDistances] = false;
  591. clip_mask &= maxwell3d->regs.clip_distance_enabled;
  592. if (clip_mask == last_clip_distance_mask) {
  593. return;
  594. }
  595. last_clip_distance_mask = clip_mask;
  596. for (std::size_t i = 0; i < Maxwell::Regs::NumClipDistances; ++i) {
  597. oglEnable(static_cast<GLenum>(GL_CLIP_DISTANCE0 + i), (clip_mask >> i) & 1);
  598. }
  599. }
  600. void RasterizerOpenGL::SyncClipCoef() {
  601. UNIMPLEMENTED();
  602. }
  603. void RasterizerOpenGL::SyncCullMode() {
  604. auto& flags = maxwell3d->dirty.flags;
  605. const auto& regs = maxwell3d->regs;
  606. if (flags[Dirty::CullTest]) {
  607. flags[Dirty::CullTest] = false;
  608. if (regs.cull_test_enabled) {
  609. glEnable(GL_CULL_FACE);
  610. glCullFace(MaxwellToGL::CullFace(regs.cull_face));
  611. } else {
  612. glDisable(GL_CULL_FACE);
  613. }
  614. }
  615. }
  616. void RasterizerOpenGL::SyncPrimitiveRestart() {
  617. auto& flags = maxwell3d->dirty.flags;
  618. if (!flags[Dirty::PrimitiveRestart]) {
  619. return;
  620. }
  621. flags[Dirty::PrimitiveRestart] = false;
  622. if (maxwell3d->regs.primitive_restart.enabled) {
  623. glEnable(GL_PRIMITIVE_RESTART);
  624. glPrimitiveRestartIndex(maxwell3d->regs.primitive_restart.index);
  625. } else {
  626. glDisable(GL_PRIMITIVE_RESTART);
  627. }
  628. }
  629. void RasterizerOpenGL::SyncDepthTestState() {
  630. auto& flags = maxwell3d->dirty.flags;
  631. const auto& regs = maxwell3d->regs;
  632. if (flags[Dirty::DepthMask]) {
  633. flags[Dirty::DepthMask] = false;
  634. glDepthMask(regs.depth_write_enabled ? GL_TRUE : GL_FALSE);
  635. }
  636. if (flags[Dirty::DepthTest]) {
  637. flags[Dirty::DepthTest] = false;
  638. if (regs.depth_test_enable) {
  639. glEnable(GL_DEPTH_TEST);
  640. glDepthFunc(MaxwellToGL::ComparisonOp(regs.depth_test_func));
  641. } else {
  642. glDisable(GL_DEPTH_TEST);
  643. }
  644. }
  645. }
  646. void RasterizerOpenGL::SyncStencilTestState() {
  647. auto& flags = maxwell3d->dirty.flags;
  648. if (!flags[Dirty::StencilTest]) {
  649. return;
  650. }
  651. flags[Dirty::StencilTest] = false;
  652. const auto& regs = maxwell3d->regs;
  653. oglEnable(GL_STENCIL_TEST, regs.stencil_enable);
  654. glStencilFuncSeparate(GL_FRONT, MaxwellToGL::ComparisonOp(regs.stencil_front_func_func),
  655. regs.stencil_front_func_ref, regs.stencil_front_func_mask);
  656. glStencilOpSeparate(GL_FRONT, MaxwellToGL::StencilOp(regs.stencil_front_op_fail),
  657. MaxwellToGL::StencilOp(regs.stencil_front_op_zfail),
  658. MaxwellToGL::StencilOp(regs.stencil_front_op_zpass));
  659. glStencilMaskSeparate(GL_FRONT, regs.stencil_front_mask);
  660. if (regs.stencil_two_side_enable) {
  661. glStencilFuncSeparate(GL_BACK, MaxwellToGL::ComparisonOp(regs.stencil_back_func_func),
  662. regs.stencil_back_func_ref, regs.stencil_back_func_mask);
  663. glStencilOpSeparate(GL_BACK, MaxwellToGL::StencilOp(regs.stencil_back_op_fail),
  664. MaxwellToGL::StencilOp(regs.stencil_back_op_zfail),
  665. MaxwellToGL::StencilOp(regs.stencil_back_op_zpass));
  666. glStencilMaskSeparate(GL_BACK, regs.stencil_back_mask);
  667. } else {
  668. glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 0, 0xFFFFFFFF);
  669. glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_KEEP);
  670. glStencilMaskSeparate(GL_BACK, 0xFFFFFFFF);
  671. }
  672. }
  673. void RasterizerOpenGL::SyncRasterizeEnable() {
  674. auto& flags = maxwell3d->dirty.flags;
  675. if (!flags[Dirty::RasterizeEnable]) {
  676. return;
  677. }
  678. flags[Dirty::RasterizeEnable] = false;
  679. oglEnable(GL_RASTERIZER_DISCARD, maxwell3d->regs.rasterize_enable == 0);
  680. }
  681. void RasterizerOpenGL::SyncPolygonModes() {
  682. auto& flags = maxwell3d->dirty.flags;
  683. if (!flags[Dirty::PolygonModes]) {
  684. return;
  685. }
  686. flags[Dirty::PolygonModes] = false;
  687. const auto& regs = maxwell3d->regs;
  688. if (regs.fill_rectangle) {
  689. if (!GLAD_GL_NV_fill_rectangle) {
  690. LOG_ERROR(Render_OpenGL, "GL_NV_fill_rectangle used and not supported");
  691. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  692. return;
  693. }
  694. flags[Dirty::PolygonModeFront] = true;
  695. flags[Dirty::PolygonModeBack] = true;
  696. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL_RECTANGLE_NV);
  697. return;
  698. }
  699. if (regs.polygon_mode_front == regs.polygon_mode_back) {
  700. flags[Dirty::PolygonModeFront] = false;
  701. flags[Dirty::PolygonModeBack] = false;
  702. glPolygonMode(GL_FRONT_AND_BACK, MaxwellToGL::PolygonMode(regs.polygon_mode_front));
  703. return;
  704. }
  705. if (flags[Dirty::PolygonModeFront]) {
  706. flags[Dirty::PolygonModeFront] = false;
  707. glPolygonMode(GL_FRONT, MaxwellToGL::PolygonMode(regs.polygon_mode_front));
  708. }
  709. if (flags[Dirty::PolygonModeBack]) {
  710. flags[Dirty::PolygonModeBack] = false;
  711. glPolygonMode(GL_BACK, MaxwellToGL::PolygonMode(regs.polygon_mode_back));
  712. }
  713. }
  714. void RasterizerOpenGL::SyncColorMask() {
  715. auto& flags = maxwell3d->dirty.flags;
  716. if (!flags[Dirty::ColorMasks]) {
  717. return;
  718. }
  719. flags[Dirty::ColorMasks] = false;
  720. const bool force = flags[Dirty::ColorMaskCommon];
  721. flags[Dirty::ColorMaskCommon] = false;
  722. const auto& regs = maxwell3d->regs;
  723. if (regs.color_mask_common) {
  724. if (!force && !flags[Dirty::ColorMask0]) {
  725. return;
  726. }
  727. flags[Dirty::ColorMask0] = false;
  728. auto& mask = regs.color_mask[0];
  729. glColorMask(mask.R != 0, mask.B != 0, mask.G != 0, mask.A != 0);
  730. return;
  731. }
  732. // Path without color_mask_common set
  733. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  734. if (!force && !flags[Dirty::ColorMask0 + i]) {
  735. continue;
  736. }
  737. flags[Dirty::ColorMask0 + i] = false;
  738. const auto& mask = regs.color_mask[i];
  739. glColorMaski(static_cast<GLuint>(i), mask.R != 0, mask.G != 0, mask.B != 0, mask.A != 0);
  740. }
  741. }
  742. void RasterizerOpenGL::SyncMultiSampleState() {
  743. auto& flags = maxwell3d->dirty.flags;
  744. if (!flags[Dirty::MultisampleControl]) {
  745. return;
  746. }
  747. flags[Dirty::MultisampleControl] = false;
  748. const auto& regs = maxwell3d->regs;
  749. oglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE, regs.multisample_control.alpha_to_coverage);
  750. oglEnable(GL_SAMPLE_ALPHA_TO_ONE, regs.multisample_control.alpha_to_one);
  751. }
  752. void RasterizerOpenGL::SyncFragmentColorClampState() {
  753. auto& flags = maxwell3d->dirty.flags;
  754. if (!flags[Dirty::FragmentClampColor]) {
  755. return;
  756. }
  757. flags[Dirty::FragmentClampColor] = false;
  758. glClampColor(GL_CLAMP_FRAGMENT_COLOR, maxwell3d->regs.frag_color_clamp ? GL_TRUE : GL_FALSE);
  759. }
  760. void RasterizerOpenGL::SyncBlendState() {
  761. auto& flags = maxwell3d->dirty.flags;
  762. const auto& regs = maxwell3d->regs;
  763. if (flags[Dirty::BlendColor]) {
  764. flags[Dirty::BlendColor] = false;
  765. glBlendColor(regs.blend_color.r, regs.blend_color.g, regs.blend_color.b,
  766. regs.blend_color.a);
  767. }
  768. // TODO(Rodrigo): Revisit blending, there are several registers we are not reading
  769. if (!flags[Dirty::BlendStates]) {
  770. return;
  771. }
  772. flags[Dirty::BlendStates] = false;
  773. if (!regs.independent_blend_enable) {
  774. if (!regs.blend.enable[0]) {
  775. glDisable(GL_BLEND);
  776. return;
  777. }
  778. glEnable(GL_BLEND);
  779. glBlendFuncSeparate(MaxwellToGL::BlendFunc(regs.blend.factor_source_rgb),
  780. MaxwellToGL::BlendFunc(regs.blend.factor_dest_rgb),
  781. MaxwellToGL::BlendFunc(regs.blend.factor_source_a),
  782. MaxwellToGL::BlendFunc(regs.blend.factor_dest_a));
  783. glBlendEquationSeparate(MaxwellToGL::BlendEquation(regs.blend.equation_rgb),
  784. MaxwellToGL::BlendEquation(regs.blend.equation_a));
  785. return;
  786. }
  787. const bool force = flags[Dirty::BlendIndependentEnabled];
  788. flags[Dirty::BlendIndependentEnabled] = false;
  789. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  790. if (!force && !flags[Dirty::BlendState0 + i]) {
  791. continue;
  792. }
  793. flags[Dirty::BlendState0 + i] = false;
  794. if (!regs.blend.enable[i]) {
  795. glDisablei(GL_BLEND, static_cast<GLuint>(i));
  796. continue;
  797. }
  798. glEnablei(GL_BLEND, static_cast<GLuint>(i));
  799. const auto& src = regs.independent_blend[i];
  800. glBlendFuncSeparatei(static_cast<GLuint>(i), MaxwellToGL::BlendFunc(src.factor_source_rgb),
  801. MaxwellToGL::BlendFunc(src.factor_dest_rgb),
  802. MaxwellToGL::BlendFunc(src.factor_source_a),
  803. MaxwellToGL::BlendFunc(src.factor_dest_a));
  804. glBlendEquationSeparatei(static_cast<GLuint>(i),
  805. MaxwellToGL::BlendEquation(src.equation_rgb),
  806. MaxwellToGL::BlendEquation(src.equation_a));
  807. }
  808. }
  809. void RasterizerOpenGL::SyncLogicOpState() {
  810. auto& flags = maxwell3d->dirty.flags;
  811. if (!flags[Dirty::LogicOp]) {
  812. return;
  813. }
  814. flags[Dirty::LogicOp] = false;
  815. const auto& regs = maxwell3d->regs;
  816. if (regs.logic_op.enable) {
  817. glEnable(GL_COLOR_LOGIC_OP);
  818. glLogicOp(MaxwellToGL::LogicOp(regs.logic_op.operation));
  819. } else {
  820. glDisable(GL_COLOR_LOGIC_OP);
  821. }
  822. }
  823. void RasterizerOpenGL::SyncScissorTest() {
  824. auto& flags = maxwell3d->dirty.flags;
  825. if (!flags[Dirty::Scissors] && !flags[VideoCommon::Dirty::RescaleScissors]) {
  826. return;
  827. }
  828. flags[Dirty::Scissors] = false;
  829. const bool force = flags[VideoCommon::Dirty::RescaleScissors];
  830. flags[VideoCommon::Dirty::RescaleScissors] = false;
  831. const auto& regs = maxwell3d->regs;
  832. const auto& resolution = Settings::values.resolution_info;
  833. const bool is_rescaling{texture_cache.IsRescaling()};
  834. const u32 up_scale = is_rescaling ? resolution.up_scale : 1U;
  835. const u32 down_shift = is_rescaling ? resolution.down_shift : 0U;
  836. const auto scale_up = [up_scale, down_shift](u32 value) -> u32 {
  837. if (value == 0) {
  838. return 0U;
  839. }
  840. const u32 upset = value * up_scale;
  841. u32 acumm{};
  842. if ((up_scale >> down_shift) == 0) {
  843. acumm = upset % 2;
  844. }
  845. const u32 converted_value = upset >> down_shift;
  846. return std::max<u32>(converted_value + acumm, 1U);
  847. };
  848. for (std::size_t index = 0; index < Maxwell::NumViewports; ++index) {
  849. if (!force && !flags[Dirty::Scissor0 + index]) {
  850. continue;
  851. }
  852. flags[Dirty::Scissor0 + index] = false;
  853. const auto& src = regs.scissor_test[index];
  854. if (src.enable) {
  855. glEnablei(GL_SCISSOR_TEST, static_cast<GLuint>(index));
  856. glScissorIndexed(static_cast<GLuint>(index), scale_up(src.min_x), scale_up(src.min_y),
  857. scale_up(src.max_x - src.min_x), scale_up(src.max_y - src.min_y));
  858. } else {
  859. glDisablei(GL_SCISSOR_TEST, static_cast<GLuint>(index));
  860. }
  861. }
  862. }
  863. void RasterizerOpenGL::SyncPointState() {
  864. auto& flags = maxwell3d->dirty.flags;
  865. if (!flags[Dirty::PointSize]) {
  866. return;
  867. }
  868. flags[Dirty::PointSize] = false;
  869. oglEnable(GL_POINT_SPRITE, maxwell3d->regs.point_sprite_enable);
  870. oglEnable(GL_PROGRAM_POINT_SIZE, maxwell3d->regs.vp_point_size.enable);
  871. const bool is_rescaling{texture_cache.IsRescaling()};
  872. const float scale = is_rescaling ? Settings::values.resolution_info.up_factor : 1.0f;
  873. glPointSize(std::max(1.0f, maxwell3d->regs.point_size * scale));
  874. }
  875. void RasterizerOpenGL::SyncLineState() {
  876. auto& flags = maxwell3d->dirty.flags;
  877. if (!flags[Dirty::LineWidth]) {
  878. return;
  879. }
  880. flags[Dirty::LineWidth] = false;
  881. const auto& regs = maxwell3d->regs;
  882. oglEnable(GL_LINE_SMOOTH, regs.line_smooth_enable);
  883. glLineWidth(regs.line_smooth_enable ? regs.line_width_smooth : regs.line_width_aliased);
  884. }
  885. void RasterizerOpenGL::SyncPolygonOffset() {
  886. auto& flags = maxwell3d->dirty.flags;
  887. if (!flags[Dirty::PolygonOffset]) {
  888. return;
  889. }
  890. flags[Dirty::PolygonOffset] = false;
  891. const auto& regs = maxwell3d->regs;
  892. oglEnable(GL_POLYGON_OFFSET_FILL, regs.polygon_offset_fill_enable);
  893. oglEnable(GL_POLYGON_OFFSET_LINE, regs.polygon_offset_line_enable);
  894. oglEnable(GL_POLYGON_OFFSET_POINT, regs.polygon_offset_point_enable);
  895. if (regs.polygon_offset_fill_enable || regs.polygon_offset_line_enable ||
  896. regs.polygon_offset_point_enable) {
  897. // Hardware divides polygon offset units by two
  898. glPolygonOffsetClamp(regs.polygon_offset_factor, regs.polygon_offset_units / 2.0f,
  899. regs.polygon_offset_clamp);
  900. }
  901. }
  902. void RasterizerOpenGL::SyncAlphaTest() {
  903. auto& flags = maxwell3d->dirty.flags;
  904. if (!flags[Dirty::AlphaTest]) {
  905. return;
  906. }
  907. flags[Dirty::AlphaTest] = false;
  908. const auto& regs = maxwell3d->regs;
  909. if (regs.alpha_test_enabled) {
  910. glEnable(GL_ALPHA_TEST);
  911. glAlphaFunc(MaxwellToGL::ComparisonOp(regs.alpha_test_func), regs.alpha_test_ref);
  912. } else {
  913. glDisable(GL_ALPHA_TEST);
  914. }
  915. }
  916. void RasterizerOpenGL::SyncFramebufferSRGB() {
  917. auto& flags = maxwell3d->dirty.flags;
  918. if (!flags[Dirty::FramebufferSRGB]) {
  919. return;
  920. }
  921. flags[Dirty::FramebufferSRGB] = false;
  922. oglEnable(GL_FRAMEBUFFER_SRGB, maxwell3d->regs.framebuffer_srgb);
  923. }
  924. void RasterizerOpenGL::BeginTransformFeedback(GraphicsPipeline* program, GLenum primitive_mode) {
  925. const auto& regs = maxwell3d->regs;
  926. if (regs.tfb_enabled == 0) {
  927. return;
  928. }
  929. program->ConfigureTransformFeedback();
  930. UNIMPLEMENTED_IF(regs.IsShaderConfigEnabled(Maxwell::ShaderProgram::TesselationControl) ||
  931. regs.IsShaderConfigEnabled(Maxwell::ShaderProgram::TesselationEval) ||
  932. regs.IsShaderConfigEnabled(Maxwell::ShaderProgram::Geometry));
  933. UNIMPLEMENTED_IF(primitive_mode != GL_POINTS);
  934. // We may have to call BeginTransformFeedbackNV here since they seem to call different
  935. // implementations on Nvidia's driver (the pointer is different) but we are using
  936. // ARB_transform_feedback3 features with NV_transform_feedback interactions and the ARB
  937. // extension doesn't define BeginTransformFeedback (without NV) interactions. It just works.
  938. glBeginTransformFeedback(GL_POINTS);
  939. }
  940. void RasterizerOpenGL::EndTransformFeedback() {
  941. if (maxwell3d->regs.tfb_enabled != 0) {
  942. glEndTransformFeedback();
  943. }
  944. }
  945. void RasterizerOpenGL::InitializeChannel(Tegra::Control::ChannelState& channel) {
  946. CreateChannel(channel);
  947. {
  948. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  949. texture_cache.CreateChannel(channel);
  950. buffer_cache.CreateChannel(channel);
  951. }
  952. shader_cache.CreateChannel(channel);
  953. query_cache.CreateChannel(channel);
  954. state_tracker.SetupTables(channel);
  955. }
  956. void RasterizerOpenGL::BindChannel(Tegra::Control::ChannelState& channel) {
  957. const s32 channel_id = channel.bind_id;
  958. BindToChannel(channel_id);
  959. {
  960. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  961. texture_cache.BindToChannel(channel_id);
  962. buffer_cache.BindToChannel(channel_id);
  963. }
  964. shader_cache.BindToChannel(channel_id);
  965. query_cache.BindToChannel(channel_id);
  966. state_tracker.ChangeChannel(channel);
  967. state_tracker.InvalidateState();
  968. }
  969. void RasterizerOpenGL::ReleaseChannel(s32 channel_id) {
  970. EraseChannel(channel_id);
  971. {
  972. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  973. texture_cache.EraseChannel(channel_id);
  974. buffer_cache.EraseChannel(channel_id);
  975. }
  976. shader_cache.EraseChannel(channel_id);
  977. query_cache.EraseChannel(channel_id);
  978. }
  979. AccelerateDMA::AccelerateDMA(BufferCache& buffer_cache_) : buffer_cache{buffer_cache_} {}
  980. bool AccelerateDMA::BufferCopy(GPUVAddr src_address, GPUVAddr dest_address, u64 amount) {
  981. std::scoped_lock lock{buffer_cache.mutex};
  982. return buffer_cache.DMACopy(src_address, dest_address, amount);
  983. }
  984. bool AccelerateDMA::BufferClear(GPUVAddr src_address, u64 amount, u32 value) {
  985. std::scoped_lock lock{buffer_cache.mutex};
  986. return buffer_cache.DMAClear(src_address, amount, value);
  987. }
  988. } // namespace OpenGL