Esempio n. 1
0
 public GalVertexAttrib(
     bool IsConst,
     int Offset,
     GalVertexAttribSize Size,
     GalVertexAttribType Type,
     bool IsBgra)
 {
     this.IsConst = IsConst;
     this.Offset  = Offset;
     this.Size    = Size;
     this.Type    = Type;
     this.IsBgra  = IsBgra;
 }
Esempio n. 2
0
 public GalVertexAttrib(
     int index,
     bool isConst,
     int offset,
     byte[]              data,
     GalVertexAttribSize size,
     GalVertexAttribType type,
     bool isBgra)
 {
     Index   = index;
     IsConst = isConst;
     Data    = data;
     Offset  = offset;
     Size    = size;
     Type    = type;
     IsBgra  = isBgra;
 }
Esempio n. 3
0
 public GalVertexAttrib(
     int Index,
     int Buffer,
     bool IsConst,
     int Offset,
     GalVertexAttribSize Size,
     GalVertexAttribType Type,
     bool IsBgra)
 {
     this.Index   = Index;
     this.Buffer  = Buffer;
     this.IsConst = IsConst;
     this.Offset  = Offset;
     this.Size    = Size;
     this.Type    = Type;
     this.IsBgra  = IsBgra;
 }
Esempio n. 4
0
 public GalVertexAttrib(
     int Index,
     bool IsConst,
     int Offset,
     byte[]              Data,
     GalVertexAttribSize Size,
     GalVertexAttribType Type,
     bool IsBgra)
 {
     this.Index   = Index;
     this.IsConst = IsConst;
     this.Data    = Data;
     this.Offset  = Offset;
     this.Size    = Size;
     this.Type    = Type;
     this.IsBgra  = IsBgra;
 }
Esempio n. 5
0
 public GalVertexAttrib(
     int Index,
     bool IsConst,
     int Offset,
     IntPtr Pointer,
     GalVertexAttribSize Size,
     GalVertexAttribType Type,
     bool IsBgra)
 {
     this.Index   = Index;
     this.IsConst = IsConst;
     this.Pointer = Pointer;
     this.Offset  = Offset;
     this.Size    = Size;
     this.Type    = Type;
     this.IsBgra  = IsBgra;
 }
Esempio n. 6
0
        private void UploadVertexArrays(NvGpuVmm vmm, GalPipelineState state)
        {
            Profile.Begin(Profiles.GPU.Engine3d.UploadVertexArrays);

            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.ConvertQuadsToTris(buffer, indexEntrySize, indexCount);
                        }
                        else /* if (PrimType == GalPrimitiveType.QuadStrip) */
                        {
                            buffer = QuadHelper.ConvertQuadStripToTris(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.ConvertSizeQuadsToTris(ibSize), indexFormat);
                    }
                    else /* if (PrimType == GalPrimitiveType.QuadStrip) */
                    {
                        _gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertSizeQuadStripToTris(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);

                if (vbPosition == 0)
                {
                    continue;
                }

                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;

                // Check vertex array is enabled to avoid out of bounds exception when reading bytes
                bool enable = (ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + arrayIndex * 4) & 0x1000) != 0;

                // Note: 16 is the maximum size of an attribute,
                // having a component size of 32-bits with 4 elements (a vec4).
                if (enable)
                {
                    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;
                int  modifiedVbSize = (int)vbSize;


                // If quads convert size to triangle length
                if (stride == 0)
                {
                    if (primType == GalPrimitiveType.Quads)
                    {
                        modifiedVbSize = QuadHelper.ConvertSizeQuadsToTris(modifiedVbSize);
                    }
                    else if (primType == GalPrimitiveType.QuadStrip)
                    {
                        modifiedVbSize = QuadHelper.ConvertSizeQuadStripToTris(modifiedVbSize);
                    }
                }

                bool vboCached = _gpu.Renderer.Rasterizer.IsVboCached(vboKey, modifiedVbSize);

                if (!vboCached || _gpu.ResourceManager.MemoryRegionModified(vmm, vboKey, vbSize, NvGpuBufferType.Vertex))
                {
                    if ((primType == GalPrimitiveType.Quads | primType == GalPrimitiveType.QuadStrip) && stride != 0)
                    {
                        // Convert quad buffer to triangles
                        byte[] data = vmm.ReadBytes(vbPosition, vbSize);

                        if (primType == GalPrimitiveType.Quads)
                        {
                            data = QuadHelper.ConvertQuadsToTris(data, stride, (int)(vbSize / stride));
                        }
                        else
                        {
                            data = QuadHelper.ConvertQuadStripToTris(data, stride, (int)(vbSize / stride));
                        }
                        _gpu.Renderer.Rasterizer.CreateVbo(vboKey, data);
                    }
                    else 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();
            }

            Profile.End(Profiles.GPU.Engine3d.UploadVertexArrays);
        }
Esempio n. 7
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);

                if (VbPosition == 0)
                {
                    continue;
                }

                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();
            }
        }