Shader Source Code

I’ve posted the source code to a basic implementation of the Cocoa Shaders I talked about here. There’s code for conditional shaders, shader lists, clip shaders, and affine transform shaders. As far as shaders that actually draw there’s a solid color shader and one for drawing an image. The image shader lets you tweak the compositing operation, source rect, and set a drawing scale.

Included is a sample app to show you how you could use this stuff in practice. The app has a custom view which draws using a shader. The demo shader will change its background from blue to red when the view is clicked. Its implemented using only the simple shaders provided.

Fancier gradient fill shaders, CoreImage based shaders and that kind of thing are left, for now, as an exercise for the reader. I mentioned it in the original post but if you’re looking to do gradients you’d do yourself a favour by looking at CTGradient.

One thought on “Shader Source Code”

  1. Hi Guy,

    Thanks for sharing your ideas regarding expressing your custom drawing code as a data structure. I wanted to send you an email, but I can’t find your email address, so I’m just going to you use this comment box instead!

    Anyway, your Cocoa Shaders blog post was very influential to me, and I implemented a similar system in Python. It suited my purpose, but I never did anything with shaders in Cocoa until I discovered the “TTStyle” code in Joe Hewitt’s Three20 library for the iPhone. He has implemented a shader system very similar to yours, except that it it expressed strictly as a linked list.

    So while I was reading Joe’s shader code, I remembered what you said about building a tool to construct shader trees. Since one of the purposes of Three20’s shader system is to allow clients of the library to customize/theme its view components, I figured that this would be a perfect place for a more user-friendly tool to play around with the shader lists. And so I began building TTStyleBuilder (

    Anyway, I just wanted to show you what I built since your blog post was the inspiration.

    Thanks! -keith

Comments are closed.