Преглед на файлове

renderer: invert colour for mouse cursor

osy преди 3 години
родител
ревизия
e13eab7c18
променени са 3 файла, в които са добавени 18 реда и са изтрити 3 реда
  1. 9 0
      Renderer/UTMRenderer.m
  2. 5 0
      Renderer/UTMShaderTypes.h
  3. 4 3
      Renderer/UTMShaders.metal

+ 9 - 0
Renderer/UTMRenderer.m

@@ -171,6 +171,7 @@ static matrix_float4x4 matrix_scale_translate(CGFloat scale, CGPoint translate)
         // Render the screen first
         
         bool hasAlpha = NO;
+        bool isInverted = NO;
         matrix_float4x4 transform = matrix_scale_translate(source.viewportScale,
                                                            source.viewportOrigin);
 
@@ -200,6 +201,10 @@ static matrix_float4x4 matrix_scale_translate(CGFloat scale, CGPoint translate)
         
         [renderEncoder setFragmentSamplerState:_sampler
                                        atIndex:UTMSamplerIndexTexture];
+        
+        [renderEncoder setFragmentBytes:&isInverted
+                                 length:sizeof(isInverted)
+                                atIndex:UTMFragmentBufferIndexIsInverted];
 
         // Draw the vertices of our triangles
         [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle
@@ -210,6 +215,7 @@ static matrix_float4x4 matrix_scale_translate(CGFloat scale, CGPoint translate)
         if (source.cursorVisible) {
             // Next render the cursor
             bool hasAlpha = YES;
+            bool isInverted = YES;
             matrix_float4x4 transform = matrix_scale_translate(source.viewportScale,
                                                                CGPointMake(source.viewportOrigin.x +
                                                                            source.cursorOrigin.x,
@@ -231,6 +237,9 @@ static matrix_float4x4 matrix_scale_translate(CGFloat scale, CGPoint translate)
                                       atIndex:UTMTextureIndexBaseColor];
             [renderEncoder setFragmentSamplerState:_sampler
                                            atIndex:UTMSamplerIndexTexture];
+            [renderEncoder setFragmentBytes:&isInverted
+                                     length:sizeof(isInverted)
+                                    atIndex:UTMFragmentBufferIndexIsInverted];
             [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle
                               vertexStart:0
                               vertexCount:source.cursorNumVertices];

+ 5 - 0
Renderer/UTMShaderTypes.h

@@ -32,6 +32,11 @@ typedef enum UTMSamplerIndex
     UTMSamplerIndexTexture = 0,
 } UTMSamplerIndex;
 
+typedef enum UTMFragmentBufferIndex
+{
+    UTMFragmentBufferIndexIsInverted = 0,
+} UTMFragmentBufferIndex;
+
 //  This structure defines the layout of each vertex in the array of vertices set as an input to our
 //    Metal vertex shader.  Since this header is shared between our .metal shader and C code,
 //    we can be sure that the layout of the vertex array in the code matches the layout that

+ 4 - 3
Renderer/UTMShaders.metal

@@ -85,7 +85,8 @@ vertexShader(uint vertexID [[ vertex_id ]],
 fragment float4
 samplingShader(RasterizerData in [[stage_in]],
                texture2d<half> colorTexture [[ texture(UTMTextureIndexBaseColor) ]],
-               sampler textureSampler [[ sampler(UTMSamplerIndexTexture) ]])
+               sampler textureSampler [[ sampler(UTMSamplerIndexTexture) ]],
+               constant bool *isInverted [[ buffer(UTMFragmentBufferIndexIsInverted) ]])
 {
     // Sample the texture to obtain a color
     half4 colorSample = colorTexture.sample(textureSampler, in.textureCoordinate);
@@ -95,7 +96,7 @@ samplingShader(RasterizerData in [[stage_in]],
         colorSample.a = 0xff;
     }
 
-    // We return the color of the texture inverted
-    return float4(colorSample);
+    // We return the color of the texture inverted when requested
+    return float4(*isInverted ? colorSample.bgra : colorSample);
 }