Esempio n. 1
0
        public unsafe void SetUniform(Uniform uniform, ParameterType type, int arraySize, IntPtr valueData)
        {
            //int1, vec4, mat3, mat4
            var uniformType = GetUniformTypeByParameterType(type);

            //var uniform = GpuProgramManager.RegisterUniform( name, uniformType, arraySize );

            switch (uniformType)
            {
            //case UniformType.Sampler:
            //Log.Fatal( "ViewportRenderingContext: SetUniform: UniformType.Int1 impl." );
            //break;

            case UniformType.Vector4:
            {
                int sizeInBytes = ParameterTypeUtility.GetElementSizeInBytes(type);
                if (sizeInBytes == 16)
                {
                    Bgfx.SetUniform(uniform, valueData, arraySize);
                }
                else if (sizeInBytes < 16)
                {
                    if (arraySize != 1)
                    {
                        Log.Fatal("ViewportRenderingContext: SetUniform: Arrays with type \'{0}\' are not supported. Only Vec4, Mat3, Mat4 arrays are supported.", type);
                    }

                    Vector4F value = Vector4F.Zero;
                    Buffer.MemoryCopy((void *)valueData, &value, sizeInBytes, sizeInBytes);
                    Bgfx.SetUniform(uniform, &value, 1);
                }
                else
                {
                    Log.Fatal("ViewportRenderingContext: SetUniform: The type \'{0}\' is not supported.", type);
                }
            }
            break;

            case UniformType.Matrix3x3:
            case UniformType.Matrix4x4:
                Bgfx.SetUniform(uniform, valueData, arraySize);
                break;
            }
        }
Esempio n. 2
0
        public void BindParameterContainer(ParameterContainer container)
        {
            if (container.NamedParameters != null)
            {
                foreach (var tuple in container.NamedParameters)
                {
                    var name = tuple.Key;
                    var p    = tuple.Value;

                    if (ParameterTypeUtility.CanConvertToByteArray(p.Type))
                    {
                        //uniforms
                        SetUniform(name, p.Type, p.ElementCount, p.GetValue(container));
                    }
                    else
                    {
                        //Log.Fatal( "ViewportRenderingContext: BindParameterContainer: Is not supported named parameter." );
                    }
                    //else if( ParameterTypeUtility.IsTexture( p.Type ) )
                    //{
                    //	//textures

                    //	//!!!!текстуры в вершинах

                    //	var value = (GpuMaterialPass.TextureParameterValue)p.Value;
                    //	var index = int.Parse( p.Name );
                    //	BindTexture( index, value );
                    //}
                }
            }

            if (container.UniformParameters != null)
            {
                for (int n = 0; n < container.UniformParameters.Count; n++)
                {
                    ref var p = ref container.UniformParameters.Data[n];
                    SetUniform(p.Uniform, p.Type, p.ElementCount, p.GetValue(container));
                }
            }
Esempio n. 3
0
        public void Set(string name, ParameterType type, int elementCount, IntPtr value, int checkTotalSizeInBytes)
        {
            if (NamedParameters == null)
            {
                NamedParameters = new Dictionary <string, NamedParameter>(32);
            }

            if (string.IsNullOrEmpty(name))
            {
                Log.Fatal("ParameterContainer: Set: Name can't be empty.");
            }
            if (!ParameterTypeUtility.CanConvertToByteArray(type))
            {
                Log.Fatal("ParameterContainer: Set: !ParameterTypeUtils.CanConvertToByteArray( type )");
            }

            //ParameterItem parameter = null;

            ////use current parameter or new depending format
            //{
            //	ParameterItem currentItem;
            //	if( parameters.TryGetValue( name, out currentItem ) )
            //	{
            //		if( type == currentItem.type && elementCount == currentItem.elementCount )
            //		{
            //			//!!!!!так?
            //			parameter = currentItem;
            //		}
            //		else
            //			Remove( name );
            //	}
            //}

            ////create parameter
            //if( parameter == null )
            //{
            //	parameter = new ParameterItem();
            //	parameter.container = this;
            //	parameter.name = name;
            //	parameter.type = type;
            //	parameter.elementCount = elementCount;
            //	parameters[ name ] = parameter;
            //}

            var parameter = new NamedParameter();

            parameter.Type         = type;
            parameter.ElementCount = elementCount;

            if (parameter.GetTotalSizeInBytes() != checkTotalSizeInBytes)
            {
                Log.Fatal("ParameterContainer: Set: parameter.GetValueSizeInBytes() != checkTotalSizeInBytes.");
            }

            //prepare data array
            //if( parameter.valueDataPosition == -1 )
            {
                int needSize = dataUsedBytes + parameter.GetTotalSizeInBytes();

                if (data == null)
                {
                    data = new byte[256];
                }

                if (needSize > data.Length)
                {
                    int newSize = data.Length * 2;
                    while (newSize < needSize)
                    {
                        newSize *= 2;
                    }
                    byte[] newData = new byte[newSize];
                    Buffer.BlockCopy(data, 0, newData, 0, dataUsedBytes);
                    data = newData;
                }

                parameter.ValueDataPosition = dataUsedBytes;
                dataUsedBytes += parameter.GetTotalSizeInBytes();
            }

            //copy value
            unsafe
            {
                fixed(byte *pData = data)
                NativeUtility.CopyMemory((IntPtr)(pData + parameter.ValueDataPosition), (IntPtr)value, parameter.GetTotalSizeInBytes());
            }

            if (NamedParameters == null)
            {
                NamedParameters = new Dictionary <string, NamedParameter>(32);
            }
            NamedParameters[name] = parameter;

            unchecked
            {
                version++;
            }

            //return parameter;
        }
Esempio n. 4
0
 public int GetTotalSizeInBytes()
 {
     return(ParameterTypeUtility.GetElementSizeInBytes(Type) * ElementCount);
 }
Esempio n. 5
0
        public void Set(string name, object value, ParameterType type = ParameterType.Unknown, int elementCount = 0)
        {
            if (string.IsNullOrEmpty(name))
            {
                Log.Fatal("ParameterContainer: Set: Name can't be empty.");
            }
            if (value == null)
            {
                Log.Fatal("ParameterContainer: Set: value == null.");
            }

            //detect elementCount
            if (elementCount == 0)
            {
                if (value.GetType().IsArray)
                {
                    Array array = (Array)value;
                    elementCount = array.GetLength(0);
                }
                else
                {
                    elementCount = 1;
                }
            }

            //detect type
            if (type == ParameterType.Unknown)
            {
                Type elementType = null;
                {
                    if (value.GetType().IsArray)
                    {
                        elementType = value.GetType().GetElementType();
                    }
                    else
                    {
                        elementType = value.GetType();
                    }
                }

                //!!!!!reference values in array

                type = ParameterTypeUtility.DetectTypeByClassType(elementType);

                //!!!!было
                ////!!!!!так?
                ////detect texture type by the value
                //if( type == ParameterType.Unknown )
                //{
                //	GpuMaterialPass.TextureParameterValue textureValue = null;
                //	if( value.GetType().IsArray )
                //	{
                //		Array array = (Array)value;
                //		if( array.GetLength( 0 ) > 0 )
                //			textureValue = array.GetValue( 0 ) as GpuMaterialPass.TextureParameterValue;
                //	}
                //	else
                //		textureValue = value as GpuMaterialPass.TextureParameterValue;

                //	if( textureValue != null && textureValue.Texture != null )
                //	{
                //		var gpuTexture = textureValue.Texture.Result;
                //		if( gpuTexture != null )
                //		{
                //			switch( gpuTexture.TextureType )
                //			{
                //			//case Component_Texture.TypeEnum._1D: type = ParameterType.Texture1D; break;
                //			case Component_Image.TypeEnum._2D: type = ParameterType.Texture2D; break;
                //			case Component_Image.TypeEnum._3D: type = ParameterType.Texture3D; break;
                //			case Component_Image.TypeEnum.Cube: type = ParameterType.TextureCube; break;
                //			}
                //		}
                //	}
                //}
            }

            //!!!!!check for invalid data

            //ParameterItem parameter = null;

            ////use current parameter or new depending format
            //{
            //	ParameterItem currentItem;
            //	if( parameters.TryGetValue( name, out currentItem ) )
            //	{
            //		if( type == currentItem.type && elementCount == currentItem.elementCount )
            //		{
            //			//!!!!!так?
            //			parameter = currentItem;
            //		}
            //		else
            //			Remove( name );
            //	}
            //}

            ////create parameter
            //if( parameter == null )
            //{
            //	parameter = new ParameterItem();
            //	parameter.container = this;
            //	parameter.name = name;
            //	parameter.type = type;
            //	parameter.elementCount = elementCount;
            //	parameters[ name ] = parameter;
            //}

            var parameter = new NamedParameter();

            parameter.Type         = type;
            parameter.ElementCount = elementCount;

            //update value
            if (ParameterTypeUtility.CanConvertToByteArray(type))
            {
                //prepare data array
                //if( parameter.valueDataPosition == -1 )
                {
                    int needSize = dataUsedBytes + parameter.GetTotalSizeInBytes();

                    if (data == null)
                    {
                        data = new byte[256];
                    }

                    if (needSize > data.Length)
                    {
                        int newSize = data.Length * 2;
                        while (newSize < needSize)
                        {
                            newSize *= 2;
                        }
                        byte[] newData = new byte[newSize];
                        Buffer.BlockCopy(data, 0, newData, 0, dataUsedBytes);
                        data = newData;
                    }

                    parameter.ValueDataPosition = dataUsedBytes;
                    dataUsedBytes += parameter.GetTotalSizeInBytes();
                }

                //copy value
                unsafe
                {
                    fixed(byte *pData = data)
                    {
                        //!!!!slowly

                        if (value.GetType().IsArray)
                        {
                            byte *pDest = pData + parameter.ValueDataPosition;

                            int elementSize = ParameterTypeUtility.GetElementSizeInBytes(parameter.Type);
                            foreach (var item in (IList)value)
                            {
                                Marshal.StructureToPtr(item, (IntPtr)pDest, false);
                                pDest += elementSize;
                            }
                        }
                        else
                        {
                            Marshal.StructureToPtr(value, (IntPtr)(pData + parameter.ValueDataPosition), false);
                        }
                    }
                }
            }
            else
            {
                parameter.ValueReference = value;
            }

            if (NamedParameters == null)
            {
                NamedParameters = new Dictionary <string, NamedParameter>(32);
            }
            NamedParameters[name] = parameter;

            unchecked
            {
                version++;
            }

            //return parameter;
        }