Advertisement
shurlburt

Graphics with Sophia

Aug 13th, 2016
1,417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.67 KB | None | 0 0
  1. Graphics with Sophia
  2.  
  3.  
  4. Let's begin. Maybe we should start with math.
  5. - Keep in mind, many graphics programmers are not good at math.
  6. - Wait, let's step back further-- you don't really need to know any of this, actually, to do a lot of cool things with graphics. But all this information is tools in your toolkit. When you approach a problem instead of needing to search through Google, you'll be a little more informed about it (and, well, can maybe make better Google searches :) ).
  7. - Also remember graphics is still a new field, compared to other disciplines you can learn about in school. In fact, graphics programming specifically changes drastically as GPU's change. This information is helpful, but the current techniques can be improved upon. Never hesitate to wonder how things can be better, and get creative.
  8.  
  9. Vectors and scalars
  10. - Scalar is just another word for a number, like an integer
  11. - Vectors have more values, 3 is common. Very useful for things like position or color.
  12.  
  13. Dot products
  14. - If two vectors overlap, the dot product is 1. If they are perpedicular (90 degree angle), the dot product is 0.
  15. - Lighting is such a great way to think about a potential use for dot products. If the light is perpendicular to an object, the dot product between the light and the normal of the surface (the direction the surface is facing) is 0-- it isn't lit, the light isn't shining on it. If the light is shining directly on the surface, the dot product between the light and the surface is 1-- full brightness. If it's at an angle, the dot product will be somewhere between 0 and 1, depending on what the angle is.
  16.  
  17. Scalars multiplying with vectors
  18. - A scalar, when multiplied with a vector, just multiplies each value of the vector by the scalar amount
  19.  
  20. Matrices
  21. - Matrices are very useful!
  22. - A large use of matrices is transforming a set of positions from one "space" into another.
  23. - UI is my favorite way to start thinking about this transformation. If I'm positioning units in a user interface that exists within the world, I don't want to think about where they actually lie in world position coordinates. It's nicer to think of, say the top corner of a grid, as the origin (0,0) coordinate. It's simpler. I can use a matrix to transform all the positions in my UI grid to world space when needed.
  24. - In graphics, you have a few important spaces. The specifics of the ones you end up using depend a bit on the graphics engine you use and the graphics API you use, but the basics remain the same. You have object/model space, world space, view space, and screen space. Matrices help you transform between each of these "spaces".
  25.  
  26. Wait, what's a graphics API anyway?
  27. - A graphics API is a set of instructions that your CPU sends to your GPU. Think things like "go draw this", "these are the vertices of the mesh you're drawing" or "this is where my camera is".
  28. - OpenGL is a graphics API, DirectX is a graphics API.
  29. - But aren't there many kinds of GPUs? How does one instruction work for all of them? Good question! This is where graphics drivers come into the picture. The graphics driver will take the instructions you've sent to the GPU, and turn them into instructions the particular GPU on your computer understands, along with specifics having to do with execution.
  30. - New graphics APIs like Vulkan or DirectX12 claim to be "lower level" graphics APIs in the sense that instead of relying so much on lots of driver code, you can provide more details the driver normally provides with your graphics API. This allows you to customize your code more, as well as bypass less-than-awesome drivers, but the downside is these APIs are more complicated.
  31.  
  32. Cool. So back to math...
  33. - Mathematics is a big deal in graphics!
  34. - A simple lighting equation could dot the normal with the direction of the light, then add an ambient value. Why the ambient value? Light bounces around everywhere in real life physics. We don't have time, energy, or compute power to deal with this in real time graphics. We have to fake it. We wish our computers were powerful enough to not need to fake it and have real time graphics. Adding the ambient value simulates the light bouncing everywhere and making things brighter.
  35. - Fog could use a similar concept, factoring in a distance value too.
  36. - A fancier solution to the "wait guys, light actually bounces everywhere, stop faking your lighting so much" problem would be something like global illumination and light probes-- sampling an image of the surrounding scene at points. For instance, if you're near a red wall, this sample image will have a big red blob on it. This will influence lighting, and make the side of your object more red.
  37. - How would we do specular? We could take the vector halfway between the normal and the light source, and dot this with the normal, and then square that. If you think about the math behind this, you can see how that would produce a more specular (shiny) light to an object. Just one solution!
  38. - Getting a good feel for mathematics is about much more than being able to solve equations without Google. If you have a good mental model for what different math operations do, if you can picture it, you can dream up artistic new effects and make them a reality.
  39.  
  40. Memory
  41. - Understanding memory is also so important.
  42. - A computer spends a significant amount of time modifying and moving around memory.
  43. - If we just had all this memory in one big block, it would take too long to access elements. We can usually figure out what we need to access fast, and keep that close.
  44. - "Keeping it close" brings us to the concept of caches. Thinking just in terms of CPU, we have the ALU, Register, L1, L2, and L3 cache-- each Level is a little farther out (harder to access) but stores a little more memory. Beyond that, we have disk and actually, network can be seen as an outer level in this mental model (streaming data from the internet, for instance).
  45. - GPUs are different.
  46.  
  47. Wait, how is GPU memory different?
  48. - GPUs process data in parallel. Very much in parallel.
  49. - Remember the ALU? GPU's have tons of those. The GPU is great at processing lots of very simple processes at the same time. The processes can't talk to each other at all, remember.
  50. - GPU's typically do also have an L1 and L2 cache, but just remember memory is more limited here and remember the parallel nature of the ALU's.
  51. - Also remember that the data sent to the GPU is sent from the CPU, over a bus. That's a bottleneck. The bus can hold a surprisngly large amount of data, but it's still something to remember.
  52. - What kind of data is sent over? Well, remember the part about graphics API's. Typically the biggest data sets will be texture data, and things like vertex buffers and index buffers.
  53.  
  54. What's a vertex buffer and an index buffer?
  55. - A vertex buffer contains a list of positions for all vertices in a mesh.
  56. - An index buffer will list which of these vertices form triangles, in order
  57. - You could just have a vertex buffer, but you'd have a lot of repeating data. An index buffer allows us to be smart about our memory usage so you can use the memory on more interesting things
  58. - Keep in mind, you can get creative with how you use buffer data sent to the GPU. Have you heard of voxels? You can send voxel data to the GPU in buffers, and read that in your shaders.
  59.  
  60. If I can get creative with how I send buffers, can I use the GPU for processes other than graphics?
  61. - Yes, absolutely! Check out compute shaders.
  62. - AI is using GPU's quite heavily, too. Any field that tends to do a lot of simple operations at the same time can make use of a GPU.
  63.  
  64. What other things do we keep in mind for optimization?
  65. - Always think about caches. Avoid cache misses. People tend to like arrays, for instance, because that data's contiguous in memory. That means when you load up an array, you're likely to also need other elements of the array, and since the arrays are contiguous in memory that block of memory will get pulled in together and other elements of the array will be in your cache too. This is opposed to structures like linked lists, where each element is just an element and a pointer to another element farther away.
  66. - Hardware's smart. It does try to fill the cache with relevant data-- for instance, it can detect whether you're accessing an array backwards or forwards, or grabbing every fourth element, and so on. Pretty amazing, actually. But knowing basics about how hardware is structured allows us to help it out.
  67. - GPU hardware is also smart. But the thing about examining GPU hardware/compilers/drivers too much is that it changes more frequently than the CPU side of things. However, we still benefit from keeping up to date with how GPU hardware is laid out. AMD and Intel are both pretty good about being open about specs for their GPUs, check out those resources.
  68. - We can also know things like if you have a lot of conditional statements in a shader, the GPU's likely to just execute everything. Remember, it has all those parallel units, it's trying to put them to use! So maybe avoid that if you know it's not necessary. By keeping up to date on GPU specs, we can also determine things like whether we should use more scalar or vector operations.
  69. - Remember that often, usability trumps optimization. Be educated about hardware and optimization, but don't sacrifice readable and usable code for optimized and unreadable code unless you have to.
  70.  
  71. Other notes
  72. - Spend some time with assembly, if you can, or microcontrollers. It gives interesting insight into hardware and the way things work under the hood. It's also really not that hard!
  73. - And I'll echo what I said in the beginning. So many graphics techniques are so new compared to techniques in other disciplines. You know, computer science is new in general! Experiment. Play. Don't be afraid to try a new idea.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement