Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 19 May 2010
Offline Last Active Sep 16 2012 12:49 PM

Topics I've Started

C++ Thread safety

04 August 2012 - 10:29 PM

Hello everyone, I am planning on making a game with C++ and I am wanting it to be multi-threaded. The biggest issue with having multiple threads is making it thread-safe. I know of the many ways to make it safe, but I want to know, what will be the BEST and FASTEST for me. My primary question is what's the fastest, but if you want to list off the pros/cons of each one, then hey, I don't mind learning more :P

Basically, I am going to have 4 threads for loading content, one thread for input/output, one thread for rendering, and one thread for networking. I am going to have a Form class to represent the current Form/Screen. This Form class will be the basis of where I need to start worrying about thread-safety. The Form class will have a HandlePacket,Draw, and HandleInput functions that will each be called from different threads. The form will have variables in there that will be accessed from each thread.

I do know that mutexes are slower than windows EnterCriticalSection/LeaveCriticalSection since the critical section is user mode and mutexes are performed at a kernel mode. But, I am not sure if boost's smart pointers are faster than critical sections. I did try out atomics for a bit, but it actually deadlocked a program for no reason at one point. I also heard of having TLS, which could possibly work.

If I were to use smart pointers, would I make the form a smart pointer, or just the data inside of it? If I were to use TLS, the data wont be synchronized per thread, correct? If I were to use EnterCriticalSection/LeaveCriticalSection, would I have a CriticalSection variable for each variable inside the form, or just one for the form?

I am going for a balance between memory and performance effeciency. So, I don't want the option that takes a ton of memory but is fast, and I don't want the option that takes no memory, but is slow. A good balance would be ideal for me.

I would greatly appreciate a pros/cons list of my options because going from website to website, someone seems to say something different. It would help set me straight on what works and what doesn't. Thank you in advance.


11 July 2012 - 08:24 PM

The topic title about says it all, does SharpDX offer a D3DFVF_TEXCOORDSIZEN enum? If not, I'm sure I could just calculate the numbers myself and even make an enum myself, just wanting to be proper and use it if they had it.

Spritesheets vs Individual Sprites

09 July 2012 - 10:41 PM

Hello, I am aware that there is a topic that was very similar to this, but it is really old, so I thought I would start a new one to get new ideas/opinions.
The question that I am asking is what is better to do, use spritesheets or individual sprites, or what is a perfect balance between the two?

I am currently working on a game, and I want to make sure that it is effecient. I actually split up all the characters/npcs/etc, and then halfway through I realized how costly it is to do that. I am using DirectX9, and I believe even with OpenGL, using SetTexture is very costly. The pros of having individual tiles in their own image file is that you won't use as much memory because you only have loaded what you needed. It is very ineffecient because SetTexture is costly and it loses a lot of performance.

Using one big tileset is also a bad idea because some graphics cards have a texture maximum size and if you're tileset is big enough(which is very possible) then you're application will not be compatible with certain people's computers. It also uses a lot of memory to do this because you have all the tiles in memory but you are probably only using 5-10% of the actual loaded textures. A pro of this is that it will be effecient performance-wise because you only have to call SetTexture once and you just clip what you need.

I believe that to be the most effecient, I will need to create a perfect balance of the two. I am thinking that I want to make each texture a 512x512 size and once it gets to that size, then I will create a second texture to continue on. Most graphics cards will be able to support a 512x512 texture, it gives you a performance boost because the spritesheets will be split up by category, so I will be calling SetTexture once, render category, and move on. It is a bit of a memory waste, but not as much as the one big tileset would be. I would much prefer to have my application render faster than worry about memory as much. Most computers can handle the memory, and as long as it isn't ridiculous, then it will be fine.

What are your guys' thoughts on this? This is meant to be a conceptual topic, but it seems like it is leaning towards my problem. I just want to hear what you guys think and maybe that will help influence me to reach a conclusion about my problem. And so you guys know, tilesheet = spritesheet, tiles = sprites. I use them interchangeably for some reason.

VB.NET User Control overrides DirectX rendering

21 June 2012 - 09:35 AM

Hello, the title isn't the most specific but here's an overview of what I meant. I have a UserControl that I am going to render DirectX to. It is going to be a character preview for this game I am creating. The problem is that whenever OnPaint is called, I will render the texture onto it, and then at the end, it turns the control back to the backcolor. I have even disabled OnPaintBackground. It is really confusing for me, and any help would be great. I actually saw a topic very similar to this and there was one suggestion that it was SharpDX/SlimDX's fault.

    Partial Public Class TestPictureBox : Inherits UserControl
	    Public Sub New()
		    SetStyle(ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.AllPaintingInWmPaint, True)
	    End Sub
	    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
	    End Sub
	    Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
	    End Sub
    End Class

DrawNewCharacterSprite renders the DirectX texture, and it does fine, it just gets overriden by User Control.
Here's what happens when I run it:
1, 2, Character Drawn, 2.1, Back to back color, 1, 2, Character Drawn, 2.1, Back to back color.
If I use e.Graphics.Draw___ then it is fine and it will render it completely, but it does not pay attention to DirectX.

[SharpDX] Textures are rendered blurry

18 June 2012 - 10:35 PM

I am sure that this topic has probably been posted a billion times, and I have looked around on Google(for quite a while) trying to find the answer to my problem. I am not the best with DirectX, or even OpenGL for that matter. For some reason, there's so many concepts to 3D programming that it overwhelms and confuses me.
I am using SharpDX(Direct3D9 to be exact) to render textures onto the screen. It's just a simple 2D game, so there's not a lot to it. I will show a few segments of my code and I will also list the things that I have tried and that have failed.

Public Sub InitD3D(ByVal form As Form, ByVal windowed As Boolean)
	    direct3D = New Direct3D()
	    Dim deviceSettings As PresentParameters
	    deviceSettings.BackBufferCount = 1
	    deviceSettings.BackBufferWidth = form.ClientSize.Width
	    deviceSettings.BackBufferHeight = form.ClientSize.Height
	    deviceSettings.DeviceWindowHandle = form.Handle
	    deviceSettings.Windowed = windowed
	    deviceSettings.PresentationInterval = PresentInterval.Immediate
	    deviceSettings.SwapEffect = SwapEffect.Discard
	    device = New Device(direct3D, 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, deviceSettings)
	    device.SetTransform(TransformState.Projection, Matrix.OrthoOffCenterLH(0.0!, Convert.ToSingle(form.ClientSize.Width), 0.0!, Convert.ToSingle(form.ClientSize.Height), 0.0!, 1.0!))
	    device.SetTransform(TransformState.View, Matrix.Identity)
	    device.SetTransform(TransformState.World, Matrix.Identity)
	    device.SetTransform(TransformState.Texture1, Matrix.Identity)
	    device.Viewport = New Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height)
	    device.SetRenderState(RenderState.Lighting, False)
	    additionalSwapChains = New Dictionary(Of String, SwapChain)()
    End Sub

    Public Sub LoadTexture(ByVal fileName As String, ByRef texture As Texture)
		    texture = texture.FromFile(device, fileName, Usage.None, Pool.Managed)
	    Catch ex As Exception
		    Throw New Exception("Failed to load texture from " + fileName)
	    End Try
    End Sub
    Public Sub Draw(ByVal texture As Texture, ByVal dx As Integer, ByVal dy As Integer, ByVal dw As Integer, ByVal dh As Integer, Optional ByVal sx As Integer = 0, Optional ByVal sy As Integer = 0)
	    device.VertexFormat = VertexFormat.Position Or VertexFormat.Texture1
	    device.SetTexture(0, texture)

	    Dim left As Single = Convert.ToSingle(dx) - 0.5!
	    Dim right As Single = Convert.ToSingle(dx + dw) - 0.5!
	    Dim bottom As Single = Convert.ToSingle(dy) - 0.5!
	    Dim top As Single = Convert.ToSingle(dy + dh) - 0.5!
	    Dim vertices(4) As CustomTextureVertex
	    vertices(0).position = New Vector3(left, top, 0.0!)
	    vertices(0).texPosition = New Vector2(0.0!, 0.0!)
	    vertices(1).position = New Vector3(right, top, 0.0!)
	    vertices(1).texPosition = New Vector2(1.0!, 0.0!)
	    vertices(2).position = New Vector3(left, bottom, 0.0!)
	    vertices(2).texPosition = New Vector2(0.0!, 1.0!)
	    vertices(3).position = New Vector3(right, bottom, 0.0!)
	    vertices(3).texPosition = New Vector2(1.0!, 1.0!)
	    device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vertices)
    End Sub

Things I have tried:
Adjusting offset by 0.5f, didn't work
The dw & dh is the actual height, not the Powers of Two width/height
The position & texPosition are perfectly fine
Setting minFilter,magFilter,mipFilter,addressU,addressV to different values and nothing(I also placed it in InitD3D and Draw functions in case it mattered)

The last thing I tried could also be my problem because I did not go through and try every possibility, I have just used settings from other help topics.

Thanks for any help, it's greatly appreciated.