/// <summary> /// Alligns the byte offset for format. /// </summary> /// <param name="format"></param> /// <param name="offset"></param> /// <returns></returns> public static uint Align(PinFormat format, uint offset) { uint size = SizeOf(format); // For now, align all to 16-byte boundaries. return(((offset + 15) / 16) * 16); }
/// <summary> /// Checks if format is a matrix. /// </summary> /// <param name="fmt">The format of pin.</param> /// <param name="scalarType">The scalar type.</param> /// <returns>Is it a matrix.</returns> /// <param name="columns">Number of columns.</param> /// <param name="rows">Number of rows.</param> public static bool IsMatrix(PinFormat fmt, out PinFormat scalarType, out uint rows, out uint columns) { scalarType = PinFormat.Undefined; rows = columns = 0; switch (fmt) { case PinFormat.Float2x2: scalarType = PinFormat.Float; rows = columns = 2; break; case PinFormat.Float3x3: scalarType = PinFormat.Float; rows = columns = 3; break; case PinFormat.Float4x4: scalarType = PinFormat.Float; rows = columns = 4; break; default: break; } return(scalarType != PinFormat.Undefined); }
/// <summary> /// Creates temporary variable. /// </summary> /// <param name="fmt">Format of variable.</param> /// <returns>Variable that can be assigned.</returns> public Operand CreateTemporary(PinFormat fmt, uint size) { int name = GenerateNextUniqueName(); compiler.RegisterTemp(name, fmt, size); return(new Operand(fmt, name, size, true)); }
/// <summary> /// Creates a texture constant. /// </summary> internal ConstantOperation([NotEmpty] string name, PinFormat texture, PinFormat textureFormat, [NotNull] ShaderCode scope) { this.name = name; this.scope = scope; output = new Pin(texture, textureFormat, Pin.NotArray, this); }
/// <summary> /// Creates a fixed value. /// </summary> /// <param name="fmt">The format of pin.</param> /// <param name="val">The pin value, as object.</param> /// <returns>Constant operand.</returns> public Operand CreateFixed(PinFormat fmt, uint size, object val) { int name = GenerateNextUniqueName(); compiler.RegisterFixed(name, fmt, size, val); return(new Operand(fmt, name, size, val)); }
/// <summary> /// Creates the input operand. /// </summary> /// <remarks>Must always first create components with lower values, /// to ensure correct input layout.</remarks> /// <param name="fmt">The format.</param> /// <param name="component">The component.</param> /// <returns>The operand.</returns> public Operand CreateInput(PinFormat fmt, PinComponent component) { Operand op = new Operand(fmt, GenerateNextUniqueName()); compiler.RegisterInput(op.Name, fmt, component); return(op); }
/// <summary> /// Converts vector formats to scalars. /// </summary> /// <param name="fmt">The format.</param> /// <returns></returns> public static PinFormat ToScalar(PinFormat fmt) { PinFormat outFormat; if (fmt == PinFormat.Float) { return(fmt); } if (fmt == PinFormat.Integer) { return(fmt); } if (fmt == PinFormat.SNorm) { return(fmt); } if (fmt == PinFormat.UNorm) { return(fmt); } if (fmt == PinFormat.UInteger) { return(fmt); } if (!IsVector(fmt, out outFormat)) { if (!IsMatrix(fmt, out outFormat)) { return(PinFormat.Undefined); } } return(outFormat); }
/// <summary> /// Converts input. /// </summary> /// <param name="input"></param> /// <param name="outFormat"></param> /// <returns></returns> public Operand Convert([NotNull] Operand input, PinFormat outFormat) { Operand tmp = CreateTemporary(outFormat, Pin.NotArray); Convert(input, tmp); return(tmp); }
/// <summary> /// Creates an expand operation. /// </summary> public ExpandOperation(PinFormat expandTo, ExpandType type) { this.type = type; this.expandTo = expandTo; inputDesc = new PinsDescriptor( new PinDescriptor(PinFormatHelper.ExpandableTo(expandTo), "Data to be expanded.")); }
/// <summary> /// Creates a non-fixed constant. /// </summary> internal ConstantOperation([NotEmpty] string name, PinFormat fmt, uint size, [NotNull] ShaderCode scope) { this.name = name; this.scope = scope; // We create the pin. output = new Pin(fmt, size, this); }
/// <summary> /// Creates a texture. /// </summary> /// <param name="fmt">The texture type.</param> /// <param name="textureFmt">Texture data type, may not be matrix.</param> /// <param name="register">Texture register id.</param> /// <returns></returns> public Operand CreateTexture(PinFormat fmt, PinFormat textureFmt, uint register) { int name = GenerateNextUniqueName(); compiler.RegisterTexture(name, fmt, textureFmt, register); return(new Operand(fmt, name, textureFmt)); }
/// <summary> /// Expands data. /// </summary> public Operand Expand([NotNull] Operand source, PinFormat outFormat, ExpandType type) { // TODO: constant expression Operand dest = CreateTemporary(outFormat, source.ArraySize); compiler.Expand(source.Name, dest.Name, source.Format, dest.Format, type); return(dest); }
/// <summary> /// Multiplies two operands. /// </summary> public Operand Mul(Operand in1, Operand in2) { // TODO: validation. PinFormat outFmt = in1.Format; // Same format. if (in1.Format == in2.Format) { outFmt = in1.Format; } // Matrix x vector. else if (in1.Format == PinFormat.Float4x4 && in2.Format == PinFormat.Floatx4) { outFmt = PinFormat.Floatx4; } // By scalar. else if (in1.Format == PinFormat.Float) { outFmt = in2.Format; } else if (in2.Format == PinFormat.Float) { outFmt = in1.Format; } else { throw new NotSupportedException(); } // We check if we can precache it, this is copy only op. if (in1.IsFixed && in2.IsFixed) { return(CreateFixed(outFmt, in1.ArraySize, Math.MathHelper.Mul(in1.Value, in2.Value))); } // Else create temp and return. Operand tmp = CreateTemporary(outFmt, in1.ArraySize); PinFormat dummy; // Special matrix-* or *-matrix multiplication. if (PinFormatHelper.IsMatrix(in1.Format, out dummy) || PinFormatHelper.IsMatrix(in2.Format, out dummy)) { compiler.MulEx(in1.Name, in2.Name, tmp.Name); } else { compiler.Mul(in1.Name, in2.Name, tmp.Name); } return(tmp); }
/// <summary> /// Creates an uniform parameter. /// </summary> /// <param name="fmt">The format of parameter.</param> /// <param name="size">The size of array, 0 means no array.</param> /// <param name="bufferID">The constant buffer ID.</param> /// <param name="placementInBuffer">The placement inside constant buffer.</param> /// <returns>The actual input.</returns> public Operand CreateConstant(PinFormat fmt, uint size, uint bufferID, uint placementInBuffer) { int name = GenerateNextUniqueName(); // We first "create" variable at global scope. compiler.RegisterConstant(name, fmt, size, bufferID, placementInBuffer); // Returns the input element that can be used in operations. return(new Operand(fmt, name, size, false)); }
/// <summary> /// Adds a component. /// </summary> /// <param name="c"></param> public void AddComponent(PinComponent c, PinFormat format) { if (HasComponent(c)) { throw new InvalidOperationException("The component " + c.ToString() + " already exists."); } components |= c; inputs.Add(c, new Pin(format, Pin.NotArray, null)); UpdateDesc(); }
/// <summary> /// Adds pins as output. /// </summary> /// <param name="c">The component link.</param> /// <param name="format">The pin format.</param> public void AddInput(PinComponent c, PinFormat format) { if (PinAsOutput(c) != null) { throw new ArgumentException("The component is already a part of input."); } Pin pin = new Pin(format, Pin.NotArray, this); outputs.Add(c, pin); UpdateDesc(); }
/// <summary> /// Checks if it is vector and also gives information about it's length, if it exists. /// </summary> /// <param name="fmt">The format.</param> /// <param name="scalarType">Scalar type, same as IsVector.</param> /// <param name="size">The length/size of vector.</param> /// <returns>Is it a vector.</returns> public static bool IsVector(PinFormat fmt, out PinFormat scalarType, out uint size) { scalarType = PinFormat.Undefined; size = 0; switch (fmt) { case PinFormat.Undefined: case PinFormat.Integer: break; case PinFormat.Integerx2: scalarType = PinFormat.Integer; size = 2; break; case PinFormat.Integerx3: scalarType = PinFormat.Integer; size = 3; break; case PinFormat.Integerx4: scalarType = PinFormat.Integer; size = 3; break; case PinFormat.Float: break; case PinFormat.Floatx2: scalarType = PinFormat.Float; size = 2; break; case PinFormat.Floatx3: scalarType = PinFormat.Float; size = 3; break; case PinFormat.Floatx4: scalarType = PinFormat.Float; size = 4; break; default: break; } return(scalarType != PinFormat.Undefined); }
/// <summary> /// Expands data to other. /// </summary> public T Expand <T>(PinBinder binder, ExpandType expandType) where T : PinBinder { PinFormat expandTo = FromType(typeof(T)); if (expandTo == PinFormat.Undefined) { throw new InvalidOperationException("Invalid format to expand to."); } // May also throw. ExpandOperation op = new ExpandOperation(expandTo, expandType); op.BindInputs(binder.Pin); return((T)CreateFrom(op.Outputs[0])); }
/// <summary> /// Tries to obstain data. /// </summary> /// <param name="desc">The data name.</param> /// <param name="offset">Offset of data.</param> /// <param name="format">Data format.</param> /// <returns></returns> public bool TryGetData([NotNull] string desc, out uint offset, out PinFormat format, out uint arraySize) { ParamOffset val; if (parameterLocations.TryGetValue(desc, out val)) { offset = val.Offset; format = val.Description.Pin.Format; arraySize = val.Description.Pin.Size; return(true); } offset = 0; format = PinFormat.Undefined; arraySize = 0; return(false); }
/// <summary> /// Is format a texture. /// </summary> /// <param name="fmt"></param> /// <returns></returns> public static bool IsTexture(PinFormat fmt) { switch (fmt) { case PinFormat.Texture1D: case PinFormat.BufferTexture: case PinFormat.Texture1DArray: case PinFormat.Texture2D: case PinFormat.Texture2DArray: case PinFormat.TextureCube: case PinFormat.Texture3D: return(true); default: return(false); } }
/// <summary> /// Adds element at specified offset. /// </summary> /// <param name="name">The name of parameter.</param> /// <param name="format">The format of parameter.</param> /// <param name="offset">The offset of element.</param> public void AddElement([NotEmpty] string name, PinFormat format, uint arraySize, uint offset) { // Validate format. PinFormat scalar = PinFormatHelper.ToScalar(format); if (scalar != PinFormat.Integer && scalar != PinFormat.Float) { throw new ArgumentException("Only Float and Integer format acceptable."); } if (data.ContainsKey(name)) { throw new ArgumentException(string.Format("Name {0} is already defined.", name)); } // We add element. ConstantBufferLayout.ParamOffset d = new ConstantBufferLayout.ParamOffset(); d.Description = new ParameterDescription(name, new Pin(format, arraySize, null)); d.Offset = offset; data.Add(name, d); }
static PinFormat[] AfterComponent(PinFormat fmt, PinFormat[] inputs) { int i; for (i = 0; i < inputs.Length; i++) { if (inputs[i] == fmt) { break; } } // We create array. PinFormat[] r = new PinFormat[inputs.Length - i]; for (int j = 0; i < inputs.Length; i++, j++) { r[j] = inputs[i]; } return(r); }
/// <summary> /// Is the format a vector. /// </summary> /// <param name="fmt">The format.</param> /// <param name="scalarType">Scalr type; if Floatx3, it is Float.</param> /// <returns>Is it a vector.</returns> public static bool IsVector(PinFormat fmt, out PinFormat scalarType) { scalarType = PinFormat.Undefined; switch (fmt) { case PinFormat.Integerx2: case PinFormat.Integerx3: case PinFormat.Integerx4: scalarType = PinFormat.Integer; break; case PinFormat.Floatx2: case PinFormat.Floatx3: case PinFormat.Floatx4: scalarType = PinFormat.Float; break; default: break; } return(scalarType != PinFormat.Undefined); }
/// <summary> /// Constant operand. /// </summary> /// <param name="fmt">The format.</param> /// <param name="n">The name.</param> /// <param name="v">Value of operand.</param> /// <param name="s">The size of array.</param> internal Operand(PinFormat fmt, int n, uint s, object v) : this(fmt, n, s) { value = v; }
/// <summary> /// Construction of operand. /// </summary> /// <param name="fmt">The format.</param> /// <param name="n">The name of operand.</param> /// <param name="s">The size of array.</param> internal Operand(PinFormat fmt, int n, uint s, bool w) : this(fmt, n, s) { writable = w; }
/// <summary> /// Construction of operand. /// </summary> /// <param name="fmt">The format.</param> /// <param name="n">The name of operand.</param> /// <param name="s">The size of array.</param> internal Operand(PinFormat fmt, int n, uint s) { format = fmt; name = n; size = s; }
public ConvertOperation(PinFormat outFormat) { this.outFormat = outFormat; }
/// <summary> /// Creates a texture operand. /// </summary> /// <param name="fmt"></param> /// <param name="n"></param> /// <param name="textureFmt"></param> internal Operand(PinFormat fmt, int n, PinFormat textureFmt) : this(fmt, n) { this.textureFormat = textureFmt; }
/// <summary> /// Initializes a new instance of the <see cref="ResourceAttribute"/> class. /// </summary> /// <param name="resourceType">Must be one of Sampler, Texture1D, Texture2D, Texture3D, /// Texture1DArray, Texture2DArray, TextureCube.</param> public ResourceAttribute(PinFormat resourceType) { this.resourceType = resourceType; }
/// <summary> /// Construction of operand. /// </summary> /// <param name="fmt">The format.</param> /// <param name="n">The name of operand.</param> internal Operand(PinFormat fmt, int n) { format = fmt; name = n; }