Exemple #1
0
        public void CopyBufferPointerUnsafeVirtual()
        {
            int targetX = this.Width / 4;
            int targetY = 0;

            for (int y = 0; y < this.Height; y++)
            {
                BufferPointer <byte>  source      = this.GetAreaRow(y);
                BufferPointer <Color> destination = this.GetPixelAccessorRow(targetX, targetY + y);
                this.executor.VirtualCopy(destination, source, this.area.Width);
            }
        }
Exemple #2
0
        protected override void DoInitialize()
        {
            // init shader program
            ShaderProgram program = new ShaderProgram();
            string        vertexShaderCode = null, geometryShaderCode = null, fragmentShaderCode = null;

            FillShaderCodes(this.allShaderCodes, ref vertexShaderCode, ref geometryShaderCode, ref fragmentShaderCode);
            program.Create(vertexShaderCode, fragmentShaderCode, geometryShaderCode, null);
            this.shaderProgram = program;

            // init all uniform variables
            this.uniformVariables = GetAllUniformVariables(this.allShaderCodes);

            // init property buffer objects' renderer
            var propertyBufferRenderers = new BufferPointer[propertyNameMap.Count()];
            int index = 0;

            foreach (var item in propertyNameMap)
            {
                BufferPointer bufferRenderer = this.model.GetBufferRenderer(
                    item.NameInIConvert2BufferRenderer, item.VarNameInShader);
                if (bufferRenderer == null)
                {
                    throw new Exception();
                }
                propertyBufferRenderers[index++] = bufferRenderer;
            }
            this.propertyBufferRenderers = propertyBufferRenderers;

            // init index buffer object's renderer
            this.indexBufferRenderer = this.model.GetIndexBufferRenderer();

            this.model           = null;
            this.allShaderCodes  = null;
            this.propertyNameMap = null;
            //this.uniformNameMap = null;

            {
                IndexBufferPointer renderer = this.indexBufferRenderer as IndexBufferPointer;
                if (renderer != null)
                {
                    this.elementCount = renderer.ElementCount;
                }
            }
            {
                ZeroIndexBufferPointer renderer = this.indexBufferRenderer as ZeroIndexBufferPointer;
                if (renderer != null)
                {
                    this.elementCount = renderer.VertexCount;
                }
            }
        }
        public void AsBytes()
        {
            Foo[] fooz = { new Foo(1, 2), new Foo(3, 4), new Foo(5, 6) };

            using (PinnedBuffer <Foo> colorBuf = new PinnedBuffer <Foo>(fooz))
            {
                BufferPointer <Foo>  orig    = colorBuf.Slice(1);
                BufferPointer <byte> asBytes = (BufferPointer <byte>)orig;

                Assert.Equal(asBytes.Offset, sizeof(Foo));
                Assert.Equal(orig.PointerAtOffset, asBytes.PointerAtOffset);
            }
        }
        public void ConstructWithoutOffset()
        {
            Foo[] array = Foo.CreateArray(3);
            fixed(Foo *p = array)
            {
                // Act:
                BufferPointer <Foo> ap = new BufferPointer <Foo>(array, p);

                // Assert:
                Assert.Equal(array, ap.Array);
                Assert.Equal((IntPtr)p, ap.PointerAtOffset);
            }
        }
Exemple #5
0
        private bool AddEdgeUnchecked(NodeId source, NodeId target, bool isLight)
        {
            BufferPointer <NodeEdgeListHeader> targetInEdges = InEdges.GetBufferPointer(target.Value);

            Contract.Assert(!targetInEdges.Buffer[targetInEdges.Index].IsSealed, "Attempted to add edge to sealed node");

            return(AddEdgeUnchecked(
                       source: source,
                       target: target,
                       targetInEdges: targetInEdges,
                       isLight: isLight,
                       bulkAddingTargetIncoming: false));
        }
Exemple #6
0
        public void CopyBufferPointerUnsafeInlined()
        {
            uint byteCount = (uint)this.area.Width * 4;

            int targetX = this.Width / 4;
            int targetY = 0;

            for (int y = 0; y < this.Height; y++)
            {
                BufferPointer <byte>  source      = this.GetAreaRow(y);
                BufferPointer <Color> destination = this.GetPixelAccessorRow(targetX, targetY + y);
                Unsafe.CopyBlock((void *)destination.PointerAtOffset, (void *)source.PointerAtOffset, byteCount);
            }
        }
Exemple #7
0
        private bool AddEdgeUnchecked(
            NodeId source,
            NodeId target,
            BufferPointer <NodeEdgeListHeader> targetInEdges,
            bool isLight,
            bool bulkAddingTargetIncoming)
        {
            BufferPointer <NodeEdgeListHeader> outEdges = OutEdges.GetBufferPointer(source.Value);

            var edgeSetItem = new LinkedEdgeSetItem(source, target, isLight);
            int index       = 0;

            if (!bulkAddingTargetIncoming)
            {
                ConcurrentBigSet <LinkedEdgeSetItem> .GetAddOrUpdateResult result =
                    m_edgeSet.GetOrAdd(edgeSetItem);

                if (result.IsFound)
                {
                    // Edge already existed
                    return(false);
                }

                index = result.Index;
            }
            else
            {
                index = m_edgeSet.ReservedNextIndex(m_edgeSetBuffer);
                m_edgeSetBuffer[index] = edgeSetItem;
            }

            // Update head index for in edges and out edges
            int inEdgesNext  = Interlocked.Exchange(ref targetInEdges.Buffer[targetInEdges.Index].FirstIndex, index);
            int outEdgesNext = Interlocked.Exchange(ref outEdges.Buffer[outEdges.Index].FirstIndex, index);

            var linkedEdgeSetItemPtr = m_edgeSetBuffer.GetBufferPointer(index);

            // Update next pointers
            linkedEdgeSetItemPtr.Buffer[linkedEdgeSetItemPtr.Index].NextTargetIncomingEdgeIndex = inEdgesNext;
            linkedEdgeSetItemPtr.Buffer[linkedEdgeSetItemPtr.Index].NextSourceOutgoingEdgeIndex = outEdgesNext;

            Interlocked.Increment(ref m_edgeCount);

            // Update edge counts
            targetInEdges.Buffer[targetInEdges.Index].InterlockedIncrementCount();
            outEdges.Buffer[outEdges.Index].InterlockedIncrementCount();
            return(true);
        }
        protected void InitializeVAO()
        {
            IModel model = this.model;

            this.positionBufferRenderer = model.GetPositionBufferRenderer(strin_Position);
            //this.colorBufferRenderer = model.GetColorBufferRenderer(strin_Color);
            this.normalBufferRenderer = model.GetNormalBufferRenderer(strin_Normal);
            this.indexBufferRenderer  = model.GetIndexes() as IndexBufferPointerBase;

            IndexBufferPointer renderer = this.indexBufferRenderer as IndexBufferPointer;

            if (renderer != null)
            {
                this.indexCount = renderer.ElementCount;
            }
        }
            public void ColorToBytes()
            {
                Color[] colors = { new Color(0, 1, 2, 3), new Color(4, 5, 6, 7), new Color(8, 9, 10, 11), };

                using (PinnedBuffer <Color> colorBuf = new PinnedBuffer <Color>(colors))
                    using (PinnedBuffer <byte> byteBuf = new PinnedBuffer <byte>(colors.Length * 4))
                    {
                        BufferPointer.Copy <Color>(colorBuf, byteBuf, colorBuf.Count);

                        byte[] a = byteBuf.Array;

                        for (int i = 0; i < byteBuf.Count; i++)
                        {
                            Assert.Equal((byte)i, a[i]);
                        }
                    }
            }
Exemple #10
0
        private unsafe void InitVertexBuffers()
        {
            {
                VR01PositionBuffer positionBuffer = new VR01PositionBuffer(strin_Position);
                positionBuffer.Alloc(zFrameCount);
                QuadPosition *array = (QuadPosition *)positionBuffer.FirstElement();
                for (int i = 0; i < zFrameCount; i++)
                {
                    array[i] = new QuadPosition(
                        new vec3(-xLength, -yLength, (float)i / (float)zFrameCount - 0.5f),
                        new vec3(xLength, -yLength, (float)i / (float)zFrameCount - 0.5f),
                        new vec3(xLength, yLength, (float)i / (float)zFrameCount - 0.5f),
                        new vec3(-xLength, yLength, (float)i / (float)zFrameCount - 0.5f)
                        );
                }
                this.positionBufferRenderer = positionBuffer.GetRenderer();
                positionBuffer.Dispose();
            }

            {
                VR01UVBuffer uvBuffer = new VR01UVBuffer(strin_uv);
                uvBuffer.Alloc(zFrameCount);
                QuadUV *array = (QuadUV *)uvBuffer.FirstElement();
                for (int i = 0; i < zFrameCount; i++)
                {
                    array[i] = new QuadUV(
                        new vec3(0, 0, (float)i / (float)zFrameCount),
                        new vec3(1, 0, (float)i / (float)zFrameCount),
                        new vec3(1, 1, (float)i / (float)zFrameCount),
                        new vec3(0, 1, (float)i / (float)zFrameCount)
                        );
                }
                this.uvBufferRenderer = uvBuffer.GetRenderer();
                uvBuffer.Dispose();
            }
            {
                var indexBuffer = new ZeroIndexBuffer(DrawMode.Quads, 0, zFrameCount * 4);
                indexBuffer.Alloc(zFrameCount);// this actually does nothing.
                this.indexBufferRenderer = indexBuffer.GetRenderer() as IndexBufferPointerBase;
                indexBuffer.Dispose();
            }
            this.vao = new VertexArrayObject(
                this.indexBufferRenderer,
                this.positionBufferRenderer, this.uvBufferRenderer);
        }
            public void GenericToOwnType(int count)
            {
                Foo[] source = Foo.CreateArray(count + 2);
                Foo[] dest   = new Foo[count + 5];

                fixed(Foo *pSource = source)
                fixed(Foo * pDest = dest)
                {
                    BufferPointer <Foo> apSource = new BufferPointer <Foo>(source, pSource);
                    BufferPointer <Foo> apDest   = new BufferPointer <Foo>(dest, pDest);

                    BufferPointer.Copy(apSource, apDest, count);
                }

                Assert.Equal(source[0], dest[0]);
                Assert.Equal(source[count - 1], dest[count - 1]);
                Assert.NotEqual(source[count], dest[count]);
            }
Exemple #12
0
        public void Slice()
        {
            Foo[] a = { new Foo()
                        {
                            A = 1, B = 2
                        }, new Foo()
                        {
                            A = 3, B = 4
                        } };

            using (PinnedBuffer <Foo> buffer = new PinnedBuffer <Foo>(a))
            {
                BufferPointer <Foo> arrayPtr = buffer.Slice();

                Assert.Equal(a, arrayPtr.Array);
                Assert.Equal(0, arrayPtr.Offset);
                Assert.Equal(buffer.Pointer, arrayPtr.PointerAtOffset);
            }
        }
        public void Clear(int count)
        {
            Foo[] array = Foo.CreateArray(count + 42);

            int offset = 2;

            fixed(Foo *p = array)
            {
                BufferPointer <Foo> ap = new BufferPointer <Foo>(array, p, offset);

                // Act:
                ap.Clear(count);

                Assert.NotEqual(default(Foo), array[offset - 1]);
                Assert.Equal(default(Foo), array[offset]);
                Assert.Equal(default(Foo), array[offset + count - 1]);
                Assert.NotEqual(default(Foo), array[offset + count]);
            }
        }
        public void Slice()
        {
            Foo[] array       = Foo.CreateArray(5);
            int   offset0     = 2;
            int   offset1     = 2;
            int   totalOffset = offset0 + offset1;

            fixed(Foo *p = array)
            {
                BufferPointer <Foo> ap = new BufferPointer <Foo>(array, p, offset0);

                // Act:
                ap = ap.Slice(offset1);

                // Assert:
                Assert.Equal(array, ap.Array);
                Assert.Equal(totalOffset, ap.Offset);
                Assert.Equal((IntPtr)(p + totalOffset), ap.PointerAtOffset);
            }
        }
            public void BytesToGeneric(int count)
            {
                int destCount = count * sizeof(Foo);

                byte[] source = new byte[destCount + sizeof(Foo) + 1];
                Foo[]  dest   = Foo.CreateArray(count + 2);

                fixed(byte *pSource = source)
                fixed(Foo * pDest = dest)
                {
                    BufferPointer <byte> apSource = new BufferPointer <byte>(source, pSource);
                    BufferPointer <Foo>  apDest   = new BufferPointer <Foo>(dest, pDest);

                    BufferPointer.Copy(apSource, apDest, count);
                }

                Assert.True(ElementsAreEqual(dest, source, 0));
                Assert.True(ElementsAreEqual(dest, source, count - 1));
                Assert.False(ElementsAreEqual(dest, source, count));
            }
            public void IntToBytes(int count)
            {
                int destCount = count * sizeof(int);

                int[]  source = CreateTestInts(count + 2);
                byte[] dest   = new byte[destCount + sizeof(int) + 1];

                fixed(int *pSource = source)
                fixed(byte *pDest = dest)
                {
                    BufferPointer <int>  apSource = new BufferPointer <int>(source, pSource);
                    BufferPointer <byte> apDest   = new BufferPointer <byte>(dest, pDest);

                    BufferPointer.Copy(apSource, apDest, count);
                }

                AssertNotDefault(source, 1);

                Assert.True(ElementsAreEqual(source, dest, 0));
                Assert.True(ElementsAreEqual(source, dest, count - 1));
                Assert.False(ElementsAreEqual(source, dest, count));
            }
Exemple #17
0
            /// <inheritdoc />
            internal override void Apply(float[] scanlineBuffer, int scanlineWidth, int offset, int x, int y)
            {
                Guard.MustBeGreaterThanOrEqualTo(scanlineBuffer.Length, offset + scanlineWidth, nameof(scanlineWidth));

                using (PinnedBuffer <float> buffer = new PinnedBuffer <float>(scanlineBuffer))
                {
                    BufferPointer <float> slice = buffer.Slice(offset);

                    for (int xPos = 0; xPos < scanlineWidth; xPos++)
                    {
                        int targetX = xPos + x;
                        int targetY = y;

                        float opacity = slice[xPos];
                        if (opacity > Constants.Epsilon)
                        {
                            Vector4 backgroundVector = this.Target[targetX, targetY].ToVector4();

                            Vector4 sourceVector = backgroundVector;
                            float   distance     = Vector4.DistanceSquared(sourceVector, this.sourceColor);
                            if (distance <= this.threshold)
                            {
                                float lerpAmount = (this.threshold - distance) / this.threshold;
                                sourceVector = Vector4BlendTransforms.PremultipliedLerp(
                                    sourceVector,
                                    this.targetColor,
                                    lerpAmount);

                                Vector4 finalColor = Vector4BlendTransforms.PremultipliedLerp(backgroundVector, sourceVector, opacity);

                                TColor packed = default(TColor);
                                packed.PackFromVector4(finalColor);
                                this.Target[targetX, targetY] = packed;
                            }
                        }
                    }
                }
            }
Exemple #18
0
        protected void InitializeVAO()
        {
            this.positionBufferRenderer = model.GetPositionBufferRenderer(strposition);
            this.colorBufferRenderer    = model.GetColorBufferRenderer(strcolor);
            //this.texCoordBufferRenderer = ???(strtexCoord);
            this.texCoordBufferRenderer = model.GetTexCoordBufferRenderer(strtexCoord);
            this.indexBufferRenderer    = model.GetIndexes() as IndexBufferPointerBase;

            {
                IndexBufferPointer renderer = this.indexBufferRenderer as IndexBufferPointer;
                if (renderer != null)
                {
                    this.elementCount = renderer.ElementCount;
                }
            }
            {
                ZeroIndexBufferPointer renderer = this.indexBufferRenderer as ZeroIndexBufferPointer;
                if (renderer != null)
                {
                    this.elementCount = renderer.VertexCount;
                }
            }
        }
            public void IntToInt(int count)
            {
                int[] source = CreateTestInts(count + 2);
                int[] dest   = new int[count + 5];

                fixed(int *pSource = source)
                fixed(int *pDest = dest)
                {
                    BufferPointer <int> apSource = new BufferPointer <int>(source, pSource, 1);
                    BufferPointer <int> apDest   = new BufferPointer <int>(dest, pDest, 1);

                    BufferPointer.Copy(apSource, apDest, count - 1);
                }

                AssertNotDefault(source, 1);
                AssertNotDefault(dest, 1);

                Assert.NotEqual(source[0], dest[0]);
                Assert.Equal(source[1], dest[1]);
                Assert.Equal(source[2], dest[2]);
                Assert.Equal(source[count - 1], dest[count - 1]);
                Assert.NotEqual(source[count], dest[count]);
            }
            public void GenericToOwnType_Aligned(int count)
            {
                AlignedFoo[] source = AlignedFoo.CreateArray(count + 2);
                AlignedFoo[] dest   = new AlignedFoo[count + 5];

                fixed(AlignedFoo *pSource = source)
                fixed(AlignedFoo * pDest = dest)
                {
                    BufferPointer <AlignedFoo> apSource = new BufferPointer <AlignedFoo>(source, pSource, 1);
                    BufferPointer <AlignedFoo> apDest   = new BufferPointer <AlignedFoo>(dest, pDest, 1);

                    BufferPointer.Copy(apSource, apDest, count - 1);
                }

                AssertNotDefault(source, 1);
                AssertNotDefault(dest, 1);

                Assert.NotEqual(source[0], dest[0]);
                Assert.Equal(source[1], dest[1]);
                Assert.Equal(source[2], dest[2]);
                Assert.Equal(source[count - 1], dest[count - 1]);
                Assert.NotEqual(source[count], dest[count]);
            }
        protected void InitializeVAO()
        {
            IModel model = this.model;

            this.positionBufferRenderer = model.GetPositionBufferRenderer(strin_Position);
            //this.colorBufferRenderer = model.GetColorBufferRenderer(strin_Color);
            using (var colorBuffer = new ColorBuffer(strin_Color))
            {
                colorBuffer.Alloc(4 * 6);
                for (int i = 0; i < 6; i++)
                {
                    colorBuffer[i * 4 + 0] = new vec2(0, 0);
                    colorBuffer[i * 4 + 1] = new vec2(0, 1);
                    colorBuffer[i * 4 + 2] = new vec2(1, 1);
                    colorBuffer[i * 4 + 3] = new vec2(1, 0);
                }

                this.colorBufferRenderer = colorBuffer.GetRenderer();
            }
            //this.normalBufferRenderer = model.GetNormalBufferRenderer(strin_Normal);
            this.indexBufferRenderer = model.GetIndexes() as IndexBufferPointerBase;

            {
                IndexBufferPointer renderer = this.indexBufferRenderer as IndexBufferPointer;
                if (renderer != null)
                {
                    this.elementCount = renderer.ElementCount;
                }
            }
            {
                ZeroIndexBufferPointer renderer = this.indexBufferRenderer as ZeroIndexBufferPointer;
                if (renderer != null)
                {
                    this.elementCount = renderer.VertexCount;
                }
            }
        }
            public void BytesToGeneric(int count)
            {
                int srcCount = count * sizeof(Foo);

                byte[] source = CreateTestBytes(srcCount);
                Foo[]  dest   = new Foo[count + 2];

                fixed(byte *pSource = source)
                fixed(Foo * pDest = dest)
                {
                    BufferPointer <byte> apSource = new BufferPointer <byte>(source, pSource);
                    BufferPointer <Foo>  apDest   = new BufferPointer <Foo>(dest, pDest);

                    BufferPointer.Copy(apSource, apDest, count);
                }

                AssertNotDefault(source, sizeof(Foo) + 1);
                AssertNotDefault(dest, 1);

                Assert.True(ElementsAreEqual(dest, source, 0));
                Assert.True(ElementsAreEqual(dest, source, 1));
                Assert.True(ElementsAreEqual(dest, source, count - 1));
                Assert.False(ElementsAreEqual(dest, source, count));
            }
            public void GenericToBytes_Aligned(int count)
            {
                int destCount = count * sizeof(Foo);

                AlignedFoo[] source = AlignedFoo.CreateArray(count + 2);
                byte[]       dest   = new byte[destCount + sizeof(AlignedFoo) * 2];

                fixed(AlignedFoo *pSource = source)
                fixed(byte *pDest = dest)
                {
                    BufferPointer <AlignedFoo> apSource = new BufferPointer <AlignedFoo>(source, pSource, 1);
                    BufferPointer <byte>       apDest   = new BufferPointer <byte>(dest, pDest, sizeof(AlignedFoo));

                    BufferPointer.Copy(apSource, apDest, count - 1);
                }

                AssertNotDefault(source, 1);

                Assert.False(ElementsAreEqual(source, dest, 0));
                Assert.True(ElementsAreEqual(source, dest, 1));
                Assert.True(ElementsAreEqual(source, dest, 2));
                Assert.True(ElementsAreEqual(source, dest, count - 1));
                Assert.False(ElementsAreEqual(source, dest, count));
            }
Exemple #24
0
        private bool IsSealed(NodeId target)
        {
            BufferPointer <NodeEdgeListHeader> targetInEdges = InEdges.GetBufferPointer(target.Value);

            return(targetInEdges.Buffer[targetInEdges.Index].IsSealed);
        }
        private unsafe void InitVertexBuffers()
        {
            // http://images.cnblogs.com/cnblogs_com/bitzhuwei/482613/o_Cube-small.jpg
            {
                VR03PositionBuffer positionBuffer = new VR03PositionBuffer(strin_Position);
                positionBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount);
                HexahedronPosition *array = (HexahedronPosition *)positionBuffer.FirstElement();
                int index = 0;
                for (int i = 0; i < xFrameCount; i++)
                {
                    for (int j = 0; j < yFrameCount; j++)
                    {
                        for (int k = 0; k < zFrameCount; k++)
                        {
                            var x          = ((float)i / (float)xFrameCount - 0.5f) * factor;
                            var y          = ((float)j / (float)yFrameCount - 0.5f) * factor;
                            var z          = (((float)k / (float)zFrameCount - 0.5f) * 109.0f / 256.0f) * factor;
                            var hexahedron = new HexahedronPosition();
                            hexahedron.v0  = new vec3(x + hexahedronHalfLength, y + hexahedronHalfLength, z + hexahedronHalfLength);
                            hexahedron.v1  = new vec3(x - hexahedronHalfLength, y + hexahedronHalfLength, z + hexahedronHalfLength);
                            hexahedron.v2  = new vec3(x + hexahedronHalfLength, y - hexahedronHalfLength, z + hexahedronHalfLength);
                            hexahedron.v3  = new vec3(x - hexahedronHalfLength, y - hexahedronHalfLength, z + hexahedronHalfLength);
                            hexahedron.v4  = new vec3(x + hexahedronHalfLength, y + hexahedronHalfLength, z - hexahedronHalfLength);
                            hexahedron.v5  = new vec3(x - hexahedronHalfLength, y + hexahedronHalfLength, z - hexahedronHalfLength);
                            hexahedron.v6  = new vec3(x + hexahedronHalfLength, y - hexahedronHalfLength, z - hexahedronHalfLength);
                            hexahedron.v7  = new vec3(x - hexahedronHalfLength, y - hexahedronHalfLength, z - hexahedronHalfLength);
                            array[index++] = hexahedron;
                        }
                    }
                }
                this.positionBufferRenderer = positionBuffer.GetRenderer();
                positionBuffer.Dispose();
            }

            {
                VR03UVBuffer uvBuffer = new VR03UVBuffer(strin_uv);
                uvBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount);
                HexahedronUV *array = (HexahedronUV *)uvBuffer.FirstElement();
                int           index = 0;
                for (int i = 0; i < xFrameCount; i++)
                {
                    for (int j = 0; j < yFrameCount; j++)
                    {
                        for (int k = 0; k < zFrameCount; k++)
                        {
                            var x     = (float)i / (float)xFrameCount;
                            var y     = (float)j / (float)yFrameCount;
                            var z     = (float)k / (float)zFrameCount;
                            var color = new vec3(x, y, z);
                            var uv    = new HexahedronUV();
                            uv.v0          = color;
                            uv.v1          = color;
                            uv.v2          = color;
                            uv.v3          = color;
                            uv.v4          = color;
                            uv.v5          = color;
                            uv.v6          = color;
                            uv.v7          = color;
                            array[index++] = uv;
                        }
                    }
                }
                this.uvBufferRenderer = uvBuffer.GetRenderer();
                uvBuffer.Dispose();
            }
            {
                var indexBuffer = new VR03IndexBuffer();
                indexBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount);
                HexahedronIndex *array = (HexahedronIndex *)indexBuffer.FirstElement();
                int index = 0;
                for (int i = 0; i < xFrameCount; i++)
                {
                    for (int j = 0; j < yFrameCount; j++)
                    {
                        for (int k = 0; k < zFrameCount; k++)
                        {
                            uint centerIndex = (uint)((i * yFrameCount * zFrameCount + j * zFrameCount + k) * 8);
                            array[index++] = new HexahedronIndex(centerIndex);
                        }
                    }
                }
                this.indexBufferRenderer = indexBuffer.GetRenderer() as IndexBufferPointerBase;
                indexBuffer.Dispose();
            }

            this.vao = new VertexArrayObject(
                this.indexBufferRenderer,
                this.positionBufferRenderer, this.uvBufferRenderer);
        }
Exemple #26
0
        protected override void DoInitialize()
        {
            // init shader program
            ShaderProgram program = new ShaderProgram();
            string        vertexShaderCode = null, geometryShaderCode = null, fragmentShaderCode = null;

            FillShaderCodes(this.allShaderCodes, ref vertexShaderCode, ref geometryShaderCode, ref fragmentShaderCode);
            program.Create(vertexShaderCode, fragmentShaderCode, geometryShaderCode, null);
            this.shaderProgram = program;

            // init all uniform variables
            UniformVariableBase[] uniformVariables = GetAllUniformVariables(this.allShaderCodes);
            if (uniformVariables.Length != this.uniformNameMap.Count())
            {
                throw new Exception(String.Format("uniforms' count not match! [{0}] in shaders but [{1}] in map.",
                                                  uniformVariables.Length, this.uniformNameMap.Count()));
            }
            var pairs = from variable in uniformVariables
                        join mappingPair in this.uniformNameMap on variable.VarName equals mappingPair.UniformNameInShader
                        select new { variable, mappingPair };

            if (pairs.Count() != uniformVariables.Length)
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(string.Format("Only {0} of {1} uniforms are paired:",
                                                 pairs.Count(), uniformVariables.Length));
                foreach (var item in pairs)
                {
                    builder.AppendLine(string.Format("{0}", item.mappingPair));
                }

                throw new Exception(builder.ToString());
            }
            //var samplers = from variable in uniformVariables
            //               where variable.GetValue().GetType() == typeof(samplerValue)
            //               select variable;
            //this.uniformSamplers = samplers.ToArray();
            this.uniformVariables = uniformVariables;

            // init property buffer objects' renderer
            var propertyBufferRenderers = new BufferPointer[propertyNameMap.Count()];
            int index = 0;

            foreach (var item in propertyNameMap)
            {
                BufferPointer bufferRenderer = this.model.GetBufferRenderer(
                    item.NameInIConvert2BufferRenderer, item.VarNameInShader);
                if (bufferRenderer == null)
                {
                    throw new Exception();
                }
                propertyBufferRenderers[index++] = bufferRenderer;
            }
            this.propertyBufferPointers = propertyBufferRenderers;

            // init index buffer object's renderer
            this.indexBufferPointer = this.model.GetIndexBufferRenderer();

            this.model           = null;
            this.allShaderCodes  = null;
            this.propertyNameMap = null;
            //this.uniformNameMap = null;

            {
                IndexBufferPointer renderer = this.indexBufferPointer as IndexBufferPointer;
                if (renderer != null)
                {
                    this.elementCount = renderer.ElementCount;
                }
            }
            {
                ZeroIndexBufferPointer renderer = this.indexBufferPointer as ZeroIndexBufferPointer;
                if (renderer != null)
                {
                    this.elementCount = renderer.VertexCount;
                }
            }
        }
Exemple #27
0
 internal override unsafe void VirtualCopy(BufferPointer <Color> destination, BufferPointer <byte> source, int count)
 {
     Unsafe.CopyBlock((void *)destination.PointerAtOffset, (void *)source.PointerAtOffset, (uint)count * 4);
 }
Exemple #28
0
        private unsafe void InitVertexBuffers()
        {
            {
                VR04PositionBuffer positionBuffer = new VR04PositionBuffer(strin_Position);
                positionBuffer.Alloc(zFrameCount);
                QuadPosition *array = (QuadPosition *)positionBuffer.FirstElement();
                for (int i = 0; i < zFrameCount; i++)
                {
                    array[i] = new QuadPosition(
                        new vec3(-xLength, -yLength, (float)i / (float)zFrameCount - 0.5f),
                        new vec3(xLength, -yLength, (float)i / (float)zFrameCount - 0.5f),
                        new vec3(xLength, yLength, (float)i / (float)zFrameCount - 0.5f),
                        new vec3(-xLength, yLength, (float)i / (float)zFrameCount - 0.5f)
                        );
                }
                this.positionBufferRenderer = positionBuffer.GetRenderer();
                positionBuffer.Dispose();
            }

            {
                VR04UVBuffer uvBuffer = new VR04UVBuffer(strin_uv);
                uvBuffer.Alloc(zFrameCount);
                QuadUV *array = (QuadUV *)uvBuffer.FirstElement();
                for (int i = 0; i < zFrameCount; i++)
                {
                    array[i] = new QuadUV(
                        new vec3(0, 0, (float)i / (float)zFrameCount),
                        new vec3(1, 0, (float)i / (float)zFrameCount),
                        new vec3(1, 1, (float)i / (float)zFrameCount),
                        new vec3(0, 1, (float)i / (float)zFrameCount)
                        );
                }
                this.uvBufferRenderer = uvBuffer.GetRenderer();
                uvBuffer.Dispose();
            }
            {
                var indexBuffer = new VR04IndexBuffer();
                indexBuffer.Alloc(zFrameCount);
                QuadIndex *array = (QuadIndex *)indexBuffer.FirstElement();
                for (uint i = 0; i < zFrameCount; i++)
                {
                    if (this.reverSide)
                    {
                        array[i] = new QuadIndex((zFrameCount - i - 1) * 4 + 0,
                                                 (zFrameCount - i - 1) * 4 + 1,
                                                 (zFrameCount - i - 1) * 4 + 2,
                                                 (zFrameCount - i - 1) * 4 + 3
                                                 );
                    }
                    else
                    {
                        array[i] = new QuadIndex(i * 4 + 0, i * 4 + 1, i * 4 + 2, i * 4 + 3);
                    }
                }
                this.indexBufferRenderer = indexBuffer.GetRenderer() as IndexBufferPointerBase;
                indexBuffer.Dispose();
            }

            //if (!this.reverSide)
            //{
            //    var indexBuffer = new ZeroIndexBuffer(DrawMode.Quads, zFrameCount * 4);
            //    indexBuffer.Alloc(zFrameCount);// this actually does nothing.
            //    this.indexBufferRenderer = indexBuffer.GetRenderer();
            //    indexBuffer.Dispose();
            //}
            //else
            //{
            //    var indexBuffer = new VR04IndexBuffer();
            //    indexBuffer.Alloc(zFrameCount);
            //    QuadIndex* array = (QuadIndex*)indexBuffer.FirstElement();
            //    for (uint i = 0; i < zFrameCount; i++)
            //    {
            //        //array[i] = new QuadIndex(i * 4 + 0, i * 4 + 1, i * 4 + 2, i * 4 + 3);
            //        array[i] = new QuadIndex((zFrameCount - i - 1) * 4 + 0,
            //            (zFrameCount - i - 1) * 4 + 1,
            //            (zFrameCount - i - 1) * 4 + 2,
            //            (zFrameCount - i - 1) * 4 + 3
            //            );
            //    }
            //    this.indexBufferRenderer = indexBuffer.GetRenderer();
            //    indexBuffer.Dispose();
            //}
            this.vao = new VertexArrayObject(
                this.indexBufferRenderer,
                this.positionBufferRenderer, this.uvBufferRenderer);
        }
Exemple #29
0
 internal abstract void VirtualCopy(BufferPointer <Color> destination, BufferPointer <byte> source, int count);
Exemple #30
0
		public static void GetBufferPointerv(BufferTarget target, BufferPointer pname, IntPtr parameters)
		{
			glGetBufferPointerv deleg = BaseGraphicsContext.Current.Loader.Get<glGetBufferPointerv>();
			if (deleg != null)
				deleg(target, pname, parameters);
		}
Exemple #31
0
        unsafe void InitializeVAO()
        {
            vec3[] colors = new vec3[] { new vec3(1, 0, 0), new vec3(0, 1, 0), new vec3(0, 0, 1) };
            // 计算三个坐标轴
            for (int axisIndex = 0; axisIndex < 3; axisIndex++)
            {
                var axisVertexCount = faceCount * 2;

                //  Create a vertex buffer for the vertex data.
                using (var positionBuffer = new AxisPositionBuffer(strin_Position, BufferUsage.StaticDraw))
                {
                    positionBuffer.Alloc(axisVertexCount);
                    vec3 *positionArray = (vec3 *)positionBuffer.FirstElement();
                    for (int i = 0; i < axisVertexCount; i++)
                    {
                        int     face       = i / 2;
                        float[] components = new float[] {
                            i % 2 == 1 ? 0 : this.axisLength,
                            (float)(this.radius * Math.Cos(face * (Math.PI * 2) / faceCount)),
                            (float)(this.radius * Math.Sin(face * (Math.PI * 2) / faceCount))
                        };
                        positionArray[i] = new vec3(
                            components[(0 + axisIndex) % 3], components[(2 + axisIndex) % 3], components[(4 + axisIndex) % 3]);
                    }
                    this.positionBufferRenderers[axisIndex] = positionBuffer.GetRenderer();
                }

                //  Now do the same for the color data.
                using (var colorBuffer = new AxisColorBuffer(strin_Color, BufferUsage.StaticDraw))
                {
                    colorBuffer.Alloc(axisVertexCount);
                    vec3 *colorArray = (vec3 *)colorBuffer.FirstElement();
                    for (int i = 0; i < axisVertexCount; i++)
                    {
                        colorArray[i] = colors[axisIndex];
                    }
                    this.colorBufferRenderers[axisIndex] = colorBuffer.GetRenderer();
                }

                // Now for the index data.
                using (var indexBuffer = new AxisIndexBuffer())
                {
                    int indexLength = axisVertexCount + 2;
                    indexBuffer.Alloc(indexLength);
                    byte *cylinderIndex = (byte *)indexBuffer.FirstElement();
                    for (int i = 0; i < indexLength - 2; i++)
                    {
                        cylinderIndex[i] = (byte)i;
                    }
                    cylinderIndex[indexLength - 2]       = 0;
                    cylinderIndex[indexLength - 1]       = 1;
                    this.indexBufferRenderers[axisIndex] = indexBuffer.GetRenderer() as IndexBufferPointerBase;
                }
            }
            // 计算XZ平面
            {
                int planVertexCount = 4;

                //  Create a vertex buffer for the vertex data.
                using (var positionBuffer = new AxisPositionBuffer(strin_Position, BufferUsage.StaticDraw))
                {
                    positionBuffer.Alloc(planVertexCount);
                    vec3 *plan = (vec3 *)positionBuffer.FirstElement();
                    {
                        float length = this.axisLength;
                        plan[0] = new vec3(-length, 0, -length);
                        plan[1] = new vec3(-length, 0, length);
                        plan[2] = new vec3(length, 0, length);
                        plan[3] = new vec3(length, 0, -length);
                    }
                    this.planPositionBufferRenderer = positionBuffer.GetRenderer();
                }

                //  Now do the same for the colour data.
                using (var colorBuffer = new AxisColorBuffer(strin_Color, BufferUsage.StaticDraw))
                {
                    colorBuffer.Alloc(planVertexCount);
                    vec3 *colorArray = (vec3 *)colorBuffer.FirstElement();
                    {
                        for (int i = 0; i < planVertexCount; i++)
                        {
                            colorArray[i] = this.planColor;
                        }
                    }
                    this.planColorBufferRenderer = colorBuffer.GetRenderer();
                }

                using (var indexBuffer = new ZeroIndexBuffer(DrawMode.LineLoop, 0, planVertexCount))
                {
                    indexBuffer.Alloc(planVertexCount);//这句话实际上什么都没有做。
                    this.planIndexBufferRenderer = indexBuffer.GetRenderer() as IndexBufferPointerBase;
                }
            }
        }