teichgraf's picture

AccessViolationException using GL.CopyTexImage2D

It's me again with another annoying question ;)

In my little demo I use Two-Pass-Rendering:
In the first step I render everything to the back-buffer.
After that, the buffer is copied to a texture with GL.CopyTexImage2D.
In the second step the copied texture is rendered on a big quad.

My problem:
Everytime the demo is running and I swith to the desktop (pressing [Windows] + [D]), I get an
AccessViolationException: "Attemped to read or write from protected memory"
from GL.CopyTexImage2D.

This also happens If I swith to another graphics application.

static int TEX_SIZE = 512;
GL.BindTexture(TextureTarget.Texture2d, texRender);
GL.CopyTexImage2D(TextureTarget.Texture2d, 0, PixelInternalFormat.Rgba, 0, 0, TEX_SIZE, TEX_SIZE, 0);
// ^^^ The exception occurs here

I added an example of the Two-Pass-Rendering mentioned above, using the QuickStart-Project. Check out the RenderFrame-method. While executing switch to the windows desktop (e.g. [Windows] + [D]).

QuickStart_2PassRender.zip9.26 KB


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Ok, can you try the following? In the resize event, add an if statement to avoid passing negative values to GL.Viewport:

if (e.Width > 0 && e.Height > 0)

There is a known issue where GameWindow.Resize reports a negative height when it is iconified (only affects Windows, not Linux).

Does this solve the crash?

the Fiddler's picture

Also, check this:
If any of the pixels within the specified rectangle of the current GL_READ_BUFFER are outside the window associated with the current rendering context, then the values obtained for those pixels are undefined.
[source: CopyTexImage2D manpage]

You should check if the current viewport is equal to, or larger than TEX_SIZE before calling the function.

teichgraf's picture

Thanks for your ideas. But they don't solve the problem.

I added an example of the Two-Pass-Rendering in the initial post (see above).

the Fiddler's picture

Ok, I can confirm the problem under Windows/Mesa, but it doesn't occur under Windows/Microsoft or Linux/flgrx combinations. I haven't tried under hardware accelerated Windows/Ati or software accelerated Linux/Mesa drivers yet.

It is possible that iconified windows should be treated differently from normal windows (e.g. the rendering should be suspended). I'll check the MSDN and cook up a solution or workaround.

Thanks for reporting and for the test case!