• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Narf the Mouse

"Attempted to read or write protected memory" on DrawIndexed()

12 posts in this topic

I've looked at tutorials in Google, examined my buffers and pointers, and I can't find what I'm doing wrong to get this error. I don't even have a clue what's actually causing it. So - Help, please, thanks.

Drawing code:
[code]
graphicsDevice.device.ImmediateContext.Rasterizer.State = SlimDX.Direct3D11.RasterizerState.FromDescription(
graphicsDevice.device,
new SlimDX.Direct3D11.RasterizerStateDescription( )
{
CullMode = SlimDX.Direct3D11.CullMode.Back,
DepthBias = 0,
DepthBiasClamp = 0,
FillMode = SlimDX.Direct3D11.FillMode.Solid,
IsAntialiasedLineEnabled = true,
IsDepthClipEnabled = true,
IsFrontCounterclockwise = false,
IsMultisampleEnabled = false,
IsScissorEnabled = false,
SlopeScaledDepthBias = 0
} );
graphicsDevice.device.ImmediateContext.InputAssembler.PrimitiveTopology = SlimDX.Direct3D11.PrimitiveTopology.TriangleList ;
graphicsDevice.device.ImmediateContext.InputAssembler.SetIndexBuffer( indexBuffer.Indices, indexBuffer.Format, indexBuffer.Offset );
SlimDX.Direct3D11.InputLayout layout;
graphicsDevice.device.ImmediateContext.InputAssembler.InputLayout =
layout =
new SlimDX.Direct3D11.InputLayout(
graphicsDevice.device,
graphicsDevice.defaultEffect.Signature,
vertexBuffer.InputElements );
graphicsDevice.device.ImmediateContext.InputAssembler.SetVertexBuffers( 0, vertexBuffer.BufferBinding );
graphicsDevice.device.ImmediateContext.DrawIndexed( indexBuffer.IndexCount, indexBuffer.StartIndexLocation, vertexBuffer.BaseVertexLocation );
layout.Dispose( );
graphicsDevice.defaultEffect.EndPass( );
[/code]

Vertex buffer class construction:
[code]
this.graphicsDevice = graphicsDevice;
var buffer =
new SlimDX.Direct3D11.Buffer(
graphicsDevice.Device,
streamSource,
(int)streamSource.Length,
SlimDX.Direct3D11.ResourceUsage.Default,
SlimDX.Direct3D11.BindFlags.VertexBuffer,
SlimDX.Direct3D11.CpuAccessFlags.None,
SlimDX.Direct3D11.ResourceOptionFlags.None,
0 );
BufferBinding = new SlimDX.Direct3D11.VertexBufferBinding( buffer, vertexStride, 0 );
streamSource.Position = 0;
this.InputElements = GetInputElements( vertexDataInfo );
BaseVertexLocation = 0;
[/code]

Index buffer class construction:
[code]
this.graphicsDeviceDirectX11 = graphicsDeviceDirectX9;
Format = ( indexStride == 2 ? SlimDX.DXGI.Format.R16_UInt : SlimDX.DXGI.Format.R32_UInt );
Offset = 0;
this.Indices = new SlimDX.Direct3D11.Buffer(
graphicsDeviceDirectX9.Device,
streamSource,
(int)streamSource.Length,
SlimDX.Direct3D11.ResourceUsage.Default,
SlimDX.Direct3D11.BindFlags.IndexBuffer,
SlimDX.Direct3D11.CpuAccessFlags.None,
SlimDX.Direct3D11.ResourceOptionFlags.None,
0 );
DisposableManager.Singleton.Register( this.Indices );
streamSource.Position = 0;

IndexCount = (int)streamSource.Length / indexStride;
StartIndexLocation = 0;
[/code]

Edit: And in further adventures with the forum software, control-V doesn't work with blank spaces, so formatting code for the forum is a lot slower. Edited by Narf the Mouse
0

Share this post


Link to post
Share on other sites
Identify where the problem comes from first, does the issue still occur if you use DrawPrimitives instead of DrawIndexedPrimitives without the index buffer? Doesn't matter if it's drawing meaningless crap, but does it crash? If not, the issue comes from the index buffer, if it does, then it comes from either the vertex buffer or your drawing code. It's important to pinpoint where the error comes from exactly.

Then, try and only draw part of the buffer, this might reveal if you are trying to draw more than you have (because of a logic or arithmetic error on your part, perhaps you got primitives and vertices mixed up or something). Etc... Also try listening to what the DirectX debug output has to tell you, it may have some information on why it is crashing (google how to get the debug output into your IDE's console window or just use DebugView otherwise).

[size=2]Also, don't reinitialize your device's input assembler and rasterizer state every frame[/size] [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] Edited by Bacterius
0

Share this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1343084364' post='4962391']
Identify where the problem comes from first, does the issue still occur if you use DrawPrimitives instead of DrawIndexedPrimitives without the index buffer? Doesn't matter if it's drawing meaningless crap, but does it crash? If not, the issue comes from the index buffer, if it does, then it comes from either the vertex buffer or your drawing code. It's important to pinpoint where the error comes from exactly.

Then, try and only draw part of the buffer, this might reveal if you are trying to draw more than you have (because of a logic or arithmetic error on your part, perhaps you got primitives and vertices mixed up or something). Etc... Also try listening to what the DirectX debug output has to tell you, it may have some information on why it is crashing (google how to get the debug output into your IDE's console window or just use DebugView otherwise).

[size=2]Also, don't reinitialize your device's input assembler and rasterizer state every frame[/size] [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
[/quote]
Good points. Think I was too focused on the immediate, there.

I know about the rasterizer part; this is just a hack until I get the triangles up on the screen, at which point it will be eradicated. Lets me fiddle with it directly.

But...Can you explain the input assembler part?
0

Share this post


Link to post
Share on other sites
[quote name='Narf the Mouse' timestamp='1343085274' post='4962396']
But...Can you explain the input assembler part?
[/quote]
You're redefining the vertex/index buffers, topology, input layout - they persist across frames, and I think you only have one vertex/index buffer pair at the moment, so it's a waste to reset them every frame (not that it makes much of a performance difference anyway). Anyway that was just a remark. What have you found with more proactive debugging?
0

Share this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1343086392' post='4962401']
[quote name='Narf the Mouse' timestamp='1343085274' post='4962396']
But...Can you explain the input assembler part?
[/quote]
You're redefining the vertex/index buffers, topology, input layout - they persist across frames, and I think you only have one vertex/index buffer pair at the moment, so it's a waste to reset them every frame (not that it makes much of a performance difference anyway). Anyway that was just a remark. What have you found with more proactive debugging?
[/quote]
Well, I just finished the debugging you suggested; Draw( ) gives the exact same error and DebugView ( [url="http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx"]http://technet.micro...s/bb896647.aspx[/url] ) gives no output whatsoever, with or without the DirectX Control Panel set to force the debug layer on for the application, with the default DebugView settings.

Edit: To clarify further, my vertex buffer contains vertices:
[code]
[System.Runtime.InteropServices.StructLayout( System.Runtime.InteropServices.LayoutKind.Explicit, Size = 12, Pack = 1 )]
public struct Vertex
{
public Vector3F Position
{
get { return position; }
set { position = value; }
}
public static VertexDataInfo[] VertexDataInfo
{
get
{
return new VertexDataInfo[ ] {
new VertexDataInfo( VertexDataType.Position, VertexDataFrom.Float3 )
};
}
}

[System.Runtime.InteropServices.FieldOffset( 0 )]
private Vector3F position;

public Vertex( Vector3F position )
{
this.position = position;
}
}
[/code]

and the following draw call: "graphicsDevice.device.ImmediateContext.Draw( 3, 0 );" results in the exact same error message. Edited by Narf the Mouse
0

Share this post


Link to post
Share on other sites
[quote]DebugView ( [url="http://technet.micro...s/bb896647.aspx"]http://technet.micro...s/bb896647.aspx[/url] ) gives no output whatsoever, with or without the DirectX Control Panel set to force the debug layer on for the application, with the default DebugView settings.[/quote]
No output whatsoever? That's not right, it should at least output some trivial metadata even if it doesn't complain about the crash. For instance this is what I get upon closing one of my unfinished SlimDX projects (with D3D11):

[CODE][1860] Object of type SlimDX.Direct3D11.RasterizerState was not disposed. Stack trace of object creation:
[1860] Object of type SlimDX.Direct3D11.Buffer was not disposed. Stack trace of object creation:
[1860] Object of type SlimDX.Direct3D11.Buffer was not disposed. Stack trace of object creation:
[1860] Total of 3 objects still alive.[/CODE]

You want to capture Win32 in DebugView, and have you set the debug verbosity to maximum in the control panel? Also, if you have the 64-bit SDK, the two control panels (x86 and x64) do not actually share their settings, so you want to do it for both if unsure which DLL your program ends up using.

[quote]Well, I just finished the debugging you suggested; Draw( ) gives the exact same error[/quote]
Curious. The vertex buffer looks fine from here, so it must be the drawing code that's screwing up. I suspect it has something to do with the input layout being incorrect, but have you confirmed that all your parameters are what they should be (StartIndexLocation, IndexCount, Offset, ...).

I haven't used SlimDX in a while and I have missed a few updates, can you upload the whole project if it isn't too big so people can get a chance to run it and reproduce the error?

EDIT: with your last edit, clearly it is the drawing code or your vertex description as even a trivial vertex buffer doesn't work. I am heavily suspecting some padding issue, since your vertex stride is 12 bytes! Is there a way you can make it a float4 for testing (setting the W-component to 1)? I mean, I can't see anything else wrong. Edited by Bacterius
0

Share this post


Link to post
Share on other sites
Verbose kernal output doesn't help. I wonder if I'm confusing it by using C# destructors to dispose of disposables? Both control panels show the same settings, so it does look like they share them, since I only set one.

indexBuffer.Format is: SlimDX.DXGI.Format.R16_UInt
indexBuffer.Offset is: 0
indexBuffer.IndexCount is: 36
indexBuffer.StartIndexLocation is: 0
vertexBuffer.BaseVertexLocation is: 0

Vertex buffer size: 288. 288 / 12 = 24.
Index buffer size: 72. 72 / 2 = 36.

Mesh box creation function code:
[code]
float halfWidth = width * 0.5F,
halfHeight = height * 0.5F,
halfDepth = depth * 0.5F;
Vertex[ ] vertices = new Vertex[ 24 ] {
new Vertex( new Vector3F( -halfWidth, -halfHeight, -halfDepth ) ),
new Vertex( new Vector3F( halfWidth, -halfHeight, -halfDepth ) ),
new Vertex( new Vector3F( halfWidth, halfHeight, -halfDepth ) ),
new Vertex( new Vector3F( -halfWidth, halfHeight, -halfDepth ) ),

new Vertex( new Vector3F( -halfWidth, halfHeight, -halfDepth ) ),
new Vertex( new Vector3F( -halfWidth, halfHeight, halfDepth ) ),
new Vertex( new Vector3F( -halfWidth, -halfHeight, halfDepth ) ),
new Vertex( new Vector3F( -halfWidth, -halfHeight, -halfDepth ) ),

new Vertex( new Vector3F( halfWidth, -halfHeight, -halfDepth ) ),
new Vertex( new Vector3F( halfWidth, -halfHeight, halfDepth ) ),
new Vertex( new Vector3F( halfWidth, halfHeight, halfDepth ) ),
new Vertex( new Vector3F( halfWidth, halfHeight, -halfDepth ) ),

new Vertex( new Vector3F( -halfWidth, -halfHeight, halfDepth ) ),
new Vertex( new Vector3F( halfWidth, -halfHeight, halfDepth ) ),
new Vertex( new Vector3F( halfWidth, -halfHeight, -halfDepth ) ),
new Vertex( new Vector3F( -halfWidth, -halfHeight, -halfDepth ) ),

new Vertex( new Vector3F( -halfWidth, halfHeight, -halfDepth ) ),
new Vertex( new Vector3F( halfWidth, halfHeight, -halfDepth ) ),
new Vertex( new Vector3F( halfWidth, halfHeight, halfDepth ) ),
new Vertex( new Vector3F( -halfWidth, halfHeight, halfDepth ) ),

new Vertex( new Vector3F( -halfWidth, halfHeight, halfDepth ) ),
new Vertex( new Vector3F( halfWidth, halfHeight, halfDepth ) ),
new Vertex( new Vector3F( halfWidth, -halfHeight, halfDepth ) ),
new Vertex( new Vector3F( -halfWidth, -halfHeight, halfDepth ) )
};
short[ ] indices = new short[ 36 ] {
0, 2, 1,
0, 3, 2,
4, 6, 5,
4, 7, 6,
8, 10, 9,
8, 11, 10,
12, 14, 13,
12, 15, 14,
16, 18, 17,
16, 19, 18,
20, 22, 21,
20, 23, 22
};
return new Mesh(
graphicsDevice: graphicsDevice,
vertexBuffer: VertexBuffer.Create( graphicsDevice, vertices, Marshal.SizeOf( typeof( Vertex ) ), Vertex.VertexDataInfo ),
indexBuffer: IndexBuffer.Create( graphicsDevice, indices, sizeof( short ) ) );
[/code]
Changing the indices to ushort made no difference.

Edit: I get from arrays to SlimDX.DataStreams like so:
[code]
using ( var streamSource = new SlimDX.DataStream( vertices.Length * vertexStride, true, true ) )
{
streamSource.WriteRange( vertices );
streamSource.Position = 0;
[/code]
[code]
using ( var streamSource = new SlimDX.DataStream( indices.Length * indexStride, true, true ) )
{
streamSource.WriteRange( indices );
streamSource.Position = 0;
[/code]
The vertices are correctly written; I've previously tested this...Just did a quick array-readout of the index buffer; data looks valid.

Only thing left might be the "Vector4 versus Vector3" thing.

...It's not Vector4F versus Vector3F. I changed the Vertex struct to use Vector4Fs; same error.

Done the post. Edited by Narf the Mouse
0

Share this post


Link to post
Share on other sites
I am out of ideas. Please upload the whole zipped solution (or a minimal sample with the same error) and specify the version of the SlimDX DLL used, and I will take a closer look.
0

Share this post


Link to post
Share on other sites
Hmm:

RenderTarget.Description.FirstDepthSlice for SlimDX SimpleTriangle tutorial is: 2048
RenderTarget.Description.FirstDepthSlice for my code tutorial is: 2.

Now, I'm not sure what that is, but my number doesn't look right.
0

Share this post


Link to post
Share on other sites
Isolated the error. It happens when I use my effect. Here's the .fx code:
[code]
matrix Projection ;
matrix View ;
matrix World ;

struct VSInput
{
float4 VertexPosition : POSITION ;
};
struct VSOutput
{
float4 ScreenPosition : SV_POSITION ;
float4 WorldPosition : TEXCOORD0 ;
float4 CameraPosition : TEXCOORD1 ;
};

VSOutput VShader( VSInput input )
{
VSOutput output ;
output.WorldPosition = mul( World, input.VertexPosition ) ;
output.CameraPosition = mul( Projection, mul( View, output.WorldPosition ) ) ;
output.ScreenPosition = output.CameraPosition ;
return output ;
}

struct PSInput
{
float4 ScreenPosition : SV_POSITION ;
float4 WorldPosition : TEXCOORD0 ;
float4 CameraPosition : TEXCOORD1 ;
} ;
struct PSOutput
{
float4 Colour : SV_TARGET ;
float Depth : SV_DEPTH ;
} ;

PSOutput PShader( PSInput input )
{
PSOutput output ;
output.Colour = float4( 1.0, 1.0, 1.0, 1.0 ) ;
output.Depth = input.CameraPosition.z / input.CameraPosition.w ;
return output ;
}

technique11 Shader
{
Pass P0
{
SetVertexShader( CompileShader( vs_5_0, VShader( ) ) ) ;
SetComputeShader( NULL ) ;
SetGeometryShader( NULL ) ;
SetDomainShader( NULL ) ;
SetHullShader( NULL ) ;
SetPixelShader( CompileShader( ps_5_0, PShader( ) ) ) ;
}
}
[/code]
As you can see, it's brilliantly complicated.

And here is how I apply the effect:
[code]effect.GetTechniqueByIndex( 0 ).GetPassByIndex( pass ).Apply( device.ImmediateContext );[/code]

...Neither the technique nor the pass is valid. Maybe I was more tired than I thought, because I checked them and was sure they were valid.

Added the technique to the SimpleTriangle triangle.fx file; loaded it into the effect class I'm using for this test - Exact same error.
[code]
float4 VShader(float4 position : POSITION) : SV_POSITION
{
return position;
}
float4 PShader(float4 position : SV_POSITION) : SV_Target
{
return float4(1.0f, 1.0f, 0.0f, 1.0f);
}

technique11 Shader
{
Pass P0
{
SetVertexShader( CompileShader( vs_5_0, VShader( ) ) ) ;
SetComputeShader( NULL ) ;
SetGeometryShader( NULL ) ;
SetDomainShader( NULL ) ;
SetHullShader( NULL ) ;
SetPixelShader( CompileShader( ps_5_0, PShader( ) ) ) ;
}
}
[/code]
Well, I'm not sure how this code is incorrect, but it seems the culprit has been found - Or at least, we've narrowed down the list of suspects. Edited by Narf the Mouse
0

Share this post


Link to post
Share on other sites
...My graphics card supports Feature Level 10.0.

*Puts "Get new graphics card" on the list of things to do, money depending*

Anyway, backstepped to a shader model 4_0 .fx file; fixed.

And now I feel a bit stupid.
0

Share this post


Link to post
Share on other sites
How did the D3D runtime even let you get that far? I would've expected it to fail a lot sooner. Anyway, glad it's fixed [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1343158926' post='4962708']
How did the D3D runtime even let you get that far? I would've expected it to fail a lot sooner. Anyway, glad it's fixed [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]
Well, presumably nothing else used DirectX11 functionality, and the fact that I was loading a DX11 effect file just never occurred to it, I guess. Thanks.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0