//static
// Returns max setting for TextureMemory (in MB)
S32 LLViewerImageList::getMaxVideoRamSetting(bool get_recommended)
{
	S32 max_texmem;
	if (gGLManager.mVRAM != 0)
	{
		// Treat any card with < 32 MB (shudder) as having 32 MB
		//  - it's going to be swapping constantly regardless
		S32 max_vram = gGLManager.mVRAM;
		max_vram = llmax(max_vram, getMinVideoRamSetting());
		max_texmem = max_vram;
		if (!get_recommended)
			max_texmem *= 2;
	}
	else
	{
		if (get_recommended)
			max_texmem = 128;
		else
			max_texmem = 512;
		llwarns << "VRAM amount not detected, defaulting to " << max_texmem << " MB" << llendl;
	}

	S32 system_ram = (S32)BYTES_TO_MEGA_BYTES(gSysMemory.getPhysicalMemoryClamped()); // In MB
	//llinfos << "*** DETECTED " << system_ram << " MB of system memory." << llendl;
	if (get_recommended)
		max_texmem = llmin(max_texmem, (S32)(system_ram/2));
	else
		max_texmem = llmin(max_texmem, (S32)(system_ram));
		
	max_texmem = llclamp(max_texmem, getMinVideoRamSetting(), MAX_VIDEO_RAM_IN_MEGA_BYTES); 
	
	return max_texmem;
}
void LLViewerImageList::updateMaxResidentTexMem(S32 mem)
{
	// Initialize the image pipeline VRAM settings
	S32 cur_mem = gSavedSettings.getS32("TextureMemory");
	F32 mem_multiplier = gSavedSettings.getF32("RenderTextureMemoryMultiple");
	S32 default_mem = getMaxVideoRamSetting(true); // recommended default
	if (mem == 0)
	{
		mem = cur_mem > 0 ? cur_mem : default_mem;
	}
	else if (mem < 0)
	{
		mem = default_mem;
	}

	// limit the texture memory to a multiple of the default if we've found some cards to behave poorly otherwise
	mem = llmin(mem, (S32) (mem_multiplier * (F32) default_mem));

	mem = llclamp(mem, getMinVideoRamSetting(), getMaxVideoRamSetting());
	if (mem != cur_mem)
	{
		gSavedSettings.setS32("TextureMemory", mem);
		return; //listener will re-enter this function
	}

	// TODO: set available resident texture mem based on use by other subsystems
	// currently max(12MB, VRAM/4) assumed...
	
	S32 vb_mem = mem;
	S32 fb_mem = llmax(VIDEO_CARD_FRAMEBUFFER_MEM, vb_mem/4);
	mMaxResidentTexMem = (vb_mem - fb_mem)<<20;
	
	llinfos << "Total Video Memory set to: " << vb_mem << " MB" << llendl;
	llinfos << "Available Texture Memory set to: " << (vb_mem - fb_mem) << " MB" << llendl;
}