# The upperbound of the textures in Metal

**URL:** <https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105>\
**Category:** Metal by Tutorials\
**Created:** [January 31, 2019, 12:56pm UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105 "2019-01-31T12:56:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![monkyu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/monkyu/32/82294_2.png) [@monkyu](https://forums.kodeco.com/u/monkyu)\
**Post date:** [January 31, 2019, 12:56pm UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/1 "2019-01-31T12:56:50Z")

</div>

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?  
Thanks~

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [February 1, 2019, 7:23am UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/2 "2019-02-01T07:23:43Z")

</div>

You can find out the limits here:

> **[Metal-Feature-Set-Tables.pdf](https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf)**
>
> 2.80 MB

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?

---

<div class="post-metadata">

**Author:** ![monkyu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/monkyu/32/82294_2.png) [@monkyu](https://forums.kodeco.com/u/monkyu)\
**Post date:** [February 1, 2019, 12:12pm UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/3 "2019-02-01T12:12:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [February 1, 2019, 10:16pm UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/4 "2019-02-01T22:16:48Z")

</div>

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:

> **[Rendering Text in Metal with Signed-Distance Fields](https://metalbyexample.com/rendering-text-in-metal-with-signed-distance-fields/#more-501)**
>
> In this article, we will discuss a method for rendering high-fidelity text with Metal. It’s easy to overlook text rendering when thinking about 3D graphics. However, very few games or applica…

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?

---

<div class="post-metadata">

**Author:** ![monkyu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/monkyu/32/82294_2.png) [@monkyu](https://forums.kodeco.com/u/monkyu)\
**Post date:** [February 2, 2019, 5:36am UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/5 "2019-02-02T05:36:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [February 2, 2019, 11:32am UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/6 "2019-02-02T11:32:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![monkyu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/monkyu/32/82294_2.png) [@monkyu](https://forums.kodeco.com/u/monkyu)\
**Post date:** [February 2, 2019, 3:21pm UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/7 "2019-02-02T15:21:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![monkyu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/monkyu/32/82294_2.png) [@monkyu](https://forums.kodeco.com/u/monkyu)\
**Post date:** [August 29, 2019, 12:26pm UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/8 "2019-08-29T12:26:11Z")

</div>

Hi, caroline

> **[GitHub - MonkYU/ParticlesText: 基于文本的粒子动画 - Text diffusion animation consist...](https://github.com/MonkYU/ParticlesText)**
>
> 基于文本的粒子动画 - Text diffusion animation consist of particles in iOS project - GitHub - MonkYU/ParticlesText: 基于文本的粒子动画 - Text diffusion animation consist of particles in iOS project

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

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [August 30, 2019, 11:46pm UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/9 "2019-08-30T23:46:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![monkyu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/monkyu/32/82294_2.png) [@monkyu](https://forums.kodeco.com/u/monkyu)\
**Post date:** [September 2, 2019, 3:23am UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/10 "2019-09-02T03:23:21Z")

</div>

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](https://github.com/MonkYU/ParticlesText/commits/master). 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.  
[https://developer.apple.com/documentation/metal/calculating\_threadgroup\_and\_grid\_sizes?language=objc](https://developer.apple.com/documentation/metal/calculating_threadgroup_and_grid_sizes?language=objc)  
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

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 2, 2019, 4:34am UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/11 "2019-09-02T04:34:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![monkyu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/monkyu/32/82294_2.png) [@monkyu](https://forums.kodeco.com/u/monkyu)\
**Post date:** [September 2, 2019, 7:42am UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/12 "2019-09-02T07:42:47Z")

</div>

Really?  
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.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [September 3, 2019, 12:21am UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/13 "2019-09-03T00:21:03Z")

</div>

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. ![06%20am](https://files.chunter.kodeco.com/original/3X/b/d/bd0720a9b4f6fbbc03dfb6e81542418d6029331e.png)

---

<div class="post-metadata">

**Author:** ![monkyu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/monkyu/32/82294_2.png) [@monkyu](https://forums.kodeco.com/u/monkyu)\
**Post date:** [September 3, 2019, 10:00am UTC](https://forums.kodeco.com/t/the-upperbound-of-the-textures-in-metal/61105/14 "2019-09-03T10:00:48Z")

</div>

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