예제 #1
0
 public void SetInputRegisterValue(int contextIndex, int index0, int index1, ref Number4 value)
 {
     if (index0 < _requiredRegisters.NumPrimitives && index1 < _requiredRegisters.Inputs)
     {
         _executionContexts[contextIndex].SetInputRegisterValue(index0, index1, ref value);
     }
 }
 public override void Clear(ref Number4 color)
 {
     foreach (var subresource in _subresources)
     {
         subresource.Clear(ref color);
     }
 }
예제 #3
0
        internal Number4 DoBlend(int renderTargetIndex, ref Number4 source, ref Number4 destination, ref Number4 blendFactor)
        {
            var blendDescription = _renderTargetBlendDescriptions[renderTargetIndex];

            if (!blendDescription.IsBlendEnabled)
            {
                return(source);
            }

            var result = new Number4();

            // RGB blending
            var colorDestinationBlendFactor = GetBlendFactor(blendDescription.DestinationBlend, ref source, ref destination, ref blendFactor);
            var colorSourceBlendFactor      = GetBlendFactor(blendDescription.SourceBlend, ref source, ref destination, ref blendFactor);

            var colorDestination = Number4.Multiply(ref destination, ref colorDestinationBlendFactor);
            var colorSource      = Number4.Multiply(ref source, ref colorSourceBlendFactor);

            result.R = DoBlendOperation(blendDescription.BlendOperation, colorSource.R, colorDestination.R);
            result.G = DoBlendOperation(blendDescription.BlendOperation, colorSource.G, colorDestination.G);
            result.B = DoBlendOperation(blendDescription.BlendOperation, colorSource.B, colorDestination.B);

            // Alpha blending
            var alphaDestinationBlendFactor = GetBlendFactor(blendDescription.DestinationAlphaBlend, ref source, ref destination, ref blendFactor);
            var alphaSourceBlendFactor      = GetBlendFactor(blendDescription.SourceAlphaBlend, ref source, ref destination, ref blendFactor);

            var alphaDestination = destination.A * alphaDestinationBlendFactor.A;
            var alphaSource      = source.A * alphaSourceBlendFactor.A;

            result.A = DoBlendOperation(blendDescription.AlphaBlendOperation, alphaSource, alphaDestination);

            return(result);
        }
예제 #4
0
		public VirtualMachine(BytecodeContainer bytecode, int numContexts)
		{
            if (bytecode.Shader.Version.ProgramType == ProgramType.PixelShader && numContexts % 4 != 0)
                throw new ArgumentOutOfRangeException("numContexts", "numContexts must be a multiple of 4 for pixel shaders.");

			_bytecode = bytecode;

			var instructionTokens = bytecode.Shader.Tokens.OfType<InstructionToken>().ToArray();
			var branchingInstructions = ExplicitBranchingRewriter.Rewrite(instructionTokens);
			var controlFlowGraph = ControlFlowGraph.FromInstructions(branchingInstructions);
            _executableInstructions = ExecutableInstructionRewriter.Rewrite(controlFlowGraph).ToArray();

			_requiredRegisters = RequiredRegisters.FromShader(bytecode.Shader);

			_executionContexts = new ExecutionContext[numContexts];
			for (int i = 0; i < _executionContexts.Length; i++)
				_executionContexts[i] = new ExecutionContext(this, i, _requiredRegisters);

            ConstantBuffers = new Number4[_requiredRegisters.ConstantBuffers.Count][];
            for (int i = 0; i < _requiredRegisters.ConstantBuffers.Count; i++)
                ConstantBuffers[i] = new Number4[_requiredRegisters.ConstantBuffers[i]];

		    TextureSamplers = new TextureSampler[_requiredRegisters.Resources.Count];
			for (int i = 0; i < _requiredRegisters.Resources.Count; i++)
				TextureSamplers[i] = TextureSamplerFactory.Create(_requiredRegisters.Resources[i]);

            Textures = new ITexture[_requiredRegisters.Resources.Count];
            Samplers = new SamplerState[_requiredRegisters.Samplers];
		}
예제 #5
0
        /// <summary>
        /// Sets a callback to be used whenever texture data is requested by a shader.
        /// The callback will be passed the u, v and w coordinates.
        /// </summary>
        /// <typeparam name="TColor">Color type, i.e. Vector4.</typeparam>
        /// <param name="name">Name of the resource.</param>
        /// <param name="callback">The callback will be executed once for each texture fetch,
        /// and will be passed the u, v and w coordinates.</param>
        public void SetResource <TColor>(string name, ResourceCallback <TColor> callback)
            where TColor : struct
        {
            // Validate.
            var resourceIndex = _resourceBindings.FindIndex(x => x.Name == name);

            if (resourceIndex == -1)
            {
                throw new ArgumentException("Could not find resource named '" + name + "'", "name");
            }
            if (StructUtility.SizeOf <TColor>() != Number4.SizeInBytes)
            {
                throw new ArgumentException(string.Format("Expected color struct to be {0} bytes, but was {1}'",
                                                          Number4.SizeInBytes, StructUtility.SizeOf <TColor>()));
            }

            // Set resource into virtual machine. We also set a default sampler state.
            var registerIndex = new RegisterIndex((ushort)resourceIndex);

            _virtualMachine.SetSampler(registerIndex, new SamplerState());
            _virtualMachine.SetTexture(registerIndex,
                                       new FakeTexture((u, v, w, i) =>
            {
                var bytes = StructUtility.ToBytes(callback(u, v, w, i));
                return(Number4.FromByteArray(bytes, 0));
            }));
        }
예제 #6
0
        /// <summary>
        /// 使用Flags 进行复合显示和位运算
        /// </summary>
        public void Test04()
        {
            Number4 num = Number4.Four | Number4.Two;

            Console.WriteLine(num);
            Console.WriteLine((int)num);
        }
예제 #7
0
 public void Clear(ref Number4 value)
 {
     for (var i = 0; i < Data.Length; i++)
     {
         Data[i] = value;
     }
 }
예제 #8
0
        protected override Number4 GetLinear(
            SamplerState samplerState, ITextureMipMap mipMap, 
            ref Number4 texCoords)
        {
            int intTexelX = (int) texCoords.Float0;
            int intTexelY = (int) texCoords.Float1;

            float fracX = texCoords.Float0 - intTexelX;
            float fracY = texCoords.Float1 - intTexelY;

            texCoords.Int0 = intTexelX;
            texCoords.Int1 = intTexelY;
            var c00 = GetColor(samplerState, mipMap, ref texCoords);

            texCoords.Int0 = intTexelX + 1;
            texCoords.Int1 = intTexelY;
            var c10 = GetColor(samplerState, mipMap, ref texCoords);

            texCoords.Int0 = intTexelX;
            texCoords.Int1 = intTexelY + 1;
            var c01 = GetColor(samplerState, mipMap, ref texCoords);

            texCoords.Int0 = intTexelX + 1;
            texCoords.Int1 = intTexelY + 1;
            var c11 = GetColor(samplerState, mipMap, ref texCoords);

            var cMinV = Number4.Lerp(ref c00, ref c10, fracX);
            var cMaxV = Number4.Lerp(ref c01, ref c11, fracX);

            return Number4.Lerp(ref cMinV, ref cMaxV, fracY);
        }
예제 #9
0
        internal void Execute(IEnumerable <Pixel> inputs)
        {
            foreach (var pixel in inputs)
            {
                // TODO
                const int renderTargetIndex = 0;
                var       renderTarget      = _renderTargetViews[renderTargetIndex];

                var renderTargetArrayIndex = pixel.RenderTargetArrayIndex;

                var processedPixelHandler = ProcessedPixel;

                for (int sampleIndex = 0; sampleIndex < MultiSampleCount; ++sampleIndex)
                {
                    if (!pixel.Samples[sampleIndex].Covered)
                    {
                        continue;
                    }

                    float newDepth = pixel.Samples[sampleIndex].Depth;

                    var source      = pixel.Color;
                    var destination = renderTarget.GetColor(renderTargetArrayIndex, pixel.X, pixel.Y, sampleIndex);

                    if (_depthStencilView != null)
                    {
                        float currentDepth = _depthStencilView.GetDepth(renderTargetArrayIndex, pixel.X, pixel.Y, sampleIndex);
                        if (!DepthStencilState.DepthTestPasses(newDepth, currentDepth))
                        {
                            if (processedPixelHandler != null)
                            {
                                processedPixelHandler(this, new PixelEventArgs(
                                                          pixel.Vertices, pixel.PrimitiveID, renderTargetArrayIndex, pixel.X, pixel.Y,
                                                          ref source, ref destination, null,
                                                          PixelExclusionReason.FailedDepthTest));
                            }
                            continue;
                        }
                    }

                    // Use blend state to calculate final color.
                    var finalColor = BlendState.DoBlend(renderTargetIndex, ref source, ref destination, ref _blendFactorNumber);
                    finalColor = Number4.Saturate(ref finalColor);
                    renderTarget.SetColor(renderTargetArrayIndex, pixel.X, pixel.Y, sampleIndex, ref finalColor);

                    if (processedPixelHandler != null)
                    {
                        processedPixelHandler(this, new PixelEventArgs(
                                                  pixel.Vertices, pixel.PrimitiveID, renderTargetArrayIndex, pixel.X, pixel.Y,
                                                  ref source, ref destination, finalColor,
                                                  PixelExclusionReason.NotExcluded));
                    }

                    if (_depthStencilView != null && DepthStencilState.Description.IsDepthEnabled)
                    {
                        _depthStencilView.SetDepth(renderTargetArrayIndex, pixel.X, pixel.Y, sampleIndex, newDepth);
                    }
                }
            }
        }
예제 #10
0
 /// <summary>
 /// 2-dimensional vector dot-product of components rg, POS-swizzle.
 /// </summary>
 /// <param name="saturate">True to clamp the result to [0...1], otherwise false.</param>
 /// <param name="src0">The components in the operation.</param>
 /// <param name="src1">The components in the operation.</param>
 /// <returns>
 /// The result of the operation.
 /// <example>dest = src0.r * src1.r + src0.g * src1.g</example>
 /// </returns>
 public static Number Dp2(bool saturate, ref Number4 src0, ref Number4 src1)
 {
     return(Number.FromFloat(
                src0.Number0.Float * src1.Number0.Float
                + src0.Number1.Float * src1.Number1.Float,
                saturate));
 }
예제 #11
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="texture"></param>
        /// <param name="samplerState"></param>
        /// <param name="location"></param>
        /// <param name="lod">A number that specifies the mipmap level. If the value is &lt;=0, the zero'th (biggest map)
        /// is used. The fractional value (if supplied) is used to interpolate between two mipmap levels.</param>
        /// <returns></returns>
        public Number4 SampleLevel(
            ITexture texture, SamplerState samplerState,
            ref Number4 location,
            float lod)
        {
            // TODO: Don't always pass minifying=true to GetFilteredColor.

            switch (samplerState.Filter)
            {
                case Filter.MinPointMagLinearMipPoint:
                case Filter.MinLinearMagMipPoint:
                case Filter.MinMagLinearMipPoint:
                case Filter.MinMagMipPoint:
                {
                    // Calculate nearest mipmap level.
                    var nearestLevel = MathUtility.Round(lod);
                    return GetFilteredColor(texture, samplerState, true, nearestLevel, ref location);
                }
                case Filter.MinLinearMagPointMipLinear:
                case Filter.MinMagMipLinear:
                case Filter.MinMagPointMipLinear:
                case Filter.MinPointMagMipLinear:
                {
                    // Calculate nearest two levels and linearly filter between them.
                    var nearestLevelInt = (int) lod;
                    var d = lod - nearestLevelInt;
                    var c1 = GetFilteredColor(texture, samplerState, true, nearestLevelInt, ref location);
                    var c2 = GetFilteredColor(texture, samplerState, true, nearestLevelInt + 1, ref location);
                    return Number4.Lerp(ref c1, ref c2, d);
                }
                default:
                    throw new NotSupportedException();
            }
        }
예제 #12
0
		public void GetRegister(OperandType operandType, RegisterIndex registerIndex, out Number4[] register, out int index)
		{
			switch (operandType)
			{
				case OperandType.ConstantBuffer:
					register = _virtualMachine.ConstantBuffers[registerIndex.Index2D_0];
					index = registerIndex.Index2D_1;
					return;
				case OperandType.Input:
					// Only GS requires 2-dimensional inputs, but for simplicity we always use a 2-dimensional input array.
					register = Inputs[registerIndex.Index2D_0];
					index = registerIndex.Index2D_1;
					return;
				case OperandType.Output:
					register = Outputs;
					index = registerIndex.Index1D;
					return;
				case OperandType.Temp:
					register = Temps;
					index = registerIndex.Index1D;
					return;
				case OperandType.IndexableTemp:
					register = IndexableTemps[registerIndex.Index2D_0];
					index = registerIndex.Index2D_1;
					return;
				default:
					throw new ArgumentException("Unsupported operand type: " + operandType);
			}
		}
예제 #13
0
        internal override void GenerateMips()
        {
            for (int i = 0; i < _description.ArraySize; i++)
            {
                for (int mipSlice = 1; mipSlice < _subresources[0].Length; ++mipSlice)
                {
                    var subresource = GetSubresource(i, mipSlice);
                    int width       = subresource.Width;
                    int height      = subresource.Height;
                    for (int y = 0; y < height; ++y)
                    {
                        for (int x = 0; x < width; ++x)
                        {
                            int previousLevelX = x * 2;
                            int previousLevelY = y * 2;

                            var moreDetailedMipLevel = GetSubresource(i, mipSlice - 1);
                            var c00 = moreDetailedMipLevel.GetData(previousLevelX, previousLevelY);
                            var c10 = moreDetailedMipLevel.GetData(ClampToDimension(previousLevelX + 1, moreDetailedMipLevel.Width), previousLevelY);
                            var c01 = moreDetailedMipLevel.GetData(previousLevelX, ClampToDimension(previousLevelY + 1, moreDetailedMipLevel.Height));
                            var c11 = moreDetailedMipLevel.GetData(ClampToDimension(previousLevelX + 1, moreDetailedMipLevel.Width), ClampToDimension(previousLevelY + 1, moreDetailedMipLevel.Height));
                            var interpolatedColor = Number4.Average(ref c00, ref c10, ref c01, ref c11);

                            subresource.SetData(x, y, ref interpolatedColor);
                        }
                    }
                }
            }
        }
예제 #14
0
        protected override Number4 GetLinear(
            SamplerState samplerState, ITextureMipMap mipMap,
            ref Number4 texCoords)
        {
            int intTexelX = (int)texCoords.Float0;
            int intTexelY = (int)texCoords.Float1;

            float fracX = texCoords.Float0 - intTexelX;
            float fracY = texCoords.Float1 - intTexelY;

            texCoords.Int0 = intTexelX;
            texCoords.Int1 = intTexelY;
            var c00 = GetColor(samplerState, mipMap, ref texCoords);

            texCoords.Int0 = intTexelX + 1;
            texCoords.Int1 = intTexelY;
            var c10 = GetColor(samplerState, mipMap, ref texCoords);

            texCoords.Int0 = intTexelX;
            texCoords.Int1 = intTexelY + 1;
            var c01 = GetColor(samplerState, mipMap, ref texCoords);

            texCoords.Int0 = intTexelX + 1;
            texCoords.Int1 = intTexelY + 1;
            var c11 = GetColor(samplerState, mipMap, ref texCoords);

            var cMinV = Number4.Lerp(ref c00, ref c10, fracX);
            var cMaxV = Number4.Lerp(ref c01, ref c11, fracX);

            return(Number4.Lerp(ref cMinV, ref cMaxV, fracY));
        }
예제 #15
0
 public static Color ToColor(this Number4 value)
 {
     return(new Color(
                (byte)(value.R * 255.0f),
                (byte)(value.G * 255.0f),
                (byte)(value.B * 255.0f),
                (byte)(value.A * 255.0f)));
 }
예제 #16
0
 public IrConstantOperand(double value)
 {
     Value = new Number4(
         Number.FromFloat((float)value),
         Number.FromFloat(0),
         Number.FromFloat(0),
         Number.FromFloat(0));
 }
예제 #17
0
 protected override Number4 GetNearestNeighbor(
     SamplerState samplerState, ITextureMipMap mipMap,
     ref Number4 texCoords)
 {
     texCoords.Int0 = MathUtility.Floor(texCoords.Float0);
     texCoords.Int1 = MathUtility.Floor(texCoords.Float1);
     return(GetColor(samplerState, mipMap, ref texCoords));
 }
예제 #18
0
 public Number4 SampleGrad(
     ITexture texture, SamplerState samplerState,
     ref Number4 location,
     ref Number4 ddx, ref Number4 ddy)
 {
     var lod = CalculateLevelOfDetail(texture, samplerState, ref ddx, ref ddy);
     return SampleLevel(texture, samplerState, ref location, lod);
 }
예제 #19
0
 protected override Number4 GetNearestNeighbor(
     SamplerState samplerState, ITextureMipMap mipMap, 
     ref Number4 texCoords)
 {
     texCoords.Int0 = MathUtility.Floor(texCoords.Float0);
     texCoords.Int1 = MathUtility.Floor(texCoords.Float1);
     return GetColor(samplerState, mipMap, ref texCoords);
 }
예제 #20
0
        private Number4[] CreatePixelShaderInput(ref BarycentricCoordinates coordinates)
        {
            float w = InterpolationUtility.PrecalculateW(
                coordinates.Alpha, coordinates.Beta, coordinates.Gamma,
                _p0.W, _p1.W, _p2.W);

            var v0Data = _primitive.Vertices[0].OutputData;
            var v1Data = _primitive.Vertices[1].OutputData;
            var v2Data = _primitive.Vertices[2].OutputData;

            // Calculate interpolated attribute values for this fragment.
            // TODO: Optimize this.
            var result = new Number4[_primitive.Vertices[0].OutputData.Length];

            foreach (var outputInputBinding in OutputInputBindings.Bindings)
            {
                var v0Value = v0Data[outputInputBinding.Register];
                var v1Value = v1Data[outputInputBinding.Register];
                var v2Value = v2Data[outputInputBinding.Register];

                if (outputInputBinding.SystemValueType != Name.Undefined)
                {
                    throw new NotImplementedException();
                }

                // Create input values. Normally, this will require interpolation
                // of the vertex attributes.
                Number4 inputValue;
                switch (outputInputBinding.InterpolationMode)
                {
                case InterpolationMode.Constant:
                    inputValue = v0Value;
                    break;

                case InterpolationMode.Linear:
                    inputValue = InterpolationUtility.Perspective(
                        coordinates.Alpha, coordinates.Beta, coordinates.Gamma,
                        ref v0Value, ref v1Value, ref v2Value,
                        _p0.W, _p1.W, _p2.W, w);
                    break;

                case InterpolationMode.LinearNoPerspective:
                    inputValue = InterpolationUtility.Linear(
                        coordinates.Alpha, coordinates.Beta, coordinates.Gamma,
                        ref v0Value, ref v1Value, ref v2Value);
                    break;

                default:
                    throw new InvalidOperationException("Unrecognised interpolation mode: " + outputInputBinding.InterpolationMode);
                }

                // Apply component mask so that we don't overwrite other values in this register.
                result[outputInputBinding.Register].WriteMaskedValue(
                    inputValue, outputInputBinding.ComponentMask);
            }

            return(result);
        }
예제 #21
0
        public void SetConstantBufferRegisterValue(int index0, int index1, ref Number4 value)
        {
            var constantBuffer = ConstantBuffers[index0];

            if (index1 < constantBuffer.Length)
            {
                constantBuffer[index1] = value;
            }
        }
예제 #22
0
        private static Number4 GetColor(SamplerState samplerState, ITextureMipMap mipMap, ref Number4 texCoords)
        {
            if (!GetTextureAddress(texCoords.Int0, mipMap.Width, samplerState.AddressU, out texCoords.Int0))
                return samplerState.BorderColor;
            if (!GetTextureAddress(texCoords.Int1, mipMap.Height, samplerState.AddressV, out texCoords.Int1))
                return samplerState.BorderColor;

            return mipMap.GetData(ref texCoords);
        }
예제 #23
0
        /// <summary>
        /// Converts a 3D vector to an array index and 2D texture coordinates.
        /// </summary>
        public static void GetCubeMapCoordinates(ref Number4 location,
                                                 out int arrayIndex, out Number4 coordinates)
        {
            var absX = Math.Abs(location.X);
            var absY = Math.Abs(location.Y);
            var absZ = Math.Abs(location.Z);

            // Select cube face using greatest magnitude component.
            // Then divide other components by that component,
            // and scale to [0..1].
            coordinates = new Number4();
            if (absX > absY && absX > absZ)
            {
                if (location.X > 0)
                {
                    arrayIndex    = PositiveX;
                    coordinates.X = ScaleTexCoord(-location.Z, absX);
                    coordinates.Y = 1.0f - ScaleTexCoord(location.Y, absX);
                }
                else
                {
                    arrayIndex    = NegativeX;
                    coordinates.X = ScaleTexCoord(location.Z, absX);
                    coordinates.Y = 1.0f - ScaleTexCoord(location.Y, absX);
                }
            }
            else if (absY > absX && absY > absZ)
            {
                if (location.Y > 0)
                {
                    arrayIndex    = PositiveY;
                    coordinates.X = ScaleTexCoord(location.X, absY);
                    coordinates.Y = 1.0f - ScaleTexCoord(-location.Z, absY);
                }
                else
                {
                    arrayIndex    = NegativeY;
                    coordinates.X = ScaleTexCoord(location.X, absY);
                    coordinates.Y = 1.0f - ScaleTexCoord(location.Z, absY);
                }
            }
            else
            {
                if (location.Z > 0)
                {
                    arrayIndex    = PositiveZ;
                    coordinates.X = ScaleTexCoord(location.X, absZ);
                    coordinates.Y = 1.0f - ScaleTexCoord(location.Y, absZ);
                }
                else
                {
                    arrayIndex    = NegativeZ;
                    coordinates.X = ScaleTexCoord(-location.X, absZ);
                    coordinates.Y = 1.0f - ScaleTexCoord(location.Y, absZ);
                }
            }
        }
예제 #24
0
		/// <summary>
		/// Converts a 3D vector to an array index and 2D texture coordinates.
		/// </summary>
		public static void GetCubeMapCoordinates(ref Number4 location,
			out int arrayIndex, out Number4 coordinates)
		{
			var absX = Math.Abs(location.X);
			var absY = Math.Abs(location.Y);
			var absZ = Math.Abs(location.Z);

			// Select cube face using greatest magnitude component.
			// Then divide other components by that component,
			// and scale to [0..1].
			coordinates = new Number4();
			if (absX > absY && absX > absZ)
			{
				if (location.X > 0)
				{
					arrayIndex = PositiveX;
					coordinates.X = ScaleTexCoord(-location.Z, absX);
					coordinates.Y = 1.0f - ScaleTexCoord(location.Y, absX);
				}
				else
				{
					arrayIndex = NegativeX;
					coordinates.X = ScaleTexCoord(location.Z, absX);
					coordinates.Y = 1.0f - ScaleTexCoord(location.Y, absX);
				}
			}
			else if (absY > absX && absY > absZ)
			{
				if (location.Y > 0)
				{
					arrayIndex = PositiveY;
					coordinates.X = ScaleTexCoord(location.X, absY);
					coordinates.Y = 1.0f - ScaleTexCoord(-location.Z, absY);
				}
				else
				{
					arrayIndex = NegativeY;
					coordinates.X = ScaleTexCoord(location.X, absY);
					coordinates.Y = 1.0f - ScaleTexCoord(location.Z, absY);
				}
			}
			else
			{
				if (location.Z > 0)
				{
					arrayIndex = PositiveZ;
					coordinates.X = ScaleTexCoord(location.X, absZ);
					coordinates.Y = 1.0f - ScaleTexCoord(location.Y, absZ);
				}
				else
				{
					arrayIndex = NegativeZ;
					coordinates.X = ScaleTexCoord(-location.X, absZ);
					coordinates.Y = 1.0f - ScaleTexCoord(location.Y, absZ);
				}
			}
		}
예제 #25
0
        public Number4 SampleGrad(
            ITexture texture, SamplerState samplerState,
            ref Number4 location,
            ref Number4 ddx, ref Number4 ddy)
        {
            var lod = CalculateLevelOfDetail(texture, samplerState, ref ddx, ref ddy);

            return(SampleLevel(texture, samplerState, ref location, lod));
        }
예제 #26
0
 public IrConstantOperand(long value)
 {
     Value = new Number4(
         Number.FromInt((int)value),
         Number.FromInt(0),
         Number.FromInt(0),
         Number.FromInt(0));
     Type.VariableType = IrShaderVariableType.I32;
 }
            public override void Clear(float depth)
            {
                var color = new Number4(depth, 0, 0, 0);

                foreach (var subresource in _subresources)
                {
                    subresource.Clear(ref color);
                }
            }
예제 #28
0
 /// <summary>
 /// Component-wise square root.
 /// </summary>
 /// <param name="saturate">True to clamp the result to [0...1], otherwise false.</param>
 /// <param name="src0">The components for which to take the square root.</param>
 /// <returns>The results of the operation. dest = sqrt(src0).</returns>
 public static Number4 Sqrt(bool saturate, ref Number4 src0)
 {
     return(new Number4
     {
         Number0 = Number.FromFloat((float)Math.Sqrt(src0.Number0.Float), saturate),
         Number1 = Number.FromFloat((float)Math.Sqrt(src0.Number1.Float), saturate),
         Number2 = Number.FromFloat((float)Math.Sqrt(src0.Number2.Float), saturate),
         Number3 = Number.FromFloat((float)Math.Sqrt(src0.Number3.Float), saturate)
     });
 }
예제 #29
0
 /// <summary>
 /// Component-wise integer 2's complement.
 /// </summary>
 /// <remarks>
 /// This instruction performs component-wise 2's complement of each 32-bit value in src0.
 /// The 32-bit results are stored in dest.
 /// </remarks>
 /// <param name="src0">Contains the values for the operation.</param>
 /// <returns>The result of the operation.</returns>
 public static Number4 INeg(ref Number4 src0)
 {
     return(new Number4
     {
         Number0 = Number.FromInt(-src0.Number0.Int),
         Number1 = Number.FromInt(-src0.Number1.Int),
         Number2 = Number.FromInt(-src0.Number2.Int),
         Number3 = Number.FromInt(-src0.Number3.Int)
     });
 }
예제 #30
0
 /// <summary>
 /// Component-wise shift right.
 /// </summary>
 /// <remarks>
 /// This instruction performs a component-wise shift of each 32-bit value in src0
 /// right by an unsigned integer bit count provided by the LSB 5 bits (0-31 range)
 /// in src1.select_component, inserting 0. The 32-bit per component results are
 /// placed in dest. The count is a scalar value applied to all components.
 /// TODO: Check where shift amount comes from.
 /// </remarks>
 /// <param name="src0">Contains the values to be shifted.</param>
 /// <param name="src1">Contains the shift amount.</param>
 /// <returns>The result of the operation.</returns>
 public static Number4 IShr(ref Number4 src0, ref Number4 src1)
 {
     return(new Number4
     {
         Number0 = Number.FromInt(src0.Number0.Int >> (int)src1.Number0.UInt),
         Number1 = Number.FromInt(src0.Number1.Int >> (int)src1.Number0.UInt),
         Number2 = Number.FromInt(src0.Number2.Int >> (int)src1.Number0.UInt),
         Number3 = Number.FromInt(src0.Number3.Int >> (int)src1.Number0.UInt)
     });
 }
예제 #31
0
 /// <summary>
 /// Component-wise vector floating point less-than comparison.
 /// </summary>
 /// <remarks>
 /// This instruction performs the float comparison (src0 &lt; src1) for each component,
 /// and writes the result to dest.
 /// If the comparison is true, then 0xFFFFFFFF is returned for that component.
 /// Otherwise 0x0000000 is returned.
 /// </remarks>
 /// <param name="src0">The value to compare to src1.</param>
 /// <param name="src1">The value to compare to src0.</param>
 /// <returns>The result of the operation.</returns>
 public static Number4 Lt(ref Number4 src0, ref Number4 src1)
 {
     return(new Number4
     {
         Number0 = GetCompareResult(src0.Number0.Float < src1.Number0.Float),
         Number1 = GetCompareResult(src0.Number1.Float < src1.Number1.Float),
         Number2 = GetCompareResult(src0.Number2.Float < src1.Number2.Float),
         Number3 = GetCompareResult(src0.Number3.Float < src1.Number3.Float),
     });
 }
예제 #32
0
 /// <summary>
 /// Component-wise multiply and add.
 /// </summary>
 /// <param name="saturate">True to clamp the result to [0...1], otherwise false.</param>
 /// <param name="src0">The multiplicand.</param>
 /// <param name="src1">The multiplier.</param>
 /// <param name="src2">The addend.</param>
 /// <returns>The result of the operation. dest = src0 * src1 + src2.</returns>
 public static Number4 Mad(bool saturate, ref Number4 src0, ref Number4 src1, ref Number4 src2)
 {
     return(new Number4
     {
         Number0 = Number.FromFloat((src0.Number0.Float * src1.Number0.Float) + src2.Number0.Float, saturate),
         Number1 = Number.FromFloat((src0.Number1.Float * src1.Number0.Float) + src2.Number1.Float, saturate),
         Number2 = Number.FromFloat((src0.Number2.Float * src1.Number0.Float) + src2.Number2.Float, saturate),
         Number3 = Number.FromFloat((src0.Number3.Float * src1.Number0.Float) + src2.Number3.Float, saturate)
     });
 }
예제 #33
0
 /// <summary>
 /// Component-wise divide.
 /// </summary>
 /// <param name="saturate">True to clamp the result to [0...1], otherwise false.</param>
 /// <param name="src0">The dividend.</param>
 /// <param name="src1">The divisor.</param>
 /// <returns>The result of the operation. dest = src / src1.</returns>
 public static Number4 Div(bool saturate, ref Number4 src0, ref Number4 src1)
 {
     return(new Number4
     {
         Number0 = Number.FromFloat(src0.Number0.Float / src1.Number0.Float, saturate),
         Number1 = Number.FromFloat(src0.Number1.Float / src1.Number1.Float, saturate),
         Number2 = Number.FromFloat(src0.Number2.Float / src1.Number2.Float, saturate),
         Number3 = Number.FromFloat(src0.Number3.Float / src1.Number3.Float, saturate)
     });
 }
예제 #34
0
 /// <summary>
 /// Floating-point round to integral float.
 /// </summary>
 /// <remarks>
 /// This instruction performs a component-wise floating-point round of the values in src0,
 /// writing integral floating-point values to dest.
 ///
 /// round_z rounds toward zero.
 /// </remarks>
 /// <param name="saturate">True to clamp the result to [0...1], otherwise false.</param>
 /// <param name="src0">The components in the operation.</param>
 /// <returns>The result of the operation.</returns>
 public static Number4 RoundZ(bool saturate, ref Number4 src0)
 {
     return(new Number4
     {
         Number0 = Number.FromFloat(RoundZ(src0.Number0.Float), saturate),
         Number1 = Number.FromFloat(RoundZ(src0.Number1.Float), saturate),
         Number2 = Number.FromFloat(RoundZ(src0.Number2.Float), saturate),
         Number3 = Number.FromFloat(RoundZ(src0.Number3.Float), saturate)
     });
 }
예제 #35
0
 /// <summary>
 /// Component-wise bitwise XOR.
 /// </summary>
 /// <param name="src0">The components to XOR with src1.</param>
 /// <param name="src1">The components to XOR with src0.</param>
 /// <returns>The result of the operation. dest = src0 | src1.</returns>
 public static Number4 Xor(ref Number4 src0, ref Number4 src1)
 {
     return(new Number4
     {
         Number0 = Number.FromUInt(src0.Number0.UInt | src1.Number0.UInt),
         Number1 = Number.FromUInt(src0.Number1.UInt | src1.Number1.UInt),
         Number2 = Number.FromUInt(src0.Number2.UInt | src1.Number2.UInt),
         Number3 = Number.FromUInt(src0.Number3.UInt | src1.Number3.UInt)
     });
 }
 /// <summary>
 /// Component-wise divide.
 /// </summary>
 /// <param name="saturate">True to clamp the result to [0...1], otherwise false.</param>
 /// <param name="src0">The dividend.</param>
 /// <param name="src1">The divisor.</param>
 /// <returns>The result of the operation. dest = src / src1.</returns>
 public static Number4 Div(bool saturate, ref Number4 src0, ref Number4 src1)
 {
     return new Number4
     {
         Number0 = Number.FromFloat(src0.Number0.Float / src1.Number0.Float, saturate),
         Number1 = Number.FromFloat(src0.Number1.Float / src1.Number1.Float, saturate),
         Number2 = Number.FromFloat(src0.Number2.Float / src1.Number2.Float, saturate),
         Number3 = Number.FromFloat(src0.Number3.Float / src1.Number3.Float, saturate)
     };
 }
 /// <summary>
 /// Component-wise bitwise AND.
 /// </summary>
 /// <remarks>
 /// Component-wise logical AND of each pair of 32-bit values from
 /// <paramref name="src0"/> and <paramref name="src1"/>.
 /// </remarks>
 /// <param name="src0">The 32-bit value to AND with src1.</param>
 /// <param name="src1">The 32-bit value to AND with src0.</param>
 /// <returns>The result of the operation. dest = src0 & src1.</returns>
 public static Number4 And(ref Number4 src0, ref Number4 src1)
 {
     return new Number4
     {
         Number0 = Number.FromUInt(src0.Number0.UInt & src1.Number0.UInt),
         Number1 = Number.FromUInt(src0.Number1.UInt & src1.Number1.UInt),
         Number2 = Number.FromUInt(src0.Number2.UInt & src1.Number2.UInt),
         Number3 = Number.FromUInt(src0.Number3.UInt & src1.Number3.UInt)
     };
 }
예제 #38
0
 /// <summary>
 /// Component-wise unsigned integer to floating point conversion.
 /// </summary>
 /// <remarks>
 /// <paramref name="src0"/> must contain an unsigned 32-bit integer 4-tuple.
 /// After the instruction executes, dest will contain a floating-point 4-tuple.
 /// The conversion is performed per-component.
 /// </remarks>
 /// <param name="src0">The components to convert.</param>
 /// <returns>The result of the operation.</returns>
 public static Number4 UtoF(ref Number4 src0)
 {
     return(new Number4
     {
         Number0 = Number.FromFloat(Convert.ToSingle(src0.Number0.UInt)),
         Number1 = Number.FromFloat(Convert.ToSingle(src0.Number1.UInt)),
         Number2 = Number.FromFloat(Convert.ToSingle(src0.Number2.UInt)),
         Number3 = Number.FromFloat(Convert.ToSingle(src0.Number3.UInt))
     });
 }
예제 #39
0
 public Pixel(int x, int y)
 {
     Vertices               = null;
     X                      = x;
     Y                      = y;
     Color                  = new Number4();
     Samples                = new Samples();
     PrimitiveID            = 0;
     RenderTargetArrayIndex = 0;
 }
예제 #40
0
        protected Number4[] GetShaderOutputs(int contextIndex)
        {
            var outputs = new Number4[_outputParametersCount];

            for (ushort i = 0; i < outputs.Length; i++)
            {
                outputs[i] = _virtualMachine.GetOutputRegisterValue(contextIndex, i);
            }
            return(outputs);
        }
예제 #41
0
        protected override void GetMipMapAndTransformedCoordinates(
            ITexture texture, ref Number4 location, 
			int level, out ITextureMipMap mipMap,
            out Number4 textureCoordinates)
        {
            mipMap = texture.GetMipMap(0, level);
            textureCoordinates = new Number4(
                location.Float0 * mipMap.Width,
                location.Float1 * mipMap.Height,
                0, 0);
        }
예제 #42
0
		public static Number4 ApplyOperandSelectionMode(Number4 value, Operand operand)
		{
			switch (operand.SelectionMode)
			{
				case Operand4ComponentSelectionMode.Swizzle:
				case Operand4ComponentSelectionMode.Select1:
					return Number4.Swizzle(value, operand.Swizzles);
				default:
					throw new ArgumentOutOfRangeException();
			}
		}
 private static bool TestCondition(ref Number4 number, InstructionTestBoolean testBoolean)
 {
     switch (testBoolean)
     {
         case InstructionTestBoolean.Zero:
             return number.AllZero;
         case InstructionTestBoolean.NonZero:
             return number.AnyNonZero;
         default:
             throw new ArgumentOutOfRangeException("testBoolean");
     }
 }
		protected override void GetMipMapAndTransformedCoordinates(
			ITexture texture, ref Number4 location,
			int level, out ITextureMipMap mipMap,
			out Number4 textureCoordinates)
		{
			var arraySlice = MathUtility.Round(location.Float2);
			mipMap = texture.GetMipMap(arraySlice, level);
			textureCoordinates = new Number4(
				location.Float0 * mipMap.Width,
				location.Float1 * mipMap.Height,
				0, 0);
		}
예제 #45
0
		protected override void GetMipMapAndTransformedCoordinates(
			ITexture texture, ref Number4 location,
			int level, out ITextureMipMap mipMap,
			out Number4 textureCoordinates)
		{
			int arrayIndex;
			CubeMapUtility.GetCubeMapCoordinates(ref location,
				out arrayIndex, out textureCoordinates);

			mipMap = texture.GetMipMap(arrayIndex, level);

			textureCoordinates.X *= mipMap.Width;
			textureCoordinates.Y *= mipMap.Height;
		}
예제 #46
0
		public static Number4 ApplyOperandModifier(Number4 value, NumberType numberType, OperandModifier modifier)
		{
			switch (modifier)
			{
				case OperandModifier.None:
					return value;
				case OperandModifier.Neg:
                    return Number4.Negate(value, numberType);
				case OperandModifier.Abs:
                    return Number4.Abs(value, numberType);
				case OperandModifier.AbsNeg:
                    return Number4.Negate(Number4.Abs(value, numberType), numberType);
				default:
					throw new ArgumentOutOfRangeException("modifier");
			}
		}
예제 #47
0
		public void GetCubeMapCoordinatesWorksCorrectly(
			float x, float y, float z,
			int expectedArrayIndex,
			float expectedS, float expectedT)
		{
			// Act.
			var location = new Number4(x, y, z, 0.0f);
			int arrayIndex; Number4 coordinates;
			CubeMapUtility.GetCubeMapCoordinates(ref location,
				out arrayIndex, out coordinates);

			// Assert.
			Assert.That(arrayIndex, Is.EqualTo(expectedArrayIndex));
			Assert.That(coordinates.X, Is.EqualTo(expectedS));
			Assert.That(coordinates.Y, Is.EqualTo(expectedT));
		}
예제 #48
0
        private Number4 GetFilteredColor(
            ITexture texture, SamplerState samplerState, bool minifying, int level,
            ref Number4 location)
        {
            var clampedLevel = MathUtility.Clamp(level, 0, texture.MipMapCount - 1);

            ITextureMipMap mipMap;
            Number4 texCoords;
            GetMipMapAndTransformedCoordinates(
                texture, ref location, clampedLevel,
                out mipMap, out texCoords);

            // Minifying
            if (minifying)
                switch (samplerState.Filter)
                {
                    case Filter.MinMagMipPoint:
                    case Filter.MinMagPointMipLinear:
                    case Filter.MinPointMagLinearMipPoint:
                    case Filter.MinPointMagMipLinear:
                        return GetNearestNeighbor(samplerState, mipMap, ref texCoords);
                    case Filter.MinLinearMagMipPoint:
                    case Filter.MinLinearMagPointMipLinear:
                    case Filter.MinMagLinearMipPoint:
                    case Filter.MinMagMipLinear:
                        return GetLinear(samplerState, mipMap, ref texCoords);
                    default:
                        throw new NotSupportedException();
                }

            // Magnifying
            switch (samplerState.Filter)
            {
                case Filter.MinLinearMagMipPoint:
                case Filter.MinLinearMagPointMipLinear:
                case Filter.MinMagMipPoint:
                case Filter.MinMagPointMipLinear:
                    return GetNearestNeighbor(samplerState, mipMap, ref texCoords);
                case Filter.MinMagLinearMipPoint:
                case Filter.MinMagMipLinear:
                case Filter.MinPointMagLinearMipPoint:
                case Filter.MinPointMagMipLinear:
                    return GetLinear(samplerState, mipMap, ref texCoords);
                default:
                    throw new NotSupportedException();
            }
        }
예제 #49
0
		public ExecutionContext(VirtualMachine virtualMachine, int index, RequiredRegisters requiredRegisters)
		{
		    _virtualMachine = virtualMachine;

		    Index = index;

			Inputs = new Number4[requiredRegisters.NumPrimitives][];
			for (int i = 0; i < requiredRegisters.NumPrimitives; i++)
				Inputs[i] = new Number4[requiredRegisters.Inputs];

			Outputs = new Number4[requiredRegisters.Outputs];

			Temps = new Number4[requiredRegisters.Temps];

			IndexableTemps = new Number4[requiredRegisters.IndexableTemps.Count][];
			for (int i = 0; i < requiredRegisters.IndexableTemps.Count; i++)
				IndexableTemps[i] = new Number4[requiredRegisters.IndexableTemps[i]];
		}
예제 #50
0
        public override float CalculateLevelOfDetail(
            ITexture texture, SamplerState samplerState, 
            ref Number4 ddx, ref Number4 ddy)
        {
            var mostDetailedMipMap = texture.GetMipMap(0, 0);
            int width = mostDetailedMipMap.Width;
            int height = mostDetailedMipMap.Height;
            float xBound2 = width * width;
            float yBound2 = height * height;

            float dudx2 = ddx.Float0 * ddx.Float0 * xBound2;
            float dvdx2 = ddx.Float1 * ddx.Float1 * yBound2;
            float dudy2 = ddy.Float0 * ddy.Float0 * xBound2;
            float dvdy2 = ddy.Float1 * ddy.Float1 * yBound2;

            // Proportional to the amount of a texel on display in a single pixel
            float pixelSizeTexelRatio2 = Math.Max(dudx2 + dvdx2, dudy2 + dvdy2);

            // Uses formula for p410 of Essential Mathematics for Games and Interactive Applications
            float result = 0.5f * MathUtility.Log2(pixelSizeTexelRatio2);

            // Clamp to >= 0.
            return Math.Max(result, 0.0f);
        }
예제 #51
0
 protected abstract Number4 GetLinear(
     SamplerState samplerState, ITextureMipMap mipMap, 
     ref Number4 texCoords);
예제 #52
0
 public abstract float CalculateLevelOfDetail(
     ITexture texture, SamplerState samplerState,
     ref Number4 ddx, ref Number4 ddy);
예제 #53
0
 protected abstract Number4 GetNearestNeighbor(
     SamplerState samplerState, ITextureMipMap mipMap, 
     ref Number4 texCoords);
 /// <summary>
 /// Component-wise square root.
 /// </summary>
 /// <param name="saturate">True to clamp the result to [0...1], otherwise false.</param>
 /// <param name="src0">The components for which to take the square root.</param>
 /// <returns>The results of the operation. dest = sqrt(src0).</returns>
 public static Number4 Sqrt(bool saturate, ref Number4 src0)
 {
     return new Number4
     {
         Number0 = Number.FromFloat((float) Math.Sqrt(src0.Number0.Float), saturate),
         Number1 = Number.FromFloat((float) Math.Sqrt(src0.Number1.Float), saturate),
         Number2 = Number.FromFloat((float) Math.Sqrt(src0.Number2.Float), saturate),
         Number3 = Number.FromFloat((float) Math.Sqrt(src0.Number3.Float), saturate)
     };
 }
예제 #55
0
 protected abstract void GetMipMapAndTransformedCoordinates(
     ITexture texture, ref Number4 location, int level,
     out ITextureMipMap mipMap,
     out Number4 textureCoordinates);
예제 #56
0
 public Number4 GetData(ref Number4 coords)
 {
     return _data;
 }
예제 #57
0
 public void SetInputRegisterValue(int index0, int index1, ref Number4 value)
 {
     Inputs[index0][index1] = value;
 }
 /// <summary>
 /// Component-wise unsigned integer to floating point conversion.
 /// </summary>
 /// <remarks>
 /// <paramref name="src0"/> must contain an unsigned 32-bit integer 4-tuple. 
 /// After the instruction executes, dest will contain a floating-point 4-tuple. 
 /// The conversion is performed per-component.
 /// </remarks>
 /// <param name="src0">The components to convert.</param>
 /// <returns>The result of the operation.</returns>
 public static Number4 UtoF(ref Number4 src0)
 {
     return new Number4
     {
         Number0 = Number.FromFloat(Convert.ToSingle(src0.Number0.UInt)),
         Number1 = Number.FromFloat(Convert.ToSingle(src0.Number1.UInt)),
         Number2 = Number.FromFloat(Convert.ToSingle(src0.Number2.UInt)),
         Number3 = Number.FromFloat(Convert.ToSingle(src0.Number3.UInt))
     };
 }
예제 #59
0
 public FakeTextureMipMap(Number4 data)
 {
     _data = data;
 }
 /// <summary>
 /// 2-dimensional vector dot-product of components rg, POS-swizzle.
 /// </summary>
 /// <param name="saturate">True to clamp the result to [0...1], otherwise false.</param>
 /// <param name="src0">The components in the operation.</param>
 /// <param name="src1">The components in the operation.</param>
 /// <returns>
 /// The result of the operation.
 /// <example>dest = src0.r * src1.r + src0.g * src1.g</example>
 /// </returns>
 public static Number Dp2(bool saturate, ref Number4 src0, ref Number4 src1)
 {
     return Number.FromFloat(
         src0.Number0.Float * src1.Number0.Float
             + src0.Number1.Float * src1.Number1.Float,
         saturate);
 }