C# and MDX questions

Started by
5 comments, last by g0nzo 17 years, 9 months ago
Hi! I need to write kick ass HDR demo in more or less 2 months and i know almost nothing about c# and computer graphics. I'm trying to understand this example, by adding small parts of the code at a time. So be prepared for lots of newbie questions :) Something to start with: - do i really need to provide code like this:

public void Dispose() {
  if (sceneTexture != null)
  {
    sceneTexture.Dispose();
    sceneTexture = null;
  }
  ...
}

for every texture, mesh, font etc? Doesn't C# suppose to have a garbage collector, so that i don't have to manually delete every object? What will happen if i forgot to dispose i.e. a texture? Won't OS free the memory for me? - how to write the most basic post processing shader in HLSL(i.e. grayscale)? I render the whole scene to a texture and than put it onto the screen. I know how to convert color image into grayscale (float lum = 0.3R + 0.59G + 0.11B; return float4(lum, lum, lum, 1.0f);), but i have no idea how to write the complete shader (how to pass the texture into the shader and how to get RGB color values from it). Thanks in advance, g0nzo
Advertisement
Hi,

For the Dispose thing, it's not necessary, but when a Dispose is available, it sometimes means that an unmanaged resource is used and calling Dispose will release it. In some cases, not calling Dispose will cause the resource to be locked until the end of the application and sometimes until a reboot. So it is a precaution to do it explicitly.

What I do is implement IDisposable and call Dispose in each of my destructors to be sure all my Dispose methods are called before the object is definitly destroyed. So when I use my objects, I don't have to explicitly call Dispose but could do if I must have control on when to call it (because you don't have any control on when the destructor is called (please someone correct me if I'm wrong on this one) except by calling the garbage collector manually which is very discouraged...

I hope it helps

ThunderMusic
ThunderMusic
Thanks!

So not calling dispose won't cause any memory leaks, it just helps freeing currently unused memory?

When creating i.e. a texture one of the parameters is a memory class for the buffer (Pool). Is there some rule of thumb for specifying Pool for textures, meshes and all other 3D related objects? Should they be "managed", "default" or what?
Ok, so more or less i get what Dipose is for.

And what about the post processing shader? If i have code like this:
device.SetTexture(0, finalTexture);device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vertices);


how to write the simplest (it can even just output colors without any modifications) post processing shader? How to pass "finalTexture" to the shader?
My thoughts; I would not trust Microsoft to clean up anything. Any chance you have to initialize and clean up after yourself is the safe way and "correct" way to go.

Thank you.
Gonzo, the garbage collector will handle all of your managed objects for you. What about native objects, though? Taking your example, a texture. When DirectX creates a texture, it allocates space in video memory and/or the graphics driver's memory. That memory is owned by the graphics driver, not C#. C# doesn't know how to access and deallocate that memory.

When you have an object that uses native resources, it's a good idea to write a Dispose() function that will free all the stuff that isn't automatically handled by C#. Otherwise, you will have memory leaks, because you never tell DirectX to release your textures (or GDI and bitmaps, or many other possibilities.)
Thanks!

Is there a way to check for these non-C# memory leaks? Some program that will show me that i forgot to dispose this and that?

[EDIT] I've set debug d3d runtime and now when i set "Break on memory leaks" i get nice "Application error: unknown software exception". Is there a way to see exactly what is not being disposed (i'm using VS 2005)?

[EDIT2] Another dispose question: if a class implements IDisposable interface and defines Dispose method, do i have to call it somewhere manually or is it called automatically? I just found that i'm disposing the same texture in Dispose method and in OnLostDevice event. Now i'm a little bit confused how it should really work.

[Edited by - g0nzo on July 9, 2006 6:36:27 AM]

This topic is closed to new replies.

Advertisement