summaryrefslogtreecommitdiffstats
path: root/doc/src/07-file-formats/6-effect-reference.qdoc
blob: faaa08d2fc2209b102bb08617af272a2c7b64270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
\title Effect Reference
\page effect-reference.html
\ingroup qt3dstudio-file-formats

This page explains how to write effects.

\section1 Overview

The effects specification allows writing custom effects, which are applied to
layers. The effects can have multiple passes to calculate the final result.
Each shader in a pass must implement the \c {void frag()} and \c {void vert()}
functions for fragment and vertex shader respectively. The passes are executed
in the order they are specified in the effect file. The studio shader library
can also be accessed with \c #include directive to add functionality.

\note Unlike custom materials, effects have full support for multiple render
passes in the Qt 3D Studio 2.0 runtime.

\section1 Effect Structure

\badcode
<Effect>
    <MetaData>
        <Property name="FloatParam" type="Float" formalName="Float Param" description="" default="1.0"/>
        <Property name="TextureParam" type="Texture" filter="linear" clamp="clamp"/>
        <Property name="DepthTextureParam" type="Texture" filter="nearest" clamp="clamp"/>
    </MetaData>
    <Shaders>
        <Shared></Shared>
        <VertexShaderShared></VertexShaderShared>
        <FragmentShaderShared></FragmentShaderShared>
        <Shader name="PASS1">
            <VertexShader></VertexShader>
            <FragmentShader></FragmentShader>
        </Shader>
        <Shader name="PASS2">
            <VertexShader></VertexShader>
            <FragmentShader></FragmentShader>
        </Shader>
        <Shader name="MAIN">

            <VertexShader>
void vert()
{
}
            </VertexShader>
            <FragmentShader>
void frag()
{
}
            </FragmentShader>
        </Shader>
    </Shaders>
    <Passes>
        <Buffer name="buffer" type="fp16" format="rg" filter="nearest" wrap="clamp" size="1.0" lifetime="frame"/>
        <Buffer name="buffer2" type="fp16" format="rg" filter="nearest" wrap="clamp" size="1.0" lifetime="frame"/>
        <Buffer name="stencilBuffer" format='depth24stencil8' lifetime="frame" />

        <Pass shader="PASS1" input="[source]" output="buffer">
            <DepthInput param="DepthTextureParam"/>
            <DepthStencil buffer='stencilBuffer' flags='clear-stencil' stencil-fail='replace' depth-fail='replace' depth-pass='replace' stencil-function='always' reference='1'/>
        </Pass>

        <Pass shader="PASS2" input="buffer" output="buffer2">
            <BufferInput param="buffer" param="TextureParam" />
        </Pass>
        <Pass shader="MAIN" input="[source]" output="[dest]">
            <Blending source="SrcAlpha" dest="One"/>
            <SetParam name="FloatParam" value="0.0"/>
        </Pass>
    </Passes>
</Effect>

\endcode

The effect file structure follows the same pattern as custom materials, first is
the metadata with parameters in \c <Metadata> element, then effect shaders in
\c <Shaders> element and render passes in \c <Passes> element.

\section1 Parameters

Effect metadata parameters are the same as for custom materials, but also have
a couple of effect-specific parameters. Parameters with \c Texture type and the
\c <Buffer> and \c <BufferInput> elements allow using sampler-variables in
shader.

\note The Qt 3D Studio 2.0 runtime does not support other types of buffers
(shader storage buffers, indirect drawing, etc.). A temporary buffer specified
via the \c <Buffer> element maps to a texture. This can be bound to a sampler
variable in the shader via \c <BufferInput> in the individual passes.

\section1 Elements

\section2 <Shared>

This element can be used to insert block of shader code that is shared among all shaders.
It gets inserted to all shaders in the effect.

\section2 <VertexShaderShared> and <FragmentShaderShared>

These elements can be used to insert block of shader code that is shared among all vertex
and fragments shaders. The code inside \c <VertexShaderShared> element gets inserted to
all vertex shaders and similarly code inside \c <FragmentShaderShared> element gets inserted
to all fragment shaders.

\section2 <Shader>, <VertexShader> and <FragmentShader>

The \c <Shader> element specifies a shader with shader name specified with name attribute.
The name is used inside \c <Pass> element to specify which shader to execute.
\c <VertexShader> and \c <FragmentShader> elements are used to specify vertex shader code
and fragment shader code respectively. The vertex shader must implement the \c {void vert()}
function and fragment shader must implement the \c {void frag()} function, unless left empty.

The shader can use the \c varying, \c in and \c out specifiers to define custom variables
to pass parameters between shader stages.

\section2 <Passes>

The \c <Passes> element is used to specify render passes for the effect. Each
render pass must be specified inside \c <Pass> element. The \c <Passes> element
can also contain \c <Buffer> elements to specify different kinds of data
storages for the effect.

\note In the Qt 3D Studio 2.0 runtime a buffer always maps to a texture.

\section2 <Buffer>

This element can be used to create a buffer to be used inside the effect. Different passes
can read and write to these buffers while implementing different stages of the effect.
The \c <BufferInput> element can be used to bind the buffer to shader variable so that it
can be read in shader.

The element attributes are:
\table
\header
    \li attribute
    \li values
    \li description
\row
    \li name
    \li \c string
    \li Used to specify unique name for the buffer.
\row
    \li type
    \li \list
        \li \c ubyte - 8-bit unsigned integer
        \li \c ushort - 16-bit unsigned integer
        \li \c fp16 - 16-bit floating point number
        \li \c fp32 - 32-bit floating point number
        \endlist
    \li Used to specify the type of the buffer.
\row
    \li format
    \li \list
        \li \c source - take format from the source
        \li \c depth24stencil8 - combined depth24 stencil 8
        \li \c alpha - alpha
        \li \c lum - luminance
        \li \c lum_alpha - luminance alpha
        \li \c rg - 2-component RG
        \li \c rgb - 3-component RGB
        \li \c rgba - 4-component RGBA
        \endlist
    \li Used to specify the format of the buffer.
\row
    \li filter
    \li \list
        \li \c nearest - nearest neighbor
        \li \c linear - linear interpolation
        \endlist
    \li Used to specify the input filtering mode.
\row
    \li wrap
    \li \list
        \li \c clamp - clamp to edges
        \li \c repeat - repeat values
        \endlist
    \li Used to specify the wrapping mode of the texture coordinates.
\row
    \li size
    \li \c float
    \li Used to specify the size of the buffer relative to the frame buffer.
        1.0 corresponds to same size. 0.5 corresponds to the buffer width and height
        being half the size.
\row
    \li lifetime
    \li \list
        \li \c scene - The buffer is allocated at the beginning of the scene
        \li \c frame - The buffer is allocated at the beginning of the frame
        \endlist
    \li Used to specify how the buffer is allocated and deleted. Buffers with
        \c frame lifetime can be reused by other effects.
\endtable

\section2 <Pass>

This element specifies one render pass in effect. The passes
are executed in the order they are specified in the effect file.

The element attributes are:
\table
\header
    \li attribute
    \li values
    \li description
\row
    \li shader
    \li string
    \li The name of the shader executed in this pass. Must match to a shader name
        in the \c <Shaders> element.
\row
    \li input
    \li string
    \li The name of one of the \c <Buffer> elements or [source].
\row
    \li output
    \li string
    \li The name of one of the \c <Buffer> elements or [dest].
\row
    \li format
    \li \list
        \li \c alpha - alpha
        \li \c lum - luminance
        \li \c lum_alpha - luminance alpha
        \li \c rg - 2-component RG
        \li \c rgb - 3-component RGB
        \li \c rgba - 4-component RGBA
        \endlist
    \li If output is [dest], specifies the output format.
\endtable

\section2 <BufferInput>

This element binds a buffer (texture) to one of the shader sampler variables.

The element attributes are:
\table
\header
    \li attribute
    \li values
    \li description
\row
    \li value
    \li string
    \li The name of one of the \c <Buffer> elements or [source].
\row
    \li param
    \li string
    \li The name of the shader variable.
\endtable

\section2 <DepthInput>

This element binds frame buffer depth as a texture to shader sampler variable.

The element attributes are:
\table
\header
    \li attribute
    \li values
    \li description
\row
    \li param
    \li string
    \li The name of the shader variable.
\endtable

\section2 <SetParam>

This element can be used to set render pass specific shader parameters.
The parameter must match to a parameter specified in the \c <Metadata> element.

The element attributes are:
\table
\header
    \li attribute
    \li values
    \li description
\row
    \li name
    \li string
    \li The name of the property.
\row
    \li value
    \li float or integer
    \li Specifies the value of the property.
\endtable

\section2 <Blending>

This element can be used to change the blending of the a render pass.

The element attributes are:
\table
\header
    \li attribute
    \li values
    \li description
\row
    \li dest
    \li \list
        \li \c SrcAlpha
        \li \c OneMinusSrcAlpha
        \li \c One
        \endlist
    \li Specifies the destination blending mode.
\row
    \li source
    \li \list
        \li \c SrcAlpha
        \li \c OneMinusSrcAlpha
        \li \c One
        \endlist
    \li Specifies the source blending mode.
\endtable

\section2 <RenderState>

This element can be used to enable/disable a render state for a pass.

The element attributes are:
\table
\header
    \li attribute
    \li values
    \li description
\row
    \li name
    \li \list
        \li \c Stencil
        \li \c CullFace
        \endlist
    \li Name of the render state
\row
    \li value
    \li boolean
    \li Specifies whether to enable or disable the render state.
\endtable

\section2 <DepthStencil>

This element can be used to specify depth-stencil buffer and change the
depth-stencil test for a render pass.

The element attributes are:
\table
\header
    \li attribute
    \li values
    \li description
\row
    \li buffer
    \li string
    \li Name of the buffer used as a depth-stencil buffer.
\row
    \li flags
    \li \list
        \li \c clear-stencil - Clear stencil buffer
        \li \c clear-depth - Clear depth buffer
        \endlist
    \li Specifies flags for the element. These can be combined with logical or('|').
\row
    \li stencil-fail
    \li One of the stencil operation values
    \li Specifies stencil fail operation.
\row
    \li depth-fail
    \li One of the stencil operation values
    \li Specifies depth fail operation.
\row
    \li depth-pass
    \li One of the stencil operation values
    \li Specifies depth pass operation.
\row
    \li stencil-function
    \li One of the boolean operator values
    \li Specifies stencil function.
\row
    \li reference
    \li integer
    \li Specifies stencil reference value.
\row
    \li mask
    \li integer
    \li Specifies stencil mask value.
\endtable

The stencil operation values are:
\list
\li \c keep
\li \c zero
\li \c replace
\li \c increment
\li \c increment-wrap
\li \c decrement
\li \c decrement-wrap
\li \c invert
\endlist

The boolean operator values are:
\list
\li \c never
\li \c less
\li \c less-than-or-equal
\li \c equal
\li \c not-equal
\li \c greater
\li \c greater-than-or-equal
\li \c always
\endlist

\section2 <Culling>

This element can be used to select which faces are culled by CullFace.

The element attributes are:
\table
\header
    \li attribute
    \li values
    \li description
\row
    \li mode
    \li \list
        \li \c Back - Back faces.
        \li \c Front - Front faces.
        \li \c All - All faces.
        \endlist
    \li Specifies CullFace mode.
\endtable
*/