graphics.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. Rendering Graphics
  2. ==================
  3. Libobs has a custom-made programmable graphics subsystem that wraps both
  4. Direct3D 11 and OpenGL. The reason why it was designed with a custom
  5. graphics subsystem was to accommodate custom capture features only
  6. available on specific operating systems.
  7. *(Author's note: In retrospect, I probably should have used something
  8. like ANGLE, but I would have to modify it to accommodate my specific
  9. use-cases.)*
  10. Most rendering is dependent upon effects. Effects are used by all video
  11. objects in libobs; they're used to easily bundle related vertex/pixel
  12. shaders in to one file.
  13. An effect file has a nearly identical syntax to Direct3D 11 HLSL effect
  14. files. The only differences are as follows:
  15. - Sampler states are named "sampler_state"
  16. - Position semantic is called "POSITION" rather than "SV_Position"
  17. - Target semantic is called "TARGET" rather than "SV_Target"
  18. *(Author's note: I'm probably missing a few exceptions here, if I am
  19. please let me know)*
  20. The Graphics Context
  21. --------------------
  22. Using graphics functions isn't possible unless the current thread has
  23. entered a graphics context, and the graphics context can only be used by
  24. one thread at a time. To enter the graphics context, use
  25. :c:func:`obs_enter_graphics()`, and to leave the graphics context, use
  26. :c:func:`obs_leave_graphics()`.
  27. Certain callback will automatically be within the graphics context:
  28. :c:member:`obs_source_info.video_render`, and the draw callback
  29. parameter of :c:func:`obs_display_add_draw_callback()`, and
  30. :c:func:`obs_add_main_render_callback()`.
  31. Creating Effects
  32. ----------------
  33. Effect Parameters
  34. ^^^^^^^^^^^^^^^^^
  35. To create an effect, it's recommended to start with the uniforms
  36. (parameters) of the effect.
  37. There are a number of different types of uniforms:
  38. +------------------+---------------+------------------+------------+------------+
  39. | Floating points: | **float** | **float2** | **float3** | **float4** |
  40. +------------------+---------------+------------------+------------+------------+
  41. | Matrices: | **float3x3** | **float4x4** | | |
  42. +------------------+---------------+------------------+------------+------------+
  43. | Integers: | **int** | **int2** | **int3** | **int4** |
  44. +------------------+---------------+------------------+------------+------------+
  45. | Booleans: | **bool** | | | |
  46. +------------------+---------------+------------------+------------+------------+
  47. | Textures: | **texture2d** | **texture_cube** | | |
  48. +------------------+---------------+------------------+------------+------------+
  49. To get the effect uniform parameters, you use
  50. :c:func:`gs_effect_get_param_by_name()` or
  51. :c:func:`gs_effect_get_param_by_idx()`.
  52. Then the uniforms are set through the following functions:
  53. - :c:func:`gs_effect_set_bool()`
  54. - :c:func:`gs_effect_set_float()`
  55. - :c:func:`gs_effect_set_int()`
  56. - :c:func:`gs_effect_set_matrix4()`
  57. - :c:func:`gs_effect_set_vec2()`
  58. - :c:func:`gs_effect_set_vec3()`
  59. - :c:func:`gs_effect_set_vec4()`
  60. - :c:func:`gs_effect_set_texture()`
  61. There are two "universal" effect parameters that may be expected of
  62. effects: **ViewProj**, and **image**. The **ViewProj** parameter
  63. (which is a float4x4) is used for the primary view/projection matrix
  64. combination. The **image** parameter (which is a texture2d) is a
  65. commonly used parameter for the main texture; this parameter will be
  66. used with the functions :c:func:`obs_source_draw()`,
  67. :c:func:`gs_draw_sprite()`, and
  68. :c:func:`obs_source_process_filter_end()`.
  69. Here is an example of effect parameters:
  70. .. code:: cpp
  71. uniform float4x4 ViewProj;
  72. uniform texture2d image;
  73. uniform float4 my_color_param;
  74. uniform float my_float_param;
  75. Effect parameters can also have default values. Default parameters of
  76. elements that have multiple elements should be treated as an array.
  77. Here are some examples of default parameters:
  78. .. code:: cpp
  79. uniform float4x4 my_matrix = {1.0, 0.0, 0.0, 0.0,
  80. 0.0, 1.0, 0.0, 0.0,
  81. 0.0, 0.0, 1.0, 0.0,
  82. 0.0, 0.0, 0.0, 1.0};
  83. uniform float4 my_float4 = {1.0, 0.5, 0.25, 0.0};
  84. uniform float my_float = 4.0;
  85. uniform int my_int = 5;
  86. Effect Sampler States
  87. ^^^^^^^^^^^^^^^^^^^^^
  88. Then, if textures are used, sampler states should be defined. Sampler
  89. states have certain sub-parameters:
  90. - **Filter** - The type of filtering to use. Can be one of the
  91. following values:
  92. - **Anisotropy**
  93. - **Point**
  94. - **Linear**
  95. - **MIN_MAG_POINT_MIP_LINEAR**
  96. - **MIN_POINT_MAG_LINEAR_MIP_POINT**
  97. - **MIN_POINT_MAG_MIP_LINEAR**
  98. - **MIN_LINEAR_MAG_MIP_POINT**
  99. - **MIN_LINEAR_MAG_POINT_MIP_LINEAR**
  100. - **MIN_MAG_LINEAR_MIP_POINT**
  101. - **AddressU**, **AddressV** - Specifies how to handle the sampling
  102. when the coordinate goes beyond 0.0..1.0. Can be one of the following
  103. values:
  104. - **Wrap** or **Repeat**
  105. - **Clamp** or **None**
  106. - **Mirror**
  107. - **Border** (uses *BorderColor* to fill the color)
  108. - **MirrorOnce**
  109. - **BorderColor** - Specifies the border color if using the "Border"
  110. address mode. This value should be a hexadecimal value representing
  111. the color, in the format of: AARRGGBB. For example, 7FFF0000 would
  112. have its alpha value at 127, its red value at 255, and blue and green
  113. at 0. If *Border* is not used as an addressing type, this value is
  114. ignored.
  115. Here is an example of writing a sampler state in an effect file:
  116. .. code:: cpp
  117. sampler_state defaultSampler {
  118. Filter = Linear;
  119. AddressU = Border;
  120. AddressV = Border;
  121. BorderColor = 7FFF0000;
  122. };
  123. This sampler state would use linear filtering, would use border
  124. addressing for texture coordinate values beyond 0.0..1.0, and the border
  125. color would be the color specified above.
  126. When a sampler state is used, it's used identically to the HLSL form:
  127. .. code:: cpp
  128. [...]
  129. uniform texture2d image;
  130. sampler_state defaultSampler {
  131. Filter = Linear;
  132. AddressU = Clamp;
  133. AddressV = Clamp;
  134. };
  135. [...]
  136. float4 MyPixelShaderFunc(VertInOut vert_in) : TARGET
  137. {
  138. return image.Sample(def_sampler, vert_in.uv);
  139. }
  140. Effect Vertex/Pixel Semantics
  141. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  142. Then structures should be defined for inputs and outputs vertex
  143. semantics.
  144. Vertex components can have the following semantics:
  145. - **COLOR** - Color value (*float4*).
  146. - **POSITION** - Position value (*float4*).
  147. - **NORMAL** - Normal value (*float4*).
  148. - **TANGENT** - Tangent value (*float4*).
  149. - **TEXCOORD[0..7]** - Texture cooordinate value (*float2*, *float3*, or
  150. *float4*).
  151. Here is an example of a vertex semantic structure:
  152. .. code:: cpp
  153. struct VertexIn {
  154. float4 my_position : POSITION;
  155. float2 my_texcoord : TEXCOORD0;
  156. };
  157. These semantic structures are then passed in as a parameter to the
  158. primary shader entry point, and used as a return value for the vertex
  159. shader. Note that the vertex shader is allowed to return different
  160. semantics than it takes in; but the return type of the vertex shader and
  161. the parameter of the pixel shader must match.
  162. The semantic structure used for the parameter to the vertex shader
  163. function will require that the vertex buffer have those values, so if
  164. you have POSITION and TEXCOORD0, the vertex buffer will have to have at
  165. least a position buffer and a texture coordinate buffer in it.
  166. For pixel shaders, they need to return with a TARGET semantic (which is
  167. a float4 RGBA value). Here is an example of how it's usually used with
  168. a pixel shader function:
  169. .. code:: cpp
  170. float4 MyPixelShaderFunc(VertInOut vert_in) : TARGET
  171. {
  172. return image.Sample(def_sampler, vert_in.uv);
  173. }
  174. Effect Techniques
  175. ^^^^^^^^^^^^^^^^^
  176. Techniques are used to define the primary vertex/pixel shader entry
  177. functions per pass. One technique can have multiple passes or custom
  178. pass setup.
  179. *(Author's note: These days, multiple passes aren't really needed; GPUs
  180. are powerful enough to where you can perform all actions in the same
  181. shader. Named passes can be useful for custom draw setups, but even
  182. then you can just make it a separate technique. For that reason, it's
  183. best to just ignore the extra pass functionality.)*
  184. If you're making an effect filter for video sources, typically you'd
  185. name the pass **Draw**, and then
  186. :c:func:`obs_source_process_filter_end()` will automatically call that
  187. specific effect name. However, you can also use
  188. :c:func:`obs_source_process_filter_tech_end()` to make the filter use a
  189. specific technique by its name.
  190. The first parameter of the vertex/pixel shader functions in passes
  191. should always be the name of its vertex semantic structure parameter.
  192. For techniques, it's better to show some examples of how techniques
  193. would be used:
  194. .. code:: cpp
  195. uniform float4x4 ViewProj;
  196. uniform texture2d image;
  197. struct VertInOut {
  198. float4 my_position : POSITION;
  199. float2 my_texcoord : TEXCOORD0;
  200. };
  201. VertInOut MyVertexShaderFunc(VertInOut vert_in)
  202. {
  203. VertInOut vert_out;
  204. vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj);
  205. vert_out.uv = vert_in.uv;
  206. return vert_out;
  207. }
  208. float4 MyPixelShaderFunc(VertInOut vert_in) : TARGET
  209. {
  210. return image.Sample(def_sampler, vert_in.uv);
  211. }
  212. technique Draw
  213. {
  214. pass
  215. {
  216. vertex_shader = MyVertexShaderFunc(vert_in);
  217. pixel_shader = MyPixelShaderFunc(vert_in);
  218. }
  219. };
  220. Using Effects
  221. -------------
  222. The recommended way to use effects is like so:
  223. .. code:: cpp
  224. for (gs_effect_loop(effect, "technique")) {
  225. [draw calls go here]
  226. }
  227. This will automatically handle loading/unloading of the effect and its
  228. shaders for a given technique name.
  229. Rendering Video Sources
  230. -----------------------
  231. A synchronous video source renders in its
  232. :c:member:`obs_source_info.video_render` callback.
  233. Sources can render with custom drawing (via the OBS_SOURCE_CUSTOM_DRAW
  234. output capability flag), or without. When sources render without custom
  235. rendering, it's recommended to render a single texture with
  236. :c:func:`obs_source_draw()`. Otherwise the source is expected to
  237. perform rendering on its own and manage its own effects.
  238. Libobs comes with a set of default/standard effects that can be accessed
  239. via the :c:func:`obs_get_base_effect()` function. You can use these
  240. effects to render, or you can create custom effects with
  241. :c:func:`gs_effect_create_from_file()` and render with a custom effect.
  242. Rendering Video Effect Filters
  243. ------------------------------
  244. For most video effect filters, it comprises of adding a layer of
  245. processing shaders to an existing image in its
  246. :c:member:`obs_source_info.video_render` callback. When this is the
  247. case, it's expected that the filter has its own effect created, and to
  248. draw the effect, one would simply use the
  249. :c:func:`obs_source_process_filter_begin()` function, set the parameters
  250. on your custom effect, then call either
  251. :c:func:`obs_source_process_filter_end()` or
  252. :c:func:`obs_source_process_filter_tech_end()` to finish rendering the
  253. filter.
  254. Here's an example of rendering a filter from the color key filter:
  255. .. code:: cpp
  256. static void color_key_render(void *data, gs_effect_t *effect)
  257. {
  258. struct color_key_filter_data *filter = data;
  259. if (!obs_source_process_filter_begin(filter->context, GS_RGBA,
  260. OBS_ALLOW_DIRECT_RENDERING))
  261. return;
  262. gs_effect_set_vec4(filter->color_param, &filter->color);
  263. gs_effect_set_float(filter->contrast_param, filter->contrast);
  264. gs_effect_set_float(filter->brightness_param, filter->brightness);
  265. gs_effect_set_float(filter->gamma_param, filter->gamma);
  266. gs_effect_set_vec4(filter->key_color_param, &filter->key_color);
  267. gs_effect_set_float(filter->similarity_param, filter->similarity);
  268. gs_effect_set_float(filter->smoothness_param, filter->smoothness);
  269. obs_source_process_filter_end(filter->context, filter->effect, 0, 0);
  270. UNUSED_PARAMETER(effect);
  271. }