| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093 |
- using OpenTK.Graphics.OpenGL;
- using Ryujinx.Common.Logging;
- using Ryujinx.Graphics.GAL;
- using Ryujinx.Graphics.OpenGL.Image;
- using Ryujinx.Graphics.OpenGL.Queries;
- using Ryujinx.Graphics.Shader;
- using System;
- namespace Ryujinx.Graphics.OpenGL
- {
- class Pipeline : IPipeline, IDisposable
- {
- private Program _program;
- private bool _rasterizerDiscard;
- private VertexArray _vertexArray;
- private Framebuffer _framebuffer;
- private IntPtr _indexBaseOffset;
- private DrawElementsType _elementsType;
- private PrimitiveType _primitiveType;
- private int _stencilFrontMask;
- private bool _depthMask;
- private bool _depthTest;
- private bool _hasDepthBuffer;
- private int _boundDrawFramebuffer;
- private int _boundReadFramebuffer;
- private TextureBase _unit0Texture;
- private ClipOrigin _clipOrigin;
- private ClipDepthMode _clipDepthMode;
- private readonly uint[] _componentMasks;
- private bool _scissor0Enable = false;
- ColorF _blendConstant = new ColorF(0, 0, 0, 0);
- internal Pipeline()
- {
- _rasterizerDiscard = false;
- _clipOrigin = ClipOrigin.LowerLeft;
- _clipDepthMode = ClipDepthMode.NegativeOneToOne;
- _componentMasks = new uint[Constants.MaxRenderTargets];
- for (int index = 0; index < Constants.MaxRenderTargets; index++)
- {
- _componentMasks[index] = 0xf;
- }
- }
- public void Barrier()
- {
- GL.MemoryBarrier(MemoryBarrierFlags.AllBarrierBits);
- }
- public void ClearRenderTargetColor(int index, uint componentMask, ColorF color)
- {
- GL.ColorMask(
- index,
- (componentMask & 1) != 0,
- (componentMask & 2) != 0,
- (componentMask & 4) != 0,
- (componentMask & 8) != 0);
- float[] colors = new float[] { color.Red, color.Green, color.Blue, color.Alpha };
- GL.ClearBuffer(ClearBuffer.Color, index, colors);
- RestoreComponentMask(index);
- _framebuffer.SignalModified();
- }
- public void ClearRenderTargetDepthStencil(float depthValue, bool depthMask, int stencilValue, int stencilMask)
- {
- bool stencilMaskChanged =
- stencilMask != 0 &&
- stencilMask != _stencilFrontMask;
- bool depthMaskChanged = depthMask && depthMask != _depthMask;
- if (stencilMaskChanged)
- {
- GL.StencilMaskSeparate(StencilFace.Front, stencilMask);
- }
- if (depthMaskChanged)
- {
- GL.DepthMask(depthMask);
- }
- if (depthMask && stencilMask != 0)
- {
- GL.ClearBuffer(ClearBufferCombined.DepthStencil, 0, depthValue, stencilValue);
- }
- else if (depthMask)
- {
- GL.ClearBuffer(ClearBuffer.Depth, 0, ref depthValue);
- }
- else if (stencilMask != 0)
- {
- GL.ClearBuffer(ClearBuffer.Stencil, 0, ref stencilValue);
- }
- if (stencilMaskChanged)
- {
- GL.StencilMaskSeparate(StencilFace.Front, _stencilFrontMask);
- }
- if (depthMaskChanged)
- {
- GL.DepthMask(_depthMask);
- }
- _framebuffer.SignalModified();
- }
- public void CopyBuffer(BufferHandle source, BufferHandle destination, int srcOffset, int dstOffset, int size)
- {
- Buffer.Copy(source, destination, srcOffset, dstOffset, size);
- }
- public void DispatchCompute(int groupsX, int groupsY, int groupsZ)
- {
- if (!_program.IsLinked)
- {
- Logger.PrintDebug(LogClass.Gpu, "Dispatch error, shader not linked.");
- return;
- }
- PrepareForDispatch();
- GL.DispatchCompute(groupsX, groupsY, groupsZ);
- }
- public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
- {
- if (!_program.IsLinked)
- {
- Logger.PrintDebug(LogClass.Gpu, "Draw error, shader not linked.");
- return;
- }
- PrepareForDraw();
- if (_primitiveType == PrimitiveType.Quads)
- {
- DrawQuadsImpl(vertexCount, instanceCount, firstVertex, firstInstance);
- }
- else if (_primitiveType == PrimitiveType.QuadStrip)
- {
- DrawQuadStripImpl(vertexCount, instanceCount, firstVertex, firstInstance);
- }
- else
- {
- DrawImpl(vertexCount, instanceCount, firstVertex, firstInstance);
- }
- _framebuffer.SignalModified();
- }
- private void DrawQuadsImpl(
- int vertexCount,
- int instanceCount,
- int firstVertex,
- int firstInstance)
- {
- // TODO: Instanced rendering.
- int quadsCount = vertexCount / 4;
- int[] firsts = new int[quadsCount];
- int[] counts = new int[quadsCount];
- for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
- {
- firsts[quadIndex] = firstVertex + quadIndex * 4;
- counts[quadIndex] = 4;
- }
- GL.MultiDrawArrays(
- PrimitiveType.TriangleFan,
- firsts,
- counts,
- quadsCount);
- }
- private void DrawQuadStripImpl(
- int vertexCount,
- int instanceCount,
- int firstVertex,
- int firstInstance)
- {
- int quadsCount = (vertexCount - 2) / 2;
- if (firstInstance != 0 || instanceCount != 1)
- {
- for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
- {
- GL.DrawArraysInstancedBaseInstance(PrimitiveType.TriangleFan, firstVertex + quadIndex * 2, 4, instanceCount, firstInstance);
- }
- }
- else
- {
- int[] firsts = new int[quadsCount];
- int[] counts = new int[quadsCount];
- firsts[0] = firstVertex;
- counts[0] = 4;
- for (int quadIndex = 1; quadIndex < quadsCount; quadIndex++)
- {
- firsts[quadIndex] = firstVertex + quadIndex * 2;
- counts[quadIndex] = 4;
- }
- GL.MultiDrawArrays(
- PrimitiveType.TriangleFan,
- firsts,
- counts,
- quadsCount);
- }
- }
- private void DrawImpl(
- int vertexCount,
- int instanceCount,
- int firstVertex,
- int firstInstance)
- {
- if (firstInstance == 0 && instanceCount == 1)
- {
- GL.DrawArrays(_primitiveType, firstVertex, vertexCount);
- }
- else if (firstInstance == 0)
- {
- GL.DrawArraysInstanced(_primitiveType, firstVertex, vertexCount, instanceCount);
- }
- else
- {
- GL.DrawArraysInstancedBaseInstance(
- _primitiveType,
- firstVertex,
- vertexCount,
- instanceCount,
- firstInstance);
- }
- }
- public void DrawIndexed(
- int indexCount,
- int instanceCount,
- int firstIndex,
- int firstVertex,
- int firstInstance)
- {
- if (!_program.IsLinked)
- {
- Logger.PrintDebug(LogClass.Gpu, "Draw error, shader not linked.");
- return;
- }
- PrepareForDraw();
- int indexElemSize = 1;
- switch (_elementsType)
- {
- case DrawElementsType.UnsignedShort: indexElemSize = 2; break;
- case DrawElementsType.UnsignedInt: indexElemSize = 4; break;
- }
- IntPtr indexBaseOffset = _indexBaseOffset + firstIndex * indexElemSize;
- if (_primitiveType == PrimitiveType.Quads)
- {
- DrawQuadsIndexedImpl(
- indexCount,
- instanceCount,
- indexBaseOffset,
- indexElemSize,
- firstVertex,
- firstInstance);
- }
- else if (_primitiveType == PrimitiveType.QuadStrip)
- {
- DrawQuadStripIndexedImpl(
- indexCount,
- instanceCount,
- indexBaseOffset,
- indexElemSize,
- firstVertex,
- firstInstance);
- }
- else
- {
- DrawIndexedImpl(
- indexCount,
- instanceCount,
- indexBaseOffset,
- firstVertex,
- firstInstance);
- }
- _framebuffer.SignalModified();
- }
- private void DrawQuadsIndexedImpl(
- int indexCount,
- int instanceCount,
- IntPtr indexBaseOffset,
- int indexElemSize,
- int firstVertex,
- int firstInstance)
- {
- int quadsCount = indexCount / 4;
- if (firstInstance != 0 || instanceCount != 1)
- {
- if (firstVertex != 0 && firstInstance != 0)
- {
- for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
- {
- GL.DrawElementsInstancedBaseVertexBaseInstance(
- PrimitiveType.TriangleFan,
- 4,
- _elementsType,
- indexBaseOffset + quadIndex * 4 * indexElemSize,
- instanceCount,
- firstVertex,
- firstInstance);
- }
- }
- else if (firstInstance != 0)
- {
- for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
- {
- GL.DrawElementsInstancedBaseInstance(
- PrimitiveType.TriangleFan,
- 4,
- _elementsType,
- indexBaseOffset + quadIndex * 4 * indexElemSize,
- instanceCount,
- firstInstance);
- }
- }
- else
- {
- for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
- {
- GL.DrawElementsInstanced(
- PrimitiveType.TriangleFan,
- 4,
- _elementsType,
- indexBaseOffset + quadIndex * 4 * indexElemSize,
- instanceCount);
- }
- }
- }
- else
- {
- IntPtr[] indices = new IntPtr[quadsCount];
- int[] counts = new int[quadsCount];
- int[] baseVertices = new int[quadsCount];
- for (int quadIndex = 0; quadIndex < quadsCount; quadIndex++)
- {
- indices[quadIndex] = indexBaseOffset + quadIndex * 4 * indexElemSize;
- counts[quadIndex] = 4;
- baseVertices[quadIndex] = firstVertex;
- }
- GL.MultiDrawElementsBaseVertex(
- PrimitiveType.TriangleFan,
- counts,
- _elementsType,
- indices,
- quadsCount,
- baseVertices);
- }
- }
- private void DrawQuadStripIndexedImpl(
- int indexCount,
- int instanceCount,
- IntPtr indexBaseOffset,
- int indexElemSize,
- int firstVertex,
- int firstInstance)
- {
- // TODO: Instanced rendering.
- int quadsCount = (indexCount - 2) / 2;
- IntPtr[] indices = new IntPtr[quadsCount];
- int[] counts = new int[quadsCount];
- int[] baseVertices = new int[quadsCount];
- indices[0] = indexBaseOffset;
- counts[0] = 4;
- baseVertices[0] = firstVertex;
- for (int quadIndex = 1; quadIndex < quadsCount; quadIndex++)
- {
- indices[quadIndex] = indexBaseOffset + quadIndex * 2 * indexElemSize;
- counts[quadIndex] = 4;
- baseVertices[quadIndex] = firstVertex;
- }
- GL.MultiDrawElementsBaseVertex(
- PrimitiveType.TriangleFan,
- counts,
- _elementsType,
- indices,
- quadsCount,
- baseVertices);
- }
- private void DrawIndexedImpl(
- int indexCount,
- int instanceCount,
- IntPtr indexBaseOffset,
- int firstVertex,
- int firstInstance)
- {
- if (firstInstance == 0 && firstVertex == 0 && instanceCount == 1)
- {
- GL.DrawElements(_primitiveType, indexCount, _elementsType, indexBaseOffset);
- }
- else if (firstInstance == 0 && instanceCount == 1)
- {
- GL.DrawElementsBaseVertex(
- _primitiveType,
- indexCount,
- _elementsType,
- indexBaseOffset,
- firstVertex);
- }
- else if (firstInstance == 0 && firstVertex == 0)
- {
- GL.DrawElementsInstanced(
- _primitiveType,
- indexCount,
- _elementsType,
- indexBaseOffset,
- instanceCount);
- }
- else if (firstInstance == 0)
- {
- GL.DrawElementsInstancedBaseVertex(
- _primitiveType,
- indexCount,
- _elementsType,
- indexBaseOffset,
- instanceCount,
- firstVertex);
- }
- else if (firstVertex == 0)
- {
- GL.DrawElementsInstancedBaseInstance(
- _primitiveType,
- indexCount,
- _elementsType,
- indexBaseOffset,
- instanceCount,
- firstInstance);
- }
- else
- {
- GL.DrawElementsInstancedBaseVertexBaseInstance(
- _primitiveType,
- indexCount,
- _elementsType,
- indexBaseOffset,
- instanceCount,
- firstVertex,
- firstInstance);
- }
- }
- public void SetBlendState(int index, BlendDescriptor blend)
- {
- if (!blend.Enable)
- {
- GL.Disable(IndexedEnableCap.Blend, index);
- return;
- }
- GL.BlendEquationSeparate(
- index,
- blend.ColorOp.Convert(),
- blend.AlphaOp.Convert());
- GL.BlendFuncSeparate(
- index,
- (BlendingFactorSrc)blend.ColorSrcFactor.Convert(),
- (BlendingFactorDest)blend.ColorDstFactor.Convert(),
- (BlendingFactorSrc)blend.AlphaSrcFactor.Convert(),
- (BlendingFactorDest)blend.AlphaDstFactor.Convert());
- if (_blendConstant != blend.BlendConstant)
- {
- _blendConstant = blend.BlendConstant;
- GL.BlendColor(
- blend.BlendConstant.Red,
- blend.BlendConstant.Green,
- blend.BlendConstant.Blue,
- blend.BlendConstant.Alpha);
- }
- GL.Enable(IndexedEnableCap.Blend, index);
- }
- public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp)
- {
- if ((enables & PolygonModeMask.Point) != 0)
- {
- GL.Enable(EnableCap.PolygonOffsetPoint);
- }
- else
- {
- GL.Disable(EnableCap.PolygonOffsetPoint);
- }
- if ((enables & PolygonModeMask.Line) != 0)
- {
- GL.Enable(EnableCap.PolygonOffsetLine);
- }
- else
- {
- GL.Disable(EnableCap.PolygonOffsetLine);
- }
- if ((enables & PolygonModeMask.Fill) != 0)
- {
- GL.Enable(EnableCap.PolygonOffsetFill);
- }
- else
- {
- GL.Disable(EnableCap.PolygonOffsetFill);
- }
- if (enables == 0)
- {
- return;
- }
- GL.PolygonOffset(factor, units / 2f);
- // TODO: Enable when GL_EXT_polygon_offset_clamp is supported.
- // GL.PolygonOffsetClamp(factor, units, clamp);
- }
- public void SetDepthClamp(bool clamp)
- {
- if (!clamp)
- {
- GL.Disable(EnableCap.DepthClamp);
- return;
- }
- GL.Enable(EnableCap.DepthClamp);
- }
- public void SetDepthMode(DepthMode mode)
- {
- ClipDepthMode depthMode = mode.Convert();
- if (_clipDepthMode != depthMode)
- {
- _clipDepthMode = depthMode;
- GL.ClipControl(_clipOrigin, depthMode);
- }
- }
- public void SetDepthTest(DepthTestDescriptor depthTest)
- {
- GL.DepthFunc((DepthFunction)depthTest.Func.Convert());
- _depthMask = depthTest.WriteEnable;
- _depthTest = depthTest.TestEnable;
- UpdateDepthTest();
- }
- public void SetFaceCulling(bool enable, Face face)
- {
- if (!enable)
- {
- GL.Disable(EnableCap.CullFace);
- return;
- }
- GL.CullFace(face.Convert());
- GL.Enable(EnableCap.CullFace);
- }
- public void SetFrontFace(FrontFace frontFace)
- {
- GL.FrontFace(frontFace.Convert());
- }
- public void SetImage(int index, ShaderStage stage, ITexture texture)
- {
- int unit = _program.GetImageUnit(stage, index);
- if (unit != -1 && texture != null)
- {
- TextureBase texBase = (TextureBase)texture;
- FormatInfo formatInfo = FormatTable.GetFormatInfo(texBase.Format);
- SizedInternalFormat format = (SizedInternalFormat)formatInfo.PixelInternalFormat;
- GL.BindImageTexture(unit, texBase.Handle, 0, true, 0, TextureAccess.ReadWrite, format);
- }
- }
- public void SetIndexBuffer(BufferRange buffer, IndexType type)
- {
- _elementsType = type.Convert();
- _indexBaseOffset = (IntPtr)buffer.Offset;
- EnsureVertexArray();
- _vertexArray.SetIndexBuffer(buffer.Handle);
- }
- public void SetOrigin(Origin origin)
- {
- ClipOrigin clipOrigin = origin == Origin.UpperLeft ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft;
- SetOrigin(clipOrigin);
- }
- public void SetPointSize(float size)
- {
- GL.PointSize(size);
- }
- public void SetPrimitiveRestart(bool enable, int index)
- {
- if (!enable)
- {
- GL.Disable(EnableCap.PrimitiveRestart);
- return;
- }
- GL.PrimitiveRestartIndex(index);
- GL.Enable(EnableCap.PrimitiveRestart);
- }
- public void SetPrimitiveTopology(PrimitiveTopology topology)
- {
- _primitiveType = topology.Convert();
- }
- public void SetProgram(IProgram program)
- {
- _program = (Program)program;
- _program.Bind();
- }
- public void SetRasterizerDiscard(bool discard)
- {
- if (discard)
- {
- GL.Enable(EnableCap.RasterizerDiscard);
- }
- else
- {
- GL.Disable(EnableCap.RasterizerDiscard);
- }
- _rasterizerDiscard = discard;
- }
- public void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMasks)
- {
- for (int index = 0; index < componentMasks.Length; index++)
- {
- _componentMasks[index] = componentMasks[index];
- RestoreComponentMask(index);
- }
- }
- public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
- {
- EnsureFramebuffer();
- for (int index = 0; index < colors.Length; index++)
- {
- TextureView color = (TextureView)colors[index];
- _framebuffer.AttachColor(index, color);
- }
- TextureView depthStencilView = (TextureView)depthStencil;
- _framebuffer.AttachDepthStencil(depthStencilView);
- _framebuffer.SetDrawBuffers(colors.Length);
- _hasDepthBuffer = depthStencil != null && depthStencilView.Format != Format.S8Uint;
- UpdateDepthTest();
- }
- public void SetSampler(int index, ShaderStage stage, ISampler sampler)
- {
- int unit = _program.GetTextureUnit(stage, index);
- if (unit != -1 && sampler != null)
- {
- ((Sampler)sampler).Bind(unit);
- }
- }
- public void SetScissorEnable(int index, bool enable)
- {
- if (enable)
- {
- GL.Enable(IndexedEnableCap.ScissorTest, index);
- }
- else
- {
- GL.Disable(IndexedEnableCap.ScissorTest, index);
- }
- if (index == 0)
- {
- _scissor0Enable = enable;
- }
- }
- public void SetScissor(int index, int x, int y, int width, int height)
- {
- GL.ScissorIndexed(index, x, y, width, height);
- }
- public void SetStencilTest(StencilTestDescriptor stencilTest)
- {
- if (!stencilTest.TestEnable)
- {
- GL.Disable(EnableCap.StencilTest);
- return;
- }
- GL.StencilOpSeparate(
- StencilFace.Front,
- stencilTest.FrontSFail.Convert(),
- stencilTest.FrontDpFail.Convert(),
- stencilTest.FrontDpPass.Convert());
- GL.StencilFuncSeparate(
- StencilFace.Front,
- (StencilFunction)stencilTest.FrontFunc.Convert(),
- stencilTest.FrontFuncRef,
- stencilTest.FrontFuncMask);
- GL.StencilMaskSeparate(StencilFace.Front, stencilTest.FrontMask);
- GL.StencilOpSeparate(
- StencilFace.Back,
- stencilTest.BackSFail.Convert(),
- stencilTest.BackDpFail.Convert(),
- stencilTest.BackDpPass.Convert());
- GL.StencilFuncSeparate(
- StencilFace.Back,
- (StencilFunction)stencilTest.BackFunc.Convert(),
- stencilTest.BackFuncRef,
- stencilTest.BackFuncMask);
- GL.StencilMaskSeparate(StencilFace.Back, stencilTest.BackMask);
- GL.Enable(EnableCap.StencilTest);
- _stencilFrontMask = stencilTest.FrontMask;
- }
- public void SetStorageBuffer(int index, ShaderStage stage, BufferRange buffer)
- {
- SetBuffer(index, stage, buffer, isStorage: true);
- }
- public void SetTexture(int index, ShaderStage stage, ITexture texture)
- {
- int unit = _program.GetTextureUnit(stage, index);
- if (unit != -1 && texture != null)
- {
- if (unit == 0)
- {
- _unit0Texture = (TextureBase)texture;
- }
- else
- {
- ((TextureBase)texture).Bind(unit);
- }
- }
- }
- public void SetUniformBuffer(int index, ShaderStage stage, BufferRange buffer)
- {
- SetBuffer(index, stage, buffer, isStorage: false);
- }
- public void SetUserClipDistance(int index, bool enableClip)
- {
- if (!enableClip)
- {
- GL.Disable(EnableCap.ClipDistance0 + index);
- return;
- }
- GL.Enable(EnableCap.ClipDistance0 + index);
- }
- public void SetVertexAttribs(ReadOnlySpan<VertexAttribDescriptor> vertexAttribs)
- {
- EnsureVertexArray();
- _vertexArray.SetVertexAttributes(vertexAttribs);
- }
- public void SetVertexBuffers(ReadOnlySpan<VertexBufferDescriptor> vertexBuffers)
- {
- EnsureVertexArray();
- _vertexArray.SetVertexBuffers(vertexBuffers);
- }
- public void SetViewports(int first, ReadOnlySpan<Viewport> viewports)
- {
- float[] viewportArray = new float[viewports.Length * 4];
- double[] depthRangeArray = new double[viewports.Length * 2];
- for (int index = 0; index < viewports.Length; index++)
- {
- int viewportElemIndex = index * 4;
- Viewport viewport = viewports[index];
- viewportArray[viewportElemIndex + 0] = viewport.Region.X;
- viewportArray[viewportElemIndex + 1] = viewport.Region.Y;
- if (HwCapabilities.SupportsViewportSwizzle)
- {
- GL.NV.ViewportSwizzle(
- index,
- viewport.SwizzleX.Convert(),
- viewport.SwizzleY.Convert(),
- viewport.SwizzleZ.Convert(),
- viewport.SwizzleW.Convert());
- }
- viewportArray[viewportElemIndex + 2] = MathF.Abs(viewport.Region.Width);
- viewportArray[viewportElemIndex + 3] = MathF.Abs(viewport.Region.Height);
- depthRangeArray[index * 2 + 0] = viewport.DepthNear;
- depthRangeArray[index * 2 + 1] = viewport.DepthFar;
- }
- GL.ViewportArray(first, viewports.Length, viewportArray);
- GL.DepthRangeArray(first, viewports.Length, depthRangeArray);
- }
- public void TextureBarrier()
- {
- GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
- }
- public void TextureBarrierTiled()
- {
- GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
- }
- private void SetBuffer(int index, ShaderStage stage, BufferRange buffer, bool isStorage)
- {
- int bindingPoint = isStorage
- ? _program.GetStorageBufferBindingPoint(stage, index)
- : _program.GetUniformBufferBindingPoint(stage, index);
- if (bindingPoint == -1)
- {
- return;
- }
- BufferRangeTarget target = isStorage
- ? BufferRangeTarget.ShaderStorageBuffer
- : BufferRangeTarget.UniformBuffer;
- if (buffer.Handle == null)
- {
- GL.BindBufferRange(target, bindingPoint, 0, IntPtr.Zero, 0);
- return;
- }
- IntPtr bufferOffset = (IntPtr)buffer.Offset;
- GL.BindBufferRange(target, bindingPoint, buffer.Handle.ToInt32(), bufferOffset, buffer.Size);
- }
- private void SetOrigin(ClipOrigin origin)
- {
- if (_clipOrigin != origin)
- {
- _clipOrigin = origin;
- GL.ClipControl(origin, _clipDepthMode);
- }
- }
- private void EnsureVertexArray()
- {
- if (_vertexArray == null)
- {
- _vertexArray = new VertexArray();
- _vertexArray.Bind();
- }
- }
- private void EnsureFramebuffer()
- {
- if (_framebuffer == null)
- {
- _framebuffer = new Framebuffer();
- int boundHandle = _framebuffer.Bind();
- _boundDrawFramebuffer = _boundReadFramebuffer = boundHandle;
- GL.Enable(EnableCap.FramebufferSrgb);
- }
- }
- internal (int drawHandle, int readHandle) GetBoundFramebuffers()
- {
- return (_boundDrawFramebuffer, _boundReadFramebuffer);
- }
- private void UpdateDepthTest()
- {
- // Enabling depth operations is only valid when we have
- // a depth buffer, otherwise it's not allowed.
- if (_hasDepthBuffer)
- {
- if (_depthTest)
- {
- GL.Enable(EnableCap.DepthTest);
- }
- else
- {
- GL.Disable(EnableCap.DepthTest);
- }
- GL.DepthMask(_depthMask);
- }
- else
- {
- GL.Disable(EnableCap.DepthTest);
- GL.DepthMask(false);
- }
- }
- private void PrepareForDispatch()
- {
- if (_unit0Texture != null)
- {
- _unit0Texture.Bind(0);
- }
- }
- private void PrepareForDraw()
- {
- _vertexArray.Validate();
- if (_unit0Texture != null)
- {
- _unit0Texture.Bind(0);
- }
- }
- private void RestoreComponentMask(int index)
- {
- GL.ColorMask(
- index,
- (_componentMasks[index] & 1u) != 0,
- (_componentMasks[index] & 2u) != 0,
- (_componentMasks[index] & 4u) != 0,
- (_componentMasks[index] & 8u) != 0);
- }
- public void RestoreScissor0Enable()
- {
- if (_scissor0Enable)
- {
- GL.Enable(IndexedEnableCap.ScissorTest, 0);
- }
- }
- public void RestoreRasterizerDiscard()
- {
- if (_rasterizerDiscard)
- {
- GL.Enable(EnableCap.RasterizerDiscard);
- }
- }
- public bool TryHostConditionalRendering(ICounterEvent value, ulong compare, bool isEqual)
- {
- if (value is CounterQueueEvent)
- {
- // Compare an event and a constant value.
- CounterQueueEvent evt = (CounterQueueEvent)value;
- // Easy host conditional rendering when the check matches what GL can do:
- // - Event is of type samples passed.
- // - Result is not a combination of multiple queries.
- // - Comparing against 0.
- // - Event has not already been flushed.
- if (evt.Disposed)
- {
- // If the event has been flushed, then just use the values on the CPU.
- // The query object may already be repurposed for another draw (eg. begin + end).
- return false;
- }
- if (compare == 0 && evt.Type == QueryTarget.SamplesPassed && evt.ClearCounter)
- {
- GL.BeginConditionalRender(evt.Query, isEqual ? ConditionalRenderType.QueryNoWaitInverted : ConditionalRenderType.QueryNoWait);
- return true;
- }
- }
- // The GPU will flush the queries to CPU and evaluate the condition there instead.
- GL.Flush(); // The thread will be stalled manually flushing the counter, so flush GL commands now.
- return false;
- }
- public bool TryHostConditionalRendering(ICounterEvent value, ICounterEvent compare, bool isEqual)
- {
- GL.Flush(); // The GPU thread will be stalled manually flushing the counter, so flush GL commands now.
- return false; // We don't currently have a way to compare two counters for conditional rendering.
- }
- public void EndHostConditionalRendering()
- {
- GL.EndConditionalRender();
- }
- public void Dispose()
- {
- _framebuffer?.Dispose();
- _vertexArray?.Dispose();
- }
- }
- }
|