private void DispatchRender(NvGpuVmm Vmm, GalPipelineState State) { int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl); GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff); bool InstanceNext = ((PrimCtrl >> 26) & 1) != 0; bool InstanceCont = ((PrimCtrl >> 27) & 1) != 0; if (InstanceNext && InstanceCont) { throw new InvalidOperationException("GPU tried to increase and reset instance count at the same time"); } if (InstanceNext) { CurrentInstance++; } else if (!InstanceCont) { CurrentInstance = 0; } State.Instance = CurrentInstance; Gpu.Renderer.Pipeline.Bind(State); Gpu.Renderer.RenderTarget.Bind(); if (IndexCount != 0) { int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst); int VertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase); long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); long IboKey = Vmm.GetPhysicalAddress(IndexPosition); //Quad primitive types were deprecated on OpenGL 3.x, //they are converted to a triangles index buffer on IB creation, //so we should use the triangles type here too. if (PrimType == GalPrimitiveType.Quads || PrimType == GalPrimitiveType.QuadStrip) { PrimType = GalPrimitiveType.Triangles; //Note: We assume that index first points to the first //vertex of a quad, if it points to the middle of a //quad (First % 4 != 0 for Quads) then it will not work properly. if (PrimType == GalPrimitiveType.Quads) { IndexFirst = QuadHelper.ConvertIbSizeQuadsToTris(IndexFirst); } else /* if (PrimType == GalPrimitiveType.QuadStrip) */ { IndexFirst = QuadHelper.ConvertIbSizeQuadStripToTris(IndexFirst); } } Gpu.Renderer.Rasterizer.DrawElements(IboKey, IndexFirst, VertexBase, PrimType); } else { int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst); int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount); Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType); } //Is the GPU really clearing those registers after draw? WriteRegister(NvGpuEngine3dReg.IndexBatchFirst, 0); WriteRegister(NvGpuEngine3dReg.IndexBatchCount, 0); }
private void UploadVertexArrays(NvGpuVmm Vmm, GalPipelineState State) { long IbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress); long IboKey = Vmm.GetPhysicalAddress(IbPosition); int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat); int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount); int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl); GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff); GalIndexFormat IndexFormat = (GalIndexFormat)IndexEntryFmt; int IndexEntrySize = 1 << IndexEntryFmt; if (IndexEntrySize > 4) { throw new InvalidOperationException("Invalid index entry size \"" + IndexEntrySize + "\"!"); } if (IndexCount != 0) { int IbSize = IndexCount * IndexEntrySize; bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IboKey, (uint)IbSize); bool UsesLegacyQuads = PrimType == GalPrimitiveType.Quads || PrimType == GalPrimitiveType.QuadStrip; if (!IboCached || Gpu.ResourceManager.MemoryRegionModified(Vmm, IboKey, (uint)IbSize, NvGpuBufferType.Index)) { if (!UsesLegacyQuads) { if (Vmm.TryGetHostAddress(IbPosition, IbSize, out IntPtr IbPtr)) { Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, IbPtr); } else { Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, Vmm.ReadBytes(IbPosition, IbSize)); } } else { byte[] Buffer = Vmm.ReadBytes(IbPosition, IbSize); if (PrimType == GalPrimitiveType.Quads) { Buffer = QuadHelper.ConvertIbQuadsToTris(Buffer, IndexEntrySize, IndexCount); } else /* if (PrimType == GalPrimitiveType.QuadStrip) */ { Buffer = QuadHelper.ConvertIbQuadStripToTris(Buffer, IndexEntrySize, IndexCount); } Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, Buffer); } } if (!UsesLegacyQuads) { Gpu.Renderer.Rasterizer.SetIndexArray(IbSize, IndexFormat); } else { if (PrimType == GalPrimitiveType.Quads) { Gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertIbSizeQuadsToTris(IbSize), IndexFormat); } else /* if (PrimType == GalPrimitiveType.QuadStrip) */ { Gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertIbSizeQuadStripToTris(IbSize), IndexFormat); } } } List <GalVertexAttrib>[] Attribs = new List <GalVertexAttrib> [32]; for (int Attr = 0; Attr < 16; Attr++) { int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr); int ArrayIndex = Packed & 0x1f; if (Attribs[ArrayIndex] == null) { Attribs[ArrayIndex] = new List <GalVertexAttrib>(); } long VbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + ArrayIndex * 4); bool IsConst = ((Packed >> 6) & 1) != 0; int Offset = (Packed >> 7) & 0x3fff; GalVertexAttribSize Size = (GalVertexAttribSize)((Packed >> 21) & 0x3f); GalVertexAttribType Type = (GalVertexAttribType)((Packed >> 27) & 0x7); bool IsRgba = ((Packed >> 31) & 1) != 0; //Note: 16 is the maximum size of an attribute, //having a component size of 32-bits with 4 elements (a vec4). byte[] Data = Vmm.ReadBytes(VbPosition + Offset, 16); Attribs[ArrayIndex].Add(new GalVertexAttrib(Attr, IsConst, Offset, Data, Size, Type, IsRgba)); } State.VertexBindings = new GalVertexBinding[32]; for (int Index = 0; Index < 32; Index++) { if (Attribs[Index] == null) { continue; } int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4); bool Enable = (Control & 0x1000) != 0; if (!Enable) { continue; } long VbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4); long VbEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2); int VertexDivisor = ReadRegister(NvGpuEngine3dReg.VertexArrayNDivisor + Index * 4); bool Instanced = ReadRegisterBool(NvGpuEngine3dReg.VertexArrayNInstance + Index); int Stride = Control & 0xfff; if (Instanced && VertexDivisor != 0) { VbPosition += Stride * (CurrentInstance / VertexDivisor); } if (VbPosition > VbEndPos) { //Instance is invalid, ignore the draw call continue; } long VboKey = Vmm.GetPhysicalAddress(VbPosition); long VbSize = (VbEndPos - VbPosition) + 1; bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VboKey, VbSize); if (!VboCached || Gpu.ResourceManager.MemoryRegionModified(Vmm, VboKey, VbSize, NvGpuBufferType.Vertex)) { if (Vmm.TryGetHostAddress(VbPosition, VbSize, out IntPtr VbPtr)) { Gpu.Renderer.Rasterizer.CreateVbo(VboKey, (int)VbSize, VbPtr); } else { Gpu.Renderer.Rasterizer.CreateVbo(VboKey, Vmm.ReadBytes(VbPosition, VbSize)); } } State.VertexBindings[Index].Enabled = true; State.VertexBindings[Index].Stride = Stride; State.VertexBindings[Index].VboKey = VboKey; State.VertexBindings[Index].Instanced = Instanced; State.VertexBindings[Index].Divisor = VertexDivisor; State.VertexBindings[Index].Attribs = Attribs[Index].ToArray(); } }