• 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
BenS1

Strange Text Rendering Crash In Release Mode Only

10 posts in this topic

I've got a strange crash that only occurs in a Release build and so its very difficult to debug the problem.

 

If I try single stepping through the code, the current instruction jumps around all over the place (Presumably the code is being reordered as an optimization), and when the crash does occur the debugger put the current instructions somewhere completely illogical, I don't think this is a genuine stack corruption issue, I think the debugger just isn't very good when working with release builds.

 

Anyway, getting to the point....

 

The code is a text rendering code (Originally written by MJP), which I've adapted to work with DirectXMath, and sure enough this is where the problem seems to lie.

 

I the text rendering class I have this structure defined:

    struct SpriteDrawData
    {
        XMMATRIX Transform;
        XMFLOAT4 Color; 
        XMFLOAT4 DrawRect;   
    };    

 

Then I have this member that uses this struct:

	SpriteDrawData				m_TextDrawData[constMaxBatchSize];

 

(Where constMaxBatchSize == 1000)

 

Then in the actual code I do this for each character:

 

	m_TextDrawData[currentDraw].Transform = XMMatrixMultiply(transform, XMMatrixTranslation(x_offset, y_offset, 0.0f));
	m_TextDrawData[currentDraw].Color = color;
	m_TextDrawData[currentDraw].DrawRect.x = desc.X;
	m_TextDrawData[currentDraw].DrawRect.y = desc.Y;
	m_TextDrawData[currentDraw].DrawRect.z = desc.Width;
	m_TextDrawData[currentDraw].DrawRect.w = desc.Height;            
	currentDraw++;

 

However, the first line causes a crash.

If I comment out the first line then it doesn't crash.

 

I've even tried changing it to this:

	m_TextDrawData[currentDraw].Transform =	XMMatrixIdentity();
	m_TextDrawData[currentDraw].Color = color;
	m_TextDrawData[currentDraw].DrawRect.x = desc.X;
	m_TextDrawData[currentDraw].DrawRect.y = desc.Y;
	m_TextDrawData[currentDraw].DrawRect.z = desc.Width;
	m_TextDrawData[currentDraw].DrawRect.w = desc.Height;            
	currentDraw++;

 

But it still crashes (Even when currentDraw == 0, so its not that currentDraw is overflowing).

 

I thought it might be an alignment problem, so I changed the structure definition to:

    __declspec(align(32)) struct SpriteDrawData
    {
        XMMATRIX Transform;
        XMFLOAT4 Color; 
        XMFLOAT4 DrawRect;   
    }; 

 

But that didn't help, and I don't think its necessary as XMMATRIX is already defined with 16 byte alignment.

 

Its really strange. It doesn't do it in a debug build, and if I comment out the code then the release build is fine too but obviously I don't see any text.

 

Any help would be appreciated.

 

Thanks

Ben

0

Share this post


Link to post
Share on other sites

The only thing I can think of, your SpriteDrawData m_TextDrawData[constMaxBatchSize]; is in dynamic memory, which might not be aligned. To allocate aligned memory use _aligned_malloc() instead of new().

2

Share this post


Link to post
Share on other sites

Thanks Zaoshi

 

Is it really in dynamic memory though? I don't use new (Or malloc) its simply a class member (Not a pointer).of DX11SpriteRenderer, and I have only one instance of DX11SpriteRenderer which is a member of my Renderer class. The Renderer class is a singleton so it is created with a new though.

 

I could try changing SpriteDrawData m_TextDrawData[constMaxBatchSize] to a pointer and using an aligned malloc (Actually, IIRC you can do an aligned new too?).

 

Interestingly, I've just created x64 versions (Which surprisingly didn't require any code changes at all) and now both the debug and release versions work fine. This fuels the theory that its an alignment issue as I believe x86 defaults to 8 byte alignment whereas x64 defaults to 16 byte alignment.

 

I'll try the aligned malloc.

 

Thanks

Ben

0

Share this post


Link to post
Share on other sites

This is an alignment issue. You can also try pragma directive to pack everything to 1 byte boundary and then build your code.

Also, to check if this really is the issue, simply look for the value of sizeof(SpriteDrawData).

1

Share this post


Link to post
Share on other sites

Ok, it is clearly an alignment issue. I added this:

 

	g_Logger.DebugOut("Address of Matrix = %p, alignment = %d", &m_TextDrawData[currentDraw].Transform, __alignof(SpriteDrawData));

	m_TextDrawData[currentDraw].Transform = XMMatrixMultiply(transform, XMMatrixTranslation(x_offset, y_offset, 0.0f));

 

And it output "Address of Matrix = 008A06F8, alignment = 16"

 

As you can see, it says the alignment is 16 but the actual address of the matrix is not on a 16 byte boundary.

 

The question is why. XMMATRIX is defined as "__declspec(align(16)) struct XMMATRIX", which the documentation suggests will cause it to be allocated on 16 byte boundary's. The docs also say it will be inherited, so as struct SpriteDrawData includes an XMMATRIX member the SpriteDrawData itself will be aligned accordingly to honor its alignment requirements. But clearly that's not happening here.

 

From doing some reading around today it appears that pragma pack actually only manages the packing within a structure but doesn't actually ensure that the structure is aligned to a multiple of the pack value.

 

Thanks

Ben

0

Share this post


Link to post
Share on other sites

x64 indeed has 16 byte alignment for new() calls.

 

Since you create Renderer using new() it might not be aligned, so you could try using _aligned_malloc() and it should fix it.

1

Share this post


Link to post
Share on other sites

Thanks again Zaoshi,

 

If I use _aligned_malloc then it'll allocate the memory, but it won't called the constructors and all the other stuff that new does. Similarly with free vs delete.

 

I've actually given up on the alignment and worked around the problem by changing from using XMMATRIX (Which requires alignment) to using XMFLOAT4X4 (Which doesn't require alignment) and using the Load/Store functions to convert between them if and where necessary. There doesn't seem to be any performance impact at all.

 

Thanks everyone for your help.

 

Ben

0

Share this post


Link to post
Share on other sites
Renderer *renderer = (Renderer*)_aligned_malloc(sizeof(Renderer), 16); // allocate
new (&renderer) Renderer(); // manually call constructor
1

Share this post


Link to post
Share on other sites

Thanks again Zaoshi

 

Can I then free the memory using delete, or does it have to be freed using free?

 

If it can be freed using delete then it'll allow me to use a smart pointer (I don't like using naked raw pointers if I can help it).

 

Thanks

Ben

0

Share this post


Link to post
Share on other sites

Since you used _aligned_malloc you'll have to use _aligned_free.

renderer->~Renderer(); // manually call destructor_aligned_free(renderer);


Edit:
You could overload new and delete operators as well, then you won't have to call constructor/destructor manually, and it allows use of smart pointers.:

struct AlignedAllocator {
	void* operator new(size_t size) {
		return _aligned_malloc(size, 16);
	}

	void operator delete(void *ptr) {
		_aligned_free(ptr);
	}
};

struct Renderer : AlignedAllocator {
	// ...
};

 

 

2

Share this post


Link to post
Share on other sites

Thanks again Zaoshi

 

The XMFLOAT4X4 solution is working for me, but I'll keep this in mind for future.

 

Of course I could create my own RAII wrapper that acts as a smart pointer for aligned mallocs and frees.

 

Thanks

Ben

0

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