gl_rasterizer.cpp 38 KB

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