D3DGraphicContextProvider::D3DGraphicContextProvider(D3DDisplayWindowProvider *window, const DisplayWindowDescription &display_desc)
:	window(window),
	current_prim_array_provider(0),
	current_program_provider(0),
	input_layout_set(false)
{
//	set_blend_color(Colorf::black);
//	set_blend_function(blend_one, blend_zero, blend_one, blend_zero);
//	enable_blending(false);

	default_depth = display_desc.get_depth_size();

	Size viewport_size = get_display_window_size();
	for (int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
	{
		viewports[i].Width = viewport_size.width;
		viewports[i].Height = viewport_size.height;
		viewports[i].TopLeftX = 0;
		viewports[i].TopLeftY = 0;
		viewports[i].MinDepth = 0.0f;
		viewports[i].MaxDepth = 1.0f;
	}
	window->get_device_context()->RSSetViewports(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, viewports);

	for (int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
	{
		scissor_rects[i].left = 0;
		scissor_rects[i].top = 0;
		scissor_rects[i].right = 0;
		scissor_rects[i].bottom = 0;
	}

	for (int i = 0; i < shadertype_num_types; i++)
		shader_bound[i] = false;

	set_default_dsv();

	SharedGCData::add_provider(this);
}
	void OpenGLCreationHelper::set_multisampling_pixel_format(const DisplayWindowDescription &desc)
	{
		PIXELFORMATDESCRIPTOR pfd;
		memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
		pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
		pfd.nVersion = 1;
		pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
		pfd.iPixelType = PFD_TYPE_RGBA;
		pfd.dwFlags |= PFD_DOUBLEBUFFER;
		pfd.cColorBits = 24;
		pfd.cRedBits = 4;
		pfd.cGreenBits = 4;
		pfd.cBlueBits = 4;
		pfd.cAlphaBits = 4;
		pfd.cDepthBits = desc.get_depth_size();
		pfd.cStencilBits = desc.get_stencil_size();
		if (desc.is_layered())
		{
			pfd.cAlphaBits = 8;
			pfd.dwFlags |= PFD_DOUBLEBUFFER_DONTCARE; // | PFD_DRAW_TO_BITMAP
		}

		if (desc.get_multisampling() < 1)
		{
			int pixelformat = ChoosePixelFormat(hdc, &pfd);
			SetPixelFormat(hdc, pixelformat, &pfd);
		}
		else
		{
			int pixelformat = ChoosePixelFormat(hdc, &pfd);

			set_active();
			ptr_wglChoosePixelFormatEXT wglChoosePixelFormatEXT = (ptr_wglChoosePixelFormatEXT)wglGetProcAddress("wglChoosePixelFormatEXT");
			if (wglChoosePixelFormatEXT == 0)
				wglChoosePixelFormatEXT = (ptr_wglChoosePixelFormatEXT)wglGetProcAddress("wglChoosePixelFormatARB");
			reset_active();

			if (wglChoosePixelFormatEXT)
			{
				std::vector<FLOAT> float_attributes;
				std::vector<int> int_attributes;

				int_attributes.push_back(WGL_DRAW_TO_WINDOW);
				int_attributes.push_back(GL_TRUE);
				int_attributes.push_back(WGL_ACCELERATION);
				int_attributes.push_back(WGL_FULL_ACCELERATION);

				int_attributes.push_back(WGL_DOUBLE_BUFFER);
				int_attributes.push_back(GL_TRUE);

				int_attributes.push_back(WGL_COLOR_BITS);
				int_attributes.push_back(4 + 4 + 4);

				int_attributes.push_back(WGL_ALPHA_BITS);
				int_attributes.push_back(4);

				int_attributes.push_back(WGL_DEPTH_BITS);
				int_attributes.push_back(desc.get_depth_size());

				int_attributes.push_back(WGL_STENCIL_BITS);
				int_attributes.push_back(desc.get_stencil_size());

				int_attributes.push_back(WGL_SAMPLE_BUFFERS);
				int_attributes.push_back(GL_TRUE);
				int_attributes.push_back(WGL_SAMPLES);
				int_attributes.push_back(desc.get_multisampling());

				float_attributes.push_back(0.0f);
				float_attributes.push_back(0.0f);
				int_attributes.push_back(0);
				int_attributes.push_back(0);
				int new_pixelformat = pixelformat;
				UINT num_formats = 0;
				BOOL result = wglChoosePixelFormatEXT(hdc, &int_attributes[0], &float_attributes[0], 1, &new_pixelformat, &num_formats);
				if (result == TRUE && num_formats > 0)
					pixelformat = new_pixelformat;
			}

			SetPixelFormat(hdc, pixelformat, &pfd);
		}
	}