diff --git a/.hgtags b/.hgtags
index a3058aa2539827fcc16f7066c88e2c5758102976..5fb06c3609c4294763082c53393d27cdc51ef86b 100755
--- a/.hgtags
+++ b/.hgtags
@@ -493,3 +493,4 @@ bcc2770e21c125e0bab59141c51db9145aec068d 3.7.17-release
 2729c1daf0257d68a40bdbc4acf1a16184974bbd 3.7.18-release
 82973b38a6c9a457333e3519e4f2b16bb5eedf47 3.7.19-release
 27094824773b907c2e559396e6f9ec3a963de52d 3.7.20-release
+9ecab4b0c7d8614767724a3422d3c1dca6bd4e4f 3.7.21-release
diff --git a/doc/contributions.txt b/doc/contributions.txt
index 2d27562e37a5cd6413b75d880fb80e464b004a8b..ab5eddd92d1941e3f5de59e2f9f0ccbc2155c571 100755
--- a/doc/contributions.txt
+++ b/doc/contributions.txt
@@ -324,6 +324,7 @@ Cinder Roxley
     STORM-2035
     STORM-2036
     STORM-2037
+    STORM-2053
 Clara Young
 Coaldust Numbers
     VWR-1095
@@ -889,6 +890,8 @@ Mm Alder
 	VWR-4794
 	VWR-13578
 Mo Hax
+Moon Metty
+	STORM-2078
 Mourna Biziou
 Mr Greggan
 	VWR-445
@@ -993,6 +996,7 @@ Nicky Perian
 	STORM-1087
 	STORM-1090
 	STORM-1828
+    STORM-2080
 Nicoladie Gymnast
 NiranV Dean
     STORM-2040
@@ -1053,6 +1057,11 @@ Peekay Semyorka
 	VWR-19
 	VWR-49
 	VWR-79
+Pell Smit
+    STORM-2069
+    STORM-2070
+    STORM-2071
+    STORM-2072
 Peter Lameth
 	VWR-7331
 PeterPunk Mooney
@@ -1396,6 +1405,7 @@ Whirly Fizzle
 	MAINT-873
 	STORM-1930
 	BUG-6659
+	STORM-2078
 Whoops Babii
 	VWR-631
 	VWR-1640
diff --git a/indra/llrender/llshadermgr.cpp b/indra/llrender/llshadermgr.cpp
index a89ec675b4b1a5cf9f9e2fa24474b485fc09d7a9..b2be3cc3b6cd30c2804e6756493f23816a13f01f 100755
--- a/indra/llrender/llshadermgr.cpp
+++ b/indra/llrender/llshadermgr.cpp
@@ -674,12 +674,17 @@ GLhandleARB LLShaderMgr::loadShaderFile(const std::string& filename, S32 & shade
 	if (defines)
 	{
 		for (boost::unordered_map<std::string,std::string>::iterator iter = defines->begin(); iter != defines->end(); ++iter)
-	{
-		std::string define = "#define " + iter->first + " " + iter->second + "\n";
-		text[count++] = (GLcharARB *) strdup(define.c_str());
-	}
+		{
+			std::string define = "#define " + iter->first + " " + iter->second + "\n";
+			text[count++] = (GLcharARB *) strdup(define.c_str());
+		}
 	}
 
+	if( gGLManager.mIsATI )
+	{
+		text[ count++ ] = strdup( "#define IS_AMD_CARD 1\n" );
+	}
+	
 	if (texture_index_channels > 0 && type == GL_FRAGMENT_SHADER_ARB)
 	{
 		//use specified number of texture channels for indexed texture rendering
diff --git a/indra/llwindow/llopenglview-objc.h b/indra/llwindow/llopenglview-objc.h
index f1fab3b2c6a8ad409e8b9ad09252dcd9011977c0..072d40f7391ec81874f2ce83ce8f2417947fc0cc 100644
--- a/indra/llwindow/llopenglview-objc.h
+++ b/indra/llwindow/llopenglview-objc.h
@@ -79,6 +79,7 @@
 @interface LLNonInlineTextView : NSTextView
 {
 	LLOpenGLView *glview;
+    unichar mKeyPressed;
 }
 
 - (void) setGLView:(LLOpenGLView*)view;
diff --git a/indra/llwindow/llopenglview-objc.mm b/indra/llwindow/llopenglview-objc.mm
index f7031341eb283d186940946346486fe435563600..ff312945b6bac5ab3cfc6fd2921a49a490884115 100644
--- a/indra/llwindow/llopenglview-objc.mm
+++ b/indra/llwindow/llopenglview-objc.mm
@@ -104,20 +104,20 @@ attributedStringInfo getSegments(NSAttributedString *str)
 - (unsigned long)getVramSize
 {
     CGLRendererInfoObj info = 0;
-	GLint vram_bytes = 0;
+	GLint vram_mbytes = 0;
     int num_renderers = 0;
     CGLError the_err = CGLQueryRendererInfo (CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay), &info, &num_renderers);
     if(0 == the_err)
     {
-        CGLDescribeRenderer (info, 0, kCGLRPTextureMemory, &vram_bytes);
+        CGLDescribeRenderer (info, 0, kCGLRPTextureMemoryMegabytes, &vram_mbytes);
         CGLDestroyRendererInfo (info);
     }
     else
     {
-        vram_bytes = (256 << 20);
+        vram_mbytes = 256;
     }
     
-	return (unsigned long)vram_bytes / 1048576; // We need this in megabytes.
+	return (unsigned long)vram_mbytes;
 }
 
 - (void)viewDidMoveToWindow
@@ -270,14 +270,14 @@ attributedStringInfo getSegments(NSAttributedString *str)
         !([theEvent modifierFlags] & NSFunctionKeyMask) &&
         !([theEvent modifierFlags] & NSHelpKeyMask))
     {
-        callRightMouseDown(mMousePos, mModifiers);
+        callRightMouseDown(mMousePos, [theEvent modifierFlags]);
         mSimulatedRightClick = true;
     } else {
         if ([theEvent clickCount] >= 2)
         {
-            callDoubleClick(mMousePos, mModifiers);
+            callDoubleClick(mMousePos, [theEvent modifierFlags]);
         } else if ([theEvent clickCount] == 1) {
-            callLeftMouseDown(mMousePos, mModifiers);
+            callLeftMouseDown(mMousePos, [theEvent modifierFlags]);
         }
     }
 }
@@ -286,21 +286,21 @@ attributedStringInfo getSegments(NSAttributedString *str)
 {
     if (mSimulatedRightClick)
     {
-        callRightMouseUp(mMousePos, mModifiers);
+        callRightMouseUp(mMousePos, [theEvent modifierFlags]);
         mSimulatedRightClick = false;
     } else {
-        callLeftMouseUp(mMousePos, mModifiers);
+        callLeftMouseUp(mMousePos, [theEvent modifierFlags]);
     }
 }
 
 - (void) rightMouseDown:(NSEvent *)theEvent
 {
-	callRightMouseDown(mMousePos, mModifiers);
+	callRightMouseDown(mMousePos, [theEvent modifierFlags]);
 }
 
 - (void) rightMouseUp:(NSEvent *)theEvent
 {
-	callRightMouseUp(mMousePos, mModifiers);
+	callRightMouseUp(mMousePos, [theEvent modifierFlags]);
 }
 
 - (void)mouseMoved:(NSEvent *)theEvent
@@ -341,12 +341,12 @@ attributedStringInfo getSegments(NSAttributedString *str)
 
 - (void) otherMouseDown:(NSEvent *)theEvent
 {
-	callMiddleMouseDown(mMousePos, mModifiers);
+	callMiddleMouseDown(mMousePos, [theEvent modifierFlags]);
 }
 
 - (void) otherMouseUp:(NSEvent *)theEvent
 {
-	callMiddleMouseUp(mMousePos, mModifiers);
+	callMiddleMouseUp(mMousePos, [theEvent modifierFlags]);
 }
 
 - (void) otherMouseDragged:(NSEvent *)theEvent
@@ -366,22 +366,27 @@ attributedStringInfo getSegments(NSAttributedString *str)
 
 - (void) keyUp:(NSEvent *)theEvent
 {
-	callKeyUp([theEvent keyCode], mModifiers);
+	callKeyUp([theEvent keyCode], [theEvent modifierFlags]);
 }
 
 - (void) keyDown:(NSEvent *)theEvent
 {
     uint keycode = [theEvent keyCode];
+    // We must not depend on flagsChange event to detect modifier flags changed,
+    // must depend on the modifire flags in the event parameter.
+    // Because flagsChange event handler misses event when other window is activated,
+    // e.g. OS Window for upload something or Input Window...
+    // mModifiers instance variable is for insertText: or insertText:replacementRange:  (by Pell Smit)
+	mModifiers = [theEvent modifierFlags];
     bool acceptsText = mHasMarkedText ? false : callKeyDown(keycode, mModifiers);
+    unichar ch;
     if (acceptsText &&
         !mMarkedTextAllowed &&
+        !(mModifiers & (NSControlKeyMask | NSCommandKeyMask)) &&  // commands don't invoke InputWindow
         ![(LLAppDelegate*)[NSApp delegate] romanScript] &&
-        [[theEvent charactersIgnoringModifiers] characterAtIndex:0] != NSDeleteCharacter &&
-        [[theEvent charactersIgnoringModifiers] characterAtIndex:0] != NSBackspaceCharacter &&
-        [[theEvent charactersIgnoringModifiers] characterAtIndex:0] != NSDownArrowFunctionKey &&
-        [[theEvent charactersIgnoringModifiers] characterAtIndex:0] != NSUpArrowFunctionKey &&
-        [[theEvent charactersIgnoringModifiers] characterAtIndex:0] != NSLeftArrowFunctionKey &&
-        [[theEvent charactersIgnoringModifiers] characterAtIndex:0] != NSRightArrowFunctionKey)
+        (ch = [[theEvent charactersIgnoringModifiers] characterAtIndex:0]) > ' ' &&
+        ch != NSDeleteCharacter &&
+        (ch < 0xF700 || ch > 0xF8FF))  // 0xF700-0xF8FF: reserved for function keys on the keyboard(from NSEvent.h)
     {
         [(LLAppDelegate*)[NSApp delegate] showInputWindow:true withEvent:theEvent];
     } else
@@ -498,31 +503,58 @@ attributedStringInfo getSegments(NSAttributedString *str)
 
 - (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange
 {
-    if ([aString class] == NSClassFromString(@"NSConcreteMutableAttributedString"))
+    // Apple says aString can be either an NSString or NSAttributedString instance.
+    // But actually it's NSConcreteMutableAttributedString or __NSCFConstantString.
+    // I observed aString was __NSCFConstantString only aString was null string(zero length).
+    // Apple also says when aString is an NSString object,
+    // the receiver is expected to render the marked text with distinguishing appearance.
+    // So I tried to make attributedStringInfo, but it won't be used...   (Pell Smit)
+
+    if (mMarkedTextAllowed)
     {
-        if (mMarkedTextAllowed)
+        unsigned int selected[2] = {
+            selectedRange.location,
+            selectedRange.length
+        };
+        
+        unsigned int replacement[2] = {
+            replacementRange.location,
+            replacementRange.length
+        };
+        
+        int string_length = [aString length];
+        unichar text[string_length];
+        attributedStringInfo segments;
+        // I used 'respondsToSelector:@selector(string)'
+        // to judge aString is an attributed string or not.
+        if ([aString respondsToSelector:@selector(string)])
+        {
+            // aString is attibuted
+            [[aString string] getCharacters:text range:NSMakeRange(0, string_length)];
+            segments = getSegments((NSAttributedString *)aString);
+        }
+        else
+        {
+            // aString is not attributed
+            [aString getCharacters:text range:NSMakeRange(0, string_length)];
+            segments.seg_lengths.push_back(string_length);
+            segments.seg_standouts.push_back(true);
+        }
+        setMarkedText(text, selected, replacement, string_length, segments);
+        if (string_length > 0)
         {
-            unsigned int selected[2] = {
-                selectedRange.location,
-                selectedRange.length
-            };
-            
-            unsigned int replacement[2] = {
-                replacementRange.location,
-                replacementRange.length
-            };
-            
-            unichar text[[aString length]];
-            [[aString mutableString] getCharacters:text range:NSMakeRange(0, [aString length])];
-            attributedStringInfo segments = getSegments((NSAttributedString *)aString);
-            setMarkedText(text, selected, replacement, [aString length], segments);
             mHasMarkedText = TRUE;
-            mMarkedTextLength = [aString length];
-        } else {
-            if (mHasMarkedText)
-            {
-                [self unmarkText];
-            }
+            mMarkedTextLength = string_length;
+        }
+        else
+        {
+            // we must clear the marked text when aString is null.
+            [self unmarkText];
+        }
+    } else {
+        if (mHasMarkedText)
+        {
+            [self unmarkText];
         }
     }
 }
@@ -641,37 +673,63 @@ attributedStringInfo getSegments(NSAttributedString *str)
 
 @implementation LLNonInlineTextView
 
+/*  Input Window is a legacy of 20 century, so we want to remove related classes.
+    But unfortunately, Viwer web browser has no support for modern inline input,
+    we need to leave these classes...
+    We will be back to get rid of Input Window after fixing viewer web browser.
+
+    How Input Window should work:
+        1) Input Window must not be empty.
+          It must close when it become empty result of edithing.
+        2) Input Window must not close when it still has input data.
+          It must keep open user types next char before commit.         by Pell Smit
+*/
+
 - (void) setGLView:(LLOpenGLView *)view
 {
 	glview = view;
 }
 
-- (void) insertText:(id)insertString
+- (void)keyDown:(NSEvent *)theEvent
 {
-	[[self inputContext] discardMarkedText];
-    [self setString:@""];
-    [_window orderOut:_window];
-	[self insertText:insertString replacementRange:NSMakeRange(0, [insertString length])];
+    // mKeyPressed is used later to determine whethere Input Window should close or not
+    mKeyPressed = [[theEvent charactersIgnoringModifiers] characterAtIndex:0];
+    // setMarkedText and insertText is called indirectly from inside keyDown: method
+    [super keyDown:theEvent];
 }
 
-- (void) insertText:(id)aString replacementRange:(NSRange)replacementRange
+// setMarkedText: is called for incomplete input(on the way to conversion).
+- (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange
 {
-	[glview insertText:aString replacementRange:replacementRange];
+    [super setMarkedText:aString selectedRange:selectedRange replacementRange:replacementRange];
+    if ([aString length] == 0)      // this means Input Widow becomes empty
+    {
+        [_window orderOut:_window];     // Close this to avoid empty Input Window
+    }
 }
 
-- (void) insertNewline:(id)sender
+// insertText: is called for inserting commited text.
+// There are two ways to be called here:
+//      a) explicitly commited (must close)
+//          In case of user typed commit key(usually return key) or delete key or something
+//      b) automatically commited (must not close)
+//          In case of user typed next letter after conversion
+- (void) insertText:(id)aString replacementRange:(NSRange)replacementRange
 {
-	[[self textStorage] setValue:@""];
-	[[self inputContext] discardMarkedText];
+    [[self inputContext] discardMarkedText];
     [self setString:@""];
-}
-
-- (void)doCommandBySelector:(SEL)aSelector
-{
-	if (aSelector == @selector(insertNewline:))
-	{
-		[self insertNewline:self];
-	}
+    [glview insertText:aString replacementRange:replacementRange];
+    if (mKeyPressed == NSEnterCharacter ||
+        mKeyPressed == NSBackspaceCharacter ||
+        mKeyPressed == NSTabCharacter ||
+        mKeyPressed == NSNewlineCharacter ||
+        mKeyPressed == NSCarriageReturnCharacter ||
+        mKeyPressed == NSDeleteCharacter ||
+        (mKeyPressed >= 0xF700 && mKeyPressed <= 0xF8FF))
+    {
+        // this is case a) of above comment
+        [_window orderOut:_window];     // to avoid empty Input Window
+    }
 }
 
 @end
diff --git a/indra/llwindow/llwindowmacosx.cpp b/indra/llwindow/llwindowmacosx.cpp
index 18d515201566fb0d3d6a9cc97e97323a5e526450..aca8bb652747369bc2a5181141f0ceaad13792e4 100755
--- a/indra/llwindow/llwindowmacosx.cpp
+++ b/indra/llwindow/llwindowmacosx.cpp
@@ -230,7 +230,10 @@ void callFocus()
 
 void callFocusLost()
 {
-	gWindowImplementation->getCallbacks()->handleFocusLost(gWindowImplementation);
+	if (gWindowImplementation)
+	{
+		gWindowImplementation->getCallbacks()->handleFocusLost(gWindowImplementation);
+	}
 }
 
 void callRightMouseDown(float *pos, MASK mask)
@@ -1805,8 +1808,6 @@ static long getDictLong (CFDictionaryRef refDict, CFStringRef key)
 
 void LLWindowMacOSX::allowLanguageTextInput(LLPreeditor *preeditor, BOOL b)
 {
-    allowDirectMarkedTextInput(b, mGLView);
-	
 	if (preeditor != mPreeditor && !b)
 	{
 		// This condition may occur by a call to
@@ -1836,6 +1837,7 @@ void LLWindowMacOSX::allowLanguageTextInput(LLPreeditor *preeditor, BOOL b)
 		return;
 	}
 	mLanguageTextInputAllowed = b;
+    allowDirectMarkedTextInput(b, mGLView); // mLanguageTextInputAllowed and mMarkedTextAllowed should be updated at once (by Pell Smit
 }
 
 void LLWindowMacOSX::interruptLanguageTextInput()
diff --git a/indra/newview/VIEWER_VERSION.txt b/indra/newview/VIEWER_VERSION.txt
index c6cff55cf75f2ee2685968efe2546e65c643cf6f..b49c35452428abb55b5a3ffd333cdbc68a8bb9af 100644
--- a/indra/newview/VIEWER_VERSION.txt
+++ b/indra/newview/VIEWER_VERSION.txt
@@ -1 +1 @@
-3.7.21
+3.7.22
diff --git a/indra/newview/app_settings/shaders/class1/avatar/avatarSkinV.glsl b/indra/newview/app_settings/shaders/class1/avatar/avatarSkinV.glsl
index bc63d07d726ccda9dee47a148166cd0592cea4be..767fad016c956736719590aacb6a6877a8e9fb6d 100755
--- a/indra/newview/app_settings/shaders/class1/avatar/avatarSkinV.glsl
+++ b/indra/newview/app_settings/shaders/class1/avatar/avatarSkinV.glsl
@@ -40,4 +40,10 @@ mat4 getSkinnedTransform()
 	ret[3] = vec4(0,0,0,1);
 
 	return ret;
+
+#ifdef IS_AMD_CARD
+	// If it's AMD make sure the GLSL compiler sees the arrays referenced once by static index. Otherwise it seems to optimise the storage awawy which leads to unfun crashes and artifacts.
+	vec4 dummy1 = matrixPalette[0];
+	vec4 dummy2 = matrixPalette[44];
+#endif
 }
diff --git a/indra/newview/app_settings/shaders/class1/avatar/objectSkinV.glsl b/indra/newview/app_settings/shaders/class1/avatar/objectSkinV.glsl
index 57129c3bd1da536c7b2c915eea6ce875a4d190f9..6cd38d8ef50661c632fbfe7249052ce8237ad097 100755
--- a/indra/newview/app_settings/shaders/class1/avatar/objectSkinV.glsl
+++ b/indra/newview/app_settings/shaders/class1/avatar/objectSkinV.glsl
@@ -63,5 +63,14 @@ mat4 getObjectSkinnedTransform()
 	ret[3] = vec4(trans, 1.0);
 				
 	return ret;
+
+#ifdef IS_AMD_CARD
+   // If it's AMD make sure the GLSL compiler sees the arrays referenced once by static index. Otherwise it seems to optimise the storage awawy which leads to unfun crashes and artifacts.
+   mat3 dummy1 = matrixPalette[0];
+   vec3 dummy2 = translationPalette[0];
+   mat3 dummy3 = matrixPalette[51];
+   vec3 dummy4 = translationPalette[51];
+#endif
+
 }
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/blurLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/blurLightF.glsl
index a4f54dff707936918b25b6519b55ba0824ec7dbb..5264d6e1b4ff3b259c2bc1c4fc72fdd8ccd8eb54 100755
--- a/indra/newview/app_settings/shaders/class1/deferred/blurLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/blurLightF.glsl
@@ -132,5 +132,11 @@ void main()
 	col.y *= col.y;
 	
 	frag_color = col;
+
+#ifdef IS_AMD_CARD
+	// If it's AMD make sure the GLSL compiler sees the arrays referenced once by static index. Otherwise it seems to optimise the storage awawy which leads to unfun crashes and artifacts.
+	vec3 dummy1 = kern[0];
+	vec3 dummy2 = kern[3];
+#endif
 }
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/multiPointLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/multiPointLightF.glsl
index 236567219bf404a660b1faa188a3aa1c90d1fc74..1a464fec5d3a26a2c5835c4c5de9f686bf95fc68 100755
--- a/indra/newview/app_settings/shaders/class1/deferred/multiPointLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/multiPointLightF.glsl
@@ -161,4 +161,12 @@ void main()
 	
 	frag_color.rgb = out_col;
 	frag_color.a = 0.0;
+
+#ifdef IS_AMD_CARD
+	// If it's AMD make sure the GLSL compiler sees the arrays referenced once by static index. Otherwise it seems to optimise the storage awawy which leads to unfun crashes and artifacts.
+	vec4 dummy1 = light[0];
+	vec4 dummy2 = light_col[0];
+	vec4 dummy3 = light[LIGHT_COUNT-1];
+	vec4 dummy4 = light_col[LIGHT_COUNT-1];
+#endif
 }
diff --git a/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl
index 0e6ab80d4d1f33ace536674d4c9ec353677eb6c2..c08e9d361af6b3f6a1b5ba6332b24151431685c9 100755
--- a/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl
@@ -127,13 +127,17 @@ vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 	vec4 ret = texture2DLod(projectionMap, tc, lod);
 	ret.rgb = srgb_to_linear(ret.rgb);
 	
-	vec2 dist = tc-vec2(0.5);
+	vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
 	
-	float det = max(1.0-lod/(proj_lod*0.5), 0.0);
+	float det = min(lod/(proj_lod*0.5), 1.0);
 	
-	float d = dot(dist,dist);
-		
-	ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0)+det, 1.0);
+	float d = min(dist.x, dist.y);
+    
+    d *= min(1, d * (proj_lod - lod));
+	
+	float edge = 0.25*det;
+    
+	ret *= clamp(d/edge, 0.0, 1.0);
 	
 	return ret;
 }
@@ -311,19 +315,17 @@ void main()
 			vec3 pfinal = pos + ref * dot(pdelta, proj_n)/ds;
 			
 			vec4 stc = (proj_mat * vec4(pfinal.xyz, 1.0));
-			stc /= stc.w;
+            
 			if (stc.z > 0.0)
 			{
-				float fatten = clamp(envIntensity*envIntensity+envIntensity*0.25, 0.25, 1.0);
-				
-				stc.xy = (stc.xy - vec2(0.5)) * fatten + vec2(0.5);
+                stc /= stc.w;
 								
 				if (stc.x < 1.0 &&
 					stc.y < 1.0 &&
 					stc.x > 0.0 &&
 					stc.y > 0.0)
 				{
-					col += color.rgb*texture2DLodSpecular(projectionMap, stc.xy, proj_lod).rgb*spec.rgb;										
+					col += color.rgb * texture2DLodSpecular(projectionMap, stc.xy, (1 - spec.a) * (proj_lod * 0.6)).rgb * envIntensity;
 				}
 			}
 		}
diff --git a/indra/newview/app_settings/shaders/class1/deferred/postgiF.glsl b/indra/newview/app_settings/shaders/class1/deferred/postgiF.glsl
index 96f962842468fdf01e73fe5737bd3cfe98c40ca5..018ced4cadb0f108c788cabe0e1e58f2b613ae4c 100755
--- a/indra/newview/app_settings/shaders/class1/deferred/postgiF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/postgiF.glsl
@@ -99,4 +99,10 @@ void main()
 	col = col*col*blur_quad.x + col*blur_quad.y + blur_quad.z;
 	
 	frag_color.rgb = col;
+
+#ifdef IS_AMD_CARD
+	// If it's AMD make sure the GLSL compiler sees the arrays referenced once by static index. Otherwise it seems to optimise the storage awawy which leads to unfun crashes and artifacts.
+	vec2 dummy1 = kern[0];
+	vec2 dummy2 = kern[31];
+#endif
 }
diff --git a/indra/newview/app_settings/shaders/class1/deferred/spotLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/spotLightF.glsl
index 8d8a6c9dde2bee4074dae24eb82daf56f27360ad..f50635a1390bc38e6fa8e968bbc39780aa7d1241 100755
--- a/indra/newview/app_settings/shaders/class1/deferred/spotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/spotLightF.glsl
@@ -130,15 +130,19 @@ vec4 correctWithGamma(vec4 col)
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
 	vec4 ret = texture2DLod(projectionMap, tc, lod);
-	ret = correctWithGamma(ret);
+	ret.rgb = srgb_to_linear(ret.rgb);
 	
-	vec2 dist = tc-vec2(0.5);
+	vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
 	
-	float det = max(1.0-lod/(proj_lod*0.5), 0.0);
+	float det = min(lod/(proj_lod*0.5), 1.0);
 	
-	float d = dot(dist,dist);
-		
-	ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0)+det, 1.0);
+	float d = min(dist.x, dist.y);
+    
+    d *= min(1, d * (proj_lod - lod));
+	
+	float edge = 0.25*det;
+    
+	ret *= clamp(d/edge, 0.0, 1.0);
 	
 	return ret;
 }
@@ -322,19 +326,14 @@ void main()
 
 			if (stc.z > 0.0)
 			{
-				stc.xy /= stc.w;
-
-				float fatten = clamp(envIntensity*envIntensity+envIntensity*0.5, 0.25, 1.0);
-				
-				//stc.xy = (stc.xy - vec2(0.5)) * fatten + vec2(0.5);
-				stc.xy = (stc.xy - vec2(0.5)) * fatten + vec2(0.5);
+				stc /= stc.w;
 								
 				if (stc.x < 1.0 &&
 					stc.y < 1.0 &&
 					stc.x > 0.0 &&
 					stc.y > 0.0)
 				{
-					col += color.rgb*texture2DLodSpecular(projectionMap, stc.xy, proj_lod-envIntensity*proj_lod).rgb*spec.rgb;										
+					col += color.rgb * texture2DLodSpecular(projectionMap, stc.xy, (1 - spec.a) * (proj_lod * 0.6)).rgb * envIntensity;
 				}
 			}
 		}
diff --git a/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
index c20e00163c64ed1649d48fabb88a269baabb7e99..94bd07bec667822d05f2668ba199e4abf3053d72 100755
--- a/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
@@ -131,15 +131,19 @@ vec4 correctWithGamma(vec4 col)
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
 	vec4 ret = texture2DLod(projectionMap, tc, lod);
-	ret = correctWithGamma(ret);
-
-	vec2 dist = tc-vec2(0.5);
+	ret.rgb = srgb_to_linear(ret.rgb);
 	
-	float det = max(1.0-lod/(proj_lod*0.5), 0.0);
+	vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
 	
-	float d = dot(dist,dist);
-		
-	ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0)+det, 1.0);
+	float det = min(lod/(proj_lod*0.5), 1.0);
+	
+	float d = min(dist.x, dist.y);
+    
+    d *= min(1, d * (proj_lod - lod));
+	
+	float edge = 0.25*det;
+    
+	ret *= clamp(d/edge, 0.0, 1.0);
 	
 	return ret;
 }
@@ -334,25 +338,21 @@ void main()
 			vec3 pfinal = pos + ref * dot(pdelta, proj_n)/ds;
 			
 			vec4 stc = (proj_mat * vec4(pfinal.xyz, 1.0));
-			stc /= stc.w;
 
 			if (stc.z > 0.0)
 			{
-				float fatten = clamp(envIntensity*envIntensity+envIntensity*0.25, 0.25, 1.0);
-
-				stc.xy = (stc.xy - vec2(0.5)) * fatten + vec2(0.5);
+                stc /= stc.w;
 								
 				if (stc.x < 1.0 &&
 					stc.y < 1.0 &&
 					stc.x > 0.0 &&
 					stc.y > 0.0)
 				{
-					col += color.rgb*texture2DLodSpecular(projectionMap, stc.xy, proj_lod).rgb*shadow*spec.rgb;										
+					col += color.rgb * texture2DLodSpecular(projectionMap, stc.xy, (1 - spec.a) * (proj_lod * 0.6)).rgb * shadow * envIntensity;
 				}
 			}
 		}
 	}
-	
 
 	//not sure why, but this line prevents MATBUG-194
 	col = max(col, vec3(0.0));
diff --git a/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
index 7689b72d202a72ad343b4f1895ca0deb8d454c6b..92e1ac95a69c410678b57a4515e9f9fa26431aa8 100755
--- a/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
@@ -131,15 +131,19 @@ vec4 correctWithGamma(vec4 col)
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
 	vec4 ret = texture2DLod(projectionMap, tc, lod);
-	ret = correctWithGamma(ret);
+	ret.rgb = srgb_to_linear(ret.rgb);
 	
-	vec2 dist = tc-vec2(0.5);
+	vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
 	
-	float det = max(1.0-lod/(proj_lod*0.5), 0.0);
+	float det = min(lod/(proj_lod*0.5), 1.0);
 	
-	float d = dot(dist,dist);
-		
-	ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0)+det, 1.0);
+	float d = min(dist.x, dist.y);
+    
+    d *= min(1, d * (proj_lod - lod));
+	
+	float edge = 0.25*det;
+    
+	ret *= clamp(d/edge, 0.0, 1.0);
 	
 	return ret;
 }
@@ -336,19 +340,14 @@ void main()
 
 			if (stc.z > 0.0)
 			{
-				stc.xy /= stc.w;
-
-				float fatten = clamp(envIntensity*envIntensity+envIntensity*0.5, 0.25, 1.0);
-				
-				//stc.xy = (stc.xy - vec2(0.5)) * fatten + vec2(0.5);
-				stc.xy = (stc.xy - vec2(0.5)) * fatten + vec2(0.5);
+				stc /= stc.w;
 								
 				if (stc.x < 1.0 &&
 					stc.y < 1.0 &&
 					stc.x > 0.0 &&
 					stc.y > 0.0)
 				{
-					col += color.rgb*texture2DLodSpecular(projectionMap, stc.xy, proj_lod-envIntensity*proj_lod).rgb*shadow*spec.rgb;										
+					col += color.rgb * texture2DLodSpecular(projectionMap, stc.xy, (1 - spec.a) * (proj_lod * 0.6)).rgb * shadow * envIntensity;
 				}
 			}
 		}
diff --git a/indra/newview/llappdelegate-objc.mm b/indra/newview/llappdelegate-objc.mm
index 988058aad34752ac5e32d52ca87fb45c7d6bfc12..aad5dbae7d22d406d2e1257123a4dbf40b2e9d95 100644
--- a/indra/newview/llappdelegate-objc.mm
+++ b/indra/newview/llappdelegate-objc.mm
@@ -84,7 +84,7 @@
 	callWindowUnfocus();
 }
 
-- (NSApplicationDelegateReply) applicationShouldTerminate:(NSApplication *)sender
+- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *)sender
 {
 	if (!runMainLoop())
 	{
diff --git a/indra/newview/lldrawpoolavatar.cpp b/indra/newview/lldrawpoolavatar.cpp
index 90e6dfe351e6fd97a194d071ff6d0ea9176a944b..e7420f32f0cebaa203420bab5378661939587cf6 100755
--- a/indra/newview/lldrawpoolavatar.cpp
+++ b/indra/newview/lldrawpoolavatar.cpp
@@ -1626,7 +1626,8 @@ void LLDrawPoolAvatar::updateRiggedFaceVertexBuffer(LLVOAvatar* avatar, LLFace*
 			{
 				F32 w = weight[j][k];
 
-				idx[k] = llclamp((S32) floorf(w), 0, 63);
+				idx[k] = llclamp((S32) floorf(w), 0, JOINT_COUNT-1);
+
 				wght[k] = w - floorf(w);
 				scale += wght[k];
 			}
diff --git a/indra/newview/llfilepicker.cpp b/indra/newview/llfilepicker.cpp
index 5debf7174473ea99a6a6168cd124a50a979afc0e..ef50594febf478f79d05d463f5a97a963a7f2159 100755
--- a/indra/newview/llfilepicker.cpp
+++ b/indra/newview/llfilepicker.cpp
@@ -600,6 +600,9 @@ std::vector<std::string>* LLFilePicker::navOpenFilterProc(ELoadFilter filter) //
             allowedv->push_back("slg");
             break;
 #endif
+        case FFLOAD_XML:
+            allowedv->push_back("xml");
+            break;
         case FFLOAD_RAW:
             allowedv->push_back("raw");
             break;
@@ -702,7 +705,14 @@ bool	LLFilePicker::doNavSaveDialog(ESaveFilter filter, const std::string& filena
 			creator = "\?\?\?\?";
 			extension = "slg";
 			break;
-#endif		
+#endif	
+			
+		case FFSAVE_XML:
+			type = "\?\?\?\?";
+			creator = "\?\?\?\?";
+			extension = "xml";
+			break;
+			
 		case FFSAVE_RAW:
 			type = "\?\?\?\?";
 			creator = "\?\?\?\?";
@@ -1100,6 +1110,12 @@ static std::string add_anim_filter_to_gtkchooser(GtkWindow *picker)
 	return filtername;
 }
 
+static std::string add_xml_filter_to_gtkchooser(GtkWindow *picker)
+{
+	return add_simple_pattern_filter_to_gtkchooser(picker,  "*.xml",
+												   LLTrans::getString("xml_files") + " (*.xml)");
+}
+
 static std::string add_collada_filter_to_gtkchooser(GtkWindow *picker)
 {
 	return add_simple_pattern_filter_to_gtkchooser(picker,  "*.dae",
@@ -1293,6 +1309,9 @@ BOOL LLFilePicker::getOpenFile( ELoadFilter filter, bool blocking )
 		case FFLOAD_ANIM:
 			filtername = add_anim_filter_to_gtkchooser(picker);
 			break;
+		case FFLOAD_XML:
+			filtername = add_xml_filter_to_gtkchooser(picker);
+			break;
 		case FFLOAD_COLLADA:
 			filtername = add_collada_filter_to_gtkchooser(picker);
 			break;
diff --git a/indra/newview/llmaniprotate.cpp b/indra/newview/llmaniprotate.cpp
index 7861573908bc7c8c0e1f2f53ac50e5e48b2d55b7..b2350e5a753938403c1b3d076aec7489e08a75de 100755
--- a/indra/newview/llmaniprotate.cpp
+++ b/indra/newview/llmaniprotate.cpp
@@ -1240,10 +1240,10 @@ LLQuaternion LLManipRotate::dragUnconstrained( S32 x, S32 y )
 	F32 dist_from_sphere_center = sqrt(delta_x * delta_x + delta_y * delta_y);
 
 	LLVector3 axis = mMouseDown % mMouseCur;
+	F32 angle = atan2(sqrtf(axis * axis), mMouseDown * mMouseCur);
 	axis.normVec();
-	F32 angle = acos(mMouseDown * mMouseCur);
 	LLQuaternion sphere_rot( angle, axis );
-
+	
 	if (is_approx_zero(1.f - mMouseDown * mMouseCur))
 	{
 		return LLQuaternion::DEFAULT;
@@ -1638,9 +1638,9 @@ LLQuaternion LLManipRotate::dragConstrained( S32 x, S32 y )
 			mInSnapRegime = FALSE;
 		}
 
-		angle = acos(mMouseCur * mMouseDown);
-
-		F32 dir = (mMouseDown % mMouseCur) * constraint_axis;  // cross product
+		LLVector3 cross_product = mMouseDown % mMouseCur;
+		angle = atan2(sqrtf(cross_product * cross_product), mMouseCur * mMouseDown);
+		F32 dir = cross_product * constraint_axis;  // cross product
 		if( dir < 0.f )
 		{
 			angle *= -1.f;
diff --git a/indra/newview/llvoavatar.cpp b/indra/newview/llvoavatar.cpp
index b527f36d61199e5b927083bab2e3594659cff904..2c1dedede0cc99ea7fab0e81af887f03efb1fe62 100755
--- a/indra/newview/llvoavatar.cpp
+++ b/indra/newview/llvoavatar.cpp
@@ -464,7 +464,10 @@ class LLBreatheMotionRot :
 		mCharacter = character;
 		BOOL success = true;
 
-		if ( !mChestState->setJoint( character->getJoint( "mChest" ) ) ) { success = false; }
+		if ( !mChestState->setJoint( character->getJoint( "mChest" ) ) )
+		{
+			success = false;
+		}
 
 		if ( success )
 		{
@@ -819,14 +822,14 @@ void LLVOAvatar::debugAvatarRezTime(std::string notification_name, std::string c
 //------------------------------------------------------------------------
 LLVOAvatar::~LLVOAvatar()
 {
-		if (!mFullyLoaded)
-		{
+	if (!mFullyLoaded)
+	{
 		debugAvatarRezTime("AvatarRezLeftCloudNotification","left after ruth seconds as cloud");
-		}
-		else
-		{
+	}
+	else
+	{
 		debugAvatarRezTime("AvatarRezLeftNotification","left sometime after declouding");
-		}
+	}
 
 	logPendingPhases();
 	
@@ -980,10 +983,11 @@ void LLVOAvatar::getNearbyRezzedStats(std::vector<S32>& counts)
 		 iter != LLCharacter::sInstances.end(); ++iter)
 	{
 		LLVOAvatar* inst = (LLVOAvatar*) *iter;
-		if (!inst)
-			continue;
-		S32 rez_status = inst->getRezzedStatus();
-		counts[rez_status]++;
+		if (inst)
+		{
+			S32 rez_status = inst->getRezzedStatus();
+			counts[rez_status]++;
+		}
 	}
 }
 
@@ -1353,38 +1357,36 @@ void LLVOAvatar::getSpatialExtents(LLVector4a& newMin, LLVector4a& newMax)
 	{
 		LLViewerJointAttachment* attachment = iter->second;
 
-		if (!attachment->getValid())
-		{
-			continue ;
-		}
-
-		for (LLViewerJointAttachment::attachedobjs_vec_t::iterator attachment_iter = attachment->mAttachedObjects.begin();
-			 attachment_iter != attachment->mAttachedObjects.end();
-			 ++attachment_iter)
+		if (attachment->getValid())
 		{
-			const LLViewerObject* attached_object = (*attachment_iter);
-			if (attached_object && !attached_object->isHUDAttachment())
+			for (LLViewerJointAttachment::attachedobjs_vec_t::iterator attachment_iter = attachment->mAttachedObjects.begin();
+				 attachment_iter != attachment->mAttachedObjects.end();
+				 ++attachment_iter)
 			{
-				LLDrawable* drawable = attached_object->mDrawable;
-				if (drawable && !drawable->isState(LLDrawable::RIGGED))
+				const LLViewerObject* attached_object = (*attachment_iter);
+				if (attached_object && !attached_object->isHUDAttachment())
 				{
-					LLSpatialBridge* bridge = drawable->getSpatialBridge();
-					if (bridge)
+					LLDrawable* drawable = attached_object->mDrawable;
+					if (drawable && !drawable->isState(LLDrawable::RIGGED))
 					{
-						const LLVector4a* ext = bridge->getSpatialExtents();
-						LLVector4a distance;
-						distance.setSub(ext[1], ext[0]);
-						LLVector4a max_span(max_attachment_span);
+						LLSpatialBridge* bridge = drawable->getSpatialBridge();
+						if (bridge)
+						{
+							const LLVector4a* ext = bridge->getSpatialExtents();
+							LLVector4a distance;
+							distance.setSub(ext[1], ext[0]);
+							LLVector4a max_span(max_attachment_span);
 
-						S32 lt = distance.lessThan(max_span).getGatheredBits() & 0x7;
+							S32 lt = distance.lessThan(max_span).getGatheredBits() & 0x7;
 						
-						// Only add the prim to spatial extents calculations if it isn't a megaprim.
-						// max_attachment_span calculated at the start of the function 
-						// (currently 5 times our max prim size) 
-						if (lt == 0x7)
-						{
-							update_min_max(newMin,newMax,ext[0]);
-							update_min_max(newMin,newMax,ext[1]);
+							// Only add the prim to spatial extents calculations if it isn't a megaprim.
+							// max_attachment_span calculated at the start of the function 
+							// (currently 5 times our max prim size) 
+							if (lt == 0x7)
+							{
+								update_min_max(newMin,newMax,ext[0]);
+								update_min_max(newMin,newMax,ext[1]);
+							}
 						}
 					}
 				}
@@ -1961,7 +1963,7 @@ U32 LLVOAvatar::processUpdateMessage(LLMessageSystem *mesgsys,
 		if (has_name && getNVPair("FirstName"))
 		{
 			mDebugExistenceTimer.reset();
-		debugAvatarRezTime("AvatarRezArrivedNotification","avatar arrived");
+			debugAvatarRezTime("AvatarRezArrivedNotification","avatar arrived");
 		}
 
 	if(retval & LLViewerObject::INVALID_UPDATE)
@@ -1973,9 +1975,6 @@ U32 LLVOAvatar::processUpdateMessage(LLMessageSystem *mesgsys,
 		}
 	}
 
-	//LL_INFOS() << getRotation() << LL_ENDL;
-	//LL_INFOS() << getPosition() << LL_ENDL;
-
 	return retval;
 }
 
@@ -1991,7 +1990,7 @@ LLViewerFetchedTexture *LLVOAvatar::getBakedTextureImage(const U8 te, const LLUU
 		result = gTextureList.findImage(uuid);
 	}
 	if (!result)
-{
+	{
 		const std::string url = getImageURL(te,uuid);
 
 		if (url.empty())
@@ -3335,7 +3334,7 @@ BOOL LLVOAvatar::updateCharacter(LLAgent &agent)
 			removeAnimationData("Walk Speed");
 		}
 		mMotionController.setTimeStep(time_step);
-//		LL_INFOS() << "Setting timestep to " << time_quantum * pixel_area_scale << LL_ENDL;
+		//		LL_INFOS() << "Setting timestep to " << time_quantum * pixel_area_scale << LL_ENDL;
 	}
 
 	if (getParent() && !mIsSitting)
@@ -3478,7 +3477,6 @@ BOOL LLVOAvatar::updateCharacter(LLAgent &agent)
 						fwdDir.normalize();
 					}
 				}
-				
 			}
 
 			LLQuaternion root_rotation = mRoot->getWorldMatrix().quaternion();
@@ -3596,10 +3594,14 @@ BOOL LLVOAvatar::updateCharacter(LLAgent &agent)
 
 	// update animations
 	if (mSpecialRenderMode == 1) // Animation Preview
+	{
 		updateMotions(LLCharacter::FORCE_UPDATE);
+	}
 	else
+	{
 		updateMotions(LLCharacter::NORMAL_UPDATE);
-
+	}
+	
 	// update head position
 	updateHeadOffset();
 
@@ -3696,10 +3698,6 @@ BOOL LLVOAvatar::updateCharacter(LLAgent &agent)
 
 	//mesh vertices need to be reskinned
 	mNeedsSkin = TRUE;
-
-
-		
-	
 	return TRUE;
 }
 //-----------------------------------------------------------------------------
@@ -3979,13 +3977,13 @@ U32 LLVOAvatar::renderSkinned()
 			if (face)
 			{
 				LLVertexBuffer* vb = face->getVertexBuffer();
-			if (vb)
-			{
-				vb->flush();
+				if (vb)
+				{
+					vb->flush();
+				}
 			}
 		}
 	}
-	}
 	else
 	{
 		mNeedsSkin = FALSE;
@@ -4165,7 +4163,7 @@ U32 LLVOAvatar::renderTransparent(BOOL first_pass)
 		{
 			LLViewerJoint* hair_mesh = getViewerJoint(MESH_ID_HAIR);
 			if (hair_mesh)
-		{
+			{
 				num_indices += hair_mesh->render(mAdjustedPixelArea, first_pass, mIsDummy);
 			}
 			first_pass = FALSE;
@@ -5217,8 +5215,8 @@ BOOL LLVOAvatar::loadSkeletonNode ()
 {
 	if (!LLAvatarAppearance::loadSkeletonNode())
 	{
-				return FALSE;
-			}
+		return FALSE;
+	}
 	
 	// ATTACHMENTS
 	{
@@ -5969,27 +5967,24 @@ BOOL LLVOAvatar::isWearingWearableType(LLWearableType::EType type) const
 			break; // Do nothing
 	}
 
-	/* switch(type)
-		case LLWearableType::WT_SHIRT:
-			indicator_te = TEX_UPPER_SHIRT; */
 	for (LLAvatarAppearanceDictionary::Textures::const_iterator tex_iter = LLAvatarAppearanceDictionary::getInstance()->getTextures().begin();
 		 tex_iter != LLAvatarAppearanceDictionary::getInstance()->getTextures().end();
 		 ++tex_iter)
 	{
 		const LLAvatarAppearanceDictionary::TextureEntry *texture_dict = tex_iter->second;
 		if (texture_dict->mWearableType == type)
-	{
+		{
 			// Thus, you must check to see if the corresponding baked texture is defined.
 			// NOTE: this is a poor substitute if you actually want to know about individual pieces of clothing
 			// this works for detecting a skirt (most important), but is ineffective at any piece of clothing that
 			// gets baked into a texture that always exists (upper or lower).
 			if (texture_dict->mIsUsedByBakedTexture)
-	{
+			{
 				const EBakedTextureIndex baked_index = texture_dict->mBakedTextureIndex;
 				return isTextureDefined(LLAvatarAppearanceDictionary::getInstance()->getBakedTexture(baked_index)->mTextureIndex);
-	}
+			}
 			return FALSE;
-	}
+		}
 	}
 	return FALSE;
 }
@@ -6059,7 +6054,7 @@ void LLVOAvatar::onGlobalColorChanged(const LLTexGlobalColor* global_color)
 	} 
 	else if (global_color == mTexEyeColor)
 	{
-//		LL_INFOS() << "invalidateComposite cause: onGlobalColorChanged( eyecolor )" << LL_ENDL; 
+		// LL_INFOS() << "invalidateComposite cause: onGlobalColorChanged( eyecolor )" << LL_ENDL; 
 		invalidateComposite( mBakedTextureDatas[BAKED_EYES].mTexLayerSet);
 	}
 	updateMeshTextures();
@@ -6119,9 +6114,9 @@ void LLVOAvatar::updateRezzedStatusTimers()
 		{
 			// load level has decreased. start phase timers for higher load levels.
 			for (S32 i = rez_status+1; i <= mLastRezzedStatus; i++)
-		{
+			{
 				startPhase("load_" + LLVOAvatar::rezStatusToString(i));
-		}
+			}
 		}
 		else if (rez_status > mLastRezzedStatus)
 		{
@@ -6130,16 +6125,15 @@ void LLVOAvatar::updateRezzedStatusTimers()
 			{
 				stopPhase("load_" + LLVOAvatar::rezStatusToString(i));
 				stopPhase("first_load_" + LLVOAvatar::rezStatusToString(i), false);
-		}
+			}
 			if (rez_status == 3)
-		{
+			{
 				// "fully loaded", mark any pending appearance change complete.
 				selfStopPhase("update_appearance_from_cof");
 				selfStopPhase("wear_inventory_category", false);
 				selfStopPhase("process_initial_wearables_update", false);
 			}
 		}
-		
 		mLastRezzedStatus = rez_status;
 	}
 }
@@ -6201,7 +6195,7 @@ void LLVOAvatar::stopPhase(const std::string& phase_name, bool err_check)
 void LLVOAvatar::logPendingPhases()
 {
 	if (!isAgentAvatarValid())
-		{
+	{
 		return;
 	}
 	
@@ -6217,14 +6211,14 @@ void LLVOAvatar::logPendingPhases()
 			if (!completed)
 			{
 				logMetricsTimerRecord(phase_name, elapsed, completed);
-		}
+			}
 		}
 	}
-		}
+}
 
 //static
 void LLVOAvatar::logPendingPhasesAllAvatars()
-		{
+{
 	for (std::vector<LLCharacter*>::iterator iter = LLCharacter::sInstances.begin();
 		 iter != LLCharacter::sInstances.end(); ++iter)
 	{
@@ -6235,14 +6229,14 @@ void LLVOAvatar::logPendingPhasesAllAvatars()
 		}
 		inst->logPendingPhases();
 	}
-		}
+}
 
 void LLVOAvatar::logMetricsTimerRecord(const std::string& phase_name, F32 elapsed, bool completed)
-		{
+{
 	if (!isAgentAvatarValid())
-		{
+	{
 		return;
-		}
+	}
 	
 	LLSD record;
 	record["timer_name"] = phase_name;
@@ -6251,10 +6245,10 @@ void LLVOAvatar::logMetricsTimerRecord(const std::string& phase_name, F32 elapse
 	record["completed"] = completed;
 	U32 grid_x(0), grid_y(0);
 	if (getRegion())
-		{
+	{
 		record["central_bake_version"] = LLSD::Integer(getRegion()->getCentralBakeVersion());
 		grid_from_region_handle(getRegion()->getHandle(), &grid_x, &grid_y);
-		}
+	}
 	record["grid_x"] = LLSD::Integer(grid_x);
 	record["grid_y"] = LLSD::Integer(grid_y);
 	record["is_using_server_bakes"] = true;
@@ -6315,8 +6309,8 @@ BOOL LLVOAvatar::processFullyLoadedChange(bool loading)
 	
 	mFullyLoaded = (mFullyLoadedTimer.getElapsedTimeF32() > PAUSE);
 
-		if (!mPreviousFullyLoaded && !loading && mFullyLoaded)
-		{
+	if (!mPreviousFullyLoaded && !loading && mFullyLoaded)
+	{
 		debugAvatarRezTime("AvatarRezNotification","fully loaded");
 	}
 
@@ -6372,9 +6366,7 @@ void LLVOAvatar::debugColorizeSubMeshes(U32 i, const LLColor4& color)
 			LLAvatarJointMesh* mesh = (*iter);
 			if (mesh)
 			{
-				{
-					mesh->setColor(color);
-				}
+				mesh->setColor(color);
 			}
 		}
 	}
@@ -6474,7 +6466,7 @@ void LLVOAvatar::updateMeshTextures()
 
 		LLViewerTexLayerSet* layerset = getTexLayerSet(i);
 		if (use_lkg_baked_layer[i] && !isUsingLocalAppearance() )
-	{
+		{
 			LLViewerFetchedTexture* baked_img = LLViewerTextureManager::getFetchedTexture(mBakedTextureDatas[i].mLastTextureID);
 			mBakedTextureDatas[i].mIsUsed = TRUE;
 
@@ -6483,12 +6475,12 @@ void LLVOAvatar::updateMeshTextures()
 			avatar_joint_mesh_list_t::iterator iter = mBakedTextureDatas[i].mJointMeshes.begin();
 			avatar_joint_mesh_list_t::iterator end  = mBakedTextureDatas[i].mJointMeshes.end();
 			for (; iter != end; ++iter)
-	{
+			{
 				LLAvatarJointMesh* mesh = (*iter);
 				if (mesh)
-		{
+				{
 					mesh->setTexture( baked_img );
-			}
+				}
 			}
 		}
 		else if (!isUsingLocalAppearance() && is_layer_baked[i])
@@ -6517,9 +6509,9 @@ void LLVOAvatar::updateMeshTextures()
 				baked_img->setLoadedCallback(onBakedTextureLoaded, SWITCH_TO_BAKED_DISCARD, FALSE, FALSE, new LLUUID( mID ), 
 					src_callback_list, paused );
 
-                               // this could add paused texture callbacks
-                               mLoadedCallbacksPaused |= paused; 
-                               checkTextureLoading();
+				// this could add paused texture callbacks
+				mLoadedCallbacksPaused |= paused; 
+				checkTextureLoading();
 			}
 		}
 		else if (layerset && isUsingLocalAppearance())
@@ -6538,7 +6530,7 @@ void LLVOAvatar::updateMeshTextures()
 				if (mesh)
 				{
 					mesh->setLayerSet( layerset );
-			}
+				}
 			}
 		}
 		else
@@ -6560,7 +6552,7 @@ void LLVOAvatar::updateMeshTextures()
 		{
 			LLAvatarJointMesh* mesh = (*iter);
 			if (mesh)
-		{
+			{
 				mesh->setColor( color );
 				mesh->setTexture( hair_img );
 			}
@@ -6656,13 +6648,13 @@ void LLVOAvatar::applyMorphMask(U8* tex_data, S32 width, S32 height, S32 num_com
 
 	for (morph_list_t::const_iterator iter = mBakedTextureDatas[index].mMaskedMorphs.begin();
 		 iter != mBakedTextureDatas[index].mMaskedMorphs.end(); ++iter)
-{
+	{
 		const LLMaskedMorph* maskedMorph = (*iter);
 		LLPolyMorphTarget* morph_target = dynamic_cast<LLPolyMorphTarget*>(maskedMorph->mMorphTarget);
 		if (morph_target)
-	{
+		{
 			morph_target->applyMask(tex_data, width, height, num_components, maskedMorph->mInvert);
-}
+		}
 	}
 }
 
@@ -6961,9 +6953,9 @@ void LLVOAvatar::dumpAppearanceMsgParams( const std::string& dump_prefix,
 	outfile.open(fullpath, LL_APR_WB );
 	apr_file_t* file = outfile.getFileHandle();
 	if (!file)
-		{
-			return;
-		}
+	{
+		return;
+	}
 	else
 	{
 		LL_DEBUGS("Avatar") << "dumping appearance message to " << fullpath << LL_ENDL;
@@ -6998,7 +6990,7 @@ void LLVOAvatar::dumpAppearanceMsgParams( const std::string& dump_prefix,
 		apr_file_printf( file, "\t\t<texture te=\"%i\" uuid=\"%s\"/>\n", i, uuid_str.c_str());
 	}
 	apr_file_printf(file, "</textures>\n");
-	}
+}
 
 void LLVOAvatar::parseAppearanceMessage(LLMessageSystem* mesgsys, LLAppearanceMessageContents& contents)
 {
@@ -7527,12 +7519,12 @@ void LLVOAvatar::useBakedTexture( const LLUUID& id )
 				avatar_joint_mesh_list_t::iterator iter = mBakedTextureDatas[i].mJointMeshes.begin();
 				avatar_joint_mesh_list_t::iterator end  = mBakedTextureDatas[i].mJointMeshes.end();
 				for (; iter != end; ++iter)
-			{
+				{
 					LLAvatarJointMesh* mesh = (*iter);
 					if (mesh)
-			{
+					{
 						mesh->setTexture( image_baked );
-			}
+					}
 				}
 			}
 			
@@ -7556,14 +7548,13 @@ void LLVOAvatar::useBakedTexture( const LLUUID& id )
 				{
 					LLAvatarJointMesh* mesh = (*iter);
 					if (mesh)
-				{
+					{
 						mesh->setColor( LLColor4::white );
 					}
 				}
 			}
 		}
 	}
-
 	dirtyMesh();
 }
 
@@ -7575,7 +7566,7 @@ std::string get_sequential_numbered_file_name(const std::string& prefix,
 	file_num_type::iterator it = file_nums.find(prefix);
 	S32 num = 0;
 	if (it != file_nums.end())
-{
+	{
 		num = it->second;
 	}
 	file_nums[prefix] = num+1;
@@ -7608,65 +7599,58 @@ void LLVOAvatar::dumpArchetypeXML(const std::string& prefix, bool group_by_weara
 	
 	LLAPRFile outfile;
 	std::string fullpath = gDirUtilp->getExpandedFilename(LL_PATH_LOGS,outfilename);
-	outfile.open(fullpath, LL_APR_WB );
-	apr_file_t* file = outfile.getFileHandle();
-	if (!file)
-	{
-		return;
-	}
-	else
+	if (APR_SUCCESS == outfile.open(fullpath, LL_APR_WB ))
 	{
+		apr_file_t* file = outfile.getFileHandle();
 		LL_INFOS() << "xmlfile write handle obtained : " << fullpath << LL_ENDL;
-	}
-
-	apr_file_printf( file, "<?xml version=\"1.0\" encoding=\"US-ASCII\" standalone=\"yes\"?>\n" );
-	apr_file_printf( file, "<linden_genepool version=\"1.0\">\n" );
-	apr_file_printf( file, "\n\t<archetype name=\"???\">\n" );
 
-	if (group_by_wearables)
-	{
-		for (S32 type = LLWearableType::WT_SHAPE; type < LLWearableType::WT_COUNT; type++)
-	{
-		const std::string& wearable_name = LLWearableType::getTypeName((LLWearableType::EType)type);
-		apr_file_printf( file, "\n\t\t<!-- wearable: %s -->\n", wearable_name.c_str() );
+		apr_file_printf( file, "<?xml version=\"1.0\" encoding=\"US-ASCII\" standalone=\"yes\"?>\n" );
+		apr_file_printf( file, "<linden_genepool version=\"1.0\">\n" );
+		apr_file_printf( file, "\n\t<archetype name=\"???\">\n" );
 
-			for (LLVisualParam* param = getFirstVisualParam(); param; param = getNextVisualParam())
+		if (group_by_wearables)
 		{
-			LLViewerVisualParam* viewer_param = (LLViewerVisualParam*)param;
-			if( (viewer_param->getWearableType() == type) && 
-				(viewer_param->isTweakable() ) )
+			for (S32 type = LLWearableType::WT_SHAPE; type < LLWearableType::WT_COUNT; type++)
 			{
-					dump_visual_param(file, viewer_param, viewer_param->getWeight());
-			}
-		}
+				const std::string& wearable_name = LLWearableType::getTypeName((LLWearableType::EType)type);
+				apr_file_printf( file, "\n\t\t<!-- wearable: %s -->\n", wearable_name.c_str() );
 
-		for (U8 te = 0; te < TEX_NUM_INDICES; te++)
-		{
-				if (LLAvatarAppearanceDictionary::getTEWearableType((ETextureIndex)te) == type)
-			{
-				// MULTIPLE_WEARABLES: extend to multiple wearables?
-					LLViewerTexture* te_image = getImage((ETextureIndex)te, 0);
-				if( te_image )
+				for (LLVisualParam* param = getFirstVisualParam(); param; param = getNextVisualParam())
 				{
-					std::string uuid_str;
-					te_image->getID().toString( uuid_str );
-					apr_file_printf( file, "\t\t<texture te=\"%i\" uuid=\"%s\"/>\n", te, uuid_str.c_str());
+					LLViewerVisualParam* viewer_param = (LLViewerVisualParam*)param;
+					if( (viewer_param->getWearableType() == type) && 
+					   (viewer_param->isTweakable() ) )
+					{
+						dump_visual_param(file, viewer_param, viewer_param->getWeight());
+					}
+				}
+
+				for (U8 te = 0; te < TEX_NUM_INDICES; te++)
+				{
+					if (LLAvatarAppearanceDictionary::getTEWearableType((ETextureIndex)te) == type)
+					{
+						// MULTIPLE_WEARABLES: extend to multiple wearables?
+						LLViewerTexture* te_image = getImage((ETextureIndex)te, 0);
+						if( te_image )
+						{
+							std::string uuid_str;
+							te_image->getID().toString( uuid_str );
+							apr_file_printf( file, "\t\t<texture te=\"%i\" uuid=\"%s\"/>\n", te, uuid_str.c_str());
+						}
+					}
 				}
 			}
 		}
-	}
-		}
-	else 
-	{
-		// Just dump all params sequentially.
-		for (LLVisualParam* param = getFirstVisualParam(); param; param = getNextVisualParam())
+		else 
 		{
-			LLViewerVisualParam* viewer_param = (LLViewerVisualParam*)param;
-			dump_visual_param(file, viewer_param, viewer_param->getWeight());
-		}
+			// Just dump all params sequentially.
+			for (LLVisualParam* param = getFirstVisualParam(); param; param = getNextVisualParam())
+			{
+				LLViewerVisualParam* viewer_param = (LLViewerVisualParam*)param;
+				dump_visual_param(file, viewer_param, viewer_param->getWeight());
+			}
 
-		for (U8 te = 0; te < TEX_NUM_INDICES; te++)
-		{
+			for (U8 te = 0; te < TEX_NUM_INDICES; te++)
 			{
 				// MULTIPLE_WEARABLES: extend to multiple wearables?
 				LLViewerTexture* te_image = getImage((ETextureIndex)te, 0);
@@ -7678,18 +7662,17 @@ void LLVOAvatar::dumpArchetypeXML(const std::string& prefix, bool group_by_weara
 				}
 			}
 		}
+		apr_file_printf( file, "\t</archetype>\n" );
+		apr_file_printf( file, "\n</linden_genepool>\n" );
 
+		bool ultra_verbose = false;
+		if (isSelf() && ultra_verbose)
+		{
+			// show the cloned params inside the wearables as well.
+			gAgentAvatarp->dumpWearableInfo(outfile);
+		}
+		outfile.close();
 	}
-	apr_file_printf( file, "\t</archetype>\n" );
-	apr_file_printf( file, "\n</linden_genepool>\n" );
-
-	bool ultra_verbose = false;
-	if (isSelf() && ultra_verbose)
-	{
-		// show the cloned params inside the wearables as well.
-		gAgentAvatarp->dumpWearableInfo(outfile);
-	}
-	// File will close when handle goes out of scope
 }
 
 
@@ -7803,7 +7786,7 @@ void LLVOAvatar::startAppearanceAnimation()
 
 // virtual
 void LLVOAvatar::removeMissingBakedTextures()
-			{
+{
 }
 
 //virtual
@@ -8057,7 +8040,6 @@ void LLVOAvatar::calculateUpdateRenderCost()
 					}
 				}
 			}
-
 		}
 
 		// Diagnostic output to identify all avatar-related textures.
@@ -8069,9 +8051,8 @@ void LLVOAvatar::calculateUpdateRenderCost()
 			for (LLVOVolume::texture_cost_t::iterator it = textures.begin(); it != textures.end(); ++it)
 			{
 				LLUUID image_id = it->first;
-				if( image_id.isNull() || image_id == IMG_DEFAULT || image_id == IMG_DEFAULT_AVATAR)
-					continue;
-				if (all_textures.find(image_id) == all_textures.end())
+				if( ! (image_id.isNull() || image_id == IMG_DEFAULT || image_id == IMG_DEFAULT_AVATAR)
+				   && (all_textures.find(image_id) == all_textures.end()))
 				{
 					// attachment texture not previously seen.
 					LL_INFOS() << "attachment_texture: " << image_id.asString() << LL_ENDL;
@@ -8137,15 +8118,17 @@ LLColor4 LLVOAvatar::calcMutedAVColor(F32 value, S32 range_low, S32 range_high)
 // static
 BOOL LLVOAvatar::isIndexLocalTexture(ETextureIndex index)
 {
-	if (index < 0 || index >= TEX_NUM_INDICES) return false;
-	return LLAvatarAppearanceDictionary::getInstance()->getTexture(index)->mIsLocalTexture;
+	return (index < 0 || index >= TEX_NUM_INDICES)
+		? false
+		: LLAvatarAppearanceDictionary::getInstance()->getTexture(index)->mIsLocalTexture;
 }
 
 // static
 BOOL LLVOAvatar::isIndexBakedTexture(ETextureIndex index)
 {
-	if (index < 0 || index >= TEX_NUM_INDICES) return false;
-	return LLAvatarAppearanceDictionary::getInstance()->getTexture(index)->mIsBakedTexture;
+	return (index < 0 || index >= TEX_NUM_INDICES)
+		? false
+		: LLAvatarAppearanceDictionary::getInstance()->getTexture(index)->mIsBakedTexture;
 }
 
 const std::string LLVOAvatar::getBakedStatusForPrintout() const
@@ -8195,7 +8178,7 @@ BOOL LLVOAvatar::isTextureDefined(LLAvatarAppearanceDefines::ETextureIndex te, U
 	{
 		return FALSE;
 	}
-
+	
 	if( !getImage( te, index ) )
 	{
 		LL_WARNS() << "getImage( " << te << ", " << index << " ) returned 0" << LL_ENDL;