How can I create Direct3d 11 renderer using SDL 2.0.9?

Started by
3 comments, last by andamaru 5 years, 5 months ago

Hi, sorry for my English. My comp specs are: Win 8.1, DirectX 11.2, Geforce GTX750 Ti with latest drivers. In my project I must use color blend mode max via SDL_ComposeCustomBlendMode which is supported in SDL 2.0.9 by direct3d11 renderer only. Changing defines in SDL_config.h or SDL_config_windows.h (SDL_VIDEO_RENDER_D3D11 to 1 and SDL_VIDEO_RENDER_D3D to 0) doesn't help. SDL says my system supports direct3d, opengl, opengles2 and software renderers. What should I do to activate direct3d11 renderer so I can use blend mode max?

Advertisement

This is a section of what I did in my own project, if you need more of an example I can post my full class.


SDL_Window* window;
ID3D11Device* device;
ID3D11DeviceContext* context;

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
window = SDL_CreateWindow("Window Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 4800, SDL_WINDOW_SHOWN);

D3D_FEATURE_LEVEL featureLevel;
D3D11CreateDevice(0, D3D_DRIVER_TYPE_HARDWARE, 0, createDeviceFlags, 0, 0, D3D11_SDK_VERSION, &device, &featureLevel, &context));

int width = 0;
int height = 0;
SDL_GetWindowSize(window, &width, &height);

DXGI_SWAP_CHAIN_DESC sd;
sd.BufferDesc.Width = width;
sd.BufferDesc.Height = height;

SDL_SysWMinfo sysWMInfo;
SDL_VERSION(&sysWMInfo.version);
SDL_GetWindowWMInfo(window, &sysWMInfo);

sd.OutputWindow = sysWMInfo.info.win.window;

context->Release();
context = nullptr;

device->Release();
device = nullptr;

SDL_DestroyWindow(window);
window = nullptr;

SDL_Quit();

1) you initialize SDL and create a window the normal SDL way.

2) create your DirectX 11 device normal DirectX way.

What's different is when you create your swap chain, use SDL_GetWindowSize to get the size of the window and use SDL_GetWindowWMInfo to get a reference to the underlined HWND used by the SDL_window.

After that use the swap chain to flip the window and use SDL to poll the window events. 

Could you please post your full class with includes and so on. I'm newbie with directx and it would be great if i can just paste your code into main.cpp and get simple SDL window and dx11 renderer and simple texture to render.


	SDL_Window* window;
	ID3D11Device* device;
	ID3D11DeviceContext* context;
	SDL_Init( SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER );
	window = SDL_CreateWindow( "Window Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN );
	D3D_FEATURE_LEVEL featureLevel;
	D3D11CreateDevice( 0, D3D_DRIVER_TYPE_HARDWARE, 0, D3D11_CREATE_DEVICE_SINGLETHREADED, 0, 0, D3D11_SDK_VERSION, &device, &featureLevel, &context );
	int width = 0;
	int height = 0;
	SDL_GetWindowSize( window, &width, &height );
	DXGI_SWAP_CHAIN_DESC sd;
	sd.BufferDesc.Width = width;
	sd.BufferDesc.Height = height;
	SDL_SysWMinfo sysWMInfo;
	SDL_VERSION( &sysWMInfo.version );
	SDL_GetWindowWMInfo( window, &sysWMInfo );
	sd.OutputWindow = sysWMInfo.info.win.window;
	SDL_Renderer *renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
	string path = SDL_GetBasePath();
	SDL_Surface *surfRed = IMG_Load( ( path + "\\Red.png" ).c_str() );
	SDL_Texture *textRed = SDL_CreateTextureFromSurface( renderer, surfRed );
	SDL_FreeSurface( surfRed );
	SDL_Surface *surfBlue = IMG_Load( ( path + "\\Blue.png" ).c_str() );
	SDL_Texture *textBlue = SDL_CreateTextureFromSurface( renderer, surfBlue );
	SDL_FreeSurface( surfBlue );
	SDL_Rect destRed, destBlue;
	destRed.x = 128;
	destRed.y = 128;
	destBlue.x = 196;
	destBlue.y = 196;
	SDL_QueryTexture( textRed, NULL, NULL, &destRed.w, &destRed.h );
	SDL_QueryTexture( textBlue, NULL, NULL, &destBlue.w, &destBlue.h );
	SDL_BlendMode blendMode = SDL_ComposeCustomBlendMode( SDL_BLENDFACTOR_ONE,
		SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_MAXIMUM, SDL_BLENDFACTOR_ONE,
		SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_MAXIMUM );
	SDL_SetTextureBlendMode( textRed, blendMode );
	SDL_SetTextureBlendMode( textBlue, blendMode );
	SDL_RendererInfo *rendererInfo = new SDL_RendererInfo();
	SDL_GetRendererInfo( renderer, rendererInfo );
	string info = rendererInfo->name;
	SDL_SetWindowTitle( window, info.c_str() );
	SDL_SetRenderDrawColor( renderer, 0, 0, 0, 255 );
	SDL_Event event;
	bool isRunning = true;
	while ( isRunning ) {
		if ( SDL_PollEvent( &event ) ) {
			if ( event.type == SDL_QUIT ) {
				isRunning = false;
			}
		}
		SDL_RenderClear( renderer );
		SDL_RenderCopy( renderer, textRed, NULL, &destRed );
		SDL_RenderCopy( renderer, textBlue, NULL, &destBlue );
		SDL_RenderPresent( renderer );

	}

Cause this code doesn't work for me.

Ignore my previous post, I read your question wrong my solution isn't going to help you. When you create an SDL 2 renderer on windows it should default to DirectX 11, I'm not sure what's happening. I did find a post with a similar question
https://stackoverflow.com/questions/53269015/how-can-i-create-direct3d-11-renderer-using-sdl-2-0-9

(I just noticed that was you, good luck man)

This topic is closed to new replies.

Advertisement