Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash-preview-04-17· GoogleHugging Face LMArena Human Preference 140K

#define Transparent vec4(.0,.0,.0,.0) #define Tolerance 0.004 //If you feel like your…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with grok-3-preview-02-24’s answer.
  1. User

    #define Transparent vec4(.0,.0,.0,.0) #define Tolerance 0.004 //If you feel like your colors should be matching but aren't, increase this number a bit.

    varying vec2 v_vTexcoord; varying vec4 v_vColour;

    uniform sampler2D u_palTexture; uniform vec4 u_Uvs; uniform float u_paletteId; uniform vec2 u_pixelSize;

    vec4 findAltColor(vec4 inCol, vec2 corner) { if(inCol.a == 0.) return Transparent;

    float dist;
    vec2 testPos;
    vec4 leftCol;
    for(float i = corner.y; i < u_Uvs.w; i+=u_pixelSize.y )
    {
    		testPos = vec2(corner.x,i);
      leftCol = texture2D( u_palTexture, testPos);
        
    		dist = distance(leftCol,inCol);
    
    		if(dist < Tolerance)
      {
    			testPos = vec2(corner.x + u_pixelSize.x * floor(u_paletteId + 1.0), i);
        return mix(texture2D(u_palTexture, vec2(testPos.x - u_pixelSize.x, testPos.y)), texture2D(u_palTexture, testPos), fract(u_paletteId));
      }
    }
    return inCol;
    

    }

    void main() { vec4 col = texture2D( gm_BaseTexture, v_vTexcoord); DoAlphaTest( col ); col = findAltColor(col, u_Uvs.xy); gl_FragColor = v_vColour * col; }

    attribute vec3 in_Position; // (x,y,z) attribute vec4 in_Colour; // (r,g,b,a) attribute vec2 in_TextureCoord; // (u,v)

    varying vec2 v_vTexcoord; varying vec4 v_vColour;

    void main() { vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0); gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;

    v_vColour = in_Colour;
    v_vTexcoord = in_TextureCoord;
    

    }

    #macro rps_type_standard 0 #macro rps_type_html5_sprite 1 #macro rps_type_html5_surface 2

    ///@func pal_swap_init_system(shader, [html5 sprite shader], [html5 surface shader]) function pal_swap_init_system(_shader = shd_pal_swapper, _html5_sprite_shader = shd_pal_html_sprite, _html5_surface_shader = shd_pal_html_surface) {

    //Initiates the palette system.  Call once at the beginning of your game somewhere.
    //IMPORTANT: if you plan on using this for HTML5, you need to specify your two HTML5 
    

    //shader assets var _swapper = { shader: noone, html5: false, html5_sprite: noone, html5_surface: noone, texel_size: [0], uvs: [0], index: [0], texture: [0], layer_data: {} }

    _swapper.html5 = os_browser != browser_not_a_browser;
    
    if(!_swapper.html5) {
    	//Init non-html5 mode
    	_swapper.shader = _shader;
    	_swapper.texel_size[rps_type_standard] = shader_get_uniform(_shader, "u_pixelSize");
    	_swapper.uvs[rps_type_standard] = shader_get_uniform(_shader, "u_Uvs");
    	_swapper.index[rps_type_standard] = shader_get_uniform(_shader, "u_paletteId");
    	_swapper.texture[rps_type_standard] = shader_get_sampler_index(_shader, "u_palTexture");
    }	else	{
    	if(_html5_sprite_shader == undefined || _html5_surface_shader == undefined) {
    		show_message("Must provide pal_swap_init_system() with 2 additional arguments for HTML5 Compatible Sprite and Surface Shaders");
    		game_end();
    	}
    	_swapper.html5_sprite = _html5_sprite_shader;
    	_swapper.html5_surface = _html5_surface_shader;
    
    	_swapper.texel_size[rps_type_html5_sprite] = shader_get_uniform(_html5_sprite_shader, "u_pixelSize");
    	_swapper.uvs[rps_type_html5_sprite] = shader_get_uniform(_html5_sprite_shader, "u_Uvs");
    	_swapper.index[rps_type_html5_sprite] = shader_get_uniform(_html5_sprite_shader, "u_paletteId");
    	_swapper.texture[rps_type_html5_sprite] = shader_get_sampler_index(_html5_sprite_shader, "u_palTexture");
    
    
    	_swapper.texel_size[rps_type_html5_surface] = shader_get_uniform(_html5_surface_shader, "u_pixelSize");
    	_swapper.uvs[rps_type_html5_surface] = shader_get_uniform(_html5_surface_shader, "u_Uvs");
    	_swapper.index[rps_type_html5_surface] = shader_get_uniform(_html5_surface_shader, "u_paletteId");
    	_swapper.texture[rps_type_html5_surface] = shader_get_sampler_index(_html5_surface_shader, "u_palTexture");
    }
    
    global.retro_pal_swapper = _swapper;
    

    } pal_swap_init_system();

    ///@func pal_swap_set(palette_sprite_index, palette_index, palette is surface); function pal_swap_set(_pal_sprite, _pal_index, _is_surface) {

    var _swapper = global.retro_pal_swapper;
    
    if(_pal_index == 0) exit;
    
    var _mode = rps_type_standard;
    
    if(!_is_surface) {   
    	//Using a sprite based palette
    	if(_swapper.html5)	{
    		shader_set(_swapper.html5_sprite);
    		_mode = rps_type_html5_sprite;
    	} else shader_set(_swapper.shader);
    
    	var _tex = sprite_get_texture(_pal_sprite, 0),
    			_UVs = sprite_get_uvs(_pal_sprite, 0);
      texture_set_stage(_swapper.texture[_mode], _tex);
    
      var _texel_x = texture_get_texel_width(_tex),
          _texel_y = texture_get_texel_height(_tex),
          _texel_hx = _texel_x * 0.5,
          _texel_hy = _texel_y * 0.5;
    
      shader_set_uniform_f(_swapper.texel_size[_mode], _texel_x, _texel_y);
      shader_set_uniform_f(_swapper.uvs[_mode], _UVs[0] + _texel_hx, _UVs[1] + _texel_hy, _UVs[2], _UVs[3]);
      shader_set_uniform_f(_swapper.index[_mode], _pal_index);
    } else {   
    	//Using a surface based palette
    	if(_swapper.html5) {
    		shader_set(_swapper.html5_surface);
    		_mode = rps_type_html5_surface;
    	}	else shader_set(_swapper.shader);
    
    	var _tex = surface_get_texture(_pal_sprite);
    
      texture_set_stage(_swapper.texture[_mode], _tex);
    
      var _texel_x = texture_get_texel_width(_tex),
          _texel_y = texture_get_texel_height(_tex),
          _texel_hx = _texel_x * 0.5,
          _texel_hy = _texel_y * 0.5;
    	shader_set_uniform_f(_swapper.texel_size[_mode], _texel_x, _texel_y);
      shader_set_uniform_f(_swapper.uvs[_mode], _texel_hx, _texel_hy, 1.0 + _texel_hx, 1.0 + _texel_hy);
      shader_set_uniform_f(_swapper.index[_mode], _pal_index);
    }
    

    }

    /// @func pal_swap_reset(); function pal_swap_reset() { //Resets the shader if(shader_current() != -1){ shader_reset(); } }

    ///@func pal_swap_layer_init();
    // Should be called once before setting up any layers to pal swap. // This should be done on room start function pal_swap_layer_init() { global.retro_pal_swapper.layer_data = {}; }

    // @func pal_swap_set_layer(pal_sprite, pal_index, layer_index, pal_is_surface) //draws the specified layer using the specified palette in the specified event. function pal_swap_set_layer(_pal_sprite, _pal_index, _layer_index, _pal_is_surface) { global.retro_pal_swapper.layer_data[$ _layer_index] = {sprite: _pal_sprite, index: _pal_index, is_surf: _pal_is_surface}; }

    ///@func pal_swap_enable_layer(layer index) /// This script sets layer start and end scripts. /// This script should not be called in either a step event or a draw event.
    /// It should only be called once. function pal_swap_enable_layer(_layer_index) { if(!layer_exists(_layer_index)) return;

    var _onLayerBegin = method({layer: _layer_index}, function(){ if(event_type == ev_draw) { var _data = global.retro_pal_swapper.layer_data[$ layer]; if(_data == undefined) return;

        pal_swap_set(_data.sprite,_data.index,_data.is_surf);
    }
    

    });

    layer_script_begin(_layer_index, _onLayerBegin);

    layer_script_end(_layer_index,function() { if(event_type == ev_draw) { pal_swap_reset(); } }); }

    pal_swap_batch_begin(spr_roof_palette, false); for (var i = array_length(decorations) -1; i >= 0; --i) { with (decorations[i]){ pal_swap_batch_pack_data(palette_index,1); Draw(); } } pal_swap_batch_end();

    I want to use image alpha of the objects to pass both palette + alpha (max 32 color palette so adjust bits accorddingly between alpha and color)

    So I can batch draw decorations with a single shader set without breaking vertex batchs in gamemaker studio 2.3. Add a new version my shader and add additional functions to achieve htat.

  2. Gemini Assistant

    Okay, let's break this down. The current system relies on setting a uniform (u_paletteId) per draw call when you want to change the palette. Uniforms are constant for an entire draw batch, so this breaks batching.

    To achieve per-object palette swapping within a single batch, the palette index needs to be passed per vertex. GameMaker allows you to influence the in_Colour vertex attribute (which becomes v_vColour in your shader) using draw_set_colour and draw_set_alpha.

    Your idea to use the alpha channel of v_vColour is a good approach. Since a 32-color palette requires 5 bits (2^5 = 32), and the alpha channel is typically 8 bits (0-255), we can encode the 5-bit palette index and still have 3 bits left for the object's desired alpha (allowing 8 levels of alpha: 0, 1/7, 2/7, ... 1).

    Here's how we can modify your code:

    1. Create a new shader: This shader will read the palette index and desired alpha from the vertex color's alpha channel (v_vColour.a).
    2. Modify the GML functions:
      • Add a new type constant for the batch shader.
      • Modify pal_swap_init_system to load the new shader and get its uniform handles.
      • Create new batching functions (pal_swap_batch_begin_vcolor, pal_swap_batch_pack_data_vcolor, pal_swap_batch_end_vcolor) that use draw_set_alpha to encode the palette index and desired alpha before each object's drawing call within the batch.

    Step 1: Create the New Shader

    Copy your existing shader code and save it as a new shader asset in GameMaker (e.g., shd_pal_swapper_batch).

    Vertex Shader (shd_pal_swapper_batch - Vertex) This remains the same as the original vertex shader.

    #version 110 // Or 100 for GLES
    
    attribute vec3 in_Position;                  // (x,y,z)
    attribute vec4 in_Colour;                    // (r,g,b,a)
    attribute vec2 in_TextureCoord;              // (u,v)
    
    varying vec2 v_vTexcoord;
    varying vec4 v_vColour;
    
    void main()
    {
        vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
        gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
    
        // v_vColour will now contain the encoded palette index and alpha in its alpha component
        v_vColour = in_Colour;
        v_vTexcoord = in_TextureCoord;
    }
    

    Fragment Shader (shd_pal_swapper_batch - Fragment)

    We need to modify this to:

    1. Decode the palette index and desired alpha from v_vColour.a.
    2. Pass the decoded palette index to findAltColor.
    3. Apply the decoded desired alpha to the final fragment color.
    4. Remove the u_paletteId uniform as it's no longer needed.
    #version 110 // Or 100 for GLES
    
    #define Transparent vec4(.0,.0,.0,.0)
    #define Tolerance 0.004
    //If you feel like your colors should be matching but aren't, increase this number a bit.
    
    varying vec2 v_vTexcoord;
    varying vec4 v_vColour; // v_vColour.a contains encoded palette index and alpha
    
    uniform sampler2D u_palTexture;
    uniform vec4 u_Uvs;
    // uniform float u_paletteId; // Removed - palette index is now in v_vColour.a
    uniform vec2 u_pixelSize;
    
    // Function to find the alternative color using the decoded palette index
    vec4 findAltColor(vec4 inCol, vec2 corner, int palette_idx) // Added palette_idx parameter
    {
        if(inCol.a == 0.) return Transparent; // Skip lookup for fully transparent pixels
    
        // Palette index 0 might be reserved or map to column 0 (original).
        // Your original shader uses floor(u_paletteId + 1.0) and samples from column N+1
        // for palette index N. Let's replicate that logic, assuming column 0 is original.
        // So, palette_idx 0 samples from column 1, palette_idx 1 samples from column 2, etc.
        // The column offset in the texture is palette_idx + 1.
        float lookup_col_offset = float(palette_idx + 1);
    
        vec2 testPos;
        vec4 leftCol; // This will hold the color from the original color column (column 0)
    
        // Iterate through the original color column of the palette texture
        for(float i = corner.y; i < u_Uvs.w; i+=u_pixelSize.y )
        {
            // Sample the original color from column 0
            testPos = vec2(corner.x, i);
            leftCol = texture2D( u_palTexture, testPos);
    
            // Check if the input color matches this original color (within tolerance)
            // The original code used distance(leftCol, inCol). Let's keep that.
            if(distance(leftCol, inCol) < Tolerance)
            {
                // Found a match! Now sample the color from the target palette column.
                testPos = vec2(corner.x + u_pixelSize.x * lookup_col_offset, i);
                // Your original shader had a mix here:
                // mix(texture2D(u_palTexture, vec2(testPos.x - u_pixelSize.x, testPos.y)), texture2D(u_palTexture, testPos), fract(u_paletteId));
                // Since palette_idx is an integer, fract(palette_idx) is 0.0, and floor(palette_idx + 1.0) is palette_idx + 1.0.
                // The mix effectively becomes mix(color_from_col_N, color_from_col_N+1, 0.0) which is just color_from_col_N.
                // However, the lookup position was float(palette_idx + 1). So it was sampling column palette_idx + 1.
                // Let's simplify based on the likely intent: sample directly from column palette_idx + 1.
                return texture2D(u_palTexture, testPos);
            }
        }
        // If the original color wasn't found in the palette's original column,
        // return the input color (respecting its alpha).
        return inCol;
    }
    
    void main()
    {
        vec4 col = texture2D( gm_BaseTexture, v_vTexcoord);
        DoAlphaTest( col ); // Standard alpha test based on source texture alpha
    
        // Decode palette index and desired alpha from v_vColour.a
        // We encoded: encoded_byte = (palette_index & 31) | ((alpha_level & 7) << 5);
        // Where palette_index is 0-31, alpha_level is 0-7.
        // v_vColour.a = float(encoded_byte) / 255.0;
    
        int encoded_byte = int(v_vColour.a * 255.0 + 0.5); // Decode byte value, +0.5 for rounding
        int palette_index = encoded_byte & 31; // Extract lower 5 bits for palette index (0-31)
        float desired_alpha = float(encoded_byte >> 5) / 7.0; // Extract upper 3 bits, map 0-7 to 0.0-1.0
    
        // Look up the palette color using the decoded index
        vec4 looked_up_color = findAltColor(col, u_Uvs.xy, palette_index);
    
        // Apply vertex color RGB tint (if any, usually c_white) to the looked-up color
        gl_FragColor = v_vColour.rgb * looked_up_color;
    
        // Apply the looked-up alpha (from the palette texture) AND the desired vertex alpha
        gl_FragColor.a = looked_up_color.a * desired_alpha;
    }
    

    HTML5 Shaders: If you use the separate HTML5 shaders, create shd_pal_html5_sprite_batch and shd_pal_html5_surface_batch with the same fragment shader code as shd_pal_swapper_batch and the standard vertex shader.


    Step 2: Modify GML Functions

    We need a helper function to encode the palette index and alpha, update pal_swap_init_system, and create the new batch functions.

    Add a new constant for the batch type:

    #macro rps_type_standard 0
    #macro rps_type_html5_sprite 1
    #macro rps_type_html5_surface 2
    #macro rps_type_batch 3 // New constant for the batch shader
    #macro rps_type_html5_sprite_batch 4 // New HTML5 batch sprite constant
    #macro rps_type_html5_surface_batch 5 // New HTML5 batch surface constant
    

    Add a helper function to encode the data into the alpha channel:

    /// @func encode_palette_alpha(palette_index, desired_alpha_float)
    /// @param palette_index The palette index (0-31).
    /// @param desired_alpha_float The desired alpha (0.0-1.0).
    /// @return The encoded float value (0.0-1.0) for draw_set_alpha.
    function encode_palette_alpha(_pal_index, _desired_alpha_float) {
        // Clamp palette index to 0-31
        var pal_index_clamped = clamp(_pal_index, 0, 31);
    
        // Map desired alpha float (0.0-1.0) to an integer level (0-7)
        var alpha_level = round(clamp(_desired_alpha_float, 0.0, 1.0) * 7);
    
        // Encode: lower 5 bits for index, upper 3 bits for alpha level
        var encoded_byte = (pal_index_clamped & 31) | ((alpha_level & 7) << 5);
    
        // Return as float 0-1 range
        return encoded_byte / 255.0;
    }
    

    Modify pal_swap_init_system:

    ///@func pal_swap_init_system(shader, html5_sprite_shader, html5_surface_shader, batch_shader, html5_sprite_batch_shader, html5_surface_batch_shader)
    function pal_swap_init_system(
        _shader = shd_pal_swapper,
        _html5_sprite_shader = shd_pal_html_sprite,
        _html5_surface_shader = shd_pal_html_surface,
        _batch_shader = shd_pal_swapper_batch, // New batch shader argument
        _html5_sprite_batch_shader = -1, // Placeholder, provide your actual HTML5 batch sprite shader
        _html5_surface_batch_shader = -1 // Placeholder, provide your actual HTML5 batch surface shader
        ) {
    
    	//Initiates the palette system.  Call once at the beginning of your game somewhere.
    	//IMPORTANT: if you plan on using this for HTML5, you need to specify your additional 
        //shader assets for batching mode as well.
    	var _swapper = {
      	shader: noone,
      	html5: false,
      	html5_sprite: noone,
      	html5_surface: noone,
        batch_shader: noone, // New field
        html5_sprite_batch: noone, // New field
        html5_surface_batch: noone, // New field
      	texel_size: array_create(6), // Increase size for new types
      	uvs: array_create(6), // Increase size
      	index: array_create(6), // Keep for old modes, new mode doesn't use
      	texture: array_create(6), // Increase size
      	layer_data: {}
      }
    
    	_swapper.html5 = os_browser != browser_not_a_browser;
    
    	// Initialize standard non-batch shaders (original functionality)
    	if(!_swapper.html5) {
    		_swapper.shader = _shader;
    		_swapper.texel_size[rps_type_standard] = shader_get_uniform(_shader, "u_pixelSize");
    		_swapper.uvs[rps_type_standard] = shader_get_uniform(_shader, "u_Uvs");
    		_swapper.index[rps_type_standard] = shader_get_uniform(_shader, "u_paletteId"); // This is for old mode
    		_swapper.texture[rps_type_standard] = shader_get_sampler_index(_shader, "u_palTexture");
    	}	else	{
    		if(_html5_sprite_shader == undefined || _html5_surface_shader == undefined || _html5_sprite_batch_shader == undefined || _html5_surface_batch_shader == undefined) {
    			show_message("Must provide pal_swap_init_system() with all HTML5 shader arguments for standard and batch modes");
    			game_end();
    		}
    		_swapper.html5_sprite = _html5_sprite_shader;
    		_swapper.html5_surface = _html5_surface_shader;
    
    		_swapper.texel_size[rps_type_html5_sprite] = shader_get_uniform(_html5_sprite_shader, "u_pixelSize");
    		_swapper.uvs[rps_type_html5_sprite] = shader_get_uniform(_html5_sprite_shader, "u_Uvs");
    		_swapper.index[rps_type_html5_sprite] = shader_get_uniform(_html5_sprite_shader, "u_paletteId"); // Old mode
    		_swapper.texture[rps_type_html5_sprite] = shader_get_sampler_index(_html5_sprite_shader, "u_palTexture");
    
    		_swapper.texel_size[rps_type_html5_surface] = shader_get_uniform(_html5_surface_shader, "u_pixelSize");
    		_swapper.uvs[rps_type_html5_surface] = shader_get_uniform(_html5_surface_shader, "u_Uvs");
    		_swapper.index[rps_type_html5_surface] = shader_get_uniform(_html5_surface_shader, "u_paletteId"); // Old mode
    		_swapper.texture[rps_type_html5_surface] = shader_get_sampler_index(_html5_surface_shader, "u_palTexture");
    
            // Initialize HTML5 batch shaders
            _swapper.html5_sprite_batch = _html5_sprite_batch_shader;
            _swapper.html5_surface_batch = _html5_surface_batch_shader;
    
            _swapper.texel_size[rps_type_html5_sprite_batch] = shader_get_uniform(_html5_sprite_batch_shader, "u_pixelSize");
            _swapper.uvs[rps_type_html5_sprite_batch] = shader_get_uniform(_html5_sprite_batch_shader, "u_Uvs");
            // No index uniform needed for batch shader
            _swapper.texture[rps_type_html5_sprite_batch] = shader_get_sampler_index(_html5_sprite_batch_shader, "u_palTexture");
    
            _swapper.texel_size[rps_type_html5_surface_batch] = shader_get_uniform(_html5_surface_batch_shader, "u_pixelSize");
            _swapper.uvs[rps_type_html5_surface_batch] = shader_get_uniform(_html5_surface_batch_shader, "u_Uvs");
             // No index uniform needed for batch shader
            _swapper.texture[rps_type_html5_surface_batch] = shader_get_sampler_index(_html5_surface_batch_shader, "u_palTexture");
    	}
    
        // Initialize standard non-html5 batch shader
        _swapper.batch_shader = _batch_shader;
        _swapper.texel_size[rps_type_batch] = shader_get_uniform(_batch_shader, "u_pixelSize");
        _swapper.uvs[rps_type_batch] = shader_get_uniform(_batch_shader, "u_Uvs");
        // No index uniform needed for batch shader
        _swapper.texture[rps_type_batch] = shader_get_sampler_index(_batch_shader, "u_palTexture");
    
    
    	global.retro_pal_swapper = _swapper;
    }
    // Update the call to include batch shaders (use -1 or your actual HTML5 batch shader assets)
    pal_swap_init_system(shd_pal_swapper, shd_pal_html_sprite, shd_pal_html_surface, shd_pal_swapper_batch, -1, -1);
    

    NOTE: You need to replace the -1 placeholders in the pal_swap_init_system call and definition with your actual HTML5 batch shader assets if you create them.

    Create the new batching functions:

    /// @func pal_swap_batch_begin_vcolor(palette_sprite_index, palette_is_surface);
    /// @param palette_sprite_index The index of the sprite or surface containing palettes.
    /// @param palette_is_surface Set to true if the palette source is a surface.
    function pal_swap_batch_begin_vcolor(_pal_sprite, _is_surface) {
    
    	var _swapper = global.retro_pal_swapper;
    	var _mode;
        var _current_shader;
    
    	if(!_is_surface) {
    		// Using a sprite based palette
    		if(_swapper.html5)	{
    			_current_shader = _swapper.html5_sprite_batch;
    			_mode = rps_type_html5_sprite_batch;
    		} else {
                _current_shader = _swapper.batch_shader;
                _mode = rps_type_batch;
            }
    
    		shader_set(_current_shader);
    
    		var _tex = sprite_get_texture(_pal_sprite, 0),
    				_UVs = sprite_get_uvs(_pal_sprite, 0);
    
    	    texture_set_stage(_swapper.texture[_mode], _tex);
    
    	    var _texel_x = texture_get_texel_width(_tex),
    	        _texel_y = texture_get_texel_height(_tex),
    	        _texel_hx = _texel_x * 0.5,
    	        _texel_hy = _texel_y * 0.5;
    
    	    shader_set_uniform_f(_swapper.texel_size[_mode], _texel_x, _texel_y);
    	    shader_set_uniform_f(_swapper.uvs[_mode], _UVs[0] + _texel_hx, _UVs[1] + _texel_hy, _UVs[2], _UVs[3]);
    
    	} else {
    		// Using a surface based palette
    		if(_swapper.html5) {
    			_current_shader = _swapper.html5_surface_batch;
    			_mode = rps_type_html5_surface_batch;
    		}	else {
                _current_shader = _swapper.batch_shader;
                _mode = rps_type_batch;
            }
    
            shader_set(_current_shader);
    
    		var _tex = surface_get_texture(_pal_sprite);
    
    	    texture_set_stage(_swapper.texture[_mode], _tex);
    
    	    var _texel_x = texture_get_texel_width(_tex),
    	        _texel_y = texture_get_texel_height(_tex),
    	        _texel_hx = _texel_x * 0.5,
    	        _texel_hy = _texel_y * 0.5;
    		shader_set_uniform_f(_swapper.texel_size[_mode], _texel_x, _texel_y);
    	    shader_set_uniform_f(_swapper.uvs[_mode], _texel_hx, _texel_hy, 1.0 + _texel_hx, 1.0 + _texel_hy);
    	}
    
        // Store the current draw alpha/color to restore them later
        global.__pal_swap_batch_old_alpha = draw_get_alpha();
        global.__pal_swap_batch_old_colour = draw_get_colour();
    }
    
    /// @func pal_swap_batch_pack_data_vcolor(palette_index, desired_alpha);
    /// @param palette_index The palette index for the *next* drawing call (0-31).
    /// @param desired_alpha The desired alpha for the *next* drawing call (0.0-1.0).
    /// Call this function *before* each drawing call (e.g., draw_self(), draw_sprite()) within a pal_swap_batch_begin_vcolor / pal_swap_batch_end_vcolor block.
    function pal_swap_batch_pack_data_vcolor(_pal_index, _desired_alpha) {
        // Encode palette index and alpha into the alpha channel
        var encoded_alpha = encode_palette_alpha(_pal_index, _desired_alpha);
    
        // Set the draw alpha. GameMaker passes this as v_vColour.a to the shader.
        draw_set_alpha(encoded_alpha);
    
        // Optionally, set the draw colour RGB if you want to use v_vColour.rgb for tinting.
        // If not needed, leave it as c_white or whatever the object's image_blend is set to.
        // draw_set_colour(c_white); // Set to white if you don't want RGB tinting
    }
    
    /// @func pal_swap_batch_end_vcolor();
    function pal_swap_batch_end_vcolor() {
    	// Resets the shader
    	if(shader_current() != -1){
    		shader_reset();
    	}
        // Restore original draw state
        draw_set_alpha(global.__pal_swap_batch_old_alpha);
        draw_set_colour(global.__pal_swap_batch_old_colour);
        global.__pal_swap_batch_old_alpha = undefined;
        global.__pal_swap_batch_old_colour = undefined;
    }
    

    Step 3: Update Your Batch Drawing Code

    Replace your original pal_swap_batch_begin/pack_data/end calls with the new _vcolor versions.

    // Old Code:
    // pal_swap_batch_begin(spr_roof_palette, false);
    // for (var i = array_length(decorations) -1; i >= 0; --i) {
    // 	with (decorations[i]){
    // 		pal_swap_batch_pack_data(palette_index,1); // This function didn't actually modify vertex data for the old shader
    // 		Draw(); // Draws with the uniform set in pal_swap_batch_begin
    // 	}
    // }
    // pal_swap_batch_end();
    
    // New Code using per-vertex color alpha encoding:
    pal_swap_batch_begin_vcolor(spr_roof_palette, false); // Sets the batch shader and palette texture uniforms
    for (var i = array_length(decorations) -1; i >= 0; --i) {
    	with (decorations[i]){
            // Prepare the vertex data (specifically the alpha channel) for this object
    		pal_swap_batch_pack_data_vcolor(palette_index, image_alpha); // Encode palette index and object's alpha
    
            // Draw the object. GameMaker will use the draw_set_alpha value we just set,
            // passing it as the in_Colour.a vertex attribute to the shader.
    		Draw();
    	}
    }
    pal_swap_batch_end_vcolor(); // Resets the shader and draw state (alpha/colour)
    

    Explanation:

    • Shader: The new fragment shader shd_pal_swapper_batch reads v_vColour.a, which now contains an encoded byte value from 0-255. It decodes the lower 5 bits as the palette_index (0-31) and the upper 3 bits as an alpha_level (0-7). alpha_level is mapped back to a desired_alpha float (0.0-1.0). The findAltColor function is called with the decoded palette_index to get the looked-up color from the palette texture. Finally, the fragment color is calculated by applying v_vColour.rgb (usually 1,1,1 if you use draw_set_colour(c_white)) to the looked-up RGB, and setting the alpha to the looked-up alpha multiplied by the desired_alpha decoded from the vertex color.
    • encode_palette_alpha: This helper takes an integer palette index (0-31) and a float alpha (0.0-1.0), maps the alpha to 8 discrete levels (0-7), combines the index and alpha level into a single byte, and returns it scaled to 0.0-1.0 for draw_set_alpha.
    • pal_swap_batch_begin_vcolor: Sets the new batching shader and binds the palette texture and its UV/texel size uniforms once for the entire batch. It also saves the current draw state to restore later.
    • pal_swap_batch_pack_data_vcolor: This is called per object within the batch loop. It uses encode_palette_alpha to calculate the special alpha value needed for the shader and calls draw_set_alpha. Any subsequent drawing call (like Draw()) will use this encoded alpha value for the vertices. It also sets draw_set_colour(c_white) explicitly to ensure v_vColour.rgb is (1,1,1) unless you intend to use it for per-object color tinting in addition to palette swapping and alpha.
    • pal_swap_batch_end_vcolor: Resets the shader and restores the previous draw_set_alpha and draw_set_colour.

    This setup allows GameMaker to batch drawing calls for multiple objects, even if they use different palettes and alphas, because the varying data (palette index + desired alpha) is passed efficiently via the vertex color attribute rather than breaking the batch to set uniforms. Remember that using draw_set_colour and draw_set_alpha does affect the vertex data passed to the shader.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.