| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025 |
- using OpenTK.Graphics.OpenGL;
- using Ryujinx.Common.Logging;
- using Ryujinx.Graphics.GAL;
- 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 TextureView _unit0Texture;
- private ClipOrigin _clipOrigin;
- private ClipDepthMode _clipDepthMode;
- private uint[] _componentMasks;
- private bool _scissor0Enable = false;
- ColorF _blendConstant = new ColorF(0, 0, 0, 0);
- internal Pipeline()
- {
- _rasterizerDiscard = false;
- _clipOrigin = ClipOrigin.LowerLeft;
- _clipDepthMode = ClipDepthMode.NegativeOneToOne;
- }
- 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 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);
- // TODO: Enable when GL_EXT_polygon_offset_clamp is supported.
- // GL.PolygonOffsetClamp(factor, units, clamp);
- }
- public void SetDepthClamp(bool clampNear, bool clampFar)
- {
- // TODO: Use GL_AMD_depth_clamp_separate or similar if available?
- // Currently enables clamping if either is set.
- bool clamp = clampNear || clampFar;
- 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)
- {
- TextureView view = (TextureView)texture;
- FormatInfo formatInfo = FormatTable.GetFormatInfo(view.Format);
- SizedInternalFormat format = (SizedInternalFormat)formatInfo.PixelInternalFormat;
- GL.BindImageTexture(unit, view.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)buffer.Buffer);
- }
- 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(uint[] componentMasks)
- {
- _componentMasks = (uint[])componentMasks.Clone();
- for (int index = 0; index < componentMasks.Length; 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 = ((TextureView)texture);
- }
- else
- {
- ((TextureView)texture).Bind(unit);
- }
- }
- }
- public void SetUniformBuffer(int index, ShaderStage stage, BufferRange buffer)
- {
- SetBuffer(index, stage, buffer, isStorage: false);
- }
- public void SetVertexAttribs(VertexAttribDescriptor[] vertexAttribs)
- {
- EnsureVertexArray();
- _vertexArray.SetVertexAttributes(vertexAttribs);
- }
- public void SetVertexBuffers(VertexBufferDescriptor[] vertexBuffers)
- {
- EnsureVertexArray();
- _vertexArray.SetVertexBuffers(vertexBuffers);
- }
- public void SetViewports(int first, Viewport[] viewports)
- {
- bool flipY = false;
- 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;
- // OpenGL does not support per-viewport flipping, so
- // instead we decide that based on the viewport 0 value.
- // It will apply to all viewports.
- if (index == 0)
- {
- flipY = viewport.Region.Height < 0;
- }
- if (viewport.SwizzleY == ViewportSwizzle.NegativeY)
- {
- flipY = !flipY;
- }
- 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);
- SetOrigin(flipY ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft);
- }
- 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.Buffer == null)
- {
- GL.BindBufferRange(target, bindingPoint, 0, IntPtr.Zero, 0);
- return;
- }
- int bufferHandle = ((Buffer)buffer.Buffer).Handle;
- IntPtr bufferOffset = (IntPtr)buffer.Offset;
- GL.BindBufferRange(target, bindingPoint, bufferHandle, 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();
- _framebuffer.Bind();
- GL.Enable(EnableCap.FramebufferSrgb);
- }
- }
- 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)
- {
- if (_componentMasks != null)
- {
- 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 void Dispose()
- {
- _framebuffer?.Dispose();
- _vertexArray?.Dispose();
- }
- }
- }
|