Path: blob/main_old/extensions/ANGLE_timer_query.txt
1693 views
Name12ANGLE_timer_query34Name Strings56GL_ANGLE_timer_query78Contributors910Contributors to ARB_occlusion_query11Contributors to EXT_timer_query12Contributors to ARB_timer_query13Ben Vanik, Google Inc.14Daniel Koch, TransGaming Inc.1516Contact1718Ben Vanik, Google Inc. (benvanik 'at' google 'dot' com)1920Status2122Draft2324Version2526Last Modified Date: Apr 28, 201127Author Revision: 12829Number3031OpenGL ES Extension #??3233Dependencies3435OpenGL ES 2.0 is required.3637The extension is written against the OpenGL ES 2.0 specification.3839Overview4041Applications can benefit from accurate timing information in a number of42different ways. During application development, timing information can43help identify application or driver bottlenecks. At run time,44applications can use timing information to dynamically adjust the amount45of detail in a scene to achieve constant frame rates. OpenGL46implementations have historically provided little to no useful timing47information. Applications can get some idea of timing by reading timers48on the CPU, but these timers are not synchronized with the graphics49rendering pipeline. Reading a CPU timer does not guarantee the completion50of a potentially large amount of graphics work accumulated before the51timer is read, and will thus produce wildly inaccurate results.52glFinish() can be used to determine when previous rendering commands have53been completed, but will idle the graphics pipeline and adversely affect54application performance.5556This extension provides a query mechanism that can be used to determine57the amount of time it takes to fully complete a set of GL commands, and58without stalling the rendering pipeline. It uses the query object59mechanisms first introduced in the occlusion query extension, which allow60time intervals to be polled asynchronously by the application.6162IP Status6364No known IP claims.6566New Procedures and Functions6768void GenQueriesANGLE(sizei n, uint *ids);69void DeleteQueriesANGLE(sizei n, const uint *ids);70boolean IsQueryANGLE(uint id);71void BeginQueryANGLE(enum target, uint id);72void EndQueryANGLE(enum target);73void QueryCounterANGLE(uint id, enum target);74void GetQueryivANGLE(enum target, enum pname, int *params);75void GetQueryObjectivANGLE(uint id, enum pname, int *params);76void GetQueryObjectuivANGLE(uint id, enum pname, uint *params);77void GetQueryObjecti64vANGLE(uint id, enum pname, int64 *params);78void GetQueryObjectui64vANGLE(uint id, enum pname, uint64 *params);7980New Tokens8182Accepted by the <pname> parameter of GetQueryivANGLE:8384QUERY_COUNTER_BITS_ANGLE 0x886485CURRENT_QUERY_ANGLE 0x88658687Accepted by the <pname> parameter of GetQueryObjectivANGLE,88GetQueryObjectuivANGLE, GetQueryObjecti64vANGLE, and89GetQueryObjectui64vANGLE:9091QUERY_RESULT_ANGLE 0x886692QUERY_RESULT_AVAILABLE_ANGLE 0x88679394Accepted by the <target> parameter of BeginQueryANGLE, EndQueryANGLE, and95GetQueryivANGLE:9697TIME_ELAPSED_ANGLE 0x88BF9899Accepted by the <target> parameter of GetQueryivANGLE and100QueryCounterANGLE:101102TIMESTAMP_ANGLE 0x8E28103104Additions to Chapter 2 of the OpenGL ES 2.0 Specification (OpenGL ES Operation)105106(Modify table 2.1, Correspondence of command suffix letters to GL argument)107Add two new types:108109Letter Corresponding GL Type110------ ---------------------111i64 int64ANGLE112ui64 uint64ANGLE113114(Modify table 2.2, GL data types) Add two new types:115116GL Type Minimum Bit Width Description117------- ----------------- -----------------------------118int64ANGLE 64 Signed 2's complement integer119uint64ANGLE 64 Unsigned binary integer120121Additions to Chapter 5 of the OpenGL ES 2.0 Specification (Special Functions)122123Add a new section 5.3 "Timer Queries":124125"5.3 Timer Queries126127Timer queries use query objects to track the amount of time needed to128fully complete a set of GL commands, or to determine the current time129of the GL.130131Timer queries are associated with query objects. The command132133void GenQueriesANGLE(sizei n, uint *ids);134135returns <n> previously unused query object names in <ids>. These136names are marked as used, but no object is associated with them until137the first time they are used by BeginQueryANGLE. Query objects contain138one piece of state, an integer result value. This result value is139initialized to zero when the object is created. Any positive integer140except for zero (which is reserved for the GL) is a valid query141object name.142143Query objects are deleted by calling144145void DeleteQueriesANGLE(sizei n, const uint *ids);146147<ids> contains <n> names of query objects to be deleted. After a148query object is deleted, its name is again unused. Unused names in149<ids> are silently ignored.150If an active query object is deleted its name immediately becomes unused,151but the underlying object is not deleted until it is no longer active.152153A timer query can be started and finished by calling154155void BeginQueryANGLE(enum target, uint id);156void EndQueryANGLE(enum target);157158where <target> is TIME_ELAPSED_ANGLE. If BeginQueryANGLE is called159with an unused <id>, that name is marked as used and associated with160a new query object.161162If BeginQueryANGLE is called with an <id> of zero, if the active query163object name for <target> is non-zero, if <id> is the name of an existing164query object whose type does not match <target>, or if <id> is the active165query object name for any query type, the error INVALID_OPERATION is166generated. If EndQueryANGLE is called while no query with the same target167is in progress, an INVALID_OPERATION error is generated.168169When BeginQueryANGLE and EndQueryANGLE are called with a <target> of170TIME_ELAPSED_ANGLE, the GL prepares to start and stop the timer used for171timer queries. The timer is started or stopped when the effects from all172previous commands on the GL client and server state and the framebuffer173have been fully realized. The BeginQueryANGLE and EndQueryANGLE commands174may return before the timer is actually started or stopped. When the timer175query timer is finally stopped, the elapsed time (in nanoseconds) is176written to the corresponding query object as the query result value, and177the query result for that object is marked as available.178179If the elapsed time overflows the number of bits, <n>, available to hold180elapsed time, its value becomes undefined. It is recommended, but not181required, that implementations handle this overflow case by saturating at1822^n - 1.183184The necessary state is a single bit indicating whether an timer185query is active, the identifier of the currently active timer186query, and a counter keeping track of the time that has passed.187188When the command189190void QueryCounterANGLE(uint id, enum target);191192is called with <target> TIMESTAMP_ANGLE, the GL records the current time193into the corresponding query object. The time is recorded after all194previous commands on the GL client and server state and the framebuffer195have been fully realized. When the time is recorded, the query result for196that object is marked available. QueryCounterANGLE timer queries can be197used within a BeginQueryANGLE / EndQueryANGLE block where the <target> is198TIME_ELAPSED_ANGLE and it does not affect the result of that query object.199The error INVALID_OPERATION is generated if the <id> is already in use200within a BeginQueryANGLE/EndQueryANGLE block."201202Additions to Chapter 6 of the OpenGL ES 2.0 Specification (State and State203Requests)204205Add a new section 6.1.9 "Timer Queries":206207"The command208209boolean IsQueryANGLE(uint id);210211returns TRUE if <id> is the name of a query object. If <id> is zero,212or if <id> is a non-zero value that is not the name of a query213object, IsQueryANGLE returns FALSE.214215Information about a query target can be queried with the command216217void GetQueryivANGLE(enum target, enum pname, int *params);218219<target> identifies the query target and can be TIME_ELAPSED_ANGLE or220TIMESTAMP_ANGLE for timer queries.221222If <pname> is CURRENT_QUERY_ANGLE, the name of the currently active query223for <target>, or zero if no query is active, will be placed in <params>.224225If <pname> is QUERY_COUNTER_BITS_ANGLE, the implementation-dependent number226of bits used to hold the query result for <target> will be placed in227<params>. The number of query counter bits may be zero, in which case228the counter contains no useful information.229230For timer queries (TIME_ELAPSED_ANGLE and TIMESTAMP_ANGLE), if the number231of bits is non-zero, the minimum number of bits allowed is 30 which232will allow at least 1 second of timing.233234The state of a query object can be queried with the commands235236void GetQueryObjectivANGLE(uint id, enum pname, int *params);237void GetQueryObjectuivANGLE(uint id, enum pname, uint *params);238void GetQueryObjecti64vANGLE(uint id, enum pname, int64 *params);239void GetQueryObjectui64vANGLE(uint id, enum pname, uint64 *params);240241If <id> is not the name of a query object, or if the query object242named by <id> is currently active, then an INVALID_OPERATION error is243generated.244245If <pname> is QUERY_RESULT_ANGLE, then the query object's result246value is returned as a single integer in <params>. If the value is so247large in magnitude that it cannot be represented with the requested type,248then the nearest value representable using the requested type is249returned. If the number of query counter bits for target is zero, then250the result is returned as a single integer with the value zero.251252There may be an indeterminate delay before the above query returns. If253<pname> is QUERY_RESULT_AVAILABLE_ANGLE, FALSE is returned if such a delay254would be required; otherwise TRUE is returned. It must always be true255that if any query object returns a result available of TRUE, all queries256of the same type issued prior to that query must also return TRUE.257258Querying the state for a given timer query forces that timer query to259complete within a finite amount of time.260261If multiple queries are issued on the same target and id prior to262calling GetQueryObject[u]i[64]vANGLE, the result returned will always be263from the last query issued. The results from any queries before the264last one will be lost if the results are not retrieved before starting265a new query on the same <target> and <id>."266267Errors268269The error INVALID_VALUE is generated if GenQueriesANGLE is called where270<n> is negative.271272The error INVALID_VALUE is generated if DeleteQueriesANGLE is called273where <n> is negative.274275The error INVALID_OPERATION is generated if BeginQueryANGLE is called276when a query of the given <target> is already active.277278The error INVALID_OPERATION is generated if EndQueryANGLE is called279when a query of the given <target> is not active.280281The error INVALID_OPERATION is generated if BeginQueryANGLE is called282where <id> is zero.283284The error INVALID_OPERATION is generated if BeginQueryANGLE is called285where <id> is the name of a query currently in progress.286287The error INVALID_OPERATION is generated if BeginQueryANGLE is called288where <id> is the name of an existing query object whose type does not289match <target>.290291The error INVALID_ENUM is generated if BeginQueryANGLE or EndQueryANGLE292is called where <target> is not TIME_ELAPSED_ANGLE.293294The error INVALID_ENUM is generated if GetQueryivANGLE is called where295<target> is not TIME_ELAPSED_ANGLE or TIMESTAMP_ANGLE.296297The error INVALID_ENUM is generated if GetQueryivANGLE is called where298<pname> is not QUERY_COUNTER_BITS_ANGLE or CURRENT_QUERY_ANGLE.299300The error INVALID_ENUM is generated if QueryCounterANGLE is called where301<target> is not TIMESTAMP_ANGLE.302303The error INVALID_OPERATION is generated if QueryCounterANGLE is called304on a query object that is already in use inside a305BeginQueryANGLE/EndQueryANGLE.306307The error INVALID_OPERATION is generated if GetQueryObjectivANGLE,308GetQueryObjectuivANGLE, GetQueryObjecti64vANGLE, or309GetQueryObjectui64vANGLE is called where <id> is not the name of a query310object.311312The error INVALID_OPERATION is generated if GetQueryObjectivANGLE,313GetQueryObjectuivANGLE, GetQueryObjecti64vANGLE, or314GetQueryObjectui64vANGLE is called where <id> is the name of a currently315active query object.316317The error INVALID_ENUM is generated if GetQueryObjectivANGLE,318GetQueryObjectuivANGLE, GetQueryObjecti64vANGLE, or319GetQueryObjectui64vANGLE is called where <pname> is not320QUERY_RESULT_ANGLE or QUERY_RESULT_AVAILABLE_ANGLE.321322New State323324(Add a new table 6.xx, "Query Operations")325326Get Value Type Get Command Initial Value Description Sec327--------- ---- ----------- ------------- ----------- ------328- B - FALSE query active 5.3329CURRENT_QUERY_ANGLE Z+ GetQueryivANGLE 0 active query ID 5.3330QUERY_RESULT_ANGLE Z+ GetQueryObjectuivANGLE, 0 samples-passed count 5.3331GetQueryObjectui64vANGLE332QUERY_RESULT_AVAILABLE_ANGLE B GetQueryObjectivANGLE FALSE query result available 5.3333334New Implementation Dependent State335336(Add the following entry to table 6.18):337338Get Value Type Get Command Minimum Value Description Sec339-------------------------- ---- ----------- ------------- ---------------- ------340QUERY_COUNTER_BITS_ANGLE Z+ GetQueryivANGLE see 6.1.9 Number of bits in 6.1.9341query counter342343Examples344345(1) Here is some rough sample code that demonstrates the intended usage346of this extension.347348GLint queries[N];349GLint available = 0;350// timer queries can contain more than 32 bits of data, so always351// query them using the 64 bit types to avoid overflow352GLuint64ANGLE timeElapsed = 0;353354// Create a query object.355glGenQueriesANGLE(N, queries);356357// Start query 1358glBeginQueryANGLE(GL_TIME_ELAPSED_ANGLE, queries[0]);359360// Draw object 1361....362363// End query 1364glEndQueryANGLE(GL_TIME_ELAPSED_ANGLE);365366...367368// Start query N369glBeginQueryANGLE(GL_TIME_ELAPSED_ANGLE, queries[N-1]);370371// Draw object N372....373374// End query N375glEndQueryANGLE(GL_TIME_ELAPSED_ANGLE);376377// Wait for all results to become available378while (!available) {379glGetQueryObjectivANGLE(queries[N-1], GL_QUERY_RESULT_AVAILABLE_ANGLE, &available);380}381382for (i = 0; i < N; i++) {383// See how much time the rendering of object i took in nanoseconds.384glGetQueryObjectui64vANGLE(queries[i], GL_QUERY_RESULT_ANGLE, &timeElapsed);385386// Do something useful with the time. Note that care should be387// taken to use all significant bits of the result, not just the388// least significant 32 bits.389AdjustObjectLODBasedOnDrawTime(i, timeElapsed);390}391392This example is sub-optimal in that it stalls at the end of every393frame to wait for query results. Ideally, the collection of results394would be delayed one frame to minimize the amount of time spent395waiting for the GPU to finish rendering.396397(2) This example is basically the same as the example above but uses398QueryCounter instead.399400GLint queries[N+1];401GLint available = 0;402// timer queries can contain more than 32 bits of data, so always403// query them using the 64 bit types to avoid overflow404GLuint64ANGLE timeStart, timeEnd, timeElapsed = 0;405406// Create a query object.407glGenQueriesANGLE(N+1, queries);408409// Query current timestamp 1410glQueryCounterANGLE(queries[0], GL_TIMESTAMP_ANGLE);411412// Draw object 1413....414415// Query current timestamp N416glQueryCounterANGLE(queries[N-1], GL_TIMESTAMP_ANGLE);417418// Draw object N419....420421// Query current timestamp N+1422glQueryCounterANGLE(queries[N], GL_TIMESTAMP_ANGLE);423424// Wait for all results to become available425while (!available) {426glGetQueryObjectivANGLE(queries[N], GL_QUERY_RESULT_AVAILABLE_ANGLE, &available);427}428429for (i = 0; i < N; i++) {430// See how much time the rendering of object i took in nanoseconds.431glGetQueryObjectui64vANGLE(queries[i], GL_QUERY_RESULT_ANGLE, &timeStart);432glGetQueryObjectui64vANGLE(queries[i+1], GL_QUERY_RESULT_ANGLE, &timeEnd);433timeElapsed = timeEnd - timeStart;434435// Do something useful with the time. Note that care should be436// taken to use all significant bits of the result, not just the437// least significant 32 bits.438AdjustObjectLODBasedOnDrawTime(i, timeElapsed);439}440441Issues from EXT_timer_query442443(1) What time interval is being measured?444445RESOLVED: The timer starts when all commands prior to BeginQuery() have446been fully executed. At that point, everything that should be drawn by447those commands has been written to the framebuffer. The timer stops448when all commands prior to EndQuery() have been fully executed.449450(2) What unit of time will time intervals be returned in?451452RESOLVED: Nanoseconds (10^-9 seconds). This unit of measurement allows453for reasonably accurate timing of even small blocks of rendering454commands. The granularity of the timer is implementation-dependent. A45532-bit query counter can express intervals of up to approximately 4456seconds.457458(3) What should be the minimum number of counter bits for timer queries?459460RESOLVED: 30 bits, which will allow timing sections that take up to 1461second to render.462463(4) How are counter results of more than 32 bits returned?464465RESOLVED: Via two new datatypes, int64ANGLE and uint64ANGLE, and their466corresponding GetQueryObject entry points. These types hold integer467values and have a minimum bit width of 64.468469(5) Should the extension measure total time elapsed between the full470completion of the BeginQuery and EndQuery commands, or just time471spent in the graphics library?472473RESOLVED: This extension will measure the total time elapsed between474the full completion of these commands. Future extensions may implement475a query to determine time elapsed at different stages of the graphics476pipeline.477478(6) If multiple query types are supported, can multiple query types be479active simultaneously?480481RESOLVED: Yes; an application may perform a timer query and another482type of query simultaneously. An application can not perform multiple483timer queries or multiple queries of other types simultaneously. An484application also can not use the same query object for another query485and a timer query simultaneously.486487(7) Do query objects have a query type permanently associated with them?488489RESOLVED: No. A single query object can be used to perform different490types of queries, but not at the same time.491492Having a fixed type for each query object simplifies some aspects of the493implementation -- not having to deal with queries with different result494sizes, for example. It would also mean that BeginQuery() with a query495object of the "wrong" type would result in an INVALID_OPERATION error.496497UPDATE: This resolution was relevant for EXT_timer_query and OpenGL 2.0.498Since EXT_transform_feedback has since been incorporated into the core,499the resolution is that BeginQuery will generate error INVALID_OPERATION500if <id> represents a query object of a different type.501502(8) How predictable/repeatable are the results returned by the timer503query?504505RESOLVED: In general, the amount of time needed to render the same506primitives should be fairly constant. But there may be many other507system issues (e.g., context switching on the CPU and GPU, virtual508memory page faults, memory cache behavior on the CPU and GPU) that can509cause times to vary wildly.510511Note that modern GPUs are generally highly pipelined, and may be512processing different primitives in different pipeline stages513simultaneously. In this extension, the timers start and stop when the514BeginQuery/EndQuery commands reach the bottom of the rendering pipeline.515What that means is that by the time the timer starts, the GL driver on516the CPU may have started work on GL commands issued after BeginQuery,517and the higher pipeline stages (e.g., vertex transformation) may have518started as well.519520(9) What should the new 64 bit integer type be called?521522RESOLVED: The new types will be called GLint64ANGLE/GLuint64ANGLE. The new523command suffixes will be i64 and ui64. These names clearly convey the524minimum size of the types. These types are similar to the C99 standard525type int_least64_t, but we use names similar to the C99 optional type526int64_t for simplicity.527528Issues from ARB_timer_query529530(10) What about tile-based implementations? The effects of a command are531not complete until the frame is completely rendered. Timing recorded532before the frame is complete may not be what developers expect. Also533the amount of time needed to render the same primitives is not534consistent, which conflicts with issue (8) above. The time depends on535how early or late in the scene it is placed.536537RESOLVED: The current language supports tile-based rendering okay as it538is written. Developers are warned that using timers on tile-based539implementation may not produce results they expect since rendering is not540done in a linear order. Timing results are calculated when the frame is541completed and may depend on how early or late in the scene it is placed.542543(11) Can the GL implementation use different clocks to implement the544TIME_ELAPSED and TIMESTAMP queries?545546RESOLVED: Yes, the implemenation can use different internal clocks to547implement TIME_ELAPSED and TIMESTAMP. If different clocks are548used it is possible there is a slight discrepancy when comparing queries549made from TIME_ELAPSED and TIMESTAMP; they may have slight550differences when both are used to measure the same sequence. However, this551is unlikely to affect real applications since comparing the two queries is552not expected to be useful.553554Issues555556(12) What should we call this extension?557558RESOLVED: ANGLE_timer_query559560(13) Why is this done as a separate extension instead of just supporting561ARB_timer_query?562563ARB_timer_query is written against OpenGL 3.2, which includes a lot of564the required support for dealing with query objects. None of these565functions or tokens exist in OpenGL ES, and as such have to be added in566this specification.567568(14) How does this extension differ from ARB_timer_query?569570This extension contains most ARB_timer_query behavior unchanged as well571as a subset of the query support required to use it from the core572OpenGL 3.2 spec. It omits the glGetInteger(TIMESTAMP) functionality used to573query the current time on the GPU, but the behavior for all remaining574functionality taken from ARB_timer_query is the same.575576(15) Are query objects shareable between multiple contexts?577578RESOLVED: No. Query objects are lightweight and we normally share579large data across contexts. Also, being able to share query objects580across contexts is not particularly useful. In order to do the async581query across contexts, a query on one context would have to be finished582before the other context could query it.583584Revision History585586Revision 1, 2011/04/28587- copied from revision 9 of ARB_timer_query and revision 7 of588ARB_occlusion_query589- removed language that was clearly not relevant to ES2590- rebased changes against the OpenGL ES 2.0 specification591592593