| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using OpenTK;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- namespace Ryujinx.Graphics.Gal.OpenGL
- {
- public class OpenGLRenderer : IGalRenderer
- {
- private OGLBlend Blend;
- private OGLFrameBuffer FrameBuffer;
- private OGLRasterizer Rasterizer;
- private OGLShader Shader;
- private OGLTexture Texture;
- private ConcurrentQueue<Action> ActionsQueue;
- private FrameBuffer FbRenderer;
- public OpenGLRenderer()
- {
- Blend = new OGLBlend();
- FrameBuffer = new OGLFrameBuffer();
- Rasterizer = new OGLRasterizer();
- Shader = new OGLShader();
- Texture = new OGLTexture();
- ActionsQueue = new ConcurrentQueue<Action>();
- }
- public void InitializeFrameBuffer()
- {
- FbRenderer = new FrameBuffer(1280, 720);
- }
- public void ResetFrameBuffer()
- {
- FbRenderer.Reset();
- }
- public void QueueAction(Action ActionMthd)
- {
- ActionsQueue.Enqueue(ActionMthd);
- }
- public void RunActions()
- {
- int Count = ActionsQueue.Count;
- while (Count-- > 0 && ActionsQueue.TryDequeue(out Action RenderAction))
- {
- RenderAction();
- }
- }
- public void Render()
- {
- FbRenderer.Render();
- }
- public void SetWindowSize(int Width, int Height)
- {
- FbRenderer.WindowWidth = Width;
- FbRenderer.WindowHeight = Height;
- }
- public unsafe void SetFrameBuffer(
- byte* Fb,
- int Width,
- int Height,
- float ScaleX,
- float ScaleY,
- float OffsX,
- float OffsY,
- float Rotate)
- {
- Matrix2 Transform;
- Transform = Matrix2.CreateScale(ScaleX, ScaleY);
- Transform *= Matrix2.CreateRotation(Rotate);
- Vector2 Offs = new Vector2(OffsX, OffsY);
- FbRenderer.Set(Fb, Width, Height, Transform, Offs);
- }
- public void SetBlendEnable(bool Enable)
- {
- if (Enable)
- {
- ActionsQueue.Enqueue(() => Blend.Enable());
- }
- else
- {
- ActionsQueue.Enqueue(() => Blend.Disable());
- }
- }
- public void SetBlend(
- GalBlendEquation Equation,
- GalBlendFactor FuncSrc,
- GalBlendFactor FuncDst)
- {
- ActionsQueue.Enqueue(() => Blend.Set(Equation, FuncSrc, FuncDst));
- }
- public void SetBlendSeparate(
- GalBlendEquation EquationRgb,
- GalBlendEquation EquationAlpha,
- GalBlendFactor FuncSrcRgb,
- GalBlendFactor FuncDstRgb,
- GalBlendFactor FuncSrcAlpha,
- GalBlendFactor FuncDstAlpha)
- {
- ActionsQueue.Enqueue(() =>
- {
- Blend.SetSeparate(
- EquationRgb,
- EquationAlpha,
- FuncSrcRgb,
- FuncDstRgb,
- FuncSrcAlpha,
- FuncDstAlpha);
- });
- }
- public void SetFb(int FbIndex, int Width, int Height)
- {
- ActionsQueue.Enqueue(() => FrameBuffer.Set(FbIndex, Width, Height));
- }
- public void BindFrameBuffer(int FbIndex)
- {
- ActionsQueue.Enqueue(() => FrameBuffer.Bind(FbIndex));
- }
- public void DrawFrameBuffer(int FbIndex)
- {
- ActionsQueue.Enqueue(() => FrameBuffer.Draw(FbIndex));
- }
- public void ClearBuffers(int RtIndex, GalClearBufferFlags Flags)
- {
- ActionsQueue.Enqueue(() => Rasterizer.ClearBuffers(RtIndex, Flags));
- }
- public void SetVertexArray(int VbIndex, int Stride, byte[] Buffer, GalVertexAttrib[] Attribs)
- {
- if ((uint)VbIndex > 31)
- {
- throw new ArgumentOutOfRangeException(nameof(VbIndex));
- }
- ActionsQueue.Enqueue(() => Rasterizer.SetVertexArray(VbIndex, Stride,
- Buffer ?? throw new ArgumentNullException(nameof(Buffer)),
- Attribs ?? throw new ArgumentNullException(nameof(Attribs))));
- }
- public void SetIndexArray(byte[] Buffer, GalIndexFormat Format)
- {
- if (Buffer == null)
- {
- throw new ArgumentNullException(nameof(Buffer));
- }
- ActionsQueue.Enqueue(() => Rasterizer.SetIndexArray(Buffer, Format));
- }
- public void DrawArrays(int VbIndex, GalPrimitiveType PrimType)
- {
- if ((uint)VbIndex > 31)
- {
- throw new ArgumentOutOfRangeException(nameof(VbIndex));
- }
- ActionsQueue.Enqueue(() => Rasterizer.DrawArrays(VbIndex, PrimType));
- }
- public void DrawElements(int VbIndex, int First, GalPrimitiveType PrimType)
- {
- if ((uint)VbIndex > 31)
- {
- throw new ArgumentOutOfRangeException(nameof(VbIndex));
- }
- ActionsQueue.Enqueue(() => Rasterizer.DrawElements(VbIndex, First, PrimType));
- }
- public void CreateShader(long Tag, GalShaderType Type, byte[] Data)
- {
- if (Data == null)
- {
- throw new ArgumentNullException(nameof(Data));
- }
- Shader.Create(Tag, Type, Data);
- }
- public void SetConstBuffer(long Tag, int Cbuf, byte[] Data)
- {
- if (Data == null)
- {
- throw new ArgumentNullException(nameof(Data));
- }
- ActionsQueue.Enqueue(() => Shader.SetConstBuffer(Tag, Cbuf, Data));
- }
- public void SetUniform1(string UniformName, int Value)
- {
- if (UniformName == null)
- {
- throw new ArgumentNullException(nameof(UniformName));
- }
- ActionsQueue.Enqueue(() => Shader.SetUniform1(UniformName, Value));
- }
- public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Tag)
- {
- return Shader.GetTextureUsage(Tag);
- }
- public void BindShader(long Tag)
- {
- ActionsQueue.Enqueue(() => Shader.Bind(Tag));
- }
- public void BindProgram()
- {
- ActionsQueue.Enqueue(() => Shader.BindProgram());
- }
- public void SetTexture(int Index, GalTexture Tex)
- {
- ActionsQueue.Enqueue(() => Texture.Set(Index, Tex));
- }
- public void SetSampler(int Index, GalTextureSampler Sampler)
- {
- ActionsQueue.Enqueue(() => Texture.Set(Index, Sampler));
- }
- }
- }
|