XNA with VertexBuffers only? (No loading content)

I'm trying to create just a simple test app that draws some triangles on the screen.  I don't want to load any models, just use a VertexBuffer and IndexBuffer right now.  I load the buffers and set them on the GraphicsDevice.  When I call DrawIndexedPrimitives, I get the following error:

"Both a valid vertex shader and  pixel shader (or valid effect) must be set on the device before draw operations may be performed."

After some searching, I found a few people mentioning that your graphics driver must support vertex shader 2.0 and pixel shader 3.0.  The card isn't all that old, but to be sure, I checked the GraphicCapabilities on the GraphicsDevice and both the vertex and pixel shaders are 3.0.  So I don't think that's the issue.  I checked the PixelShader and VertexShader of the GraphicsDevice at runtime and they're both set to null.  I notice there are PixelShader and VertexShader classes, but looks like you need precompiled shader code for those.  Any suggestions on how I can set these shaders without having to load a model  



Answer this question

XNA with VertexBuffers only? (No loading content)

  • kevin D. white

    you could either write a .fx file and load it as an effect, or use basic Effect. Since I haven't used BasicEffect yet, here's how I use an effect file. The underlined lines are responsible for setting the effect to be used by the GraphicsDevice

    Effect effect;
    Texture2D texture;

    protected override void LoadGraphicsContent(bool loadAllContent){

    effect=content.Load<Effect>("myShader");
    texture = content.Load<Texture2D>("myTexture");
    vb = new VertexBuffer(...);
    ib =
    new IndexBuffer(...);
    ........
    base.LoadGraphicsContent(loadAllContent);

    }

     

    public override void Draw(GameTime gameTime)

    {
    //set effect parameter
    effect.Parameters.GetParameterBySemantic(
    "WORLD").SetValue(world);
    effect.Parameters.GetParameterBySemantic(
    "VIEW").SetValue(viewMatrix);
    effect.Parameters.GetParameterBySemantic(
    "PROJECTION").SetValue(projectionMatrix);
    effect.Parameters.GetParameterBySemantic(
    "DiffuseMap").SetValue(texture);
    effect.CommitChanges();

    using (VertexDeclaration decl = new VertexDeclaration(

    GraphicsDevice, VertexPositionNormalTexture.VertexElements))

    {

             GraphicsDevice.VertexDeclaration = decl;

             effect.Begin();

             foreach (EffectPass pass in effect.CurrentTechnique.Passes)

                 {

                       pass.Begin();

                       GraphicsDevice.Vertices[0].SetSource(vb, 0, VertexPositionNormalTexture.SizeInBytes);

                      GraphicsDevice.Indices = ib;

                      GraphicsDevice.DrawIndexedPrimitives(....);

                      pass.End();

                }

             effect.End();

        }

    }

     


  • Itzik Paz

    This error message means that you have not set an effect before drawing your vertex buffer and index buffer, or that the effect that is set does not contain a valid vertex shader or pixel shader.

    You could set BasicEffect, or use one of the simple effect files described in the documentation.


  • AshN

    P.S. For simplicity, you might wanna read something on BasicEffect

    In the XNA Documentation, in XNA Game Studio Express -> Programming Guide -> Graphics -> 3D Graphics-> How To :Use Basic Effect.

    I believe they do a better job explaining it than I did,


  • redshock

    I've posted some simple programs that create all their content in memory (except for the effect file). I think you may find them helpful.

    You can find them at this link. They're the ones named "Simple Program"



  • BurritoSmith

    It's perfectly fine to create vertex and index buffers separately.

    You cannot, however, create an Effect separately. You have to either compile an effect and load it using Load<Effect>(), or use the BasicEffect that's built in (and make sure you begin that effect before drawing).

    The requirements for XNA itself is vs_1_1 and ps_1_1, i e, a DX8 level card. This means GeForce 3 or higher (but not GeForce 4 MX), or a Radeon 7000 or higher. Chances are, your card is compliant. The specific requirements of specific effects, however, may be higher, although nothing MS ships in the SDK requires more than ps_2_0 and vs_2_0 (which means GeForce 5100 or higher or Radeon 9500 or higher).


    Looking at Catalima's code, there are a few reasons to not use that as a model, though:

    1) Create a vertex declaration statically when you create your vertex buffer, and re-use it, instead of creating it each time you want to draw the vertex buffer.

    2) Get the effect parameters into member variables, and use SetValue() on those parameters, instead of looking up the parameters each time you render.



  • XNA with VertexBuffers only? (No loading content)