Ejemplo n.º 1
0
        // Convert to float four tuple
        internal static void ConvertValueToMilColorF(object value, out MilColorF newVal)
        {
            Type t = value.GetType();

            // Fill in four-tuples.  Always fill in 1.0's for where there are
            // empty slots, to avoid division by zero on vector operations that
            // these values are subjected to.

            // Should order these in terms of most likely to be hit first.
            if (t == typeof(double) || t == typeof(float))
            {
                float fVal = (t == typeof(double)) ? (float)(double)value : (float)value;

                // Scalars extend out to fill entire vector.
                newVal.r = newVal.g = newVal.b = newVal.a = fVal;
            }
            else if (t == typeof(Color))
            {
                Color col = (Color)value;
                newVal.r = (float)col.R / 255f;
                newVal.g = (float)col.G / 255f;
                newVal.b = (float)col.B / 255f;
                newVal.a = (float)col.A / 255f;
            }
            else if (t == typeof(Point))
            {
                Point p = (Point)value;
                newVal.r = (float)p.X;
                newVal.g = (float)p.Y;
                newVal.b = 1f;
                newVal.a = 1f;
            }
            else if (t == typeof(Size))
            {
                Size s = (Size)value;
                newVal.r = (float)s.Width;
                newVal.g = (float)s.Height;
                newVal.b = 1f;
                newVal.a = 1f;
            }
            else if (t == typeof(Vector))
            {
                Vector v = (Vector)value;
                newVal.r = (float)v.X;
                newVal.g = (float)v.Y;
                newVal.b = 1f;
                newVal.a = 1f;
            }
            else if (t == typeof(Point3D))
            {
                Point3D p = (Point3D)value;
                newVal.r = (float)p.X;
                newVal.g = (float)p.Y;
                newVal.b = (float)p.Z;
                newVal.a = 1f;
            }
            else if (t == typeof(Vector3D))
            {
                Vector3D v = (Vector3D)value;
                newVal.r = (float)v.X;
                newVal.g = (float)v.Y;
                newVal.b = (float)v.Z;
                newVal.a = 1f;
            }
            else if (t == typeof(Point4D))
            {
                Point4D p = (Point4D)value;
                newVal.r = (float)p.X;
                newVal.g = (float)p.Y;
                newVal.b = (float)p.Z;
                newVal.a = (float)p.W;
            }
            else
            {
                // We should never hit this case, since we check the type using DetermineShaderConstantType
                // before we call this method.
                Debug.Assert(false);
                newVal.r = newVal.b = newVal.g = newVal.a = 1f;
            }
        }
Ejemplo n.º 2
0
        private void ManualUpdateResource(DUCE.Channel channel, bool skipOnChannelCheck)
        {
            // If we're told we can skip the channel check, then we must be on channel
            Debug.Assert(!skipOnChannelCheck || _duceResource.IsOnChannel(channel));

            if (skipOnChannelCheck || _duceResource.IsOnChannel(channel))
            {
                if (PixelShader == null)
                {
                    throw new InvalidOperationException(SR.Get(SRID.Effect_ShaderPixelShaderSet));
                }

                checked
                {
                    DUCE.MILCMD_SHADEREFFECT data;
                    data.Type   = MILCMD.MilCmdShaderEffect;
                    data.Handle = _duceResource.GetHandle(channel);

                    data.TopPadding    = _topPadding;
                    data.BottomPadding = _bottomPadding;
                    data.LeftPadding   = _leftPadding;
                    data.RightPadding  = _rightPadding;

                    data.DdxUvDdyUvRegisterIndex = this.DdxUvDdyUvRegisterIndex;
                    data.hPixelShader            = ((DUCE.IResource)PixelShader).GetHandle(channel);

                    unsafe
                    {
                        data.ShaderConstantFloatRegistersSize  = (uint)(sizeof(Int16) * _floatCount);
                        data.DependencyPropertyFloatValuesSize = (uint)(4 * sizeof(Single) * _floatCount);
                        data.ShaderConstantIntRegistersSize    = (uint)(sizeof(Int16) * _intCount);
                        data.DependencyPropertyIntValuesSize   = (uint)(4 * sizeof(Int32) * _intCount);
                        data.ShaderConstantBoolRegistersSize   = (uint)(sizeof(Int16) * _boolCount);
                        //
                        // Note: the multiply by 4 is not because the boolean register holds 4
                        // values, but to compensate for the difference between sizeof(bool)
                        // in managed code (1) and sizeof(BOOL) in native code (4).
                        //
                        data.DependencyPropertyBoolValuesSize    = (uint)(4 * sizeof(bool) * _boolCount);
                        data.ShaderSamplerRegistrationInfoSize   = (uint)(2 * sizeof(uint) * _samplerCount); // 2 pieces of data per sampler.
                        data.DependencyPropertySamplerValuesSize = (uint)(1 * sizeof(DUCE.ResourceHandle) * _samplerCount);

                        channel.BeginCommand(
                            (byte *)&data,
                            sizeof(DUCE.MILCMD_SHADEREFFECT),
                            (int)(data.ShaderConstantFloatRegistersSize +
                                  data.DependencyPropertyFloatValuesSize +
                                  data.ShaderConstantIntRegistersSize +
                                  data.DependencyPropertyIntValuesSize +
                                  data.ShaderConstantBoolRegistersSize +
                                  data.DependencyPropertyBoolValuesSize +
                                  data.ShaderSamplerRegistrationInfoSize +
                                  data.DependencyPropertySamplerValuesSize)
                            );

                        // Arrays appear in this order:
                        // 1) float register indices
                        // 2) float dp values
                        // 3) int register indices
                        // 4) int dp values
                        // 5) bool register indices
                        // 6) bool dp values
                        // 7) sampler registration info
                        // 8) sampler dp values

                        // 1) float register indices
                        AppendRegisters(channel, _floatRegisters);

                        // 2) float dp values
                        if (_floatRegisters != null)
                        {
                            for (int i = 0; i < _floatRegisters.Count; i++)
                            {
                                MilColorF?v = _floatRegisters[i];
                                if (v.HasValue)
                                {
                                    MilColorF valueToPush = v.Value;
                                    channel.AppendCommandData((byte *)&valueToPush, sizeof(MilColorF));
                                }
                            }
                        }

                        // 3) int register indices
                        AppendRegisters(channel, _intRegisters);

                        // 4) int dp values
                        if (_intRegisters != null)
                        {
                            for (int i = 0; i < _intRegisters.Count; i++)
                            {
                                MilColorI?v = _intRegisters[i];
                                if (v.HasValue)
                                {
                                    MilColorI valueToPush = v.Value;
                                    channel.AppendCommandData((byte *)&valueToPush, sizeof(MilColorI));
                                }
                            }
                        }

                        // 5) bool register indices
                        AppendRegisters(channel, _boolRegisters);

                        // 6) bool dp values
                        if (_boolRegisters != null)
                        {
                            for (int i = 0; i < _boolRegisters.Count; i++)
                            {
                                bool?v = _boolRegisters[i];
                                if (v.HasValue)
                                {
                                    //
                                    // Note: need 4 bytes for the bool, because the render thread
                                    // unmarshals it into a 4-byte BOOL. See the comment above for
                                    // DependencyPropertyBoolValuesSize for more details.
                                    //

                                    Int32 valueToPush = v.Value ? 1 : 0;
                                    channel.AppendCommandData((byte *)&valueToPush, sizeof(Int32));
                                }
                            }
                        }

                        // 7) sampler registration info
                        if (_samplerCount > 0)
                        {
                            int count = _samplerData.Count;
                            for (int i = 0; i < count; i++)
                            {
                                SamplerData?ssn = _samplerData[i];
                                if (ssn.HasValue)
                                {
                                    SamplerData ss = ssn.Value;

                                    // add as a 2-tuple (SamplerRegisterIndex,
                                    // SamplingMode)

                                    channel.AppendCommandData((byte *)&i, sizeof(int));

                                    int value = (int)(ss._samplingMode);
                                    channel.AppendCommandData((byte *)&value, sizeof(int));
                                }
                            }
                        }


                        // 8) sampler dp values
                        if (_samplerCount > 0)
                        {
                            for (int i = 0; i < _samplerData.Count; i++)
                            {
                                SamplerData?ssn = _samplerData[i];
                                if (ssn.HasValue)
                                {
                                    SamplerData ss = ssn.Value;

                                    // Making this assumption by storing a collection of
                                    // handles as an Int32Collection
                                    Debug.Assert(sizeof(DUCE.ResourceHandle) == sizeof(Int32));

                                    DUCE.ResourceHandle hBrush = ss._brush != null
                                        ? ((DUCE.IResource)ss._brush).GetHandle(channel)
                                        : DUCE.ResourceHandle.Null;

                                    Debug.Assert(!hBrush.IsNull || ss._brush == null, "If brush isn't null, hBrush better not be");

                                    channel.AppendCommandData((byte *)&hBrush, sizeof(DUCE.ResourceHandle));
                                }
                            }
                        }

                        // That's it...
                        channel.EndCommand();
                    }
                }
            }
        }