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. const auto& stage = index - 1; // Stage indices are 0 - 5
  159. const bool is_enabled = gpu.IsShaderStageEnabled(static_cast<Maxwell::ShaderStage>(stage));
  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 GLintptr ubo_offset = buffer_offset + static_cast<GLintptr>(ptr_pos);
  166. copy_buffer(uniform_buffers[stage].handle, ubo_offset,
  167. sizeof(GLShader::MaxwellUniformData));
  168. GLShader::MaxwellUniformData* ub_ptr =
  169. reinterpret_cast<GLShader::MaxwellUniformData*>(&buffer_ptr[ptr_pos]);
  170. ub_ptr->SetFromRegs(gpu.state.shader_stages[stage]);
  171. // Fetch program code from memory
  172. GLShader::ProgramCode program_code;
  173. const u64 gpu_address{gpu.regs.code_address.CodeAddress() + shader_config.offset};
  174. const VAddr cpu_address{gpu.memory_manager.PhysicalToVirtualAddress(gpu_address)};
  175. Memory::ReadBlock(cpu_address, program_code.data(), program_code.size() * sizeof(u64));
  176. GLShader::ShaderSetup setup{std::move(program_code)};
  177. switch (program) {
  178. case Maxwell::ShaderProgram::VertexB: {
  179. GLShader::MaxwellVSConfig vs_config{setup};
  180. shader_program_manager->UseProgrammableVertexShader(vs_config, setup);
  181. break;
  182. }
  183. case Maxwell::ShaderProgram::Fragment: {
  184. GLShader::MaxwellFSConfig fs_config{setup};
  185. shader_program_manager->UseProgrammableFragmentShader(fs_config, setup);
  186. break;
  187. }
  188. default:
  189. LOG_CRITICAL(HW_GPU, "Unimplemented shader index=%d, enable=%d, offset=0x%08X", index,
  190. shader_config.enable.Value(), shader_config.offset);
  191. UNREACHABLE();
  192. }
  193. }
  194. shader_program_manager->UseTrivialGeometryShader();
  195. }
  196. bool RasterizerOpenGL::AccelerateDrawBatch(bool is_indexed) {
  197. accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays;
  198. DrawArrays();
  199. return true;
  200. }
  201. void RasterizerOpenGL::DrawArrays() {
  202. if (accelerate_draw == AccelDraw::Disabled)
  203. return;
  204. MICROPROFILE_SCOPE(OpenGL_Drawing);
  205. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  206. // TODO(bunnei): Implement these
  207. const bool has_stencil = false;
  208. const bool using_color_fb = true;
  209. const bool using_depth_fb = false;
  210. const MathUtil::Rectangle<s32> viewport_rect{regs.viewport[0].GetRect()};
  211. const bool write_color_fb =
  212. state.color_mask.red_enabled == GL_TRUE || state.color_mask.green_enabled == GL_TRUE ||
  213. state.color_mask.blue_enabled == GL_TRUE || state.color_mask.alpha_enabled == GL_TRUE;
  214. const bool write_depth_fb =
  215. (state.depth.test_enabled && state.depth.write_mask == GL_TRUE) ||
  216. (has_stencil && state.stencil.test_enabled && state.stencil.write_mask != 0);
  217. Surface color_surface;
  218. Surface depth_surface;
  219. MathUtil::Rectangle<u32> surfaces_rect;
  220. std::tie(color_surface, depth_surface, surfaces_rect) =
  221. res_cache.GetFramebufferSurfaces(using_color_fb, using_depth_fb, viewport_rect);
  222. const u16 res_scale = color_surface != nullptr
  223. ? color_surface->res_scale
  224. : (depth_surface == nullptr ? 1u : depth_surface->res_scale);
  225. MathUtil::Rectangle<u32> draw_rect{
  226. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) +
  227. viewport_rect.left * res_scale,
  228. surfaces_rect.left, surfaces_rect.right)), // Left
  229. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
  230. viewport_rect.top * res_scale,
  231. surfaces_rect.bottom, surfaces_rect.top)), // Top
  232. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.left) +
  233. viewport_rect.right * res_scale,
  234. surfaces_rect.left, surfaces_rect.right)), // Right
  235. static_cast<u32>(MathUtil::Clamp<s32>(static_cast<s32>(surfaces_rect.bottom) +
  236. viewport_rect.bottom * res_scale,
  237. surfaces_rect.bottom, surfaces_rect.top))}; // Bottom
  238. // Bind the framebuffer surfaces
  239. BindFramebufferSurfaces(color_surface, depth_surface, has_stencil);
  240. // Sync the viewport
  241. SyncViewport(surfaces_rect, res_scale);
  242. // TODO(bunnei): Sync framebuffer_scale uniform here
  243. // TODO(bunnei): Sync scissorbox uniform(s) here
  244. // Sync and bind the texture surfaces
  245. BindTextures();
  246. // Viewport can have negative offsets or larger dimensions than our framebuffer sub-rect. Enable
  247. // scissor test to prevent drawing outside of the framebuffer region
  248. state.scissor.enabled = true;
  249. state.scissor.x = draw_rect.left;
  250. state.scissor.y = draw_rect.bottom;
  251. state.scissor.width = draw_rect.GetWidth();
  252. state.scissor.height = draw_rect.GetHeight();
  253. state.Apply();
  254. // Draw the vertex batch
  255. const bool is_indexed = accelerate_draw == AccelDraw::Indexed;
  256. AnalyzeVertexArray(is_indexed);
  257. state.draw.vertex_buffer = stream_buffer->GetHandle();
  258. state.Apply();
  259. size_t buffer_size = static_cast<size_t>(vs_input_size);
  260. if (is_indexed) {
  261. UNREACHABLE();
  262. }
  263. // Uniform space for the 5 shader stages
  264. buffer_size += sizeof(GLShader::MaxwellUniformData) * Maxwell::MaxShaderStage;
  265. size_t ptr_pos = 0;
  266. u8* buffer_ptr;
  267. GLintptr buffer_offset;
  268. std::tie(buffer_ptr, buffer_offset) =
  269. stream_buffer->Map(static_cast<GLsizeiptr>(buffer_size), 4);
  270. SetupVertexArray(buffer_ptr, buffer_offset);
  271. ptr_pos += vs_input_size;
  272. GLintptr index_buffer_offset = 0;
  273. if (is_indexed) {
  274. UNREACHABLE();
  275. }
  276. SetupShaders(buffer_ptr, buffer_offset, ptr_pos);
  277. stream_buffer->Unmap();
  278. shader_program_manager->ApplyTo(state);
  279. state.Apply();
  280. if (is_indexed) {
  281. UNREACHABLE();
  282. } else {
  283. glDrawArrays(MaxwellToGL::PrimitiveTopology(regs.draw.topology), 0,
  284. regs.vertex_buffer.count);
  285. }
  286. // Disable scissor test
  287. state.scissor.enabled = false;
  288. accelerate_draw = AccelDraw::Disabled;
  289. // Unbind textures for potential future use as framebuffer attachments
  290. for (auto& texture_unit : state.texture_units) {
  291. texture_unit.texture_2d = 0;
  292. }
  293. state.Apply();
  294. // Mark framebuffer surfaces as dirty
  295. MathUtil::Rectangle<u32> draw_rect_unscaled{
  296. draw_rect.left / res_scale, draw_rect.top / res_scale, draw_rect.right / res_scale,
  297. draw_rect.bottom / res_scale};
  298. if (color_surface != nullptr && write_color_fb) {
  299. auto interval = color_surface->GetSubRectInterval(draw_rect_unscaled);
  300. res_cache.InvalidateRegion(boost::icl::first(interval), boost::icl::length(interval),
  301. color_surface);
  302. }
  303. if (depth_surface != nullptr && write_depth_fb) {
  304. auto interval = depth_surface->GetSubRectInterval(draw_rect_unscaled);
  305. res_cache.InvalidateRegion(boost::icl::first(interval), boost::icl::length(interval),
  306. depth_surface);
  307. }
  308. }
  309. void RasterizerOpenGL::BindTextures() {
  310. using Regs = Tegra::Engines::Maxwell3D::Regs;
  311. auto maxwell3d = Core::System::GetInstance().GPU().Get3DEngine();
  312. // Each Maxwell shader stage can have an arbitrary number of textures, but we're limited to a
  313. // certain number in OpenGL. We try to only use the minimum amount of host textures by not
  314. // keeping a 1:1 relation between guest texture ids and host texture ids, ie, guest texture id 8
  315. // can be host texture id 0 if it's the only texture used in the guest shader program.
  316. u32 host_texture_index = 0;
  317. for (u32 stage = 0; stage < Regs::MaxShaderStage; ++stage) {
  318. ASSERT(host_texture_index < texture_samplers.size());
  319. const auto textures = maxwell3d.GetStageTextures(static_cast<Regs::ShaderStage>(stage));
  320. for (unsigned texture_index = 0; texture_index < textures.size(); ++texture_index) {
  321. const auto& texture = textures[texture_index];
  322. if (texture.enabled) {
  323. texture_samplers[host_texture_index].SyncWithConfig(texture.tsc);
  324. Surface surface = res_cache.GetTextureSurface(texture);
  325. if (surface != nullptr) {
  326. state.texture_units[host_texture_index].texture_2d = surface->texture.handle;
  327. } else {
  328. // Can occur when texture addr is null or its memory is unmapped/invalid
  329. state.texture_units[texture_index].texture_2d = 0;
  330. }
  331. ++host_texture_index;
  332. } else {
  333. state.texture_units[texture_index].texture_2d = 0;
  334. }
  335. }
  336. }
  337. }
  338. void RasterizerOpenGL::NotifyMaxwellRegisterChanged(u32 id) {}
  339. void RasterizerOpenGL::FlushAll() {
  340. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  341. res_cache.FlushAll();
  342. }
  343. void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
  344. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  345. res_cache.FlushRegion(addr, size);
  346. }
  347. void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
  348. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  349. res_cache.InvalidateRegion(addr, size, nullptr);
  350. }
  351. void RasterizerOpenGL::FlushAndInvalidateRegion(VAddr addr, u64 size) {
  352. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  353. res_cache.FlushRegion(addr, size);
  354. res_cache.InvalidateRegion(addr, size, nullptr);
  355. }
  356. bool RasterizerOpenGL::AccelerateDisplayTransfer(const void* config) {
  357. MICROPROFILE_SCOPE(OpenGL_Blits);
  358. UNREACHABLE();
  359. return true;
  360. }
  361. bool RasterizerOpenGL::AccelerateTextureCopy(const void* config) {
  362. UNREACHABLE();
  363. return true;
  364. }
  365. bool RasterizerOpenGL::AccelerateFill(const void* config) {
  366. UNREACHABLE();
  367. return true;
  368. }
  369. bool RasterizerOpenGL::AccelerateDisplay(const Tegra::FramebufferConfig& framebuffer,
  370. VAddr framebuffer_addr, u32 pixel_stride,
  371. ScreenInfo& screen_info) {
  372. if (framebuffer_addr == 0) {
  373. return false;
  374. }
  375. MICROPROFILE_SCOPE(OpenGL_CacheManagement);
  376. SurfaceParams src_params;
  377. src_params.addr = framebuffer_addr;
  378. src_params.width = std::min(framebuffer.width, pixel_stride);
  379. src_params.height = framebuffer.height;
  380. src_params.stride = pixel_stride;
  381. src_params.is_tiled = false;
  382. src_params.pixel_format =
  383. SurfaceParams::PixelFormatFromGPUPixelFormat(framebuffer.pixel_format);
  384. src_params.UpdateParams();
  385. MathUtil::Rectangle<u32> src_rect;
  386. Surface src_surface;
  387. std::tie(src_surface, src_rect) =
  388. res_cache.GetSurfaceSubRect(src_params, ScaleMatch::Ignore, true);
  389. if (src_surface == nullptr) {
  390. return false;
  391. }
  392. u32 scaled_width = src_surface->GetScaledWidth();
  393. u32 scaled_height = src_surface->GetScaledHeight();
  394. screen_info.display_texcoords = MathUtil::Rectangle<float>(
  395. (float)src_rect.bottom / (float)scaled_height, (float)src_rect.left / (float)scaled_width,
  396. (float)src_rect.top / (float)scaled_height, (float)src_rect.right / (float)scaled_width);
  397. screen_info.display_texture = src_surface->texture.handle;
  398. return true;
  399. }
  400. void RasterizerOpenGL::SamplerInfo::Create() {
  401. sampler.Create();
  402. mag_filter = min_filter = Tegra::Texture::TextureFilter::Linear;
  403. wrap_u = wrap_v = Tegra::Texture::WrapMode::Wrap;
  404. border_color_r = border_color_g = border_color_b = border_color_a = 0;
  405. // default is GL_LINEAR_MIPMAP_LINEAR
  406. glSamplerParameteri(sampler.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  407. // Other attributes have correct defaults
  408. }
  409. void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Tegra::Texture::TSCEntry& config) {
  410. GLuint s = sampler.handle;
  411. if (mag_filter != config.mag_filter) {
  412. mag_filter = config.mag_filter;
  413. glSamplerParameteri(s, GL_TEXTURE_MAG_FILTER, MaxwellToGL::TextureFilterMode(mag_filter));
  414. }
  415. if (min_filter != config.min_filter) {
  416. min_filter = config.min_filter;
  417. glSamplerParameteri(s, GL_TEXTURE_MIN_FILTER, MaxwellToGL::TextureFilterMode(min_filter));
  418. }
  419. if (wrap_u != config.wrap_u) {
  420. wrap_u = config.wrap_u;
  421. glSamplerParameteri(s, GL_TEXTURE_WRAP_S, MaxwellToGL::WrapMode(wrap_u));
  422. }
  423. if (wrap_v != config.wrap_v) {
  424. wrap_v = config.wrap_v;
  425. glSamplerParameteri(s, GL_TEXTURE_WRAP_T, MaxwellToGL::WrapMode(wrap_v));
  426. }
  427. if (wrap_u == Tegra::Texture::WrapMode::Border || wrap_v == Tegra::Texture::WrapMode::Border) {
  428. // TODO(Subv): Implement border color
  429. ASSERT(false);
  430. }
  431. }
  432. void RasterizerOpenGL::BindFramebufferSurfaces(const Surface& color_surface,
  433. const Surface& depth_surface, bool has_stencil) {
  434. state.draw.draw_framebuffer = framebuffer.handle;
  435. state.Apply();
  436. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
  437. color_surface != nullptr ? color_surface->texture.handle : 0, 0);
  438. if (depth_surface != nullptr) {
  439. if (has_stencil) {
  440. // attach both depth and stencil
  441. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
  442. depth_surface->texture.handle, 0);
  443. } else {
  444. // attach depth
  445. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
  446. depth_surface->texture.handle, 0);
  447. // clear stencil attachment
  448. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
  449. }
  450. } else {
  451. // clear both depth and stencil attachment
  452. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0,
  453. 0);
  454. }
  455. }
  456. void RasterizerOpenGL::SyncViewport(const MathUtil::Rectangle<u32>& surfaces_rect, u16 res_scale) {
  457. const auto& regs = Core::System().GetInstance().GPU().Maxwell3D().regs;
  458. const MathUtil::Rectangle<s32> viewport_rect{regs.viewport[0].GetRect()};
  459. state.viewport.x = static_cast<GLint>(surfaces_rect.left) + viewport_rect.left * res_scale;
  460. state.viewport.y = static_cast<GLint>(surfaces_rect.bottom) + viewport_rect.bottom * res_scale;
  461. state.viewport.width = static_cast<GLsizei>(viewport_rect.GetWidth() * res_scale);
  462. state.viewport.height = static_cast<GLsizei>(viewport_rect.GetHeight() * res_scale);
  463. }
  464. void RasterizerOpenGL::SyncClipEnabled() {
  465. UNREACHABLE();
  466. }
  467. void RasterizerOpenGL::SyncClipCoef() {
  468. UNREACHABLE();
  469. }
  470. void RasterizerOpenGL::SyncCullMode() {
  471. UNREACHABLE();
  472. }
  473. void RasterizerOpenGL::SyncDepthScale() {
  474. UNREACHABLE();
  475. }
  476. void RasterizerOpenGL::SyncDepthOffset() {
  477. UNREACHABLE();
  478. }
  479. void RasterizerOpenGL::SyncBlendEnabled() {
  480. UNREACHABLE();
  481. }
  482. void RasterizerOpenGL::SyncBlendFuncs() {
  483. UNREACHABLE();
  484. }
  485. void RasterizerOpenGL::SyncBlendColor() {
  486. UNREACHABLE();
  487. }