| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788 |
- 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;
- using System.Runtime.CompilerServices;
- namespace Ryujinx.Graphics.OpenGL
- {
- class Pipeline : IPipeline, IDisposable
- {
- private const int SavedImages = 2;
- private readonly DrawTextureEmulation _drawTexture;
- internal ulong DrawCount { get; private set; }
- 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 _depthTestEnable;
- private bool _stencilTestEnable;
- private bool _cullEnable;
- private float[] _viewportArray = Array.Empty<float>();
- private double[] _depthRangeArray = Array.Empty<double>();
- private int _boundDrawFramebuffer;
- private int _boundReadFramebuffer;
- private CounterQueueEvent _activeConditionalRender;
- private Vector4<int>[] _fpIsBgra = new Vector4<int>[SupportBuffer.FragmentIsBgraCount];
- private Vector4<float>[] _renderScale = new Vector4<float>[73];
- private int _fragmentScaleCount;
- private (TextureBase, Format)[] _images;
- private TextureBase _unit0Texture;
- private Sampler _unit0Sampler;
- private FrontFaceDirection _frontFace;
- private ClipOrigin _clipOrigin;
- private ClipDepthMode _clipDepthMode;
- private uint _fragmentOutputMap;
- private uint _componentMasks;
- private uint _currentComponentMasks;
- private bool _advancedBlendEnable;
- private uint _scissorEnables;
- private bool _tfEnabled;
- private TransformFeedbackPrimitiveType _tfTopology;
- private SupportBufferUpdater _supportBuffer;
- private readonly BufferHandle[] _tfbs;
- private readonly BufferRange[] _tfbTargets;
- private ColorF _blendConstant;
- internal Pipeline()
- {
- _drawTexture = new DrawTextureEmulation();
- _rasterizerDiscard = false;
- _clipOrigin = ClipOrigin.LowerLeft;
- _clipDepthMode = ClipDepthMode.NegativeOneToOne;
- _fragmentOutputMap = uint.MaxValue;
- _componentMasks = uint.MaxValue;
- _images = new (TextureBase, Format)[SavedImages];
- var defaultScale = new Vector4<float> { X = 1f, Y = 0f, Z = 0f, W = 0f };
- new Span<Vector4<float>>(_renderScale).Fill(defaultScale);
- _tfbs = new BufferHandle[Constants.MaxTransformFeedbackBuffers];
- _tfbTargets = new BufferRange[Constants.MaxTransformFeedbackBuffers];
- }
- public void Initialize(OpenGLRenderer renderer)
- {
- _supportBuffer = new SupportBufferUpdater(renderer);
- GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, Unsafe.As<BufferHandle, int>(ref _supportBuffer.Handle));
- _supportBuffer.UpdateFragmentIsBgra(_fpIsBgra, 0, SupportBuffer.FragmentIsBgraCount);
- _supportBuffer.UpdateRenderScale(_renderScale, 0, SupportBuffer.RenderScaleMaxCount);
- }
- public void Barrier()
- {
- GL.MemoryBarrier(MemoryBarrierFlags.AllBarrierBits);
- }
- public void BeginTransformFeedback(PrimitiveTopology topology)
- {
- GL.BeginTransformFeedback(_tfTopology = topology.ConvertToTfType());
- _tfEnabled = true;
- }
- public void ClearBuffer(BufferHandle destination, int offset, int size, uint value)
- {
- Buffer.Clear(destination, offset, size, value);
- }
- public void ClearRenderTargetColor(int index, int layer, int layerCount, uint componentMask, ColorF color)
- {
- EnsureFramebuffer();
- 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 };
- if (layer != 0 || layerCount != _framebuffer.GetColorLayerCount(index))
- {
- for (int l = layer; l < layer + layerCount; l++)
- {
- _framebuffer.AttachColorLayerForClear(index, l);
- GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Color, index, colors);
- }
- _framebuffer.DetachColorLayerForClear(index);
- }
- else
- {
- GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Color, index, colors);
- }
- RestoreComponentMask(index);
- }
- public void ClearRenderTargetDepthStencil(int layer, int layerCount, float depthValue, bool depthMask, int stencilValue, int stencilMask)
- {
- EnsureFramebuffer();
- bool stencilMaskChanged =
- stencilMask != 0 &&
- stencilMask != _stencilFrontMask;
- bool depthMaskChanged = depthMask && depthMask != _depthMask;
- if (stencilMaskChanged)
- {
- GL.StencilMaskSeparate(StencilFace.Front, stencilMask);
- }
- if (depthMaskChanged)
- {
- GL.DepthMask(depthMask);
- }
- if (layer != 0 || layerCount != _framebuffer.GetDepthStencilLayerCount())
- {
- for (int l = layer; l < layer + layerCount; l++)
- {
- _framebuffer.AttachDepthStencilLayerForClear(l);
- ClearDepthStencil(depthValue, depthMask, stencilValue, stencilMask);
- }
- _framebuffer.DetachDepthStencilLayerForClear();
- }
- else
- {
- ClearDepthStencil(depthValue, depthMask, stencilValue, stencilMask);
- }
- if (stencilMaskChanged)
- {
- GL.StencilMaskSeparate(StencilFace.Front, _stencilFrontMask);
- }
- if (depthMaskChanged)
- {
- GL.DepthMask(_depthMask);
- }
- }
- private static void ClearDepthStencil(float depthValue, bool depthMask, int stencilValue, int stencilMask)
- {
- if (depthMask && stencilMask != 0)
- {
- GL.ClearBuffer(ClearBufferCombined.DepthStencil, 0, depthValue, stencilValue);
- }
- else if (depthMask)
- {
- GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Depth, 0, ref depthValue);
- }
- else if (stencilMask != 0)
- {
- GL.ClearBuffer(OpenTK.Graphics.OpenGL.ClearBuffer.Stencil, 0, ref stencilValue);
- }
- }
- public void CommandBufferBarrier()
- {
- GL.MemoryBarrier(MemoryBarrierFlags.CommandBarrierBit);
- }
- 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.Debug?.Print(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.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
- return;
- }
- PreDraw(vertexCount);
- if (_primitiveType == PrimitiveType.Quads && !HwCapabilities.SupportsQuads)
- {
- DrawQuadsImpl(vertexCount, instanceCount, firstVertex, firstInstance);
- }
- else if (_primitiveType == PrimitiveType.QuadStrip && !HwCapabilities.SupportsQuads)
- {
- DrawQuadStripImpl(vertexCount, instanceCount, firstVertex, firstInstance);
- }
- else
- {
- DrawImpl(vertexCount, instanceCount, firstVertex, firstInstance);
- }
- PostDraw();
- }
- 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.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
- return;
- }
- PreDrawVbUnbounded();
- 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 && !HwCapabilities.SupportsQuads)
- {
- DrawQuadsIndexedImpl(
- indexCount,
- instanceCount,
- indexBaseOffset,
- indexElemSize,
- firstVertex,
- firstInstance);
- }
- else if (_primitiveType == PrimitiveType.QuadStrip && !HwCapabilities.SupportsQuads)
- {
- DrawQuadStripIndexedImpl(
- indexCount,
- instanceCount,
- indexBaseOffset,
- indexElemSize,
- firstVertex,
- firstInstance);
- }
- else
- {
- DrawIndexedImpl(
- indexCount,
- instanceCount,
- indexBaseOffset,
- firstVertex,
- firstInstance);
- }
- PostDraw();
- }
- 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 DrawIndexedIndirect(BufferRange indirectBuffer)
- {
- if (!_program.IsLinked)
- {
- Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
- return;
- }
- PreDrawVbUnbounded();
- _vertexArray.SetRangeOfIndexBuffer();
- GL.BindBuffer((BufferTarget)All.DrawIndirectBuffer, indirectBuffer.Handle.ToInt32());
- GL.DrawElementsIndirect(_primitiveType, _elementsType, (IntPtr)indirectBuffer.Offset);
- _vertexArray.RestoreIndexBuffer();
- PostDraw();
- }
- public void DrawIndexedIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
- {
- if (!_program.IsLinked)
- {
- Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
- return;
- }
- PreDrawVbUnbounded();
- _vertexArray.SetRangeOfIndexBuffer();
- GL.BindBuffer((BufferTarget)All.DrawIndirectBuffer, indirectBuffer.Handle.ToInt32());
- GL.BindBuffer((BufferTarget)All.ParameterBuffer, parameterBuffer.Handle.ToInt32());
- GL.MultiDrawElementsIndirectCount(
- _primitiveType,
- (All)_elementsType,
- (IntPtr)indirectBuffer.Offset,
- (IntPtr)parameterBuffer.Offset,
- maxDrawCount,
- stride);
- _vertexArray.RestoreIndexBuffer();
- PostDraw();
- }
- public void DrawIndirect(BufferRange indirectBuffer)
- {
- if (!_program.IsLinked)
- {
- Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
- return;
- }
- PreDrawVbUnbounded();
- GL.BindBuffer((BufferTarget)All.DrawIndirectBuffer, indirectBuffer.Handle.ToInt32());
- GL.DrawArraysIndirect(_primitiveType, (IntPtr)indirectBuffer.Offset);
- PostDraw();
- }
- public void DrawIndirectCount(BufferRange indirectBuffer, BufferRange parameterBuffer, int maxDrawCount, int stride)
- {
- if (!_program.IsLinked)
- {
- Logger.Debug?.Print(LogClass.Gpu, "Draw error, shader not linked.");
- return;
- }
- PreDrawVbUnbounded();
- GL.BindBuffer((BufferTarget)All.DrawIndirectBuffer, indirectBuffer.Handle.ToInt32());
- GL.BindBuffer((BufferTarget)All.ParameterBuffer, parameterBuffer.Handle.ToInt32());
- GL.MultiDrawArraysIndirectCount(
- _primitiveType,
- (IntPtr)indirectBuffer.Offset,
- (IntPtr)parameterBuffer.Offset,
- maxDrawCount,
- stride);
- PostDraw();
- }
- public void DrawTexture(ITexture texture, ISampler sampler, Extents2DF srcRegion, Extents2DF dstRegion)
- {
- if (texture is TextureView view && sampler is Sampler samp)
- {
- _supportBuffer.Commit();
- if (HwCapabilities.SupportsDrawTexture)
- {
- GL.NV.DrawTexture(
- view.Handle,
- samp.Handle,
- dstRegion.X1,
- dstRegion.Y1,
- dstRegion.X2,
- dstRegion.Y2,
- 0,
- srcRegion.X1 / view.Width,
- srcRegion.Y1 / view.Height,
- srcRegion.X2 / view.Width,
- srcRegion.Y2 / view.Height);
- }
- else
- {
- static void Disable(EnableCap cap, bool enabled)
- {
- if (enabled)
- {
- GL.Disable(cap);
- }
- }
- static void Enable(EnableCap cap, bool enabled)
- {
- if (enabled)
- {
- GL.Enable(cap);
- }
- }
- Disable(EnableCap.CullFace, _cullEnable);
- Disable(EnableCap.StencilTest, _stencilTestEnable);
- Disable(EnableCap.DepthTest, _depthTestEnable);
- if (_depthMask)
- {
- GL.DepthMask(false);
- }
- if (_tfEnabled)
- {
- GL.EndTransformFeedback();
- }
- GL.ClipControl(ClipOrigin.UpperLeft, ClipDepthMode.NegativeOneToOne);
- _drawTexture.Draw(
- view,
- samp,
- dstRegion.X1,
- dstRegion.Y1,
- dstRegion.X2,
- dstRegion.Y2,
- srcRegion.X1 / view.Width,
- srcRegion.Y1 / view.Height,
- srcRegion.X2 / view.Width,
- srcRegion.Y2 / view.Height);
- _program?.Bind();
- _unit0Sampler?.Bind(0);
- RestoreViewport0();
- Enable(EnableCap.CullFace, _cullEnable);
- Enable(EnableCap.StencilTest, _stencilTestEnable);
- Enable(EnableCap.DepthTest, _depthTestEnable);
- if (_depthMask)
- {
- GL.DepthMask(true);
- }
- if (_tfEnabled)
- {
- GL.BeginTransformFeedback(_tfTopology);
- }
- RestoreClipControl();
- }
- }
- }
- public void EndTransformFeedback()
- {
- GL.EndTransformFeedback();
- _tfEnabled = false;
- }
- public double GetCounterDivisor(CounterType type)
- {
- if (type == CounterType.SamplesPassed)
- {
- return _renderScale[0].X * _renderScale[0].X;
- }
- return 1;
- }
- public void SetAlphaTest(bool enable, float reference, CompareOp op)
- {
- if (!enable)
- {
- GL.Disable(EnableCap.AlphaTest);
- return;
- }
- GL.AlphaFunc((AlphaFunction)op.Convert(), reference);
- GL.Enable(EnableCap.AlphaTest);
- }
- public void SetBlendState(AdvancedBlendDescriptor blend)
- {
- if (HwCapabilities.SupportsBlendEquationAdvanced)
- {
- GL.BlendEquation((BlendEquationMode)blend.Op.Convert());
- GL.NV.BlendParameter(NvBlendEquationAdvanced.BlendOverlapNv, (int)blend.Overlap.Convert());
- GL.NV.BlendParameter(NvBlendEquationAdvanced.BlendPremultipliedSrcNv, blend.SrcPreMultiplied ? 1 : 0);
- GL.Enable(EnableCap.Blend);
- _advancedBlendEnable = true;
- }
- }
- public void SetBlendState(int index, BlendDescriptor blend)
- {
- if (_advancedBlendEnable)
- {
- GL.Disable(EnableCap.Blend);
- _advancedBlendEnable = false;
- }
- 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());
- static bool IsDualSource(BlendFactor factor)
- {
- switch (factor)
- {
- case BlendFactor.Src1Color:
- case BlendFactor.Src1ColorGl:
- case BlendFactor.Src1Alpha:
- case BlendFactor.Src1AlphaGl:
- case BlendFactor.OneMinusSrc1Color:
- case BlendFactor.OneMinusSrc1ColorGl:
- case BlendFactor.OneMinusSrc1Alpha:
- case BlendFactor.OneMinusSrc1AlphaGl:
- return true;
- }
- return false;
- }
- EnsureFramebuffer();
- _framebuffer.SetDualSourceBlend(
- IsDualSource(blend.ColorSrcFactor) ||
- IsDualSource(blend.ColorDstFactor) ||
- IsDualSource(blend.AlphaSrcFactor) ||
- IsDualSource(blend.AlphaDstFactor));
- 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;
- }
- if (HwCapabilities.SupportsPolygonOffsetClamp)
- {
- GL.PolygonOffsetClamp(factor, units, clamp);
- }
- else
- {
- GL.PolygonOffset(factor, units);
- }
- }
- 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)
- {
- if (depthTest.TestEnable)
- {
- GL.Enable(EnableCap.DepthTest);
- GL.DepthFunc((DepthFunction)depthTest.Func.Convert());
- }
- else
- {
- GL.Disable(EnableCap.DepthTest);
- }
- GL.DepthMask(depthTest.WriteEnable);
- _depthMask = depthTest.WriteEnable;
- _depthTestEnable = depthTest.TestEnable;
- }
- public void SetFaceCulling(bool enable, Face face)
- {
- _cullEnable = enable;
- if (!enable)
- {
- GL.Disable(EnableCap.CullFace);
- return;
- }
- GL.CullFace(face.Convert());
- GL.Enable(EnableCap.CullFace);
- }
- public void SetFrontFace(FrontFace frontFace)
- {
- SetFrontFace(_frontFace = frontFace.Convert());
- }
- public void SetImage(int binding, ITexture texture, Format imageFormat)
- {
- if ((uint)binding < SavedImages)
- {
- _images[binding] = (texture as TextureBase, imageFormat);
- }
- if (texture == null)
- {
- GL.BindImageTexture(binding, 0, 0, true, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba8);
- return;
- }
- TextureBase texBase = (TextureBase)texture;
- SizedInternalFormat format = FormatTable.GetImageFormat(imageFormat);
- if (format != 0)
- {
- GL.BindImageTexture(binding, 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);
- }
- public void SetLogicOpState(bool enable, LogicalOp op)
- {
- if (enable)
- {
- GL.Enable(EnableCap.ColorLogicOp);
- GL.LogicOp((LogicOp)op.Convert());
- }
- else
- {
- GL.Disable(EnableCap.ColorLogicOp);
- }
- }
- public void SetMultisampleState(MultisampleDescriptor multisample)
- {
- if (multisample.AlphaToCoverageEnable)
- {
- GL.Enable(EnableCap.SampleAlphaToCoverage);
- if (multisample.AlphaToOneEnable)
- {
- GL.Enable(EnableCap.SampleAlphaToOne);
- }
- else
- {
- GL.Disable(EnableCap.SampleAlphaToOne);
- }
- if (HwCapabilities.SupportsAlphaToCoverageDitherControl)
- {
- GL.NV.AlphaToCoverageDitherControl(multisample.AlphaToCoverageDitherEnable
- ? NvAlphaToCoverageDitherControl.AlphaToCoverageDitherEnableNv
- : NvAlphaToCoverageDitherControl.AlphaToCoverageDitherDisableNv);
- }
- }
- else
- {
- GL.Disable(EnableCap.SampleAlphaToCoverage);
- }
- }
- public void SetLineParameters(float width, bool smooth)
- {
- if (smooth)
- {
- GL.Enable(EnableCap.LineSmooth);
- }
- else
- {
- GL.Disable(EnableCap.LineSmooth);
- }
- GL.LineWidth(width);
- }
- public unsafe void SetPatchParameters(int vertices, ReadOnlySpan<float> defaultOuterLevel, ReadOnlySpan<float> defaultInnerLevel)
- {
- GL.PatchParameter(PatchParameterInt.PatchVertices, vertices);
- fixed (float* pOuterLevel = defaultOuterLevel)
- {
- GL.PatchParameter(PatchParameterFloat.PatchDefaultOuterLevel, pOuterLevel);
- }
- fixed (float* pInnerLevel = defaultInnerLevel)
- {
- GL.PatchParameter(PatchParameterFloat.PatchDefaultInnerLevel, pInnerLevel);
- }
- }
- public void SetPointParameters(float size, bool isProgramPointSize, bool enablePointSprite, Origin origin)
- {
- // GL_POINT_SPRITE was deprecated in core profile 3.2+ and causes GL_INVALID_ENUM when set.
- // As we don't know if the current context is core or compat, it's safer to keep this code.
- if (enablePointSprite)
- {
- GL.Enable(EnableCap.PointSprite);
- }
- else
- {
- GL.Disable(EnableCap.PointSprite);
- }
- if (isProgramPointSize)
- {
- GL.Enable(EnableCap.ProgramPointSize);
- }
- else
- {
- GL.Disable(EnableCap.ProgramPointSize);
- }
- GL.PointParameter(origin == Origin.LowerLeft
- ? PointSpriteCoordOriginParameter.LowerLeft
- : PointSpriteCoordOriginParameter.UpperLeft);
- // Games seem to set point size to 0 which generates a GL_INVALID_VALUE
- // From the spec, GL_INVALID_VALUE is generated if size is less than or equal to 0.
- GL.PointSize(Math.Max(float.Epsilon, size));
- }
- public void SetPolygonMode(GAL.PolygonMode frontMode, GAL.PolygonMode backMode)
- {
- if (frontMode == backMode)
- {
- GL.PolygonMode(MaterialFace.FrontAndBack, frontMode.Convert());
- }
- else
- {
- GL.PolygonMode(MaterialFace.Front, frontMode.Convert());
- GL.PolygonMode(MaterialFace.Back, backMode.Convert());
- }
- }
- 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 prg = (Program)program;
- if (_tfEnabled)
- {
- GL.EndTransformFeedback();
- prg.Bind();
- GL.BeginTransformFeedback(_tfTopology);
- }
- else
- {
- prg.Bind();
- }
- if (prg.HasFragmentShader && _fragmentOutputMap != (uint)prg.FragmentOutputMap)
- {
- _fragmentOutputMap = (uint)prg.FragmentOutputMap;
- for (int index = 0; index < Constants.MaxRenderTargets; index++)
- {
- RestoreComponentMask(index, force: false);
- }
- }
- _program = prg;
- }
- public void SetRasterizerDiscard(bool discard)
- {
- if (discard)
- {
- GL.Enable(EnableCap.RasterizerDiscard);
- }
- else
- {
- GL.Disable(EnableCap.RasterizerDiscard);
- }
- _rasterizerDiscard = discard;
- }
- public void SetRenderTargetScale(float scale)
- {
- _renderScale[0].X = scale;
- _supportBuffer.UpdateRenderScale(_renderScale, 0, 1); // Just the first element.
- }
- public void SetRenderTargetColorMasks(ReadOnlySpan<uint> componentMasks)
- {
- _componentMasks = 0;
- for (int index = 0; index < componentMasks.Length; index++)
- {
- _componentMasks |= componentMasks[index] << (index * 4);
- RestoreComponentMask(index, force: false);
- }
- }
- public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
- {
- EnsureFramebuffer();
- bool isBgraChanged = false;
- for (int index = 0; index < colors.Length; index++)
- {
- TextureView color = (TextureView)colors[index];
- _framebuffer.AttachColor(index, color);
- if (color != null)
- {
- int isBgra = color.Format.IsBgr() ? 1 : 0;
- if (_fpIsBgra[index].X != isBgra)
- {
- _fpIsBgra[index].X = isBgra;
- isBgraChanged = true;
- RestoreComponentMask(index);
- }
- }
- }
- if (isBgraChanged)
- {
- _supportBuffer.UpdateFragmentIsBgra(_fpIsBgra, 0, SupportBuffer.FragmentIsBgraCount);
- }
- TextureView depthStencilView = (TextureView)depthStencil;
- _framebuffer.AttachDepthStencil(depthStencilView);
- _framebuffer.SetDrawBuffers(colors.Length);
- }
- public void SetScissors(ReadOnlySpan<Rectangle<int>> regions)
- {
- int count = Math.Min(regions.Length, Constants.MaxViewports);
- Span<int> v = stackalloc int[count * 4];
- for (int index = 0; index < count; index++)
- {
- int vIndex = index * 4;
- var region = regions[index];
- bool enabled = (region.X | region.Y) != 0 || region.Width != 0xffff || region.Height != 0xffff;
- uint mask = 1u << index;
- if (enabled)
- {
- v[vIndex] = region.X;
- v[vIndex + 1] = region.Y;
- v[vIndex + 2] = region.Width;
- v[vIndex + 3] = region.Height;
- if ((_scissorEnables & mask) == 0)
- {
- _scissorEnables |= mask;
- GL.Enable(IndexedEnableCap.ScissorTest, index);
- }
- }
- else
- {
- if ((_scissorEnables & mask) != 0)
- {
- _scissorEnables &= ~mask;
- GL.Disable(IndexedEnableCap.ScissorTest, index);
- }
- }
- }
- GL.ScissorArray(0, count, ref v[0]);
- }
- public void SetStencilTest(StencilTestDescriptor stencilTest)
- {
- _stencilTestEnable = stencilTest.TestEnable;
- 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 SetStorageBuffers(ReadOnlySpan<BufferAssignment> buffers)
- {
- SetBuffers(buffers, isStorage: true);
- }
- public void SetTextureAndSampler(ShaderStage stage, int binding, ITexture texture, ISampler sampler)
- {
- if (texture != null)
- {
- if (binding == 0)
- {
- _unit0Texture = (TextureBase)texture;
- }
- else
- {
- ((TextureBase)texture).Bind(binding);
- }
- }
- else
- {
- TextureBase.ClearBinding(binding);
- }
- Sampler glSampler = (Sampler)sampler;
- glSampler?.Bind(binding);
- if (binding == 0)
- {
- _unit0Sampler = glSampler;
- }
- }
- public void SetTransformFeedbackBuffers(ReadOnlySpan<BufferRange> buffers)
- {
- if (_tfEnabled)
- {
- GL.EndTransformFeedback();
- }
- int count = Math.Min(buffers.Length, Constants.MaxTransformFeedbackBuffers);
- for (int i = 0; i < count; i++)
- {
- BufferRange buffer = buffers[i];
- _tfbTargets[i] = buffer;
- if (buffer.Handle == BufferHandle.Null)
- {
- GL.BindBufferBase(BufferRangeTarget.TransformFeedbackBuffer, i, 0);
- continue;
- }
- if (_tfbs[i] == BufferHandle.Null)
- {
- _tfbs[i] = Buffer.Create();
- }
- Buffer.Resize(_tfbs[i], buffer.Size);
- Buffer.Copy(buffer.Handle, _tfbs[i], buffer.Offset, 0, buffer.Size);
- GL.BindBufferBase(BufferRangeTarget.TransformFeedbackBuffer, i, _tfbs[i].ToInt32());
- }
- if (_tfEnabled)
- {
- GL.BeginTransformFeedback(_tfTopology);
- }
- }
- public void SetUniformBuffers(ReadOnlySpan<BufferAssignment> buffers)
- {
- SetBuffers(buffers, 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(ReadOnlySpan<Viewport> viewports, bool disableTransform)
- {
- Array.Resize(ref _viewportArray, viewports.Length * 4);
- Array.Resize(ref _depthRangeArray, viewports.Length * 2);
- float[] viewportArray = _viewportArray;
- double[] depthRangeArray = _depthRangeArray;
- 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 + (viewport.Region.Height < 0 ? viewport.Region.Height : 0);
- viewportArray[viewportElemIndex + 2] = viewport.Region.Width;
- viewportArray[viewportElemIndex + 3] = MathF.Abs(viewport.Region.Height);
- if (HwCapabilities.SupportsViewportSwizzle)
- {
- GL.NV.ViewportSwizzle(
- index,
- viewport.SwizzleX.Convert(),
- viewport.SwizzleY.Convert(),
- viewport.SwizzleZ.Convert(),
- viewport.SwizzleW.Convert());
- }
- depthRangeArray[index * 2 + 0] = viewport.DepthNear;
- depthRangeArray[index * 2 + 1] = viewport.DepthFar;
- }
- bool flipY = viewports.Length != 0 && viewports[0].Region.Height < 0;
- SetOrigin(flipY ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft);
- GL.ViewportArray(0, viewports.Length, viewportArray);
- GL.DepthRangeArray(0, viewports.Length, depthRangeArray);
- float disableTransformF = disableTransform ? 1.0f : 0.0f;
- if (_supportBuffer.Data.ViewportInverse.W != disableTransformF || disableTransform)
- {
- float scale = _renderScale[0].X;
- _supportBuffer.UpdateViewportInverse(new Vector4<float>
- {
- X = scale * 2f / viewports[0].Region.Width,
- Y = scale * 2f / viewports[0].Region.Height,
- Z = 1,
- W = disableTransformF
- });
- }
- }
- public void TextureBarrier()
- {
- GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
- }
- public void TextureBarrierTiled()
- {
- GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
- }
- private void SetBuffers(ReadOnlySpan<BufferAssignment> buffers, bool isStorage)
- {
- BufferRangeTarget target = isStorage ? BufferRangeTarget.ShaderStorageBuffer : BufferRangeTarget.UniformBuffer;
- for (int index = 0; index < buffers.Length; index++)
- {
- BufferAssignment assignment = buffers[index];
- BufferRange buffer = assignment.Range;
- if (buffer.Handle == BufferHandle.Null)
- {
- GL.BindBufferRange(target, assignment.Binding, 0, IntPtr.Zero, 0);
- continue;
- }
- GL.BindBufferRange(target, assignment.Binding, buffer.Handle.ToInt32(), (IntPtr)buffer.Offset, buffer.Size);
- }
- }
- private void SetOrigin(ClipOrigin origin)
- {
- if (_clipOrigin != origin)
- {
- _clipOrigin = origin;
- GL.ClipControl(origin, _clipDepthMode);
- SetFrontFace(_frontFace);
- }
- }
- private void SetFrontFace(FrontFaceDirection frontFace)
- {
- // Changing clip origin will also change the front face to compensate
- // for the flipped viewport, we flip it again here to compensate as
- // this effect is undesirable for us.
- if (_clipOrigin == ClipOrigin.UpperLeft)
- {
- frontFace = frontFace == FrontFaceDirection.Ccw ? FrontFaceDirection.Cw : FrontFaceDirection.Ccw;
- }
- GL.FrontFace(frontFace);
- }
- 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()
- {
- if (BackgroundContextWorker.InBackground)
- {
- return (0, 0);
- }
- return (_boundDrawFramebuffer, _boundReadFramebuffer);
- }
- public void UpdateRenderScale(ReadOnlySpan<float> scales, int totalCount, int fragmentCount)
- {
- bool changed = false;
- for (int index = 0; index < totalCount; index++)
- {
- if (_renderScale[1 + index].X != scales[index])
- {
- _renderScale[1 + index].X = scales[index];
- changed = true;
- }
- }
- // Only update fragment count if there are scales after it for the vertex stage.
- if (fragmentCount != totalCount && fragmentCount != _fragmentScaleCount)
- {
- _fragmentScaleCount = fragmentCount;
- _supportBuffer.UpdateFragmentRenderScaleCount(_fragmentScaleCount);
- }
- if (changed)
- {
- _supportBuffer.UpdateRenderScale(_renderScale, 0, 1 + totalCount);
- }
- }
- private void PrepareForDispatch()
- {
- _unit0Texture?.Bind(0);
- _supportBuffer.Commit();
- }
- private void PreDraw(int vertexCount)
- {
- _vertexArray.PreDraw(vertexCount);
- PreDraw();
- }
- private void PreDrawVbUnbounded()
- {
- _vertexArray.PreDrawVbUnbounded();
- PreDraw();
- }
- private void PreDraw()
- {
- DrawCount++;
- _unit0Texture?.Bind(0);
- _supportBuffer.Commit();
- }
- private void PostDraw()
- {
- if (_tfEnabled)
- {
- for (int i = 0; i < Constants.MaxTransformFeedbackBuffers; i++)
- {
- if (_tfbTargets[i].Handle != BufferHandle.Null)
- {
- Buffer.Copy(_tfbs[i], _tfbTargets[i].Handle, 0, _tfbTargets[i].Offset, _tfbTargets[i].Size);
- }
- }
- }
- }
- public void RestoreComponentMask(int index, bool force = true)
- {
- // If the bound render target is bgra, swap the red and blue masks.
- uint redMask = _fpIsBgra[index].X == 0 ? 1u : 4u;
- uint blueMask = _fpIsBgra[index].X == 0 ? 4u : 1u;
- int shift = index * 4;
- uint componentMask = _componentMasks & _fragmentOutputMap;
- uint checkMask = 0xfu << shift;
- uint componentMaskAtIndex = componentMask & checkMask;
- if (!force && componentMaskAtIndex == (_currentComponentMasks & checkMask))
- {
- return;
- }
- componentMask >>= shift;
- componentMask &= 0xfu;
- GL.ColorMask(
- index,
- (componentMask & redMask) != 0,
- (componentMask & 2u) != 0,
- (componentMask & blueMask) != 0,
- (componentMask & 8u) != 0);
- _currentComponentMasks &= ~checkMask;
- _currentComponentMasks |= componentMaskAtIndex;
- }
- public void RestoreClipControl()
- {
- GL.ClipControl(_clipOrigin, _clipDepthMode);
- }
- public void RestoreScissor0Enable()
- {
- if ((_scissorEnables & 1u) != 0)
- {
- GL.Enable(IndexedEnableCap.ScissorTest, 0);
- }
- }
- public void RestoreRasterizerDiscard()
- {
- if (_rasterizerDiscard)
- {
- GL.Enable(EnableCap.RasterizerDiscard);
- }
- }
- public void RestoreViewport0()
- {
- if (_viewportArray.Length > 0)
- {
- GL.ViewportArray(0, 1, _viewportArray);
- }
- }
- public void RestoreProgram()
- {
- _program?.Bind();
- }
- public void RestoreImages1And2()
- {
- for (int i = 0; i < SavedImages; i++)
- {
- (TextureBase texBase, Format imageFormat) = _images[i];
- if (texBase != null)
- {
- SizedInternalFormat format = FormatTable.GetImageFormat(imageFormat);
- if (format != 0)
- {
- GL.BindImageTexture(i, texBase.Handle, 0, true, 0, TextureAccess.ReadWrite, format);
- continue;
- }
- }
- GL.BindImageTexture(i, 0, 0, true, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba8);
- }
- }
- 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 (compare == 0 && evt.Type == QueryTarget.SamplesPassed && evt.ClearCounter)
- {
- if (!value.ReserveForHostAccess())
- {
- // 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;
- }
- GL.BeginConditionalRender(evt.Query, isEqual ? ConditionalRenderType.QueryNoWaitInverted : ConditionalRenderType.QueryNoWait);
- _activeConditionalRender = evt;
- 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();
- _activeConditionalRender?.ReleaseHostAccess();
- _activeConditionalRender = null;
- }
- public void Dispose()
- {
- _supportBuffer?.Dispose();
- for (int i = 0; i < Constants.MaxTransformFeedbackBuffers; i++)
- {
- if (_tfbs[i] != BufferHandle.Null)
- {
- Buffer.Delete(_tfbs[i]);
- _tfbs[i] = BufferHandle.Null;
- }
- }
- _activeConditionalRender?.ReleaseHostAccess();
- _framebuffer?.Dispose();
- _vertexArray?.Dispose();
- _drawTexture.Dispose();
- }
- }
- }
|