'fast' operations (contrast, opacity, etc.) no longer seem fast

I had expected that setting an attribute like contrast on a GratingStim would be a fast operation (as per the docs), but it seems to be very slow in the current version (2023.2.3).

For example, the script below just shows a GratingStim with a random noise texture. On each frame, the contrast is set (to a constant, just to trigger the setting consequences). A clock prints the elapsed time for setting the contrast and drawing the stimulus.

When I run this on the current version, those operations take about 100 milliseconds per frame on my machine - greatly exceeding any time budget for not dropping frames.

If I go back to version 2020.2.10 and run the same script, the operations then take the expected around 0.3 milliseconds per frame. If I go to version 2021.1.0, then it is back to 100 milliseconds per frame.

Having a look at the source code, it seems like setting these attributes now require a rebuild of the texture (and the associated time cost). That seems like unintended behaviour, given it makes setting the attributes in a temporal context not feasible - but I thought I’d check here before opening an issue on Github to see if I’ve missed something.

import psychopy.visual
import psychopy.core

import numpy as np

tex_size = 2048

tex = np.random.rand(tex_size, tex_size) * 2 - 1

with psychopy.visual.Window(
    size=(600, 600), units="pix", fullscr=False
) as win:

    stim = psychopy.visual.GratingStim(
        win=win,
        size=(tex_size, tex_size),
        tex=tex,
    )

    timer = psychopy.core.Clock()
    op_timer = psychopy.core.Clock()

    while timer.getTime() < 5:

        op_timer.reset()
        stim.contrast = 1
        stim.draw()
        print(f"{op_timer.getTime() * 1000:.04f} ms")

        win.flip()

Described in an issue on Github and fixed by Jon :slight_smile: