gl_rasterizer.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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_staging_buffer_pool.h"
  25. #include "video_core/renderer_opengl/gl_texture_cache.h"
  26. #include "video_core/renderer_opengl/maxwell_to_gl.h"
  27. #include "video_core/renderer_opengl/renderer_opengl.h"
  28. #include "video_core/shader_cache.h"
  29. #include "video_core/texture_cache/texture_cache_base.h"
  30. namespace OpenGL {
  31. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  32. using GLvec4 = std::array<GLfloat, 4>;
  33. using VideoCore::Surface::PixelFormat;
  34. using VideoCore::Surface::SurfaceTarget;
  35. using VideoCore::Surface::SurfaceType;
  36. MICROPROFILE_DEFINE(OpenGL_Drawing, "OpenGL", "Drawing", MP_RGB(128, 128, 192));
  37. MICROPROFILE_DEFINE(OpenGL_Clears, "OpenGL", "Clears", MP_RGB(128, 128, 192));
  38. MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(128, 128, 192));
  39. MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Management", MP_RGB(100, 255, 100));
  40. namespace {
  41. constexpr size_t NUM_SUPPORTED_VERTEX_ATTRIBUTES = 16;
  42. void oglEnable(GLenum cap, bool state) {
  43. (state ? glEnable : glDisable)(cap);
  44. }
  45. } // Anonymous namespace
  46. RasterizerOpenGL::RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_,
  47. Core::Memory::Memory& cpu_memory_, const Device& device_,
  48. ScreenInfo& screen_info_, ProgramManager& program_manager_,
  49. StateTracker& state_tracker_)
  50. : RasterizerAccelerated(cpu_memory_), gpu(gpu_), device(device_), screen_info(screen_info_),
  51. program_manager(program_manager_), state_tracker(state_tracker_),
  52. texture_cache_runtime(device, program_manager, state_tracker, staging_buffer_pool),
  53. texture_cache(texture_cache_runtime, *this),
  54. buffer_cache_runtime(device, staging_buffer_pool),
  55. buffer_cache(*this, cpu_memory_, buffer_cache_runtime),
  56. shader_cache(*this, emu_window_, device, texture_cache, buffer_cache, program_manager,
  57. state_tracker, gpu.ShaderNotify()),
  58. query_cache(*this, cpu_memory_), accelerate_dma(buffer_cache, texture_cache),
  59. fence_manager(*this, gpu, texture_cache, buffer_cache, query_cache),
  60. blit_image(program_manager_) {}
  61. RasterizerOpenGL::~RasterizerOpenGL() = default;
  62. void RasterizerOpenGL::SyncVertexFormats() {
  63. auto& flags = maxwell3d->dirty.flags;
  64. if (!flags[Dirty::VertexFormats]) {
  65. return;
  66. }
  67. flags[Dirty::VertexFormats] = false;
  68. // Use the vertex array as-is, assumes that the data is formatted correctly for OpenGL. Enables
  69. // the first 16 vertex attributes always, as we don't know which ones are actually used until
  70. // shader time. Note, Tegra technically supports 32, but we're capping this to 16 for now to
  71. // avoid OpenGL errors.
  72. // TODO(Subv): Analyze the shader to identify which attributes are actually used and don't
  73. // assume every shader uses them all.
  74. for (std::size_t index = 0; index < NUM_SUPPORTED_VERTEX_ATTRIBUTES; ++index) {
  75. if (!flags[Dirty::VertexFormat0 + index]) {
  76. continue;
  77. }
  78. flags[Dirty::VertexFormat0 + index] = false;
  79. const auto& attrib = maxwell3d->regs.vertex_attrib_format[index];
  80. const auto gl_index = static_cast<GLuint>(index);
  81. // Disable constant attributes.
  82. if (attrib.constant) {
  83. glDisableVertexAttribArray(gl_index);
  84. continue;
  85. }
  86. glEnableVertexAttribArray(gl_index);
  87. if (attrib.type == Maxwell::VertexAttribute::Type::SInt ||
  88. attrib.type == Maxwell::VertexAttribute::Type::UInt) {
  89. glVertexAttribIFormat(gl_index, attrib.ComponentCount(),
  90. MaxwellToGL::VertexFormat(attrib), attrib.offset);
  91. } else {
  92. glVertexAttribFormat(gl_index, attrib.ComponentCount(),
  93. MaxwellToGL::VertexFormat(attrib),
  94. attrib.IsNormalized() ? GL_TRUE : GL_FALSE, attrib.offset);
  95. }
  96. glVertexAttribBinding(gl_index, attrib.buffer);
  97. }
  98. }
  99. void RasterizerOpenGL::SyncVertexInstances() {
  100. auto& flags = maxwell3d->dirty.flags;
  101. if (!flags[Dirty::VertexInstances]) {
  102. return;
  103. }
  104. flags[Dirty::VertexInstances] = false;
  105. const auto& regs = maxwell3d->regs;
  106. for (std::size_t index = 0; index < NUM_SUPPORTED_VERTEX_ATTRIBUTES; ++index) {
  107. if (!flags[Dirty::VertexInstance0 + index]) {
  108. continue;
  109. }
  110. flags[Dirty::VertexInstance0 + index] = false;
  111. const auto gl_index = static_cast<GLuint>(index);
  112. const bool instancing_enabled = regs.vertex_stream_instances.IsInstancingEnabled(gl_index);
  113. const GLuint divisor = instancing_enabled ? regs.vertex_streams[index].frequency : 0;
  114. glVertexBindingDivisor(gl_index, divisor);
  115. }
  116. }
  117. void RasterizerOpenGL::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
  118. const VideoCore::DiskResourceLoadCallback& callback) {
  119. shader_cache.LoadDiskResources(title_id, stop_loading, callback);
  120. }
  121. void RasterizerOpenGL::Clear(u32 layer_count) {
  122. MICROPROFILE_SCOPE(OpenGL_Clears);
  123. gpu_memory->FlushCaching();
  124. const auto& regs = maxwell3d->regs;
  125. bool use_color{};
  126. bool use_depth{};
  127. bool use_stencil{};
  128. if (regs.clear_surface.R || regs.clear_surface.G || regs.clear_surface.B ||
  129. regs.clear_surface.A) {
  130. use_color = true;
  131. const GLuint index = regs.clear_surface.RT;
  132. state_tracker.NotifyColorMask(index);
  133. glColorMaski(index, regs.clear_surface.R != 0, regs.clear_surface.G != 0,
  134. regs.clear_surface.B != 0, regs.clear_surface.A != 0);
  135. // TODO(Rodrigo): Determine if clamping is used on clears
  136. SyncFragmentColorClampState();
  137. SyncFramebufferSRGB();
  138. }
  139. if (regs.clear_surface.Z) {
  140. ASSERT_MSG(regs.zeta_enable != 0, "Tried to clear Z but buffer is not enabled!");
  141. use_depth = true;
  142. state_tracker.NotifyDepthMask();
  143. glDepthMask(GL_TRUE);
  144. }
  145. if (regs.clear_surface.S) {
  146. ASSERT_MSG(regs.zeta_enable, "Tried to clear stencil but buffer is not enabled!");
  147. use_stencil = true;
  148. }
  149. if (!use_color && !use_depth && !use_stencil) {
  150. // No color surface nor depth/stencil surface are enabled
  151. return;
  152. }
  153. SyncRasterizeEnable();
  154. SyncStencilTestState();
  155. std::scoped_lock lock{texture_cache.mutex};
  156. texture_cache.UpdateRenderTargets(true);
  157. state_tracker.BindFramebuffer(texture_cache.GetFramebuffer()->Handle());
  158. SyncViewport();
  159. if (regs.clear_control.use_scissor) {
  160. SyncScissorTest();
  161. } else {
  162. state_tracker.NotifyScissor0();
  163. glDisablei(GL_SCISSOR_TEST, 0);
  164. }
  165. UNIMPLEMENTED_IF(regs.clear_control.use_viewport_clip0);
  166. if (use_color) {
  167. glClearBufferfv(GL_COLOR, regs.clear_surface.RT, regs.clear_color.data());
  168. }
  169. if (use_depth && use_stencil) {
  170. glClearBufferfi(GL_DEPTH_STENCIL, 0, regs.clear_depth, regs.clear_stencil);
  171. } else if (use_depth) {
  172. glClearBufferfv(GL_DEPTH, 0, &regs.clear_depth);
  173. } else if (use_stencil) {
  174. glClearBufferiv(GL_STENCIL, 0, &regs.clear_stencil);
  175. }
  176. ++num_queued_commands;
  177. }
  178. template <typename Func>
  179. void RasterizerOpenGL::PrepareDraw(bool is_indexed, Func&& draw_func) {
  180. MICROPROFILE_SCOPE(OpenGL_Drawing);
  181. SCOPE_EXIT({ gpu.TickWork(); });
  182. gpu_memory->FlushCaching();
  183. query_cache.UpdateCounters();
  184. GraphicsPipeline* const pipeline{shader_cache.CurrentGraphicsPipeline()};
  185. if (!pipeline) {
  186. return;
  187. }
  188. gpu.TickWork();
  189. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  190. if (pipeline->UsesLocalMemory()) {
  191. program_manager.LocalMemoryWarmup();
  192. }
  193. pipeline->SetEngine(maxwell3d, gpu_memory);
  194. pipeline->Configure(is_indexed);
  195. SyncState();
  196. const auto& draw_state = maxwell3d->draw_manager->GetDrawState();
  197. const GLenum primitive_mode = MaxwellToGL::PrimitiveTopology(draw_state.topology);
  198. BeginTransformFeedback(pipeline, primitive_mode);
  199. draw_func(primitive_mode);
  200. EndTransformFeedback();
  201. ++num_queued_commands;
  202. has_written_global_memory |= pipeline->WritesGlobalMemory();
  203. }
  204. void RasterizerOpenGL::Draw(bool is_indexed, u32 instance_count) {
  205. PrepareDraw(is_indexed, [this, is_indexed, instance_count](GLenum primitive_mode) {
  206. const auto& draw_state = maxwell3d->draw_manager->GetDrawState();
  207. const GLuint base_instance = static_cast<GLuint>(draw_state.base_instance);
  208. const GLsizei num_instances = static_cast<GLsizei>(instance_count);
  209. if (is_indexed) {
  210. const GLint base_vertex = static_cast<GLint>(draw_state.base_index);
  211. const GLsizei num_vertices = static_cast<GLsizei>(draw_state.index_buffer.count);
  212. const GLvoid* const offset = buffer_cache_runtime.IndexOffset();
  213. const GLenum format = MaxwellToGL::IndexFormat(draw_state.index_buffer.format);
  214. if (num_instances == 1 && base_instance == 0 && base_vertex == 0) {
  215. glDrawElements(primitive_mode, num_vertices, format, offset);
  216. } else if (num_instances == 1 && base_instance == 0) {
  217. glDrawElementsBaseVertex(primitive_mode, num_vertices, format, offset, base_vertex);
  218. } else if (base_vertex == 0 && base_instance == 0) {
  219. glDrawElementsInstanced(primitive_mode, num_vertices, format, offset,
  220. num_instances);
  221. } else if (base_vertex == 0) {
  222. glDrawElementsInstancedBaseInstance(primitive_mode, num_vertices, format, offset,
  223. num_instances, base_instance);
  224. } else if (base_instance == 0) {
  225. glDrawElementsInstancedBaseVertex(primitive_mode, num_vertices, format, offset,
  226. num_instances, base_vertex);
  227. } else {
  228. glDrawElementsInstancedBaseVertexBaseInstance(primitive_mode, num_vertices, format,
  229. offset, num_instances, base_vertex,
  230. base_instance);
  231. }
  232. } else {
  233. const GLint base_vertex = static_cast<GLint>(draw_state.vertex_buffer.first);
  234. const GLsizei num_vertices = static_cast<GLsizei>(draw_state.vertex_buffer.count);
  235. if (num_instances == 1 && base_instance == 0) {
  236. glDrawArrays(primitive_mode, base_vertex, num_vertices);
  237. } else if (base_instance == 0) {
  238. glDrawArraysInstanced(primitive_mode, base_vertex, num_vertices, num_instances);
  239. } else {
  240. glDrawArraysInstancedBaseInstance(primitive_mode, base_vertex, num_vertices,
  241. num_instances, base_instance);
  242. }
  243. }
  244. });
  245. }
  246. void RasterizerOpenGL::DrawIndirect() {
  247. const auto& params = maxwell3d->draw_manager->GetIndirectParams();
  248. buffer_cache.SetDrawIndirect(&params);
  249. PrepareDraw(params.is_indexed, [this, &params](GLenum primitive_mode) {
  250. const auto [buffer, offset] = buffer_cache.GetDrawIndirectBuffer();
  251. const GLvoid* const gl_offset =
  252. reinterpret_cast<const GLvoid*>(static_cast<uintptr_t>(offset));
  253. glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer->Handle());
  254. if (params.include_count) {
  255. const auto [draw_buffer, offset_base] = buffer_cache.GetDrawIndirectCount();
  256. glBindBuffer(GL_PARAMETER_BUFFER, draw_buffer->Handle());
  257. if (params.is_indexed) {
  258. const GLenum format = MaxwellToGL::IndexFormat(maxwell3d->regs.index_buffer.format);
  259. glMultiDrawElementsIndirectCount(primitive_mode, format, gl_offset,
  260. static_cast<GLintptr>(offset_base),
  261. static_cast<GLsizei>(params.max_draw_counts),
  262. static_cast<GLsizei>(params.stride));
  263. } else {
  264. glMultiDrawArraysIndirectCount(primitive_mode, gl_offset,
  265. static_cast<GLintptr>(offset_base),
  266. static_cast<GLsizei>(params.max_draw_counts),
  267. static_cast<GLsizei>(params.stride));
  268. }
  269. return;
  270. }
  271. if (params.is_indexed) {
  272. const GLenum format = MaxwellToGL::IndexFormat(maxwell3d->regs.index_buffer.format);
  273. glMultiDrawElementsIndirect(primitive_mode, format, gl_offset,
  274. static_cast<GLsizei>(params.max_draw_counts),
  275. static_cast<GLsizei>(params.stride));
  276. } else {
  277. glMultiDrawArraysIndirect(primitive_mode, gl_offset,
  278. static_cast<GLsizei>(params.max_draw_counts),
  279. static_cast<GLsizei>(params.stride));
  280. }
  281. });
  282. buffer_cache.SetDrawIndirect(nullptr);
  283. }
  284. void RasterizerOpenGL::DrawTexture() {
  285. MICROPROFILE_SCOPE(OpenGL_Drawing);
  286. SCOPE_EXIT({ gpu.TickWork(); });
  287. query_cache.UpdateCounters();
  288. texture_cache.SynchronizeGraphicsDescriptors();
  289. texture_cache.UpdateRenderTargets(false);
  290. SyncState();
  291. const auto& draw_texture_state = maxwell3d->draw_manager->GetDrawTextureState();
  292. const auto& sampler = texture_cache.GetGraphicsSampler(draw_texture_state.src_sampler);
  293. const auto& texture = texture_cache.GetImageView(draw_texture_state.src_texture);
  294. if (device.HasDrawTexture()) {
  295. state_tracker.BindFramebuffer(texture_cache.GetFramebuffer()->Handle());
  296. glDrawTextureNV(texture.DefaultHandle(), sampler->Handle(), draw_texture_state.dst_x0,
  297. draw_texture_state.dst_y0, draw_texture_state.dst_x1,
  298. draw_texture_state.dst_y1, 0,
  299. draw_texture_state.src_x0 / static_cast<float>(texture.size.width),
  300. draw_texture_state.src_y0 / static_cast<float>(texture.size.height),
  301. draw_texture_state.src_x1 / static_cast<float>(texture.size.width),
  302. draw_texture_state.src_y1 / static_cast<float>(texture.size.height));
  303. } else {
  304. Region2D dst_region = {Offset2D{.x = static_cast<s32>(draw_texture_state.dst_x0),
  305. .y = static_cast<s32>(draw_texture_state.dst_y0)},
  306. Offset2D{.x = static_cast<s32>(draw_texture_state.dst_x1),
  307. .y = static_cast<s32>(draw_texture_state.dst_y1)}};
  308. Region2D src_region = {Offset2D{.x = static_cast<s32>(draw_texture_state.src_x0),
  309. .y = static_cast<s32>(draw_texture_state.src_y0)},
  310. Offset2D{.x = static_cast<s32>(draw_texture_state.src_x1),
  311. .y = static_cast<s32>(draw_texture_state.src_y1)}};
  312. blit_image.BlitColor(texture_cache.GetFramebuffer()->Handle(), texture.DefaultHandle(),
  313. sampler->Handle(), dst_region, src_region, texture.size);
  314. state_tracker.InvalidateState();
  315. }
  316. ++num_queued_commands;
  317. }
  318. void RasterizerOpenGL::DispatchCompute() {
  319. gpu_memory->FlushCaching();
  320. ComputePipeline* const pipeline{shader_cache.CurrentComputePipeline()};
  321. if (!pipeline) {
  322. return;
  323. }
  324. if (pipeline->UsesLocalMemory()) {
  325. program_manager.LocalMemoryWarmup();
  326. }
  327. pipeline->SetEngine(kepler_compute, gpu_memory);
  328. pipeline->Configure();
  329. const auto& qmd{kepler_compute->launch_description};
  330. auto indirect_address = kepler_compute->GetIndirectComputeAddress();
  331. if (indirect_address) {
  332. // DispatchIndirect
  333. static constexpr auto sync_info = VideoCommon::ObtainBufferSynchronize::FullSynchronize;
  334. const auto post_op = VideoCommon::ObtainBufferOperation::DiscardWrite;
  335. const auto [buffer, offset] =
  336. buffer_cache.ObtainBuffer(*indirect_address, 12, sync_info, post_op);
  337. glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, buffer->Handle());
  338. glDispatchComputeIndirect(static_cast<GLintptr>(offset));
  339. return;
  340. }
  341. glDispatchCompute(qmd.grid_dim_x, qmd.grid_dim_y, qmd.grid_dim_z);
  342. ++num_queued_commands;
  343. has_written_global_memory |= pipeline->WritesGlobalMemory();
  344. }
  345. void RasterizerOpenGL::ResetCounter(VideoCore::QueryType type) {
  346. query_cache.ResetCounter(type);
  347. }
  348. void RasterizerOpenGL::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
  349. std::optional<u64> timestamp) {
  350. query_cache.Query(gpu_addr, type, timestamp);
  351. }
  352. void RasterizerOpenGL::BindGraphicsUniformBuffer(size_t stage, u32 index, GPUVAddr gpu_addr,
  353. u32 size) {
  354. std::scoped_lock lock{buffer_cache.mutex};
  355. buffer_cache.BindGraphicsUniformBuffer(stage, index, gpu_addr, size);
  356. }
  357. void RasterizerOpenGL::DisableGraphicsUniformBuffer(size_t stage, u32 index) {
  358. buffer_cache.DisableGraphicsUniformBuffer(stage, index);
  359. }
  360. void RasterizerOpenGL::FlushAll() {}
  361. void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size, VideoCommon::CacheType which) {
  362. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  363. if (addr == 0 || size == 0) {
  364. return;
  365. }
  366. if (True(which & VideoCommon::CacheType::TextureCache)) {
  367. std::scoped_lock lock{texture_cache.mutex};
  368. texture_cache.DownloadMemory(addr, size);
  369. }
  370. if ((True(which & VideoCommon::CacheType::BufferCache))) {
  371. std::scoped_lock lock{buffer_cache.mutex};
  372. buffer_cache.DownloadMemory(addr, size);
  373. }
  374. if ((True(which & VideoCommon::CacheType::QueryCache))) {
  375. query_cache.FlushRegion(addr, size);
  376. }
  377. }
  378. bool RasterizerOpenGL::MustFlushRegion(VAddr addr, u64 size, VideoCommon::CacheType which) {
  379. if ((True(which & VideoCommon::CacheType::BufferCache))) {
  380. std::scoped_lock lock{buffer_cache.mutex};
  381. if (buffer_cache.IsRegionGpuModified(addr, size)) {
  382. return true;
  383. }
  384. }
  385. if (!Settings::IsGPULevelHigh()) {
  386. return false;
  387. }
  388. if (True(which & VideoCommon::CacheType::TextureCache)) {
  389. std::scoped_lock lock{texture_cache.mutex};
  390. return texture_cache.IsRegionGpuModified(addr, size);
  391. }
  392. return false;
  393. }
  394. VideoCore::RasterizerDownloadArea RasterizerOpenGL::GetFlushArea(VAddr addr, u64 size) {
  395. {
  396. std::scoped_lock lock{texture_cache.mutex};
  397. auto area = texture_cache.GetFlushArea(addr, size);
  398. if (area) {
  399. return *area;
  400. }
  401. }
  402. {
  403. std::scoped_lock lock{buffer_cache.mutex};
  404. auto area = buffer_cache.GetFlushArea(addr, size);
  405. if (area) {
  406. return *area;
  407. }
  408. }
  409. VideoCore::RasterizerDownloadArea new_area{
  410. .start_address = Common::AlignDown(addr, Core::Memory::YUZU_PAGESIZE),
  411. .end_address = Common::AlignUp(addr + size, Core::Memory::YUZU_PAGESIZE),
  412. .preemtive = true,
  413. };
  414. return new_area;
  415. }
  416. void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size, VideoCommon::CacheType which) {
  417. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  418. if (addr == 0 || size == 0) {
  419. return;
  420. }
  421. if (True(which & VideoCommon::CacheType::TextureCache)) {
  422. std::scoped_lock lock{texture_cache.mutex};
  423. texture_cache.WriteMemory(addr, size);
  424. }
  425. if (True(which & VideoCommon::CacheType::BufferCache)) {
  426. std::scoped_lock lock{buffer_cache.mutex};
  427. buffer_cache.WriteMemory(addr, size);
  428. }
  429. if (True(which & VideoCommon::CacheType::ShaderCache)) {
  430. shader_cache.InvalidateRegion(addr, size);
  431. }
  432. if (True(which & VideoCommon::CacheType::QueryCache)) {
  433. query_cache.InvalidateRegion(addr, size);
  434. }
  435. }
  436. bool RasterizerOpenGL::OnCPUWrite(VAddr addr, u64 size) {
  437. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  438. if (addr == 0 || size == 0) {
  439. return false;
  440. }
  441. {
  442. std::scoped_lock lock{buffer_cache.mutex};
  443. if (buffer_cache.OnCPUWrite(addr, size)) {
  444. return true;
  445. }
  446. }
  447. {
  448. std::scoped_lock lock{texture_cache.mutex};
  449. texture_cache.WriteMemory(addr, size);
  450. }
  451. shader_cache.InvalidateRegion(addr, size);
  452. return false;
  453. }
  454. void RasterizerOpenGL::OnCacheInvalidation(VAddr addr, u64 size) {
  455. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  456. if (addr == 0 || size == 0) {
  457. return;
  458. }
  459. {
  460. std::scoped_lock lock{texture_cache.mutex};
  461. texture_cache.WriteMemory(addr, size);
  462. }
  463. {
  464. std::scoped_lock lock{buffer_cache.mutex};
  465. buffer_cache.CachedWriteMemory(addr, size);
  466. }
  467. shader_cache.InvalidateRegion(addr, size);
  468. }
  469. void RasterizerOpenGL::InvalidateGPUCache() {
  470. gpu.InvalidateGPUCache();
  471. }
  472. void RasterizerOpenGL::UnmapMemory(VAddr addr, u64 size) {
  473. {
  474. std::scoped_lock lock{texture_cache.mutex};
  475. texture_cache.UnmapMemory(addr, size);
  476. }
  477. {
  478. std::scoped_lock lock{buffer_cache.mutex};
  479. buffer_cache.WriteMemory(addr, size);
  480. }
  481. shader_cache.OnCacheInvalidation(addr, size);
  482. }
  483. void RasterizerOpenGL::ModifyGPUMemory(size_t as_id, GPUVAddr addr, u64 size) {
  484. {
  485. std::scoped_lock lock{texture_cache.mutex};
  486. texture_cache.UnmapGPUMemory(as_id, addr, size);
  487. }
  488. }
  489. void RasterizerOpenGL::SignalFence(std::function<void()>&& func) {
  490. fence_manager.SignalFence(std::move(func));
  491. }
  492. void RasterizerOpenGL::SyncOperation(std::function<void()>&& func) {
  493. fence_manager.SyncOperation(std::move(func));
  494. }
  495. void RasterizerOpenGL::SignalSyncPoint(u32 value) {
  496. fence_manager.SignalSyncPoint(value);
  497. }
  498. void RasterizerOpenGL::SignalReference() {
  499. fence_manager.SignalOrdering();
  500. }
  501. void RasterizerOpenGL::ReleaseFences() {
  502. fence_manager.WaitPendingFences();
  503. }
  504. void RasterizerOpenGL::FlushAndInvalidateRegion(VAddr addr, u64 size,
  505. VideoCommon::CacheType which) {
  506. if (Settings::IsGPULevelExtreme()) {
  507. FlushRegion(addr, size, which);
  508. }
  509. InvalidateRegion(addr, size, which);
  510. }
  511. void RasterizerOpenGL::WaitForIdle() {
  512. glMemoryBarrier(GL_ALL_BARRIER_BITS);
  513. SignalReference();
  514. }
  515. void RasterizerOpenGL::FragmentBarrier() {
  516. glTextureBarrier();
  517. glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT);
  518. }
  519. void RasterizerOpenGL::TiledCacheBarrier() {
  520. glTextureBarrier();
  521. }
  522. void RasterizerOpenGL::FlushCommands() {
  523. // Only flush when we have commands queued to OpenGL.
  524. if (num_queued_commands == 0) {
  525. return;
  526. }
  527. num_queued_commands = 0;
  528. // Make sure memory stored from the previous GL command stream is visible
  529. // This is only needed on assembly shaders where we write to GPU memory with raw pointers
  530. if (has_written_global_memory) {
  531. has_written_global_memory = false;
  532. glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
  533. }
  534. glFlush();
  535. }
  536. void RasterizerOpenGL::TickFrame() {
  537. // Ticking a frame means that buffers will be swapped, calling glFlush implicitly.
  538. num_queued_commands = 0;
  539. fence_manager.TickFrame();
  540. {
  541. std::scoped_lock lock{texture_cache.mutex};
  542. texture_cache.TickFrame();
  543. }
  544. {
  545. std::scoped_lock lock{buffer_cache.mutex};
  546. buffer_cache.TickFrame();
  547. }
  548. }
  549. bool RasterizerOpenGL::AccelerateConditionalRendering() {
  550. gpu_memory->FlushCaching();
  551. if (Settings::IsGPULevelHigh()) {
  552. // Reimplement Host conditional rendering.
  553. return false;
  554. }
  555. // Medium / Low Hack: stub any checks on queries written into the buffer cache.
  556. const GPUVAddr condition_address{maxwell3d->regs.render_enable.Address()};
  557. Maxwell::ReportSemaphore::Compare cmp;
  558. if (gpu_memory->IsMemoryDirty(condition_address, sizeof(cmp),
  559. VideoCommon::CacheType::BufferCache)) {
  560. return true;
  561. }
  562. return false;
  563. }
  564. bool RasterizerOpenGL::AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Surface& src,
  565. const Tegra::Engines::Fermi2D::Surface& dst,
  566. const Tegra::Engines::Fermi2D::Config& copy_config) {
  567. MICROPROFILE_SCOPE(OpenGL_Blits);
  568. std::scoped_lock lock{texture_cache.mutex};
  569. return texture_cache.BlitImage(dst, src, copy_config);
  570. }
  571. Tegra::Engines::AccelerateDMAInterface& RasterizerOpenGL::AccessAccelerateDMA() {
  572. return accelerate_dma;
  573. }
  574. void RasterizerOpenGL::AccelerateInlineToMemory(GPUVAddr address, size_t copy_size,
  575. std::span<const u8> memory) {
  576. auto cpu_addr = gpu_memory->GpuToCpuAddress(address);
  577. if (!cpu_addr) [[unlikely]] {
  578. gpu_memory->WriteBlock(address, memory.data(), copy_size);
  579. return;
  580. }
  581. gpu_memory->WriteBlockUnsafe(address, memory.data(), copy_size);
  582. {
  583. std::unique_lock<std::recursive_mutex> lock{buffer_cache.mutex};
  584. if (!buffer_cache.InlineMemory(*cpu_addr, copy_size, memory)) {
  585. buffer_cache.WriteMemory(*cpu_addr, copy_size);
  586. }
  587. }
  588. {
  589. std::scoped_lock lock_texture{texture_cache.mutex};
  590. texture_cache.WriteMemory(*cpu_addr, copy_size);
  591. }
  592. shader_cache.InvalidateRegion(*cpu_addr, copy_size);
  593. query_cache.InvalidateRegion(*cpu_addr, copy_size);
  594. }
  595. bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& config,
  596. VAddr framebuffer_addr, u32 pixel_stride) {
  597. if (framebuffer_addr == 0) {
  598. return false;
  599. }
  600. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  601. std::scoped_lock lock{texture_cache.mutex};
  602. ImageView* const image_view{texture_cache.TryFindFramebufferImageView(framebuffer_addr)};
  603. if (!image_view) {
  604. return false;
  605. }
  606. // Verify that the cached surface is the same size and format as the requested framebuffer
  607. // ASSERT_MSG(image_view->size.width == config.width, "Framebuffer width is different");
  608. // ASSERT_MSG(image_view->size.height == config.height, "Framebuffer height is different");
  609. screen_info.texture.width = image_view->size.width;
  610. screen_info.texture.height = image_view->size.height;
  611. screen_info.display_texture = image_view->Handle(Shader::TextureType::Color2D);
  612. screen_info.display_srgb = VideoCore::Surface::IsPixelFormatSRGB(image_view->format);
  613. return true;
  614. }
  615. void RasterizerOpenGL::SyncState() {
  616. SyncViewport();
  617. SyncRasterizeEnable();
  618. SyncPolygonModes();
  619. SyncColorMask();
  620. SyncFragmentColorClampState();
  621. SyncMultiSampleState();
  622. SyncDepthTestState();
  623. SyncDepthClamp();
  624. SyncStencilTestState();
  625. SyncBlendState();
  626. SyncLogicOpState();
  627. SyncCullMode();
  628. SyncPrimitiveRestart();
  629. SyncScissorTest();
  630. SyncPointState();
  631. SyncLineState();
  632. SyncPolygonOffset();
  633. SyncAlphaTest();
  634. SyncFramebufferSRGB();
  635. SyncVertexFormats();
  636. SyncVertexInstances();
  637. }
  638. void RasterizerOpenGL::SyncViewport() {
  639. auto& flags = maxwell3d->dirty.flags;
  640. const auto& regs = maxwell3d->regs;
  641. const bool rescale_viewports = flags[VideoCommon::Dirty::RescaleViewports];
  642. const bool dirty_viewport = flags[Dirty::Viewports] || rescale_viewports;
  643. const bool dirty_clip_control = flags[Dirty::ClipControl];
  644. if (dirty_viewport || dirty_clip_control || flags[Dirty::FrontFace]) {
  645. flags[Dirty::FrontFace] = false;
  646. GLenum mode = MaxwellToGL::FrontFace(regs.gl_front_face);
  647. bool flip_faces = true;
  648. if (regs.window_origin.flip_y != 0) {
  649. flip_faces = !flip_faces;
  650. }
  651. if (regs.viewport_transform[0].scale_y < 0.0f) {
  652. flip_faces = !flip_faces;
  653. }
  654. if (flip_faces) {
  655. switch (mode) {
  656. case GL_CW:
  657. mode = GL_CCW;
  658. break;
  659. case GL_CCW:
  660. mode = GL_CW;
  661. break;
  662. }
  663. }
  664. glFrontFace(mode);
  665. }
  666. if (dirty_viewport || dirty_clip_control) {
  667. flags[Dirty::ClipControl] = false;
  668. bool flip_y = false;
  669. if (regs.viewport_transform[0].scale_y < 0.0f) {
  670. flip_y = !flip_y;
  671. }
  672. const bool lower_left{regs.window_origin.mode != Maxwell::WindowOrigin::Mode::UpperLeft};
  673. if (lower_left) {
  674. flip_y = !flip_y;
  675. }
  676. const bool is_zero_to_one = regs.depth_mode == Maxwell::DepthMode::ZeroToOne;
  677. const GLenum origin = flip_y ? GL_UPPER_LEFT : GL_LOWER_LEFT;
  678. const GLenum depth = is_zero_to_one ? GL_ZERO_TO_ONE : GL_NEGATIVE_ONE_TO_ONE;
  679. state_tracker.ClipControl(origin, depth);
  680. state_tracker.SetYNegate(lower_left);
  681. }
  682. const bool is_rescaling{texture_cache.IsRescaling()};
  683. const float scale = is_rescaling ? Settings::values.resolution_info.up_factor : 1.0f;
  684. const auto conv = [scale](float value) -> GLfloat {
  685. float new_value = value * scale;
  686. if (scale < 1.0f) {
  687. const bool sign = std::signbit(value);
  688. new_value = std::round(std::abs(new_value));
  689. new_value = sign ? -new_value : new_value;
  690. }
  691. return static_cast<GLfloat>(new_value);
  692. };
  693. if (dirty_viewport) {
  694. flags[Dirty::Viewports] = false;
  695. const bool force = flags[Dirty::ViewportTransform] || rescale_viewports;
  696. flags[Dirty::ViewportTransform] = false;
  697. flags[VideoCommon::Dirty::RescaleViewports] = false;
  698. for (size_t index = 0; index < Maxwell::NumViewports; ++index) {
  699. if (!force && !flags[Dirty::Viewport0 + index]) {
  700. continue;
  701. }
  702. flags[Dirty::Viewport0 + index] = false;
  703. if (!regs.viewport_scale_offset_enabled) {
  704. const auto x = static_cast<GLfloat>(regs.surface_clip.x);
  705. const auto y = static_cast<GLfloat>(regs.surface_clip.y);
  706. const auto width = static_cast<GLfloat>(regs.surface_clip.width);
  707. const auto height = static_cast<GLfloat>(regs.surface_clip.height);
  708. glViewportIndexedf(static_cast<GLuint>(index), x, y, width != 0.0f ? width : 1.0f,
  709. height != 0.0f ? height : 1.0f);
  710. continue;
  711. }
  712. const auto& src = regs.viewport_transform[index];
  713. GLfloat x = conv(src.translate_x - src.scale_x);
  714. GLfloat y = conv(src.translate_y - src.scale_y);
  715. GLfloat width = conv(src.scale_x * 2.0f);
  716. GLfloat height = conv(src.scale_y * 2.0f);
  717. if (height < 0) {
  718. y += height;
  719. height = -height;
  720. }
  721. glViewportIndexedf(static_cast<GLuint>(index), x, y, width != 0.0f ? width : 1.0f,
  722. height != 0.0f ? height : 1.0f);
  723. const GLdouble reduce_z = regs.depth_mode == Maxwell::DepthMode::MinusOneToOne;
  724. const GLdouble near_depth = src.translate_z - src.scale_z * reduce_z;
  725. const GLdouble far_depth = src.translate_z + src.scale_z;
  726. if (device.HasDepthBufferFloat()) {
  727. glDepthRangeIndexeddNV(static_cast<GLuint>(index), near_depth, far_depth);
  728. } else {
  729. glDepthRangeIndexed(static_cast<GLuint>(index), near_depth, far_depth);
  730. }
  731. if (!GLAD_GL_NV_viewport_swizzle) {
  732. continue;
  733. }
  734. glViewportSwizzleNV(static_cast<GLuint>(index),
  735. MaxwellToGL::ViewportSwizzle(src.swizzle.x),
  736. MaxwellToGL::ViewportSwizzle(src.swizzle.y),
  737. MaxwellToGL::ViewportSwizzle(src.swizzle.z),
  738. MaxwellToGL::ViewportSwizzle(src.swizzle.w));
  739. }
  740. }
  741. }
  742. void RasterizerOpenGL::SyncDepthClamp() {
  743. auto& flags = maxwell3d->dirty.flags;
  744. if (!flags[Dirty::DepthClampEnabled]) {
  745. return;
  746. }
  747. flags[Dirty::DepthClampEnabled] = false;
  748. bool depth_clamp_disabled{maxwell3d->regs.viewport_clip_control.geometry_clip ==
  749. Maxwell::ViewportClipControl::GeometryClip::Passthrough ||
  750. maxwell3d->regs.viewport_clip_control.geometry_clip ==
  751. Maxwell::ViewportClipControl::GeometryClip::FrustumXYZ ||
  752. maxwell3d->regs.viewport_clip_control.geometry_clip ==
  753. Maxwell::ViewportClipControl::GeometryClip::FrustumZ};
  754. oglEnable(GL_DEPTH_CLAMP, !depth_clamp_disabled);
  755. }
  756. void RasterizerOpenGL::SyncClipEnabled(u32 clip_mask) {
  757. auto& flags = maxwell3d->dirty.flags;
  758. if (!flags[Dirty::ClipDistances] && !flags[VideoCommon::Dirty::Shaders]) {
  759. return;
  760. }
  761. flags[Dirty::ClipDistances] = false;
  762. clip_mask &= maxwell3d->regs.user_clip_enable.raw;
  763. if (clip_mask == last_clip_distance_mask) {
  764. return;
  765. }
  766. last_clip_distance_mask = clip_mask;
  767. for (std::size_t i = 0; i < Maxwell::Regs::NumClipDistances; ++i) {
  768. oglEnable(static_cast<GLenum>(GL_CLIP_DISTANCE0 + i), (clip_mask >> i) & 1);
  769. }
  770. }
  771. void RasterizerOpenGL::SyncClipCoef() {
  772. UNIMPLEMENTED();
  773. }
  774. void RasterizerOpenGL::SyncCullMode() {
  775. auto& flags = maxwell3d->dirty.flags;
  776. const auto& regs = maxwell3d->regs;
  777. if (flags[Dirty::CullTest]) {
  778. flags[Dirty::CullTest] = false;
  779. if (regs.gl_cull_test_enabled) {
  780. glEnable(GL_CULL_FACE);
  781. glCullFace(MaxwellToGL::CullFace(regs.gl_cull_face));
  782. } else {
  783. glDisable(GL_CULL_FACE);
  784. }
  785. }
  786. }
  787. void RasterizerOpenGL::SyncPrimitiveRestart() {
  788. auto& flags = maxwell3d->dirty.flags;
  789. if (!flags[Dirty::PrimitiveRestart]) {
  790. return;
  791. }
  792. flags[Dirty::PrimitiveRestart] = false;
  793. if (maxwell3d->regs.primitive_restart.enabled) {
  794. glEnable(GL_PRIMITIVE_RESTART);
  795. glPrimitiveRestartIndex(maxwell3d->regs.primitive_restart.index);
  796. } else {
  797. glDisable(GL_PRIMITIVE_RESTART);
  798. }
  799. }
  800. void RasterizerOpenGL::SyncDepthTestState() {
  801. auto& flags = maxwell3d->dirty.flags;
  802. const auto& regs = maxwell3d->regs;
  803. if (flags[Dirty::DepthMask]) {
  804. flags[Dirty::DepthMask] = false;
  805. glDepthMask(regs.depth_write_enabled ? GL_TRUE : GL_FALSE);
  806. }
  807. if (flags[Dirty::DepthTest]) {
  808. flags[Dirty::DepthTest] = false;
  809. if (regs.depth_test_enable) {
  810. glEnable(GL_DEPTH_TEST);
  811. glDepthFunc(MaxwellToGL::ComparisonOp(regs.depth_test_func));
  812. } else {
  813. glDisable(GL_DEPTH_TEST);
  814. }
  815. }
  816. }
  817. void RasterizerOpenGL::SyncStencilTestState() {
  818. auto& flags = maxwell3d->dirty.flags;
  819. if (!flags[Dirty::StencilTest]) {
  820. return;
  821. }
  822. flags[Dirty::StencilTest] = false;
  823. const auto& regs = maxwell3d->regs;
  824. oglEnable(GL_STENCIL_TEST, regs.stencil_enable);
  825. glStencilFuncSeparate(GL_FRONT, MaxwellToGL::ComparisonOp(regs.stencil_front_op.func),
  826. regs.stencil_front_ref, regs.stencil_front_func_mask);
  827. glStencilOpSeparate(GL_FRONT, MaxwellToGL::StencilOp(regs.stencil_front_op.fail),
  828. MaxwellToGL::StencilOp(regs.stencil_front_op.zfail),
  829. MaxwellToGL::StencilOp(regs.stencil_front_op.zpass));
  830. glStencilMaskSeparate(GL_FRONT, regs.stencil_front_mask);
  831. if (regs.stencil_two_side_enable) {
  832. glStencilFuncSeparate(GL_BACK, MaxwellToGL::ComparisonOp(regs.stencil_back_op.func),
  833. regs.stencil_back_ref, regs.stencil_back_func_mask);
  834. glStencilOpSeparate(GL_BACK, MaxwellToGL::StencilOp(regs.stencil_back_op.fail),
  835. MaxwellToGL::StencilOp(regs.stencil_back_op.zfail),
  836. MaxwellToGL::StencilOp(regs.stencil_back_op.zpass));
  837. glStencilMaskSeparate(GL_BACK, regs.stencil_back_mask);
  838. } else {
  839. glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 0, 0xFFFFFFFF);
  840. glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_KEEP);
  841. glStencilMaskSeparate(GL_BACK, 0xFFFFFFFF);
  842. }
  843. }
  844. void RasterizerOpenGL::SyncRasterizeEnable() {
  845. auto& flags = maxwell3d->dirty.flags;
  846. if (!flags[Dirty::RasterizeEnable]) {
  847. return;
  848. }
  849. flags[Dirty::RasterizeEnable] = false;
  850. oglEnable(GL_RASTERIZER_DISCARD, maxwell3d->regs.rasterize_enable == 0);
  851. }
  852. void RasterizerOpenGL::SyncPolygonModes() {
  853. auto& flags = maxwell3d->dirty.flags;
  854. if (!flags[Dirty::PolygonModes]) {
  855. return;
  856. }
  857. flags[Dirty::PolygonModes] = false;
  858. const auto& regs = maxwell3d->regs;
  859. if (regs.fill_via_triangle_mode != Maxwell::FillViaTriangleMode::Disabled) {
  860. if (!GLAD_GL_NV_fill_rectangle) {
  861. LOG_ERROR(Render_OpenGL, "GL_NV_fill_rectangle used and not supported");
  862. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  863. return;
  864. }
  865. flags[Dirty::PolygonModeFront] = true;
  866. flags[Dirty::PolygonModeBack] = true;
  867. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL_RECTANGLE_NV);
  868. return;
  869. }
  870. if (regs.polygon_mode_front == regs.polygon_mode_back) {
  871. flags[Dirty::PolygonModeFront] = false;
  872. flags[Dirty::PolygonModeBack] = false;
  873. glPolygonMode(GL_FRONT_AND_BACK, MaxwellToGL::PolygonMode(regs.polygon_mode_front));
  874. return;
  875. }
  876. if (flags[Dirty::PolygonModeFront]) {
  877. flags[Dirty::PolygonModeFront] = false;
  878. glPolygonMode(GL_FRONT, MaxwellToGL::PolygonMode(regs.polygon_mode_front));
  879. }
  880. if (flags[Dirty::PolygonModeBack]) {
  881. flags[Dirty::PolygonModeBack] = false;
  882. glPolygonMode(GL_BACK, MaxwellToGL::PolygonMode(regs.polygon_mode_back));
  883. }
  884. }
  885. void RasterizerOpenGL::SyncColorMask() {
  886. auto& flags = maxwell3d->dirty.flags;
  887. if (!flags[Dirty::ColorMasks]) {
  888. return;
  889. }
  890. flags[Dirty::ColorMasks] = false;
  891. const bool force = flags[Dirty::ColorMaskCommon];
  892. flags[Dirty::ColorMaskCommon] = false;
  893. const auto& regs = maxwell3d->regs;
  894. if (regs.color_mask_common) {
  895. if (!force && !flags[Dirty::ColorMask0]) {
  896. return;
  897. }
  898. flags[Dirty::ColorMask0] = false;
  899. auto& mask = regs.color_mask[0];
  900. glColorMask(mask.R != 0, mask.B != 0, mask.G != 0, mask.A != 0);
  901. return;
  902. }
  903. // Path without color_mask_common set
  904. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  905. if (!force && !flags[Dirty::ColorMask0 + i]) {
  906. continue;
  907. }
  908. flags[Dirty::ColorMask0 + i] = false;
  909. const auto& mask = regs.color_mask[i];
  910. glColorMaski(static_cast<GLuint>(i), mask.R != 0, mask.G != 0, mask.B != 0, mask.A != 0);
  911. }
  912. }
  913. void RasterizerOpenGL::SyncMultiSampleState() {
  914. auto& flags = maxwell3d->dirty.flags;
  915. if (!flags[Dirty::MultisampleControl]) {
  916. return;
  917. }
  918. flags[Dirty::MultisampleControl] = false;
  919. const auto& regs = maxwell3d->regs;
  920. oglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE, regs.anti_alias_alpha_control.alpha_to_coverage);
  921. oglEnable(GL_SAMPLE_ALPHA_TO_ONE, regs.anti_alias_alpha_control.alpha_to_one);
  922. }
  923. void RasterizerOpenGL::SyncFragmentColorClampState() {
  924. auto& flags = maxwell3d->dirty.flags;
  925. if (!flags[Dirty::FragmentClampColor]) {
  926. return;
  927. }
  928. flags[Dirty::FragmentClampColor] = false;
  929. glClampColor(GL_CLAMP_FRAGMENT_COLOR,
  930. maxwell3d->regs.frag_color_clamp.AnyEnabled() ? GL_TRUE : GL_FALSE);
  931. }
  932. void RasterizerOpenGL::SyncBlendState() {
  933. auto& flags = maxwell3d->dirty.flags;
  934. const auto& regs = maxwell3d->regs;
  935. if (flags[Dirty::BlendColor]) {
  936. flags[Dirty::BlendColor] = false;
  937. glBlendColor(regs.blend_color.r, regs.blend_color.g, regs.blend_color.b,
  938. regs.blend_color.a);
  939. }
  940. // TODO(Rodrigo): Revisit blending, there are several registers we are not reading
  941. if (!flags[Dirty::BlendStates]) {
  942. return;
  943. }
  944. flags[Dirty::BlendStates] = false;
  945. if (!regs.blend_per_target_enabled) {
  946. if (!regs.blend.enable[0]) {
  947. glDisable(GL_BLEND);
  948. return;
  949. }
  950. glEnable(GL_BLEND);
  951. glBlendFuncSeparate(MaxwellToGL::BlendFunc(regs.blend.color_source),
  952. MaxwellToGL::BlendFunc(regs.blend.color_dest),
  953. MaxwellToGL::BlendFunc(regs.blend.alpha_source),
  954. MaxwellToGL::BlendFunc(regs.blend.alpha_dest));
  955. glBlendEquationSeparate(MaxwellToGL::BlendEquation(regs.blend.color_op),
  956. MaxwellToGL::BlendEquation(regs.blend.alpha_op));
  957. return;
  958. }
  959. const bool force = flags[Dirty::BlendIndependentEnabled];
  960. flags[Dirty::BlendIndependentEnabled] = false;
  961. for (std::size_t i = 0; i < Maxwell::NumRenderTargets; ++i) {
  962. if (!force && !flags[Dirty::BlendState0 + i]) {
  963. continue;
  964. }
  965. flags[Dirty::BlendState0 + i] = false;
  966. if (!regs.blend.enable[i]) {
  967. glDisablei(GL_BLEND, static_cast<GLuint>(i));
  968. continue;
  969. }
  970. glEnablei(GL_BLEND, static_cast<GLuint>(i));
  971. const auto& src = regs.blend_per_target[i];
  972. glBlendFuncSeparatei(static_cast<GLuint>(i), MaxwellToGL::BlendFunc(src.color_source),
  973. MaxwellToGL::BlendFunc(src.color_dest),
  974. MaxwellToGL::BlendFunc(src.alpha_source),
  975. MaxwellToGL::BlendFunc(src.alpha_dest));
  976. glBlendEquationSeparatei(static_cast<GLuint>(i), MaxwellToGL::BlendEquation(src.color_op),
  977. MaxwellToGL::BlendEquation(src.alpha_op));
  978. }
  979. }
  980. void RasterizerOpenGL::SyncLogicOpState() {
  981. auto& flags = maxwell3d->dirty.flags;
  982. if (!flags[Dirty::LogicOp]) {
  983. return;
  984. }
  985. flags[Dirty::LogicOp] = false;
  986. const auto& regs = maxwell3d->regs;
  987. if (regs.logic_op.enable) {
  988. glEnable(GL_COLOR_LOGIC_OP);
  989. glLogicOp(MaxwellToGL::LogicOp(regs.logic_op.op));
  990. } else {
  991. glDisable(GL_COLOR_LOGIC_OP);
  992. }
  993. }
  994. void RasterizerOpenGL::SyncScissorTest() {
  995. auto& flags = maxwell3d->dirty.flags;
  996. if (!flags[Dirty::Scissors] && !flags[VideoCommon::Dirty::RescaleScissors]) {
  997. return;
  998. }
  999. flags[Dirty::Scissors] = false;
  1000. const bool force = flags[VideoCommon::Dirty::RescaleScissors];
  1001. flags[VideoCommon::Dirty::RescaleScissors] = false;
  1002. const auto& regs = maxwell3d->regs;
  1003. const auto& resolution = Settings::values.resolution_info;
  1004. const bool is_rescaling{texture_cache.IsRescaling()};
  1005. const u32 up_scale = is_rescaling ? resolution.up_scale : 1U;
  1006. const u32 down_shift = is_rescaling ? resolution.down_shift : 0U;
  1007. const auto scale_up = [up_scale, down_shift](u32 value) -> u32 {
  1008. if (value == 0) {
  1009. return 0U;
  1010. }
  1011. const u32 upset = value * up_scale;
  1012. u32 acumm{};
  1013. if ((up_scale >> down_shift) == 0) {
  1014. acumm = upset % 2;
  1015. }
  1016. const u32 converted_value = upset >> down_shift;
  1017. return std::max<u32>(converted_value + acumm, 1U);
  1018. };
  1019. for (std::size_t index = 0; index < Maxwell::NumViewports; ++index) {
  1020. if (!force && !flags[Dirty::Scissor0 + index]) {
  1021. continue;
  1022. }
  1023. flags[Dirty::Scissor0 + index] = false;
  1024. const auto& src = regs.scissor_test[index];
  1025. if (src.enable) {
  1026. glEnablei(GL_SCISSOR_TEST, static_cast<GLuint>(index));
  1027. glScissorIndexed(static_cast<GLuint>(index), scale_up(src.min_x), scale_up(src.min_y),
  1028. scale_up(src.max_x - src.min_x), scale_up(src.max_y - src.min_y));
  1029. } else {
  1030. glDisablei(GL_SCISSOR_TEST, static_cast<GLuint>(index));
  1031. }
  1032. }
  1033. }
  1034. void RasterizerOpenGL::SyncPointState() {
  1035. auto& flags = maxwell3d->dirty.flags;
  1036. if (!flags[Dirty::PointSize]) {
  1037. return;
  1038. }
  1039. flags[Dirty::PointSize] = false;
  1040. oglEnable(GL_POINT_SPRITE, maxwell3d->regs.point_sprite_enable);
  1041. oglEnable(GL_PROGRAM_POINT_SIZE, maxwell3d->regs.point_size_attribute.enabled);
  1042. const bool is_rescaling{texture_cache.IsRescaling()};
  1043. const float scale = is_rescaling ? Settings::values.resolution_info.up_factor : 1.0f;
  1044. glPointSize(std::max(1.0f, maxwell3d->regs.point_size * scale));
  1045. }
  1046. void RasterizerOpenGL::SyncLineState() {
  1047. auto& flags = maxwell3d->dirty.flags;
  1048. if (!flags[Dirty::LineWidth]) {
  1049. return;
  1050. }
  1051. flags[Dirty::LineWidth] = false;
  1052. const auto& regs = maxwell3d->regs;
  1053. oglEnable(GL_LINE_SMOOTH, regs.line_anti_alias_enable);
  1054. glLineWidth(regs.line_anti_alias_enable ? regs.line_width_smooth : regs.line_width_aliased);
  1055. }
  1056. void RasterizerOpenGL::SyncPolygonOffset() {
  1057. auto& flags = maxwell3d->dirty.flags;
  1058. if (!flags[Dirty::PolygonOffset]) {
  1059. return;
  1060. }
  1061. flags[Dirty::PolygonOffset] = false;
  1062. const auto& regs = maxwell3d->regs;
  1063. oglEnable(GL_POLYGON_OFFSET_FILL, regs.polygon_offset_fill_enable);
  1064. oglEnable(GL_POLYGON_OFFSET_LINE, regs.polygon_offset_line_enable);
  1065. oglEnable(GL_POLYGON_OFFSET_POINT, regs.polygon_offset_point_enable);
  1066. if (regs.polygon_offset_fill_enable || regs.polygon_offset_line_enable ||
  1067. regs.polygon_offset_point_enable) {
  1068. // Hardware divides polygon offset units by two
  1069. glPolygonOffsetClamp(regs.slope_scale_depth_bias, regs.depth_bias / 2.0f,
  1070. regs.depth_bias_clamp);
  1071. }
  1072. }
  1073. void RasterizerOpenGL::SyncAlphaTest() {
  1074. auto& flags = maxwell3d->dirty.flags;
  1075. if (!flags[Dirty::AlphaTest]) {
  1076. return;
  1077. }
  1078. flags[Dirty::AlphaTest] = false;
  1079. const auto& regs = maxwell3d->regs;
  1080. if (regs.alpha_test_enabled) {
  1081. glEnable(GL_ALPHA_TEST);
  1082. glAlphaFunc(MaxwellToGL::ComparisonOp(regs.alpha_test_func), regs.alpha_test_ref);
  1083. } else {
  1084. glDisable(GL_ALPHA_TEST);
  1085. }
  1086. }
  1087. void RasterizerOpenGL::SyncFramebufferSRGB() {
  1088. auto& flags = maxwell3d->dirty.flags;
  1089. if (!flags[Dirty::FramebufferSRGB]) {
  1090. return;
  1091. }
  1092. flags[Dirty::FramebufferSRGB] = false;
  1093. oglEnable(GL_FRAMEBUFFER_SRGB, maxwell3d->regs.framebuffer_srgb);
  1094. }
  1095. void RasterizerOpenGL::BeginTransformFeedback(GraphicsPipeline* program, GLenum primitive_mode) {
  1096. const auto& regs = maxwell3d->regs;
  1097. if (regs.transform_feedback_enabled == 0) {
  1098. return;
  1099. }
  1100. program->ConfigureTransformFeedback();
  1101. UNIMPLEMENTED_IF(regs.IsShaderConfigEnabled(Maxwell::ShaderType::TessellationInit) ||
  1102. regs.IsShaderConfigEnabled(Maxwell::ShaderType::Tessellation) ||
  1103. regs.IsShaderConfigEnabled(Maxwell::ShaderType::Geometry));
  1104. UNIMPLEMENTED_IF(primitive_mode != GL_POINTS);
  1105. // We may have to call BeginTransformFeedbackNV here since they seem to call different
  1106. // implementations on Nvidia's driver (the pointer is different) but we are using
  1107. // ARB_transform_feedback3 features with NV_transform_feedback interactions and the ARB
  1108. // extension doesn't define BeginTransformFeedback (without NV) interactions. It just works.
  1109. glBeginTransformFeedback(GL_POINTS);
  1110. }
  1111. void RasterizerOpenGL::EndTransformFeedback() {
  1112. if (maxwell3d->regs.transform_feedback_enabled != 0) {
  1113. glEndTransformFeedback();
  1114. }
  1115. }
  1116. void RasterizerOpenGL::InitializeChannel(Tegra::Control::ChannelState& channel) {
  1117. CreateChannel(channel);
  1118. {
  1119. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  1120. texture_cache.CreateChannel(channel);
  1121. buffer_cache.CreateChannel(channel);
  1122. }
  1123. shader_cache.CreateChannel(channel);
  1124. query_cache.CreateChannel(channel);
  1125. state_tracker.SetupTables(channel);
  1126. }
  1127. void RasterizerOpenGL::BindChannel(Tegra::Control::ChannelState& channel) {
  1128. const s32 channel_id = channel.bind_id;
  1129. BindToChannel(channel_id);
  1130. {
  1131. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  1132. texture_cache.BindToChannel(channel_id);
  1133. buffer_cache.BindToChannel(channel_id);
  1134. }
  1135. shader_cache.BindToChannel(channel_id);
  1136. query_cache.BindToChannel(channel_id);
  1137. state_tracker.ChangeChannel(channel);
  1138. state_tracker.InvalidateState();
  1139. }
  1140. void RasterizerOpenGL::ReleaseChannel(s32 channel_id) {
  1141. EraseChannel(channel_id);
  1142. {
  1143. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  1144. texture_cache.EraseChannel(channel_id);
  1145. buffer_cache.EraseChannel(channel_id);
  1146. }
  1147. shader_cache.EraseChannel(channel_id);
  1148. query_cache.EraseChannel(channel_id);
  1149. }
  1150. AccelerateDMA::AccelerateDMA(BufferCache& buffer_cache_, TextureCache& texture_cache_)
  1151. : buffer_cache{buffer_cache_}, texture_cache{texture_cache_} {}
  1152. bool AccelerateDMA::BufferCopy(GPUVAddr src_address, GPUVAddr dest_address, u64 amount) {
  1153. std::scoped_lock lock{buffer_cache.mutex};
  1154. return buffer_cache.DMACopy(src_address, dest_address, amount);
  1155. }
  1156. bool AccelerateDMA::BufferClear(GPUVAddr src_address, u64 amount, u32 value) {
  1157. std::scoped_lock lock{buffer_cache.mutex};
  1158. return buffer_cache.DMAClear(src_address, amount, value);
  1159. }
  1160. template <bool IS_IMAGE_UPLOAD>
  1161. bool AccelerateDMA::DmaBufferImageCopy(const Tegra::DMA::ImageCopy& copy_info,
  1162. const Tegra::DMA::BufferOperand& buffer_operand,
  1163. const Tegra::DMA::ImageOperand& image_operand) {
  1164. std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
  1165. const auto image_id = texture_cache.DmaImageId(image_operand, IS_IMAGE_UPLOAD);
  1166. if (image_id == VideoCommon::NULL_IMAGE_ID) {
  1167. return false;
  1168. }
  1169. const u32 buffer_size = static_cast<u32>(buffer_operand.pitch * buffer_operand.height);
  1170. static constexpr auto sync_info = VideoCommon::ObtainBufferSynchronize::FullSynchronize;
  1171. const auto post_op = IS_IMAGE_UPLOAD ? VideoCommon::ObtainBufferOperation::DoNothing
  1172. : VideoCommon::ObtainBufferOperation::MarkAsWritten;
  1173. const auto [buffer, offset] =
  1174. buffer_cache.ObtainBuffer(buffer_operand.address, buffer_size, sync_info, post_op);
  1175. const auto [image, copy] = texture_cache.DmaBufferImageCopy(
  1176. copy_info, buffer_operand, image_operand, image_id, IS_IMAGE_UPLOAD);
  1177. const std::span copy_span{&copy, 1};
  1178. if constexpr (IS_IMAGE_UPLOAD) {
  1179. texture_cache.PrepareImage(image_id, true, false);
  1180. image->UploadMemory(buffer->Handle(), offset, copy_span);
  1181. } else {
  1182. if (offset % BytesPerBlock(image->info.format)) {
  1183. return false;
  1184. }
  1185. texture_cache.DownloadImageIntoBuffer(image, buffer->Handle(), offset, copy_span,
  1186. buffer_operand.address, buffer_size);
  1187. }
  1188. return true;
  1189. }
  1190. bool AccelerateDMA::ImageToBuffer(const Tegra::DMA::ImageCopy& copy_info,
  1191. const Tegra::DMA::ImageOperand& image_operand,
  1192. const Tegra::DMA::BufferOperand& buffer_operand) {
  1193. return DmaBufferImageCopy<false>(copy_info, buffer_operand, image_operand);
  1194. }
  1195. bool AccelerateDMA::BufferToImage(const Tegra::DMA::ImageCopy& copy_info,
  1196. const Tegra::DMA::BufferOperand& buffer_operand,
  1197. const Tegra::DMA::ImageOperand& image_operand) {
  1198. return DmaBufferImageCopy<true>(copy_info, buffer_operand, image_operand);
  1199. }
  1200. } // namespace OpenGL