The upperbound of the textures in Metal

Anyone know the upperbound of the textures in Metal
Refer to : setFragmentTextures:withRange:
Recently, I want to render lots of subtitles use the kernel function, steps as below:

  1. turn the each subtitle to texture? so it’s may be a lots of texture need to send the GPU
  2. in the kernel function, update the position for each texture
  3. if some texture was out of the screen, then reuse the texture or update the list of textures

So, use the - (void)setFragmentTextures:(id _Nullable const *)textures withRange:(NSRange)range is the correct way?

You can find out the limits here:

Maximum number of entries in the texture argument table, per graphics or compute function is 31 for mobile devices and Apple TV and 128 for Macs.

I’m afraid I don’t really understand exactly what you want to do. Have you tried putting subtitles in a normal NSView or UIView and is it too slow?

1 Like

I do put subtitle in a normal MTKView, for example, I turn one text to one texture and send it to shader function and then update the position x, so it will move from the right to left in mobile phone screen. But, we know that there could be 100 or 1000 subtitles display in the screen and update it’s position at the same time, so what should I do is to create 100 or 1000 textures? Or other better way to solve this?

Another way I can think of is alloc MTLBuffer and put 100 or 1000 subtitles vertices to it. Is this a good way?
Thank you for your help.

I haven’t done much with text in Metal, I tend to use other frameworks for it, such as CATextLayer.

Warren Moore has written this on rendering text with signed distance fields:

But if you have your text successfully rendering into a texture, then can you create three textures each the width of the screen, and scroll them across the screen. As one texture goes off the edge of the screen, move it back to the original position and fill it with the next text. And cycle round the three textures like this?

Yes, as you said, three texture can handle this situation, but generally speaking,there are more than three subtitles, may be I misunderstand your opinion, one texture contains lots of subtitles or one texture contain one subtitle, is it?
What I have done is turn one text to one texture and change the position, as you see, it’s easy…
I will read about the article to figure out the detail about render text use Metal.
It’s more like the barrage system in the live app, everyone can write words and display in the screen

I just meant reuse the texture that’s off the screen for the next subtitle and keep cycling, rather than creating all the textures up front. I don’t know that this is better - I haven’t tried it.

I get it ! , I will try this and other method to see which one is feasible.
Thanks for help so much.

1 Like

Hi, caroline

according the book, i create this project to complete some particles animation,but
if i change the text to another, the animation will be disorder, so could u give me some tips about this?
thanks so much

I’ve taken a look at the project (nice particles btw!), and I don’t really understand what your problem is.

Perhaps you could rephrase it. My Objective-C is very rusty, so if you could break the project down into a smaller one that demonstrates the actual problem, that would help.

But, depending on what the problem is, I don’t know that I have anything concrete to say about it!

Thank you so much for taking the time to look at this project!! (my math is not too good!)
I have described the problem in the zip file and include the video, zip file in my latest commit. From my point, the problem is the threadGrid and thredGroup, maybe the calculation is not suitable for every case. And if the property of the text not set right, this phenomenon will happen again.
According to this document can not solve this problem, because when I use the method to set threadGrid and threadGroup, it crash for some border reason

I still can’t get it to crash or look wrong - I’ve tried various builder.text:

  1. a
  2. abcdef
  3. abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy
  4. abcdefg

They are all fine.

I’ve tried it using Catalina, Xcode 11 and both an iPad Pro 11" running iOS 13 and an iPhone 6s running iOS 12.

What is your hardware and software configuration?

My configuration is iPhone 8 plus, 12.0.1, Xcode 10.2, and when I change text to abcdef, the animation will be disordered but not crash, this problem won’t lead crash but disorder, I will be release if this situation never happen to you, but, do you think the method is right to calculate threadGrid and threadGroup, actually, I pretty sure that is the problem.

I’m sorry, I don’t really have an answer for you. All I can say is that your code didn’t crash or appear disordered on my devices.

We recommend that you use dispatchThreads rather than dispatchThreadgroups, as Metal will take care of grid edges and solve thread under-utilisation. This is described in Chapter 16 of the book.

However, dispatchThreads is only supported on A9 GPUs and newer.

There is a warning in your metal shader that might cause a crash.

I got this, so appreciate for your help!
I need review this logic, maybe other api can solve this weird problem.

1 Like