Example #1
0
        /// <summary>
        /// This event will be fired immediately after the Direct3D device has been
        /// created, which will happen during application initialization and windowed/full screen
        /// toggles. This is the best location to create Pool.Managed resources since these
        /// resources need to be reloaded whenever the device is destroyed. Resources created
        /// here should be released in the Disposing event.
        /// </summary>
        private void OnCreateDevice(object sender, DeviceEventArgs e)
        {
            // Initialize the font
            drawingFont = ResourceCache.GetGlobalInstance().CreateFont(e.Device, 15, 0, FontWeight.Bold, 1, false, CharacterSet.Default,
                                                                       Precision.Default, FontQuality.Default, PitchAndFamily.FamilyDoNotCare | PitchAndFamily.DefaultPitch
                                                                       , "Arial");

            // Create the vertex shader and declaration
            VertexElement[] elements = new VertexElement[]
            {
                new VertexElement(0, 0, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                VertexElement.VertexDeclarationEnd
            };

            vertexDecl = new VertexDeclaration(e.Device, elements);

            // Find the shader file
            string path = Utility.FindMediaFile("HLSLwithoutEffects.Fx");

            // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the
            // shader debugger. Debugging vertex shaders requires either REF or software vertex
            // processing, and debugging pixel shaders requires REF.  The
            // ShaderFlags.Force*SoftwareNoOptimizations flag improves the debug experience in the
            // shader debugger.  It enables source level debugging, prevents instruction
            // reordering, prevents dead code elimination, and forces the compiler to compile
            // against the next higher available software target, which ensures that the
            // unoptimized shaders do not exceed the shader model limitations.  Setting these
            // flags will cause slower rendering since the shaders will be unoptimized and
            // forced into software.  See the DirectX documentation for more information about
            // using the shader debugger.
            ShaderFlags shaderFlags = ShaderFlags.None;

#if (DEBUG_VS)
            shaderFlags |= ShaderFlags.ForceVertexShaderSoftwareNoOptimizations;
#endif
#if (DEBUG_PS)
            shaderFlags |= ShaderFlags.ForcePixelShaderSoftwareNoOptimizations;
#endif

            string errors;

            using (GraphicsStream code = ShaderLoader.CompileShaderFromFile(path, "Ripple", null, null,
                                                                            "vs_1_1", shaderFlags, out errors, out constantTable))
            {
                // We will store these constants in an effect handle here for performance reasons.
                // You could simply use the string value (i.e., "worldViewProj") in the SetValue call
                // and it would work just as well, but that actually requires an allocation to be made
                // and can actually slow your performance down.  It's much more efficient to simply
                // cache these handles for use later
                worldViewHandle = constantTable.GetConstant(null, "worldViewProj");
                timeHandle      = constantTable.GetConstant(null, "appTime");

                // Create the shader
                shader = new VertexShader(e.Device, code);
            }

            // Setup the camera's view parameters
            camera.SetViewQuat(new Quaternion(-0.275f, 0.3f, 0.0f, 0.7f));
        }
Example #2
0
        private ConstantTable SetupDevice(OpsContext context, ShadeArgs args)
        {
            string         errStr = null;
            ConstantTable  constantTable;
            GraphicsStream pshader;
            GraphicsStream vshader;

            try
            {
                ConstantTable dummyTable;
                errStr  = null;
                vshader = ShaderLoader.CompileShader(ShadeVertex.VertexShader, "VertexShader", null, null, "vs_3_0", ShaderFlags.None, out errStr, out dummyTable);
            }
            finally
            {
                if (errStr != null && errStr.Length > 0)
                {
                    OpsConsole.WriteLine("Vertex Shader Compiler messages: " + errStr);
                    OpsConsole.WriteLine("If this message is regarding your entry point, it may be something other than the default 'main'.  Please use the argument 'Shader' to specify it.");
                }
            }


            try
            {
                Macro dxopsMacro = new Macro();
                dxopsMacro.Name       = "__DXOPS";
                dxopsMacro.Definition = "1";
                errStr  = null;
                pshader = ShaderLoader.CompileShaderFromFile(args.File, args.Shader, new Macro[] { dxopsMacro }, null, "ps_3_0", ShaderFlags.None, out errStr, out constantTable);
            }
            finally
            {
                if (errStr != null && errStr.Length > 0)
                {
                    OpsConsole.WriteLine("Pixel Shader Compiler messages: " + errStr);
                    OpsConsole.WriteLine("If this message is regarding your entry point, it may be something other than the default 'main'.  Please use the argument 'Shader' to specify it.");
                }
            }

            context.Device.SetRenderState(RenderStates.CullMode, (int)Cull.None);
            context.Device.SetRenderState(RenderStates.FillMode, (int)FillMode.Solid);
            context.Device.SetRenderState(RenderStates.AlphaTestEnable, false);
            context.Device.SetRenderState(RenderStates.AlphaBlendEnable, false);
            context.Device.SetRenderState(RenderStates.StencilEnable, false);
            context.Device.SetRenderState(RenderStates.ZEnable, false);
            context.Device.SetRenderState(RenderStates.ZBufferWriteEnable, false);

            context.Device.DepthStencilSurface = null;

            VertexDeclaration decl = new VertexDeclaration(context.Device, ShadeVertex.VertexDeclaration);

            context.Device.VertexDeclaration = decl;

            VertexShader vs = new VertexShader(context.Device, vshader);

            context.Device.VertexShader = vs;

            PixelShader ps = new PixelShader(context.Device, pshader);

            context.Device.PixelShader = ps;



            constantTable.SetDefaults(context.Device);

            foreach (OpsParsedArgument constant in args.Constants)
            {
                EffectHandle h = constantTable.GetConstant(null, constant.Name);
                if (h == null)
                {
                    OpsConsole.WriteLine("WARNING: Parameter '{0}' was not found in shader.", constant.Name);
                    continue;
                }

                ConstantDescription[] cds = constantTable.GetConstantDescription(h, 1);
                ConstantDescription   cd  = cds[0];
                switch (cd.Class)
                {
                case ParameterClass.Object:
                {    //texture
                    switch (cd.ParameterType)
                    {
                    case ParameterType.Texture:
                    case ParameterType.Texture1D:
                    case ParameterType.Texture2D:
                    case ParameterType.Texture3D:
                    case ParameterType.TextureCube:
                    {
                        OpsTexture container = context.GetTexture(constant.Value);

                        int sampler = constantTable.GetSamplerIndex(h);
                        context.Device.SetTexture(sampler, container.Texture);
                    }
                    break;
                    }
                    break;
                }

                case ParameterClass.Scalar:
                case ParameterClass.Vector:
                case ParameterClass.MatrixColumns:
                case ParameterClass.MatrixRows:
                {
                    ArrayList floatList    = new ArrayList();
                    string[]  floatStrings = constant.Value.Split(new char[] { ' ', ',' });
                    foreach (string floatStr in floatStrings)
                    {
                        if (floatStr.Length > 0)
                        {
                            floatList.Add(float.Parse(floatStr));
                        }
                    }
                    float[] floats = (float[])floatList.ToArray(typeof(float));

                    constantTable.SetValue(context.Device, h, floats);
                }
                break;
                }
            }

            return(constantTable);
        }