Example #1
0
        public MidAttributeDecl CacheAttr(
            MidExp exp,
            MidType type)
        {
            MidAttributeDecl attrDecl = null;

            if (_attrCache.TryGetValue(exp, out attrDecl))
            {
                return(attrDecl);
            }

            if (exp is MidAttributeRef)
            {
                var attrRef = (MidAttributeRef)exp;
                attrDecl = attrRef.Decl;
                if (attrDecl.Element == this && attrDecl.Exp != null)
                {
                    _attrCache[exp] = attrDecl;
                    return(attrDecl);
                }
            }

            attrDecl = new MidAttributeDecl(
                _name.Factory.unique("attr"),
                this,
                type,
                exp);
            _attrCache[exp] = attrDecl;
            AddAttribute(attrDecl);
            return(attrDecl);
        }
Example #2
0
 public MidElementCtorArg(
     MidAttributeDecl attribute,
     MidVal val)
 {
     _attribute = attribute;
     _val       = val;
 }
Example #3
0
        public void Collect(
            MidPipelineDecl pipeline,
            MidElementDecl element,
            MidAttributeDecl attribute)
        {
            if (!attribute.IsOutput)
            {
                return;
            }

            if (!(attribute.Type is MidStructRef))
            {
                return;
            }

            // Unmark the attribute as an output...
            attribute.IsOutput = false;

            var structType = (MidStructRef)attribute.Type;

            AttributeInfo attrInfo;

            attrInfo.AttrDecl = attribute;
            attrInfo.Fields   = (from f in structType.Fields
                                 select CreateField(pipeline, element, attribute, f)).ToArray();

            _replacePass._attrInfos[attribute] = attrInfo;
        }
 public MidElementCtorArg(
     MidAttributeDecl attribute,
     MidVal val)
 {
     _attribute = attribute;
     _val = val;
 }
Example #5
0
        private FieldInfo CreateField(
            MidPipelineDecl pipeline,
            MidElementDecl element,
            MidAttributeDecl attribute,
            MidFieldDecl field)
        {
            var midExp = _exps.FieldRef(
                attribute.Range,
                _exps.AttributeRef(attribute.Range, attribute),
                field);

            var name = _identifiers.unique(
                string.Format("{0}_{1}", attribute.Name, field.Name));

            var newAttr = element.CacheAttr(
                midExp,
                field.Type);

            newAttr.TrySetName(name, attribute.Range);

            return(new FieldInfo {
                AttrDecl = newAttr,
                FieldDecl = field
            });
        }
Example #6
0
 public MidAttributeRef AttributeRef(
     SourceRange range,
     MidAttributeDecl attr)
 {
     return((MidAttributeRef)_attrRefs.Get(attr).Cache(
                () => new MidAttributeRef(range, attr, _lazyFactory)));
 }
Example #7
0
 public static void MarkOutputs(MidAttributeDecl attribute)
 {
     if (attribute.Exp != null)
     {
         MarkOutputs(attribute.Exp);
     }
 }
Example #8
0
 public void ApplyToAttribute(MidAttributeDecl attribute)
 {
     if (attribute.Exp != null)
     {
         attribute.Exp = Transform(attribute.Exp);
     }
 }
Example #9
0
 public void CollectAttributeInfo(MidAttributeDecl attribute)
 {
     // Explicit inputs and outputs needs to be kept
     if (attribute.IsForcedOutput || attribute.IsInput)
     {
         _attributesToKeep.Add(attribute);
     }
 }
Example #10
0
 public MidAttributeFetch AttributeFetch(
     SourceRange range,
     MidPath obj,
     MidAttributeDecl attribute)
 {
     return((MidAttributeFetch)_attrFetches.Get(obj).Get(attribute).Cache(
                () => new MidAttributeFetch(range, obj, attribute, _lazyFactory)));
 }
Example #11
0
 public MidAttributeRef(
     SourceRange range,
     MidAttributeDecl decl,
     ILazyFactory lazyFactory)
     : base(range, new MidDummyType())
 {
     _type = lazyFactory.New(() => Decl.Type);
     _decl = Lazy.Value(decl);
 }
Example #12
0
        private void Replace(
            MidAttributeDecl attr)
        {
            if (attr.Exp == null)
            {
                return;
            }

            attr.Exp = Replace(attr.Exp);
        }
 public MidAttributeFetch(
     SourceRange range,
     MidPath obj,
     MidAttributeDecl attribute,
     ILazyFactory lazyFactory )
     : base(range, new MidDummyType())
 {
     _obj = obj;
     _type = lazyFactory.New(() => _attribute.Value.Type);
     _attribute = Lazy.Value(attribute);
 }
Example #14
0
 public MidAttributeFetch(
     SourceRange range,
     MidPath obj,
     MidAttributeDecl attribute,
     ILazyFactory lazyFactory)
     : base(range, new MidDummyType())
 {
     _obj       = obj;
     _type      = lazyFactory.New(() => _attribute.Value.Type);
     _attribute = Lazy.Value(attribute);
 }
Example #15
0
        private MidAttributeDecl MapOldToNew(MidAttributeDecl old)
        {
            MidAttributeDecl result = null;

            if (_mapOldToNew.TryGetValue(old, out result))
            {
                return(result);
            }

            MidAttributeWrapperDecl wrapper = null;

            _mapOldToWrapper.TryGetValue(old, out wrapper);

            if (old.Exp != null)
            {
                // bootstrap the _mapOldToNew, so that
                // a recursive invocation of MapOldToNew
                // on this same attribute won't cause
                // an infinite recursion:
                _mapOldToNew[old] = old;

                old.Exp = _transform.Transform(old.Exp);
            }

            if (old.Exp == null || old.IsForcedOutput || old.IsInput)
            {
                result = old;
                old.Element.AddAttribute(old);
            }
            else if (old.Name.ToString().StartsWith("__"))
            {
                result = old;
                old.Element.AddAttribute(old);
            }
            else
            {
                result = old.Element.CacheAttr(old.Exp, old.Type);
            }

            if (wrapper != null)
            {
                result.TrySetName(wrapper.Name, wrapper.Range);
                wrapper.Attribute = result;
                _attributeWrappersToKeep.Add(wrapper);
            }

            _mapOldToNew[old] = result;
            return(result);
        }
Example #16
0
 public static void Dump(
     this MidAttributeDecl attribute,
     Span span)
 {
     if (attribute.IsOutput)
     {
         span.Write("output ");
     }
     span.Write("{0} {1}",
                attribute.Type.Dump(),
                attribute.Name);
     if (attribute.Exp != null)
     {
         span.Write(" = ");
         attribute.Exp.Dump(span.IndentSpan());
     }
     span.WriteLine(";");
 }
        public void Collect(
            MidPipelineDecl pipeline,
            MidElementDecl element,
            MidAttributeDecl attribute)
        {
            if (!attribute.IsOutput)
                return;

            if (!(attribute.Type is MidStructRef))
                return;

            // Unmark the attribute as an output...
            attribute.IsOutput = false;

            var structType = (MidStructRef)attribute.Type;

            AttributeInfo attrInfo;
            attrInfo.AttrDecl = attribute;
            attrInfo.Fields = (from f in structType.Fields
                               select CreateField(pipeline, element, attribute, f)).ToArray();

            _replacePass._attrInfos[attribute] = attrInfo;
        }
 public void SimplifyAttribute(MidAttributeDecl attribute)
 {
     attribute.Exp = SimplifyExp(attribute.Exp, new SimplifyEnv(null));
 }
 public MidAttributeMemberRef(
     MidAttributeDecl decl)
 {
     _decl = decl;
 }
        private void SetSource(
            ref MidExp srcExp,
            MidAttributeDecl attr)
        {
            if( srcExp != null )
            {
                if (srcExp is MidAttributeRef)
                {
                    var srcAttr = ((MidAttributeRef)srcExp).Decl;
                    if (srcAttr != attr)
                    {
                        throw OperationTooComplexError(srcExp.Range);
                    }
                }
                else
                {
                    throw OperationTooComplexError(srcExp.Range);
                }
            }

            srcExp = new MidAttributeRef(attr.Range, attr, new LazyFactory());
        }
        private void DecomposeAttr(
            MidAttributeDecl midAttrDecl,
            int index)
        {
            DecomposeAttrContext context;
            context.index = index;
            context.flavor = AttrCase.Combined;

            AttrInfo info = DecomposeAttr(midAttrDecl, context);

            try
            {
                var blendDesc = GetTargetBlendDesc(info);
                _renderTargetBlendDescs[index] = blendDesc;

                // Do validation stuff - basically, we need to
                // be sure that whatever expressions get put into
                // the _renderTargetSources[index] entry are more
                // or less the "same" source...
            }
            catch (D3D11PixelShaderOperationTooComplex)
            {
                // error should have been reported when
                // the exception was thrown
            }
        }
 public static void UnmarkOutputs( MidAttributeDecl attribute )
 {
     attribute.IsOutput = false;
 }
        //
        public EmitValHLSL FetchAttr(
            EmitValHLSL objVal,
            MidAttributeDecl attr,
            Span span)
        {
            var attrType = EmitType(attr.Type);
            var attrName = MapName(attr);

            int attrIndex = 0;
            foreach (var a in attr.Element.Outputs)
            {
                if (a == attr)
                    break;
                attrIndex++;
            }

            var objType = (IAggTypeHLSL)objVal.Type;

            return GetField(
                objVal,
                objType.GetFieldType(attrIndex),
                attrName,
                attrIndex,
                span);
        }
 public void ApplyToAttribute(MidAttributeDecl attribute)
 {
     if (attribute.Exp != null)
         attribute.Exp = Transform(attribute.Exp);
 }
        private FieldInfo CreateField(
            MidPipelineDecl pipeline,
            MidElementDecl element,
            MidAttributeDecl attribute,
            MidFieldDecl field)
        {
            var midExp = _exps.FieldRef(
                    attribute.Range,
                    _exps.AttributeRef( attribute.Range, attribute ),
                    field );

            var name = _identifiers.unique(
                string.Format( "{0}_{1}", attribute.Name, field.Name ) );

            var newAttr = element.CacheAttr(
                midExp,
                field.Type);
            newAttr.TrySetName(name, attribute.Range);

            return new FieldInfo{
                AttrDecl = newAttr,
                FieldDecl = field};
        }
Example #26
0
 public void AddAttribute(MidAttributeDecl attribute)
 {
     _attributes.Add(attribute);
 }
Example #27
0
 public static void UnmarkOutputs(MidAttributeDecl attribute)
 {
     attribute.IsOutput = false;
 }
 public void CollectAttributeInfo( MidAttributeDecl attribute )
 {
     // Explicit inputs and outputs needs to be kept
     if( attribute.IsForcedOutput || attribute.IsInput )
     {
         _attributesToKeep.Add( attribute );
     }
 }
        public EmitValHLSL EmitAttribRef(
            MidAttributeDecl attr,
            Span inSpan)
        {
            var span = NoteRange(inSpan, attr.Range);

            EmitValHLSL attrVal;
            if (_attrVals.TryGetValue(attr, out attrVal))
                return attrVal;

            if (attr.Exp == null)
            {
                Diagnostics.Add(
                    Severity.Error,
                    new SourceRange(),
                    "No definition for attribute '{0}' during HLSL emit",
                    attr.Name);
                return new ErrorValHLSL();
            }

            // This is a (very) special case to try to deal with
            // attribute references used to define things like
            // array sizes (which appear in types, and thus
            // shouldn't really spill out other expressions...)
            if (span == null && attr.Exp is MidVal)
            {
                return EmitVal((MidVal)attr.Exp, span);
            }

            var attrName = MapName(attr);
            var attrType = EmitType(attr.Type);
            attrVal = attrType.CreateVal(attrName);

            _attrVals[attr] = attrVal;

            PreEmitExp(attr.Exp, span);

            var initVal = EmitExpRaw(attr.Exp, span);

            DeclareAndInitLocal(
                attrVal,
                initVal,
                span);

            return attrVal ;
        }
 private AttributeInfo DecomposeAttr(
     SourceRange range,
     MidAttributeDecl midAttrDecl)
 {
     var attrInfo = _info.Cache(midAttrDecl,
         () => DecomposeAttr(range, midAttrDecl.Exp));
     attrInfo.Range = range;
     return attrInfo;
 }
        public string EmitAttrLit(
            MidAttributeDecl attr)
        {
            if (attr.Exp == null)
            {
                throw new NotImplementedException();
            }

            var init = attr.Exp;
            if (init == null)
            {
                throw new NotImplementedException();
            }

            return EmitAttrLit(init);
        }
        public MidAttributeDecl CacheAttr(
            MidExp exp,
            MidType type )
        {
            MidAttributeDecl attrDecl = null;
            if( _attrCache.TryGetValue( exp, out attrDecl ) )
                return attrDecl;

            if( exp is MidAttributeRef )
            {
                var attrRef = (MidAttributeRef) exp;
                attrDecl = attrRef.Decl;
                if( attrDecl.Element == this && attrDecl.Exp != null )
                {
                    _attrCache[ exp ] = attrDecl;
                    return attrDecl;
                }
            }

            attrDecl = new MidAttributeDecl(
                _name.Factory.unique( "attr" ),
                this,
                type,
                exp );
            _attrCache[ exp ] = attrDecl;
            AddAttribute( attrDecl );
            return attrDecl;
        }
Example #33
0
        private MidMemberDecl EmitMemberDeclImpl(
            MidFacetDecl midFacet,
            IResAttributeDecl resAttrib,
            MidEmitEnv env)
        {
            var midAttrWrap = new MidAttributeWrapperDecl(midFacet, resAttrib.Name, resAttrib.Range);

            midAttrWrap.AddBuildAction(() =>
            {
                var fqType = resAttrib.Type;
                var freq   = fqType.Freq;
                var type   = fqType.Type;

                var midElement = (MidElementDecl)midFacet.Pipeline.LookupMemberDecl(freq.MemberTerm.Decl);
                var midType    = EmitTypeExp(type, env);

                MidAttributeDecl midAttr = null;
                if (resAttrib.Init == null)
                {
                    midAttr = new MidAttributeDecl(
                        resAttrib.Name,
                        midElement,
                        midType,
                        null);
                    midElement.AddAttribute(midAttr);
                    if (resAttrib.Line.ConcretenessMode == ResMemberConcretenessMode.Abstract)
                    {
                        midAttr.IsAbstract = true;
                    }
                    else if (resAttrib.IsInput())
                    {
                        midAttr.IsInput = true;
                    }
                    else if (resAttrib.IsOptional())
                    {
                        midAttr.IsOptional = true;
                    }
                    else if (resAttrib.Line.Tags.Any((t) => t is ResBuiltinTag))
                    {
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }

                    // Eventually:
//                    else
//                        midAttr.IsInput = true;
                }
                else
                {
                    var initEnv = new MidDummyEmitEnv(env);

                    var inheritedDecls = resAttrib.Line.InheritedDecls;
                    if (inheritedDecls != null)
                    {
                        // Skip first decl if we are already using an inherited decl:
                        if (resAttrib.Line.MemberDeclMode != ResMemberDeclMode.Inherited)
                        {
                            initEnv.InheritedDecls = inheritedDecls.Cast <IResAttributeDecl>();
                        }
                        else
                        {
                            initEnv.InheritedDecls = inheritedDecls.Cast <IResAttributeDecl>().Skip(1);
                        }
                    }

                    var midExp = EmitAttrExp(midElement, midType, resAttrib.Init, initEnv);

                    if (midExp is MidAttributeRef &&
                        ((MidAttributeRef)midExp).Decl.IsOptional)
                    {
                        midAttr = ((MidAttributeRef)midExp).Decl;
                    }
                    else if (resAttrib.IsOutput())
                    {
                        midAttr = new MidAttributeDecl(
                            resAttrib.Name,
                            midElement,
                            midType,
                            midExp);
                        midElement.AddAttribute(midAttr);
                    }
                    else
                    {
                        midAttr = midElement.CacheAttr(midExp, midType);
                        midAttr.TrySetName(resAttrib.Name, resAttrib.Range);
                    }
                }
                if (resAttrib.IsOutput() && !midAttr.IsOptional)
                {
                    midAttr.IsForcedOutput = true;
                }

                midAttrWrap.Attribute = midAttr;
                midElement.AddAttributeWrapper(midAttrWrap);
            });
            midAttrWrap.DoneBuilding();

            midFacet.AddAttribute(midAttrWrap);
            return(midAttrWrap);

            /*
             *
             * var midAttrib = new MidAttributeDecl(midFacet, resAttrib.Name);
             *
             * if (resAttrib.Line.ConcretenessMode == ResMemberConcretenessMode.Abstract)
             *  midAttrib.IsAbstract = true;
             *
             * if( resAttrib.IsOutput )
             *  midAttrib.IsOutput = true;
             *
             * midAttrib.AddBuildAction(() =>
             * {
             *  var fqType = resAttrib.Type;
             *  var freq = fqType.Freq;
             *  var type = fqType.Type;
             *
             *  var midElement = (MidElementDecl)midFacet.Pipeline.LookupMemberDecl(freq.MemberTerm.Decl);
             *
             *  midAttrib.Type = EmitTypeExp(resAttrib.Type, env);
             *
             *  if (resAttrib.Init != null)
             *  {
             *      var savedElement = _currentElement;
             *      _currentElement = midElement;
             *
             *      midAttrib.Exp = EmitLocalExp(resAttrib.Init, env);
             *
             *      _currentElement = savedElement;
             *  }
             *
             *  midAttrib.Element = midElement;
             *  midElement.AddAttribute(midAttrib);
             * });
             *
             * midAttrib.DoneBuilding();
             *
             * midFacet.AddAttribute(midAttrib);
             * return midAttrib;
             * */
        }
        private void Replace(
            MidAttributeDecl attr)
        {
            if (attr.Exp == null)
                return;

            attr.Exp = Replace(attr.Exp);
        }
 public static void MarkOutputs(MidAttributeDecl attribute)
 {
     if (attribute.Exp != null)
         MarkOutputs(attribute.Exp);
 }
 //
 public string MapName(MidAttributeDecl decl)
 {
     return _shared.MapName(decl);
 }
 private AttrInfo DecomposeAttr(
     MidAttributeDecl midAttrDecl,
     DecomposeAttrContext context )
 {
     return DecomposeAttr(midAttrDecl.Exp, context);
 }
 // \todo: This is hacky and should be removed
 public void UnbindAttribute(
     MidAttributeDecl attribute)
 {
     _attrVals.Remove(attribute);
 }
Example #39
0
 public MidAttributeMemberRef(
     MidAttributeDecl decl)
 {
     _decl = decl;
 }
 public string MapName(MidAttributeDecl decl)
 {
     return MapNameImpl(decl, string.Format("a_{0}_", decl.Name));
 }
 private AttributeInfo TryDecomposeAttr(
     SourceRange range,
     MidAttributeDecl midAttrDecl)
 {
     try
     {
         return DecomposeAttr(range, midAttrDecl);
     }
     catch (D3D11InputAssemblerOperationTooComplex)
     {
         return null;
     }
 }
 public void BindAttr(
     MidAttributeDecl attr,
     EmitValHLSL val)
 {
     _attrVals[attr] = val;
 }
 public void AddAttribute(MidAttributeDecl attribute)
 {
     _attributes.Add(attribute);
 }
        private MidAttributeDecl MapOldToNew( MidAttributeDecl old )
        {
            MidAttributeDecl result = null;
            if( _mapOldToNew.TryGetValue( old, out result ) )
                return result;

            MidAttributeWrapperDecl wrapper = null;
            _mapOldToWrapper.TryGetValue( old, out wrapper );

            if (old.Exp != null)
            {
                // bootstrap the _mapOldToNew, so that
                // a recursive invocation of MapOldToNew
                // on this same attribute won't cause
                // an infinite recursion:
                _mapOldToNew[old] = old;

                old.Exp = _transform.Transform(old.Exp);
            }

            if( old.Exp == null || old.IsForcedOutput || old.IsInput )
            {
                result = old;
                old.Element.AddAttribute( old );
            }
            else if( old.Name.ToString().StartsWith( "__" ) )
            {
                result = old;
                old.Element.AddAttribute( old );
            }
            else
            {
                result = old.Element.CacheAttr( old.Exp, old.Type );
            }

            if( wrapper != null )
            {
                result.TrySetName( wrapper.Name, wrapper.Range );
                wrapper.Attribute = result;
                _attributeWrappersToKeep.Add( wrapper );
            }

            _mapOldToNew[ old ] = result;
            return result;
        }
Example #45
0
 public void SimplifyAttribute(MidAttributeDecl attribute)
 {
     attribute.Exp = SimplifyExp(attribute.Exp, new SimplifyEnv(null));
 }
        public override void EmitImplSetup()
        {
            var uniformElement = GetElement("Uniform");
            var iaElement = GetElement("AssembledVertex");

            _vertexIDAttr = GetAttribute(iaElement, "IA_VertexID").Attribute;
            _instanceIDAttr = GetAttribute(iaElement, "IA_InstanceID").Attribute;

            _info[_vertexIDAttr] = new IndexSourceInfo(_vertexIDAttr.Range)
            {
                InputSlotClass = InitBlock.Enum32("D3D11_INPUT_CLASSIFICATION", "D3D11_INPUT_PER_VERTEX_DATA", D3D11_INPUT_CLASSIFICATION.D3D11_INPUT_PER_VERTEX_DATA),
                StepRate = 0
            };

            _info[_instanceIDAttr] = new IndexSourceInfo(_instanceIDAttr.Range)
            {
                InputSlotClass = InitBlock.Enum32("D3D11_INPUT_CLASSIFICATION", "D3D11_INPUT_PER_INSTANCE_DATA", D3D11_INPUT_CLASSIFICATION.D3D11_INPUT_PER_INSTANCE_DATA),
                StepRate = 1
            };

            InitBlock.AppendComment("D3D11 Input Assembler");

            var inputElementInits = (from a in iaElement.Attributes
                     where a.IsOutput
                     let name = SharedHLSL.MapName(a)
                     let attrInfo = TryDecomposeAttr(a.Range, a)
                     where attrInfo != null
                     from e in DeclareInputElements(InitBlock, name, attrInfo)
                     select e).ToArray();

            if (_inputElementCount != 0)
            {
                var inputElementDescsVal = InitBlock.Temp(
                    "inputElementDescs",
                    InitBlock.Array(
                        EmitTarget.GetBuiltinType("D3D11_INPUT_ELEMENT_DESC"),
                        inputElementInits));

                var inputLayoutPointerType = EmitTarget.GetOpaqueType("ID3D11InputLayout*");
                inputLayoutField = EmitClass.AddPrivateField(
                    inputLayoutPointerType,
                    "_inputLayout");
                InitBlock.SetArrow(
                    CtorThis,
                    inputLayoutField,
                    EmitTarget.GetNullPointer(inputLayoutPointerType));

                InitBlock.CallCOM(
                    CtorDevice,
                    "ID3D11Device",
                    "CreateInputLayout",
                    inputElementDescsVal.GetAddress(),
                    InitBlock.LiteralU32((UInt32)_inputElementCount),
                    EmitPass.VertexShaderBytecodeVal,
                    EmitPass.VertexShaderBytecodeSizeVal,
                    InitBlock.GetArrow(CtorThis, inputLayoutField).GetAddress());

                DtorBlock.CallCOM(
                    DtorBlock.GetArrow(DtorThis, inputLayoutField),
                    "IUnknown",
                    "Release");
            }
        }