예제 #1
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);
        }
 public MidAttributeWrapperMemberRef(
     MidAttributeWrapperDecl decl )
 {
     _decl = decl;
 }
        public EmitValHLSL EmitTempRecordCtor(
            Span span,
            MidElementDecl recordToConstruct,
            MidAttributeWrapperDecl wrapper )
        {
            var attr = wrapper.Attribute;
            var attrName = attr.Name.ToString();
            var attrType = EmitType(attr.Type);
            var attrVal = attrType.CreateVal(attrName);
            BindAttr(attr, attrVal);

            return DeclareAndInitRecord(
                span,
                recordToConstruct,
                attrVal);
        }
        //
        public override void EmitImplSetup()
        {
            var uniformElement = GetElement("Uniform");
            var rasterVertex = GetElement("RasterVertex");
            var fragmentElement = GetElement("Fragment");
            var pixelElement = GetElement("Pixel");

            // Find all render targets:

            renderTargetAttributes = (from a in pixelElement.Attributes
                                      where a.Exp != null
                                      where a.IsOutput
                                      select a).ToArray();
            renderTargetCount = renderTargetAttributes.Length;

            // Depth-stencil view

            depthStencilViewAttribute = GetAttribute(uniformElement, "depthStencilView");

            // Compute the setup required by the OM

            // Blending stuff
            var blendStateType = EmitTarget.GetOpaqueType("ID3D11BlendState*");

            blendStateField = EmitClass.AddPrivateField(
                blendStateType,
                "_blendState");

            _renderTargetBlendDescs = new TargetBlendDesc[renderTargetCount];
            _renderTargetSources = new SourceInfo[renderTargetCount];
            for (int ii = 0; ii < renderTargetCount; ++ii)
            {
                DecomposeAttr(renderTargetAttributes[ii], ii);
            }

            var rtBlendDescType = EmitTarget.GetBuiltinType("D3D11_RENDER_TARGET_BLEND_DESC");
            var blendSpecVals = (from desc in _renderTargetBlendDescs
                                 select InitBlock.Struct(
                                    "D3D11_RENDER_TARGET_BLEND_DESC",
                                    InitBlock.LiteralBool(desc.blendEnable),
                                    InitBlock.Enum32(desc.color.srcBlend),
                                    InitBlock.Enum32(desc.color.destBlend),
                                    InitBlock.Enum32(desc.color.op),
                                    InitBlock.Enum32(desc.alpha.srcBlend),
                                    InitBlock.Enum32(desc.alpha.destBlend),
                                    InitBlock.Enum32(desc.alpha.op),
                                    InitBlock.LiteralU32(desc.writeMask))).ToList();
            while (blendSpecVals.Count < 8) // \todo: get the limits from somwhere!!!
            {
                blendSpecVals.Add(
                    InitBlock.Struct(
                        "D3D11_RENDER_TARGET_BLEND_DESC",
                        InitBlock.LiteralBool(false),
                        InitBlock.Enum32("D3D11_BLEND", "D3D11_BLEND_ONE", D3D11_BLEND.D3D11_BLEND_ONE),
                        InitBlock.Enum32("D3D11_BLEND", "D3D11_BLEND_ZERO", D3D11_BLEND.D3D11_BLEND_ZERO),
                        InitBlock.Enum32("D3D11_BLEND_OP", "D3D11_BLEND_OP_ADD", D3D11_BLEND_OP.D3D11_BLEND_OP_ADD),
                        InitBlock.Enum32("D3D11_BLEND", "D3D11_BLEND_ONE", D3D11_BLEND.D3D11_BLEND_ONE),
                        InitBlock.Enum32("D3D11_BLEND", "D3D11_BLEND_ZERO", D3D11_BLEND.D3D11_BLEND_ZERO),
                        InitBlock.Enum32("D3D11_BLEND_OP", "D3D11_BLEND_OP_ADD", D3D11_BLEND_OP.D3D11_BLEND_OP_ADD),
                        InitBlock.LiteralU32((UInt32)D3D11_COLOR_WRITE_ENABLE.D3D11_COLOR_WRITE_ENABLE_ALL)));
            }

            var blendSpecsVal = InitBlock.Array(
                rtBlendDescType,
                blendSpecVals);

            InitBlock.AppendComment("D3D11 Output Merger");
            var blendDescVal =
                InitBlock.Temp("blendDesc",
                    InitBlock.Struct(
                        "D3D11_BLEND_DESC",
                        InitBlock.LiteralBool(false),
                        InitBlock.LiteralBool(true),
                        blendSpecsVal));

            InitBlock.SetArrow(
                CtorThis,
                blendStateField,
                EmitTarget.GetNullPointer(blendStateType));

            InitBlock.CallCOM(
                CtorDevice,
                "ID3D11Device",
                "CreateBlendState",
                blendDescVal.GetAddress(),
                InitBlock.GetArrow(CtorThis, blendStateField).GetAddress());

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

            // Emit HLSL code for PS

            InitBlock.AppendComment("D3D11 Pixel Shader");

            hlslContext = new EmitContextHLSL(SharedHLSL, Range, this.EmitClass.GetName());

            var entryPointSpan = hlslContext.EntryPointSpan;

            entryPointSpan.WriteLine("void main(");

            bool firstParam = true;
            hlslContext.DeclareConnectorAndBind(
                rasterVertex,
                GetAttribute(fragmentElement, "__rv2f"),
                ref firstParam,
                entryPointSpan);

            hlslContext.DeclareParamAndBind(
                GetAttribute(fragmentElement, "PS_ScreenSpacePosition"),
                "SV_Position",
                ref firstParam,
                entryPointSpan);

            for (int ii = 0; ii < renderTargetCount; ++ii)
            {
                if( !firstParam ) entryPointSpan.WriteLine(",");
                firstParam = false;

                var sourceInfo = _renderTargetSources[ii];
                MidExp exp = null;
                if (sourceInfo.combinedExp != null)
                {
                    // \todo: Validate other bits and bobs!!!
                    exp = sourceInfo.combinedExp;
                }
                else
                {
                    throw new NotImplementedException();
                }

                entryPointSpan.Write("\tout {1} target{0} : SV_Target{0}", ii,
                    hlslContext.EmitType(exp.Type));
            }
            entryPointSpan.WriteLine(" )");
            entryPointSpan.WriteLine("{");

            hlslContext.EmitTempRecordCtor(
                entryPointSpan,
                fragmentElement,
                GetAttribute(pixelElement, "__ps2om"));

            var psCullFragmentAttr = GetAttribute(fragmentElement, "PS_CullFragment");
            entryPointSpan.WriteLine("\tif( {0} ) discard;",
                hlslContext.EmitAttribRef(psCullFragmentAttr, entryPointSpan));

            for (int ii = 0; ii < renderTargetCount; ++ii)
            {
                var sourceInfo = _renderTargetSources[ii];
                MidExp exp = null;
                if (sourceInfo.combinedExp != null)
                {
                    // \todo: Validate other bits and bobs!!!
                    exp = sourceInfo.combinedExp;
                }
                else
                {
                    throw new NotImplementedException();
                }

                entryPointSpan.WriteLine("\ttarget{0} = {1};",
                    ii,
                    hlslContext.EmitExp(exp, entryPointSpan));
            }

            entryPointSpan.WriteLine("}");

            hlslContext.EmitConstantBufferDecl();

            //

            EmitShaderSetup(
                hlslContext,
                "ps_5_0",
                "Pixel",
                "PS");
        }
 //
 public EmitValHLSL EmitAttribRef(
     MidAttributeWrapperDecl wrapper,
     Span span)
 {
     return EmitAttribRef(
         wrapper.Attribute,
         NoteRange(span, wrapper.Range));
 }
 // Emit an attribute that *should* have a literal value
 public string EmitAttrLit(
     MidAttributeWrapperDecl wrapper)
 {
     return EmitAttrLit(wrapper.Attribute);
 }
 // Declare a parameter that binds to a given attribute
 // (usually an 'input' attribute for the shader stage)
 public void DeclareParamAndBind(
     MidAttributeWrapperDecl attr,
     string semantic,
     ref bool firstParam,
     Span span,
     string prefix = "")
 {
     DeclareParamAndBind(
         attr,
         EmitType(attr.Type),
         semantic,
         ref firstParam,
         span,
         prefix);
 }
        public void DeclareParamAndBind(
            MidAttributeWrapperDecl attr,
            ITypeHLSL attrType,
            string semantic,
            ref bool firstParam,
            Span span,
            string prefix = "")
        {
            var attrName = attr.Name.ToString();

            // \todo: Insist that the type
            // is a "real" type (rather than
            // a synthetic one that might be zero or more
            // real parameters...)

            var attrVal = DeclareParam(
                attrName,
                attrType,
                span,
                ref firstParam,
                semantic == null ? "" : ": " + semantic,
                prefix: prefix);

            BindAttr(attr, attrVal);
        }
 public void BindAttrLit(
     MidAttributeWrapperDecl attr,
     string lit)
 {
     var type = EmitType(attr.Type);
     if (!(type is RealTypeHLSL))
     {
         throw new NotImplementedException();
     }
     var val = new LitHLSL((RealTypeHLSL)type, lit);
     BindAttr(attr, val);
 }
 public void DeclareConnectorAndBind(
     MidElementDecl element,
     MidAttributeWrapperDecl attr,
     ref bool firstParam,
     Span span)
 {
     var attrType = GenerateConnectorType(element);
     DeclareParamAndBind(
         attr,
         attrType,
         null,
         ref firstParam,
         span );
 }
 // Bind an attribute to a given value
 public void BindAttr(
     MidAttributeWrapperDecl wrapper,
     EmitValHLSL val)
 {
     BindAttr(wrapper.Attribute, val);
 }
 public void AddAttribute(MidAttributeWrapperDecl attr)
 {
     _attributes.Add(attr);
 }
예제 #13
0
 public void AddAttributeWrapper(
     MidAttributeWrapperDecl attributeWrapper)
 {
     _attributeWrappers.Add(attributeWrapper);
 }
예제 #14
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;
             * */
        }
예제 #15
0
 public MidAttributeWrapperMemberRef(
     MidAttributeWrapperDecl decl)
 {
     _decl = decl;
 }