Thank you for adding more details to clarify your question. My comments are too long, so I'm going to reply. Moving comments here to save them together:
Sorry I'm slow, but I'm trying to understand the problem and purpose. In your GLSL example, I do not see the generated matrix. I can see that one vec3 is generated by summing a random selection (changing over time) of cells from 15 x 15 textures (matrices). And this vec3 is recalculated for each pixel. Then vec3 is used as the color of the pixel.
So, I don’t understand if you really want to create a matrix or just want to calculate the value for each pixel. The latter is, in a sense, a “matrix”, but calculating a simple random value for 200 x 200 pixels will not strain your graphics driver. You also said that you want to use the matrix. Therefore, I do not think what you mean.
I'm trying to understand why you want to get a matrix - to keep a consistent random basis for all pixels? If so, you can either precompile the random texture, or use a sequential pseudo-random function, like you did in rand (), except for using time without using it. You clearly know about this, so I think I still do not understand the purpose. Why are you summing up a random selection of cells from the texture for each pixel?
I believe that the reason your shader crashes is because your main() function exceeds its time limits - either for a single pixel or for the entire set of pixels. Calling rand() 40,000 times per pixel (in a nested loop 200 * 200) can certainly explain this! If you had 200 x 200 pixels and you called sin () 40k times for each of them, then 160,000,000 calls per frame. Bad GPU!
I hope that if we understand the goal better, we can recommend a more effective way to get the desired effect.
Refresh.
(This part has been removed because it was mistakenly accepted. Although many cells in the original matrix may contribute less than the result of the visually detectable color to the result, the total number of many cells may contribute the visually detectable amount of color.)
New update based on an updated question.
OK, (thinking out loud here so you can check if I understand correctly ...) Since you need each of the random NxM values only once, there is no actual requirement to store them in a matrix; values can simply be calculated on demand and then thrown away. This is why your sample code above does not create a matrix.
This means that we cannot avoid generating (NxM) ^ 2 random values for each frame, that is, NxM random values per pixel, and there are NxM pixels. Thus, for N = M = 200, this is 160 million random values per frame.
However, we can still optimize some things.
- First, since your random values should only be one bit at a time (you only need a logical answer to decide whether to include each cell from the original texture in the mix), you can probably use a cheaper pseudo random number generator. The one you use outputs much more random data per call than one bit. For example, you could call the same PRNG function as you do now, but save the value and extract 32 random bits from it. Or at least a few, depending on how many of them are random enough. Also, instead of using the sin () function, if you have the GL_EXT_gpu_shader4 extension (for bitwise operators), you can use something like this :
.
int LFSR_Rand_Gen(in int n) {
- Secondly, you are currently doing one division action for each cell included (
/2.0 ), which is probably relatively expensive if the compiler and GPU cannot optimize it for bit-shift (is this possible with a floating point?) ., It also will not give the arithmetic mean of the input values, as discussed above ... it will bring much more weight to the later values and very little to the earlier ones. As a solution, consider how many values are included, and divide by that number once, after the loop ends.
Whether this optimization will be sufficient so that your GPU driver can control 200x200 * 200x200 pixels per frame, I do not know. They should definitely allow you to significantly increase resolution.
These are ideas that arise from my head. Although I am far from being an expert on the GPU. It would be great if someone more qualified could hear suggestions.
PS In your comment, you jokingly (?) Mentioned the option to pre-calculate N * M NxM random matrices. Maybe this is not a bad idea? 40,000x40,000 is a great texture (at least 40 MB), but if you store 32 bits of random data per cell, it will decrease to 1250 x 40,000 cells. Too bad the vanilla GLSL does not help you with bitwise operators to extract data, but even if you do not have the GL_EXT_gpu_shader4 extension, you can still fake it. (Maybe you also need a special extension for non-square textures?)