Jump to content
  • Advertisement
Sign in to follow this  
g0nzo

C# and MDX questions

This topic is 4512 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!