Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Managed

my c# to managed c++ code is too slow...

This topic is 5154 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 all... i (converted/ported/changed the syntax of) my c# d3d renderer class to managed c++...but it is too slow when rendering nothing... in c#...when i render nothing...i get 360+ frames per second...but in managed c++...i get 60-61 frames per second... can anyone please look at the source code below and tell me whats wrong...thanx in advance...
#pragma once

#include "Camera.h"
#include "Font.h"
#include "OctTree.h"
#include "Utilities.h"

namespace Graphic
{
	using namespace System;
	using namespace System::Collections;
	using namespace System::Drawing;
	using namespace System::Threading;
	using namespace System::Windows::Forms;

	using namespace Microsoft::DirectX;
	using namespace Microsoft::DirectX::Direct3D;

	public __gc class Renderer
	{
	public:
	        static Device* device;
	        static PresentParameters* presentParameters __gc[];
		static RenderStates* renderStates;
		static SamplerStates* samplerStates;
		static TextureStates* textureStates;
	        static CreateFlags createFlags;
	        static BehaviorFlags behaviorFlags;
	        static Caps deviceCaps;
	        static Form* renderTarget;

	public:
		static DateTime lastTime;
	        static __int32 frames = 0;
	        static __int32 framesPerSecond = 0;
	        static __int32 appPausedCount = 0;
	        static bool paused = false;
	        static bool ready = false;

	public:
		static Hashtable* gameConfig;

	public:
		static void LoadGameConfig()
		{
			gameConfig = new Hashtable();

            		gameConfig->Add(S"is32Bit", __box(true));
            		gameConfig->Add(S"isHalTL", __box(true));
            		gameConfig->Add(S"isWindowed", __box(true));
            		gameConfig->Add(S"screenWidth", __box(640));
            		gameConfig->Add(S"screenHeight", __box(480));
		}

		static void InitializeDevice()
		{
            		LoadGameConfig();

            		renderTarget = new Form();

			renderTarget->ClientSize = Size(*dynamic_cast<__box int*>(gameConfig->get_Item(S"screenWidth")), *dynamic_cast<__box int*>(gameConfig->get_Item(S"screenHeight")));
			renderTarget->FormBorderStyle = FormBorderStyle::Fixed3D;
            		renderTarget->MinimizeBox = false;
            		renderTarget->MaximizeBox = false;
			renderTarget->StartPosition = FormStartPosition::CenterScreen;
            		renderTarget->Text = S"";

            		PresentParameters* d3dPP __gc[] = {new PresentParameters()};

			d3dPP[0]->AutoDepthStencilFormat = DepthFormat::D16;
            		d3dPP[0]->BackBufferCount = 1;
			d3dPP[0]->BackBufferFormat = *dynamic_cast<__box bool*>(gameConfig->get_Item(S"is32Bit")) ? Format::X8R8G8B8 : Format::R5G6B5;
            		d3dPP[0]->BackBufferHeight = *dynamic_cast<__box int*>(gameConfig->get_Item(S"screenHeight"));
            		d3dPP[0]->BackBufferWidth = *dynamic_cast<__box int*>(gameConfig->get_Item(S"screenWidth"));
            		d3dPP[0]->DeviceWindow = renderTarget;
            		d3dPP[0]->DeviceWindowHandle = renderTarget->Handle;
            		d3dPP[0]->EnableAutoDepthStencil = true;
			d3dPP[0]->ForceNoMultiThreadedFlag = false;
			d3dPP[0]->FullScreenRefreshRateInHz = 0;
			d3dPP[0]->MultiSample = MultiSampleType::None;
			d3dPP[0]->MultiSampleQuality = 0;
			d3dPP[0]->PresentationInterval = PresentInterval::Default;
			d3dPP[0]->PresentFlag = PresentFlag::None;
			d3dPP[0]->SwapEffect = SwapEffect::Discard;
			d3dPP[0]->Windowed = *dynamic_cast<__box bool*>(gameConfig->get_Item(S"isWindowed"));

			presentParameters = d3dPP;

			createFlags = *dynamic_cast<__box bool*>(gameConfig->get_Item(S"isHalTL")) ? CreateFlags::HardwareVertexProcessing : CreateFlags::SoftwareVertexProcessing;
            		behaviorFlags = BehaviorFlags(createFlags);

           		try
            		{
				device = new Device(0, DeviceType::Hardware, presentParameters[0]->DeviceWindow, createFlags, presentParameters);
 
                		renderStates = device->RenderState;
                		samplerStates = device->SamplerState;
                		textureStates = device->TextureState;
                		deviceCaps = device->DeviceCaps;

                		device->add_DeviceLost(new EventHandler(renderTarget, device_DeviceLost));
                		device->add_DeviceReset(new EventHandler(renderTarget, device_DeviceReset));
                		device->add_Disposing(new EventHandler(renderTarget, device_Disposing));

				device_DeviceCreated(NULL, NULL);
				device_DeviceReset(NULL, NULL);

                		ready = true;
            		}
            		catch(Exception* exception)
            		{
                		device_DeviceLost(NULL, NULL);
				device_Disposing(NULL, NULL);

                		throw exception;
            		}
		}

	        static void device_DeviceCreated(Object* sender, EventArgs* e)
        	{
        	}

	        static void device_DeviceLost(Object* sender, EventArgs* e)
        	{
        	}

	        static void device_DeviceReset(Object* sender, EventArgs* e)
		{
		}

	        static void device_Disposing(Object* sender, EventArgs* e)
        	{
        	}

		static void Start()
		{
            		try
            		{
                		renderTarget->Show();

                		while(renderTarget->Created)
                		{
                    			if(renderTarget->Focused)
                    			{
                        			DrawScene();
                    			}
                    			else
                    			{
						Thread::Sleep(100);
                    			}

					Application::DoEvents();
                		}
            		}
            		catch(Exception* exception)
            		{
#pragma push_macro("MessageBox")
#undef MessageBox
                		MessageBox::Show(exception->ToString(), S"Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
#pragma pop_macro("MessageBox")
            		}
		}

        	static void FrameMove()
        	{
        	}

		static void DrawScene()
		{
            		if(ready)
            		{
                		try
                		{
                    			device->TestCooperativeLevel();

                    			FrameMove();

                    			device->BeginScene();
                    			device->Clear((ClearFlags)(ClearFlags::Target | ClearFlags::ZBuffer), 0, 1.0f, 0);

                    			UpdateFPSCounter();

					device->EndScene();
                    			device->Present();
		                }
        		        catch(DeviceLostException*)
                		{
                    			return;
                		}
                		catch(DeviceNotResetException*)
                		{
                    			device->Reset(presentParameters);
                		}
            		}
		}

		static void UpdateFPSCounter()
		{
	        	frames += 1;

			if(lastTime.Second < DateTime::Now.Second)
            		{
                		framesPerSecond = frames;
                		frames = 0;
				lastTime = DateTime::Now;

				renderTarget->Text = framesPerSecond.ToString();
            		}
		}
	};
}
 

Share this post


Link to post
Share on other sites
Advertisement
How about no?

Run the profiler over your code and see where the bottleneck is. This isn''t a "Here is the code, please fix my bugs NOW!" forum. Pinpoint the problem of your code yourself and come back.

Toolmaker



My site
/* -Earth is 98% full. Please delete anybody you can.*/

Share this post


Link to post
Share on other sites
First, use [ source ] and [ /source ] tags (without spaces) around your source code. Also, isn''t it a little early to worry about optimizing? You''re probably just limited to the monitor refresh rate; try changing your video driver''s or D3D settings.

Share this post


Link to post
Share on other sites
ok..first thanx for quick replay... :D

@toolmaker:
ok...i will look in the profiler...

i didnt mean "here is the code, go and fix it"...if u understood my post that way than u r fucking sensitive... :D (no offend)

@nagromo:
i thought it is [ code ][ /code ]...i forgot...thanx for reminding...

and no...vertical sync is off...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
think you are waiting for vsync:
d3dPP[0]->PresentationInterval = PresentInterval::Default;

use D3DPRESENT_INTERVAL_IMMEDIATE instead, D3DPRESENT_INTERVAL_DEFAULT == D3DPRESENT_INTERVAL_ONE, at least in regular c++.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
it works... ...thanx...

reached 423 frames per second...

thanx Anonymous Poster...

Share this post


Link to post
Share on other sites

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