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

Understanding XMVECTOR parameter passing to own functions

3 posts in this topic

Hi - I am having a bit of difficulty understanding exactly what the documentation is telling me on this... I am using Visual C++ 2010 express compiling for 32 or 64 bit implementation (Windows 7). I am using the XNA Math library (xnamath.h) and wish to write some of my own functions to handle more complex collision detection where I pass XMVECTOR parameters (going further than xnacollision.h). As I understand it, to achieve reasonable optimization, I should:

1) inline the functions

      ** This is because standard function call conventions are not great at allowing register passing of SIMD registers, so call overhead best avoided if poss...?**

2) specify _fastcall in function declaration.

3) pass FXMVECTOR for 1st 3 args and CXMVECTOR thereafter.

      ** 2 & 3 give the best chance of an optimal transfer of values within the SIMD registers if the inline is ignored by the compiler? **

      ** For x64 the docs seem to imply no possibility of SIMD register passing in a function call - so I have to hope it inlines... is that right? **

Thanks for any comments/explanations/insight.

0

Share this post


Link to post
Share on other sites

I actualy tried to benchmark some DirectXMath code,one part was doing "unoptimized" functions,by taking XMFLOAT4X4 and XMFLOAT4 as arguments,then loading them into XMVECTOR/XMMATRIX,then doing operations,then storing again,passing again to another function that loads again and then stores again and outputs to the progra.The other test functions were doing the same operations,but with only loading/unloading when it has to output,otherwise taking CXMMATRIX and FXMVECTOR as arguments and being as fast as possible.Even with millions of calls,the "optimized" version was only slightly faster than the "unoptimized" one.

2

Share this post


Link to post
Share on other sites

Maybe I'm doing it wrong?

Profiler.hpp

#pragma once

#ifndef _PROFILER_HPP_
#define _PROFILER_HPP_

#pragma comment (lib,"Winmm.lib")

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>

class Profiler
{
public:
	static Profiler* Instance();
	static void Destroy();

	void operator()();
	UINT64 GetDTime()
	{
		return(DTime);
	}

private:
	Profiler();
	~Profiler();

	static Profiler *instance;
	UINT64 lastGameTime;
	UINT64 DTime;
};

#endif 

Benchmarks.hpp

#pragma once

#ifndef _TEST_FUNCTIONS_HPP_
#define _TEST_FUNCTIONS_HPP_

#include <Windows.h>
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <fstream>
#include <ostream>
#include <istream>
#include <DirectXMath.h>
using namespace DirectX;

namespace Benchmark
{
	XMFLOAT4X4 UnoptA(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec);
	XMFLOAT4X4 UnoptB(XMFLOAT4X4 inputMat);

	XMMATRIX OptA(CXMMATRIX inputMat, FXMVECTOR inputVec);
	XMMATRIX OptB(CXMMATRIX inputMat);

	void BenchUnopt(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec, UINT count, std::vector<XMFLOAT4X4> &buffer);
	void BenchOpt(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec, UINT count, std::vector<XMFLOAT4X4> &buffer);
}

#endif

Profiler.cpp

#include "Profiler.hpp"

Profiler *Profiler::instance = nullptr;

Profiler *Profiler::Instance()
{
	if(instance)
	{
		return(instance);
	}
	return(instance = new Profiler);
}

void Profiler::Destroy()
{
	if(instance)
	{
		delete instance;
	}
}

Profiler::Profiler(): DTime(0)
{
	lastGameTime = (UINT64)timeGetTime();
}

Profiler::~Profiler()
{

}

void Profiler::operator()()
{
	UINT64 ticks = (UINT64)timeGetTime();

	DTime = ticks - lastGameTime;

	lastGameTime = ticks;
}

BenchmarksOpt.cpp

#include "Benchmarks.hpp"

namespace Benchmark
{
	XMMATRIX OptA(CXMMATRIX inputMat, FXMVECTOR inputVec)
	{
		return(OptB(XMMatrixMultiply(inputMat, XMMatrixRotationQuaternion(inputVec))));
	}

	XMMATRIX OptB(CXMMATRIX inputMat)
	{
		XMVECTOR determinant;
		return(XMMatrixInverse(&determinant, inputMat));
	}

	void BenchOpt(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec, UINT count, std::vector<XMFLOAT4X4> &buffer)
	{
		XMMATRIX matrixInput = XMLoadFloat4x4(&inputMat);
		XMVECTOR vectorInput = XMLoadFloat4(&inputVec);

		for(UINT i = 0; i < count; i++)
		{
			XMFLOAT4X4 bufferMat;
			XMStoreFloat4x4(&bufferMat, OptA(matrixInput, vectorInput));
			buffer.push_back(bufferMat);
		}
	}
}

BenchmarksUnopt.cpp

#include "Benchmarks.hpp"

namespace Benchmark
{
	XMFLOAT4X4 UnoptA(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec)
	{
		XMMATRIX matrixInput = XMLoadFloat4x4(&inputMat);
		XMVECTOR vectorInput = XMLoadFloat4(&inputVec);
		
		matrixInput = XMMatrixMultiply(matrixInput, XMMatrixRotationQuaternion(vectorInput));

		XMFLOAT4X4 outputMat;
		XMStoreFloat4x4(&outputMat, matrixInput);

		return(UnoptB(outputMat));
	}

	XMFLOAT4X4 UnoptB(XMFLOAT4X4 inputMat)
	{
		XMMATRIX matrixInput = XMLoadFloat4x4(&inputMat);
		XMVECTOR determinant;
		matrixInput = XMMatrixInverse(&determinant, matrixInput);
		XMFLOAT4X4 outputMat;
		XMStoreFloat4x4(&outputMat, matrixInput);
		
		return(outputMat);
	}

	void BenchUnopt(XMFLOAT4X4 inputMat, XMFLOAT4 inputVec, UINT count, std::vector<XMFLOAT4X4> &buffer)
	{
		for(UINT i = 0; i < count; i++)
		{
			buffer.push_back(UnoptA(inputMat, inputVec));
		}
	}
}

main.cpp

#include "Benchmarks.hpp"
#include "Profiler.hpp"

using namespace std;
using namespace Benchmark;

int main()
{
	Profiler &ProfilerRef = *Profiler::Instance();
	UINT count;
	cout<<"Select iteration amount:";
	cin>>count;
	cout<<endl<<"Test will now begin"<<endl<<endl;

	std::vector<XMFLOAT4X4> bufferUnopt;
	std::vector<XMFLOAT4X4> bufferOpt;
	bufferUnopt.reserve(count);
	bufferOpt.reserve(count);

	XMFLOAT4 testVector = XMFLOAT4(1.0f, 3.0f, 2.0f, 4.0f);
	XMFLOAT4X4 testMatrix = XMFLOAT4X4(3.0f,3.0f,3.0f,3.0f,
									   3.0f,3.0f,3.0f,3.0f,
									   3.0f,3.0f,3.0f,3.0f,
									   3.0f,3.0f,3.0f,3.0f);

	ProfilerRef();
	BenchUnopt(testMatrix, testVector, count, bufferUnopt);
	ProfilerRef();
	cout<<"Unoptimized Benchmkar: "<<ProfilerRef.GetDTime()<<endl;

	ProfilerRef();
	BenchOpt(testMatrix, testVector, count, bufferOpt);
	ProfilerRef();
	cout<<"Optimized Benchmkar: "<<ProfilerRef.GetDTime()<<endl;

	ofstream myfile;
	myfile.open ("example.txt");
	for(UINT i = 0; i < bufferOpt.size(); i++)
	{
		myfile<<bufferOpt[i]._11;
	}
	for(UINT i = 0; i < bufferUnopt.size(); i++)
	{
		myfile<<bufferUnopt[i]._11;
	}
	myfile.close();

	return 0;
}

I split the Benchmarks# into 2 .cpp files,so I can get 2 seperate ASM/Machine code lists,the Unopt version always gets over 20 times more output,but the speed difference is almosst 0

1

Share this post


Link to post
Share on other sites

MrHeisenberg - thanks for your observations. I ran your code on my pc (replacing your test matrix with the identity matrix as it was giving error values in my output, 5mm iterations). I see a 12% speed gain for the optimized functions on x64 and 27% gain on x86 compilations. Given I really only care about x64 at this point, I agree, it doesn't seem a big deal... Then I tested: 1) add inline to the optimized functions and 2) declaring them as _fastcall (which the docs recommend) - neither seem to make any difference within the noise of the data for either x86 or x64. I Then refactored the code to eliminate the calls completely for the optimized calculation: to get a total 21% gain in x64 and 33% total gain in x86 which starts to be more significant. I checked my compiler (VC++) setting for inlining functions: it is at the max (/Ob2): if I change the inline declaration to _forceinline (!) I finally get the same effect using functions as if I had refactored the code... hmm maybe some other settings on the compiler are interfering...

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