gl_rasterizer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <string>
  6. #include <tuple>
  7. #include <utility>
  8. #include <glad/glad.h>
  9. #include "common/alignment.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/vector_math.h"
  16. #include "core/core.h"
  17. #include "core/hle/kernel/process.h"
  18. #include "core/settings.h"
  19. #include "video_core/engines/maxwell_3d.h"
  20. #include "video_core/renderer_opengl/gl_rasterizer.h"
  21. #include "video_core/renderer_opengl/gl_shader_gen.h"
  22. #include "video_core/renderer_opengl/maxwell_to_gl.h"
  23. #include "video_core/renderer_opengl/renderer_opengl.h"
  24. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  25. using PixelFormat = SurfaceParams::PixelFormat;
  26. using SurfaceType = SurfaceParams::SurfaceType;
  27. MICROPROFILE_DEFINE(OpenGL_VAO, "OpenGL", "Vertex Array Setup", MP_RGB(128, 128, 192));
  28. MICROPROFILE_DEFINE(OpenGL_VS, "OpenGL", "Vertex Shader Setup", MP_RGB(128, 128, 192));
  29. MICROPROFILE_DEFINE(OpenGL_FS, "OpenGL", "Fragment Shader Setup", MP_RGB(128, 128, 192));
  30. MICROPROFILE_DEFINE(OpenGL_Drawing, "OpenGL", "Drawing", MP_RGB(128, 128, 192));
  31. MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(100, 100, 255));
  32. MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Mgmt", MP_RGB(100, 255, 100));
  33. RasterizerOpenGL::RasterizerOpenGL() {
  34. has_ARB_buffer_storage = false;
  35. has_ARB_direct_state_access = false;
  36. has_ARB_separate_shader_objects = false;
  37. has_ARB_vertex_attrib_binding = false;
  38. // Create sampler objects
  39. for (size_t i = 0; i < texture_samplers.size(); ++i) {
  40. texture_samplers[i].Create();
  41. state.texture_units[i].sampler = texture_samplers[i].sampler.handle;
  42. }
  43. GLint ext_num;
  44. glGetIntegerv(GL_NUM_EXTENSIONS, &ext_num);
  45. for (GLint i = 0; i < ext_num; i++) {
  46. std::string extension{reinterpret_cast<const char*>(glGetStringi(GL_EXTENSIONS, i))};
  47. if (extension == "GL_ARB_buffer_storage") {
  48. has_ARB_buffer_storage = true;
  49. } else if (extension == "GL_ARB_direct_state_access") {
  50. has_ARB_direct_state_access = true;
  51. } else if (extension == "GL_ARB_separate_shader_objects") {
  52. has_ARB_separate_shader_objects = true;
  53. } else if (extension == "GL_ARB_vertex_attrib_binding") {
  54. has_ARB_vertex_attrib_binding = true;
  55. }
  56. }
  57. ASSERT_MSG(has_ARB_separate_shader_objects, "has_ARB_separate_shader_objects is unsupported");
  58. // Clipping plane 0 is always enabled for PICA fixed clip plane z <= 0
  59. state.clip_distance[0] = true;
  60. // Generate VBO, VAO and UBO
  61. vertex_buffer = OGLStreamBuffer::MakeBuffer(GLAD_GL_ARB_buffer_storage, GL_ARRAY_BUFFER);
  62. vertex_buffer->Create(VERTEX_BUFFER_SIZE, VERTEX_BUFFER_SIZE / 2);
  63. sw_vao.Create();
  64. uniform_buffer.Create();
  65. state.draw.vertex_array = sw_vao.handle;
  66. state.draw.vertex_buffer = vertex_buffer->GetHandle();
  67. state.draw.uniform_buffer = uniform_buffer.handle;
  68. state.Apply();
  69. // Create render framebuffer
  70. framebuffer.Create();
  71. hw_vao.Create();
  72. hw_vao_enabled_attributes.fill(false);
  73. stream_buffer = OGLStreamBuffer::MakeBuffer(has_ARB_buffer_storage, GL_ARRAY_BUFFER);
  74. stream_buffer->Create(STREAM_BUFFER_SIZE, STREAM_BUFFER_SIZE / 2);
  75. state.draw.vertex_buffer = stream_buffer->GetHandle();
  76. shader_program_manager = std::make_unique<GLShader::ProgramManager>();
  77. state.draw.shader_program = 0;
  78. state.draw.vertex_array = hw_vao.handle;
  79. state.Apply();
  80. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, stream_buffer->GetHandle());
  81. for (unsigned index = 0; index < uniform_buffers.size(); ++index) {
  82. auto& buffer = uniform_buffers[index];
  83. buffer.Create();
  84. glBindBuffer(GL_UNIFORM_BUFFER, buffer.handle);
  85. glBufferData(GL_UNIFORM_BUFFER, sizeof(GLShader::MaxwellUniformData), nullptr,
  86. GL_STREAM_COPY);
  87. glBindBufferBase(GL_UNIFORM_BUFFER, index, buffer.handle);
  88. }
  89. accelerate_draw = AccelDraw::Disabled;
  90. glEnable(GL_BLEND);
  91. LOG_CRITICAL(Render_OpenGL, "Sync fixed function OpenGL state here!");
  92. }
  93. RasterizerOpenGL::~RasterizerOpenGL() {
  94. if (stream_buffer != nullptr) {
  95. state.draw.vertex_buffer = stream_buffer->GetHandle();
  96. state.Apply();
  97. stream_buffer->Release();
  98. }
  99. }
  100. void RasterizerOpenGL::AnalyzeVertexArray(bool is_indexed) {
  101. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  102. if (is_indexed) {
  103. UNREACHABLE();
  104. }
  105. // TODO(bunnei): Add support for 1+ vertex arrays
  106. vs_input_size = regs.vertex_buffer.count * regs.vertex_array[0].stride;
  107. }
  108. void RasterizerOpenGL::SetupVertexArray(u8* array_ptr, GLintptr buffer_offset) {
  109. MICROPROFILE_SCOPE(OpenGL_VAO);
  110. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  111. const auto& memory_manager = Core::System().GetInstance().GPU().memory_manager;
  112. state.draw.vertex_array = hw_vao.handle;
  113. state.draw.vertex_buffer = stream_buffer->GetHandle();
  114. state.Apply();
  115. // TODO(bunnei): Add support for 1+ vertex arrays
  116. const auto& vertex_array{regs.vertex_array[0]};
  117. ASSERT_MSG(vertex_array.enable, "vertex array 0 is disabled?");
  118. ASSERT_MSG(!vertex_array.divisor, "vertex array 0 divisor is unimplemented!");
  119. for (unsigned index = 1; index < Maxwell::NumVertexArrays; ++index) {
  120. ASSERT_MSG(!regs.vertex_array[index].enable, "vertex array %d is unimplemented!", index);
  121. }
  122. // Use the vertex array as-is, assumes that the data is formatted correctly for OpenGL.
  123. // Enables the first 16 vertex attributes always, as we don't know which ones are actually used
  124. // until shader time. Note, Tegra technically supports 32, but we're cappinig this to 16 for now
  125. // to avoid OpenGL errors.
  126. for (unsigned index = 0; index < 16; ++index) {
  127. auto& attrib = regs.vertex_attrib_format[index];
  128. glVertexAttribPointer(index, attrib.ComponentCount(), MaxwellToGL::VertexType(attrib),
  129. attrib.IsNormalized() ? GL_TRUE : GL_FALSE, vertex_array.stride,
  130. reinterpret_cast<GLvoid*>(buffer_offset + attrib.offset));
  131. glEnableVertexAttribArray(index);
  132. hw_vao_enabled_attributes[index] = true;
  133. }
  134. // Copy vertex array data
  135. const u32 data_size{vertex_array.stride * regs.vertex_buffer.count};
  136. const VAddr data_addr{memory_manager->PhysicalToVirtualAddress(vertex_array.StartAddress())};
  137. res_cache.FlushRegion(data_addr, data_size, nullptr);
  138. Memory::ReadBlock(data_addr, array_ptr, data_size);
  139. array_ptr += data_size;
  140. buffer_offset += data_size;
  141. }
  142. void RasterizerOpenGL::SetupShaders(u8* buffer_ptr, GLintptr buffer_offset, size_t ptr_pos) {
  143. // Helper function for uploading uniform data
  144. const auto copy_buffer = [&](GLuint handle, GLintptr offset, GLsizeiptr size) {
  145. if (has_ARB_direct_state_access) {
  146. glCopyNamedBufferSubData(stream_buffer->GetHandle(), handle, offset, 0, size);
  147. } else {
  148. glBindBuffer(GL_COPY_WRITE_BUFFER, handle);
  149. glCopyBufferSubData(GL_ARRAY_BUFFER, GL_COPY_WRITE_BUFFER, offset, 0, size);
  150. }
  151. };
  152. auto& gpu = Core::System().GetInstance().GPU().Maxwell3D();
  153. ASSERT_MSG(!gpu.regs.shader_config[0].enable, "VertexA is unsupported!");
  154. for (unsigned index = 1; index < Maxwell::MaxShaderProgram; ++index) {
  155. ptr_pos += sizeof(GLShader::MaxwellUniformData);
  156. auto& shader_config = gpu.regs.shader_config[index];
  157. const Maxwell::ShaderProgram program{static_cast<Maxwell::ShaderProgram>(index)};
  158. // VertexB program is always enabled, despite bit setting
  159. const bool is_enabled{shader_config.enable || program == Maxwell::ShaderProgram::VertexB};
  160. // Skip stages that are not enabled
  161. if (!is_enabled) {
  162. continue;
  163. }
  164. // Upload uniform data as one UBO per stage
  165. const auto& stage = index - 1; // Stage indices are 0 - 5
  166. const GLintptr ubo_offset = buffer_offset + static_cast<GLintptr>(ptr_pos);
  167. copy_buffer(uniform_buffers[stage].handle, ubo_offset,
  168. sizeof(GLShader::MaxwellUniformData));
  169. GLShader::MaxwellUniformData* ub_ptr =
  170. reinterpret_cast<GLShader::MaxwellUniformData*>(&buffer_ptr[ptr_pos]);
  171. ub_ptr->SetFromRegs(gpu.state.shader_stages[stage]);
  172. // Fetch program code from memory
  173. GLShader::ProgramCode program_code;
  174. const u64 gpu_address{gpu.regs.code_address.CodeAddress() + shader_config.offset};
  175. const VAddr cpu_address{gpu.memory_manager.PhysicalToVirtualAddress(gpu_address)};
  176. Memory::ReadBlock(cpu_address, program_code.data(), program_code.size() * sizeof(u64));
  177. GLShader::ShaderSetup setup{std::move(program_code)};
  178. switch (program) {
  179. case Maxwell::ShaderProgram::VertexB: {
  180. GLShader::MaxwellVSConfig vs_config{setup};
  181. shader_program_manager->UseProgrammableVertexShader(vs_config, setup);
  182. break;
  183. }
  184. case Maxwell::ShaderProgram::Fragment: {
  185. GLShader::MaxwellFSConfig fs_config{setup};
  186. shader_program_manager->UseProgrammableFragmentShader(fs_config, setup);
  187. break;
  188. }
  189. default:
  190. LOG_CRITICAL(HW_GPU, "Unimplemented shader index=%d, enable=%d, offset=0x%08X", index,
  191. shader_config.enable.Value(), shader_config.offset);
  192. UNREACHABLE();
  193. }
  194. }
  195. shader_program_manager->UseTrivialGeometryShader();
  196. }
  197. bool RasterizerOpenGL::AccelerateDrawBatch(bool is_indexed) {
  198. accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays;
  199. DrawArrays();
  200. return true;
  201. }
  202. void RasterizerOpenGL::DrawArrays() {
  203. if (accelerate_draw == AccelDraw::Disabled)
  204. return;
  205. MICROPROFILE_SCOPE(OpenGL_Drawing);
  206. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  207. // TODO(bunnei): Implement these
  208. const bool has_stencil = false;
  209. const bool using_color_fb = true;
  210. const bool using_depth_fb = false;
  211. const MathUtil::Rectangle<s32> viewport_rect{regs.viewport[0].GetRect()};
  212. const bool write_color_fb =
  213. state.color_mask.red_enabled == GL_TRUE || state.color_mask.green_enabled == GL_TRUE ||
  214. state.color_mask.blue_enabled == GL_TRUE || state.color_mask.alpha_enabled == GL_TRUE;
  215. const bool write_depth_fb =
  216. (state.depth.test_enabled && state.depth.write_mask == GL_TRUE) ||
  217. (has_stencil && state.stencil.test_enabled && state.stencil.write_mask != 0);
  218. Surface color_surface;
  219. Surface depth_surface;
  220. MathUtil::Rectangle<u32> surfaces_rect;
  221. std::tie(color_surface, depth_surface, surfaces_rect) =
  222. res_cache.GetFramebufferSurfaces(using_color_fb, using_depth_fb, viewport_rect);
  223. const u16 res_scale = color_surface != nullptr
  224. ? color_surface->res_scale
  225. : (depth_surface == nullptr ? 1u : depth_surface->res_scale);
  226. MathUtil::Rectangle<u32> draw_rect{
  227. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) +
  228. viewport_rect.left * res_scale,
  229. surfaces_rect.left, surfaces_rect.right)), // Left
  230. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
  231. viewport_rect.top * res_scale,
  232. surfaces_rect.bottom, surfaces_rect.top)), // Top
  233. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) +
  234. viewport_rect.right * res_scale,
  235. surfaces_rect.left, surfaces_rect.right)), // Right
  236. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
  237. viewport_rect.bottom * res_scale,
  238. surfaces_rect.bottom, surfaces_rect.top))}; // Bottom
  239. // Bind the framebuffer surfaces
  240. BindFramebufferSurfaces(color_surface, depth_surface, has_stencil);
  241. // Sync the viewport
  242. SyncViewport(surfaces_rect, res_scale);
  243. // TODO(bunnei): Sync framebuffer_scale uniform here
  244. // TODO(bunnei): Sync scissorbox uniform(s) here
  245. // Sync and bind the texture surfaces
  246. BindTextures();
  247. // Viewport can have negative offsets or larger dimensions than our framebuffer sub-rect. Enable
  248. // scissor test to prevent drawing outside of the framebuffer region
  249. state.scissor.enabled = true;
  250. state.scissor.x = draw_rect.left;
  251. state.scissor.y = draw_rect.bottom;
  252. state.scissor.width = draw_rect.GetWidth();
  253. state.scissor.height = draw_rect.GetHeight();
  254. state.Apply();
  255. // Draw the vertex batch
  256. const bool is_indexed = accelerate_draw == AccelDraw::Indexed;
  257. AnalyzeVertexArray(is_indexed);
  258. state.draw.vertex_buffer = stream_buffer->GetHandle();
  259. state.Apply();
  260. size_t buffer_size = static_cast<size_t>(vs_input_size);
  261. if (is_indexed) {
  262. UNREACHABLE();
  263. }
  264. // Uniform space for the 5 shader stages
  265. buffer_size += sizeof(GLShader::MaxwellUniformData) * Maxwell::MaxShaderStage;
  266. size_t ptr_pos = 0;
  267. u8* buffer_ptr;
  268. GLintptr buffer_offset;
  269. std::tie(buffer_ptr, buffer_offset) =
  270. stream_buffer->Map(static_cast<GLsizeiptr>(buffer_size), 4);
  271. SetupVertexArray(buffer_ptr, buffer_offset);
  272. ptr_pos += vs_input_size;
  273. GLintptr index_buffer_offset = 0;
  274. if (is_indexed) {
  275. UNREACHABLE();
  276. }
  277. SetupShaders(buffer_ptr, buffer_offset, ptr_pos);
  278. stream_buffer->Unmap();
  279. shader_program_manager->ApplyTo(state);
  280. state.Apply();
  281. if (is_indexed) {
  282. UNREACHABLE();
  283. } else {
  284. glDrawArrays(MaxwellToGL::PrimitiveTopology(regs.draw.topology), 0,
  285. regs.vertex_buffer.count);
  286. }
  287. // Disable scissor test
  288. state.scissor.enabled = false;
  289. accelerate_draw = AccelDraw::Disabled;
  290. // Unbind textures for potential future use as framebuffer attachments
  291. for (auto& texture_unit : state.texture_units) {
  292. texture_unit.texture_2d = 0;
  293. }
  294. state.Apply();
  295. // Mark framebuffer surfaces as dirty
  296. MathUtil::Rectangle<u32> draw_rect_unscaled{
  297. draw_rect.left / res_scale, draw_rect.top / res_scale, draw_rect.right / res_scale,
  298. draw_rect.bottom / res_scale};
  299. if (color_surface != nullptr && write_color_fb) {
  300. auto interval = color_surface->GetSubRectInterval(draw_rect_unscaled);
  301. res_cache.InvalidateRegion(boost::icl::first(interval), boost::icl::length(interval),
  302. color_surface);
  303. }
  304. if (depth_surface != nullptr && write_depth_fb) {
  305. auto interval = depth_surface->GetSubRectInterval(draw_rect_unscaled);
  306. res_cache.InvalidateRegion(boost::icl::first(interval), boost::icl::length(interval),
  307. depth_surface);
  308. }
  309. }
  310. void RasterizerOpenGL::BindTextures() {
  311. using Regs = Tegra::Engines::Maxwell3D::Regs;
  312. auto maxwell3d = Core::System::GetInstance().GPU().Get3DEngine();
  313. // Each Maxwell shader stage can have an arbitrary number of textures, but we're limited to a
  314. // certain number in OpenGL. We try to only use the minimum amount of host textures by not
  315. // keeping a 1:1 relation between guest texture ids and host texture ids, ie, guest texture id 8
  316. // can be host texture id 0 if it's the only texture used in the guest shader program.
  317. u32 host_texture_index = 0;
  318. for (u32 stage = 0; stage < Regs::MaxShaderStage; ++stage) {
  319. ASSERT(host_texture_index < texture_samplers.size());
  320. const auto textures = maxwell3d.GetStageTextures(static_cast<Regs::ShaderStage>(stage));
  321. for (unsigned texture_index = 0; texture_index < textures.size(); ++texture_index) {
  322. const auto& texture = textures[texture_index];
  323. if (texture.enabled) {
  324. texture_samplers[host_texture_index].SyncWithConfig(texture.tsc);
  325. Surface surface = res_cache.GetTextureSurface(texture);
  326. if (surface != nullptr) {
  327. state.texture_units[host_texture_index].texture_2d = surface->texture.handle;
  328. } else {
  329. // Can occur when texture addr is null or its memory is unmapped/invalid
  330. state.texture_units[texture_index].texture_2d = 0;
  331. }
  332. ++host_texture_index;
  333. } else {
  334. state.texture_units[texture_index].texture_2d = 0;
  335. }
  336. }
  337. }
  338. }
  339. void RasterizerOpenGL::NotifyMaxwellRegisterChanged(u32 id) {}
  340. void RasterizerOpenGL::FlushAll() {
  341. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  342. res_cache.FlushAll();
  343. }
  344. void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
  345. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  346. res_cache.FlushRegion(addr, size);
  347. }
  348. void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
  349. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  350. res_cache.InvalidateRegion(addr, size, nullptr);
  351. }
  352. void RasterizerOpenGL::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  353. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  354. res_cache.FlushRegion(addr, size);
  355. res_cache.InvalidateRegion(addr, size, nullptr);
  356. }
  357. bool RasterizerOpenGL::AccelerateDisplayTransfer(const void* config) {
  358. MICROPROFILE_SCOPE(OpenGL_Blits);
  359. UNREACHABLE();
  360. return true;
  361. }
  362. bool RasterizerOpenGL::AccelerateTextureCopy(const void* config) {
  363. UNREACHABLE();
  364. return true;
  365. }
  366. bool RasterizerOpenGL::AccelerateFill(const void* config) {
  367. UNREACHABLE();
  368. return true;
  369. }
  370. bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& framebuffer,
  371. VAddr framebuffer_addr, u32 pixel_stride,
  372. ScreenInfo& screen_info) {
  373. if (framebuffer_addr == 0) {
  374. return false;
  375. }
  376. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  377. SurfaceParams src_params;
  378. src_params.addr = framebuffer_addr;
  379. src_params.width = std::min(framebuffer.width, pixel_stride);
  380. src_params.height = framebuffer.height;
  381. src_params.stride = pixel_stride;
  382. src_params.is_tiled = false;
  383. src_params.pixel_format =
  384. SurfaceParams::PixelFormatFromGPUPixelFormat(framebuffer.pixel_format);
  385. src_params.UpdateParams();
  386. MathUtil::Rectangle<u32> src_rect;
  387. Surface src_surface;
  388. std::tie(src_surface, src_rect) =
  389. res_cache.GetSurfaceSubRect(src_params, ScaleMatch::Ignore, true);
  390. if (src_surface == nullptr) {
  391. return false;
  392. }
  393. u32 scaled_width = src_surface->GetScaledWidth();
  394. u32 scaled_height = src_surface->GetScaledHeight();
  395. screen_info.display_texcoords = MathUtil::Rectangle<float>(
  396. (float)src_rect.bottom / (float)scaled_height, (float)src_rect.left / (float)scaled_width,
  397. (float)src_rect.top / (float)scaled_height, (float)src_rect.right / (float)scaled_width);
  398. screen_info.display_texture = src_surface->texture.handle;
  399. return true;
  400. }
  401. void RasterizerOpenGL::SamplerInfo::Create() {
  402. sampler.Create();
  403. mag_filter = min_filter = Tegra::Texture::TextureFilter::Linear;
  404. wrap_u = wrap_v = Tegra::Texture::WrapMode::Wrap;
  405. border_color_r = border_color_g = border_color_b = border_color_a = 0;
  406. // default is GL_LINEAR_MIPMAP_LINEAR
  407. glSamplerParameteri(sampler.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  408. // Other attributes have correct defaults
  409. }
  410. void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Tegra::Texture::TSCEntry& config) {
  411. GLuint s = sampler.handle;
  412. if (mag_filter != config.mag_filter) {
  413. mag_filter = config.mag_filter;
  414. glSamplerParameteri(s, GL_TEXTURE_MAG_FILTER, MaxwellToGL::TextureFilterMode(mag_filter));
  415. }
  416. if (min_filter != config.min_filter) {
  417. min_filter = config.min_filter;
  418. glSamplerParameteri(s, GL_TEXTURE_MIN_FILTER, MaxwellToGL::TextureFilterMode(min_filter));
  419. }
  420. if (wrap_u != config.wrap_u) {
  421. wrap_u = config.wrap_u;
  422. glSamplerParameteri(s, GL_TEXTURE_WRAP_S, MaxwellToGL::WrapMode(wrap_u));
  423. }
  424. if (wrap_v != config.wrap_v) {
  425. wrap_v = config.wrap_v;
  426. glSamplerParameteri(s, GL_TEXTURE_WRAP_T, MaxwellToGL::WrapMode(wrap_v));
  427. }
  428. if (wrap_u == Tegra::Texture::WrapMode::Border || wrap_v == Tegra::Texture::WrapMode::Border) {
  429. // TODO(Subv): Implement border color
  430. ASSERT(false);
  431. }
  432. }
  433. void RasterizerOpenGL::BindFramebufferSurfaces(const Surface& color_surface,
  434. const Surface& depth_surface, bool has_stencil) {
  435. state.draw.draw_framebuffer = framebuffer.handle;
  436. state.Apply();
  437. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  438. color_surface != nullptr ? color_surface->texture.handle : 0, 0);
  439. if (depth_surface != nullptr) {
  440. if (has_stencil) {
  441. // attach both depth and stencil
  442. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
  443. depth_surface->texture.handle, 0);
  444. } else {
  445. // attach depth
  446. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
  447. depth_surface->texture.handle, 0);
  448. // clear stencil attachment
  449. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  450. }
  451. } else {
  452. // clear both depth and stencil attachment
  453. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0,
  454. 0);
  455. }
  456. }
  457. void RasterizerOpenGL::SyncViewport(const MathUtil::Rectangle<u32>& surfaces_rect, u16 res_scale) {
  458. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  459. const MathUtil::Rectangle<s32> viewport_rect{regs.viewport[0].GetRect()};
  460. state.viewport.x = static_cast<GLint>(surfaces_rect.left) + viewport_rect.left * res_scale;
  461. state.viewport.y = static_cast<GLint>(surfaces_rect.bottom) + viewport_rect.bottom * res_scale;
  462. state.viewport.width = static_cast<GLsizei>(viewport_rect.GetWidth() * res_scale);
  463. state.viewport.height = static_cast<GLsizei>(viewport_rect.GetHeight() * res_scale);
  464. }
  465. void RasterizerOpenGL::SyncClipEnabled() {
  466. UNREACHABLE();
  467. }
  468. void RasterizerOpenGL::SyncClipCoef() {
  469. UNREACHABLE();
  470. }
  471. void RasterizerOpenGL::SyncCullMode() {
  472. UNREACHABLE();
  473. }
  474. void RasterizerOpenGL::SyncDepthScale() {
  475. UNREACHABLE();
  476. }
  477. void RasterizerOpenGL::SyncDepthOffset() {
  478. UNREACHABLE();
  479. }
  480. void RasterizerOpenGL::SyncBlendEnabled() {
  481. UNREACHABLE();
  482. }
  483. void RasterizerOpenGL::SyncBlendFuncs() {
  484. UNREACHABLE();
  485. }
  486. void RasterizerOpenGL::SyncBlendColor() {
  487. UNREACHABLE();
  488. }