📄 slang_core.gc
字号:
/* * Mesa 3-D graphics library * Version: 6.5 * * Copyright (C) 2006 Brian Paul All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *///// This file defines nearly all constructors and operators for built-in data// types, using extended language syntax. In general, compiler treats// constructors and operators as ordinary functions with some exceptions.// For example, the language does not allow functions to be called in// constant expressions - here the exception is made to allow it.//// Each implementation provides its own version of this file. Each// implementation can define the required set of operators and constructors// in its own fashion.//// The extended language syntax is only present when compiling this file.// It is implicitly included at the very beginning of the compiled shader,// so no built-in functions can be used.//// To communicate with the implementation, a special extended "__asm" keyword// is used, followed by an instruction name (any valid identifier), a// destination variable identifier and a list of zero or more source// variable identifiers.//// A variable identifier is a variable name declared earlier in the code// (as a function parameter, local or global variable).//// An instruction name designates an instruction that must be exported// by the implementation. Each instruction receives data from source// variable identifiers and returns data in the destination variable// identifier.//// It is up to the implementation how to define a particular operator// or constructor. If it is expected to being used rarely, it can be// defined in terms of other operators and constructors,// for example://// ivec2 __operator + (const ivec2 x, const ivec2 y) {// return ivec2 (x[0] + y[0], x[1] + y[1]);// }//// If a particular operator or constructor is expected to be used very// often or is an atomic operation (that is, an operation that cannot be// expressed in terms of other operations or would create a dependency// cycle) it must be defined using one or more __asm constructs.//// Each implementation must define constructors for all scalar types// (bool, float, int). There are 9 scalar-to-scalar constructors// (including identity constructors). However, since the language// introduces special constructors (like matrix constructor with a single// scalar value), implementations must also implement these cases.// The compiler provides the following algorithm when resolving a constructor:// - try to find a constructor with a prototype matching ours,// - if no constructor is found and this is a scalar-to-scalar constructor,// raise an error,// - if a constructor is found, execute it and return,// - count the size of the constructor parameter list - if it is less than// the size of our constructor's type, raise an error,// - for each parameter in the list do a recursive constructor matching for// appropriate scalar fields in the constructed variable,//// Each implementation must also define a set of operators that deal with// built-in data types.// There are four kinds of operators:// 1) Operators that are implemented only by the compiler: "()" (function// call), "," (sequence) and "?:" (selection).// 2) Operators that are implemented by the compiler by expressing it in// terms of other operators:// - "." (field selection) - translated to subscript access,// - "&&" (logical and) - translated to "<left_expr> ? <right_expr> :// false",// - "||" (logical or) - translated to "<left_expr> ? true : <right_expr>",// 3) Operators that can be defined by the implementation and if the required// prototype is not found, standard behaviour is used:// - "==", "!=", "=" (equality, assignment) - compare or assign// matching fields one-by-one;// note that at least operators for scalar data types must be defined// by the implementation to get it work,// 4) All other operators not mentioned above. If no required prototype is// found, an error is raised. An implementation must follow the language// specification to provide all valid operator prototypes.////// Basic, scalar constructors/castsint __constructor(const float f){ __asm vec4_to_ivec4 __retVal, f;}int __constructor(const bool b){ __retVal = b;}int __constructor(const int i){ __retVal = i;}bool __constructor(const int i){ __asm vec4_sne __retVal, i, 0.0;}bool __constructor(const float f){ __asm vec4_sne __retVal, f, 0.0;}bool __constructor(const bool b){ __retVal = b;}float __constructor(const int i){ __asm ivec4_to_vec4 __retVal, i;}float __constructor(const bool b){ __asm ivec4_to_vec4 __retVal, b;}float __constructor(const float f){ __retVal = f;}//// vec2 constructorsvec2 __constructor(const float x, const float y){ __retVal.x = x; __retVal.y = y;}vec2 __constructor(const float f){ __asm vec4_move __retVal.xy, f;}vec2 __constructor(const int i){ __asm ivec4_to_vec4 __retVal.xy, i;}vec2 __constructor(const bool b){ __asm ivec4_to_vec4 __retVal.xy, b;}vec2 __constructor(const bvec2 b){// __retVal = b; __asm ivec4_to_vec4 __retVal.xy, b;}vec2 __constructor(const vec3 v){ __asm vec4_move __retVal.xy, v.xy;}vec2 __constructor(const vec4 v){ __asm vec4_move __retVal.xy, v.xy;}//// vec3 constructorsvec3 __constructor(const float x, const float y, const float z){ __retVal.x = x; __retVal.y = y; __retVal.z = z;}vec3 __constructor(const float f){ // Note: this could be "__retVal.xyz = f" but that's an illegal assignment __asm vec4_move __retVal.xyz, f;}vec3 __constructor(const int i){ __asm ivec4_to_vec4 __retVal.xyz, i;}vec3 __constructor(const bool b){ __asm ivec4_to_vec4 __retVal.xyz, b;}vec3 __constructor(const bvec3 b){ __asm ivec4_to_vec4 __retVal.xyz, b;}vec3 __constructor(const vec4 v){ __asm vec4_move __retVal.xyz, v;}//// vec4 constructorsvec4 __constructor(const float x, const float y, const float z, const float w){ __retVal.x = x; __retVal.y = y; __retVal.z = z; __retVal.w = w;}vec4 __constructor(const float f){ // Note: this could be "__retVal = f" but that's an illegal assignment __asm vec4_move __retVal, f;}vec4 __constructor(const int i){ __asm ivec4_to_vec4 __retVal, i;}vec4 __constructor(const bool b){ __asm ivec4_to_vec4 __retVal, b;}vec4 __constructor(const bvec4 b){ __asm ivec4_to_vec4 __retVal, b;}vec4 __constructor(const ivec4 i){ __asm ivec4_to_vec4 __retVal, i;}vec4 __constructor(const vec3 v3, const float f){ // XXX this constructor shouldn't be needed anymore __retVal.xyz = v3; __retVal.w = f;}vec4 __constructor(const vec2 v2, const float f1, const float f2){ // XXX this constructor shouldn't be needed anymore __retVal.xy = v2; __retVal.z = f1; __retVal.w = f2;}//// ivec2 constructorsivec2 __constructor(const int i, const int j){ __retVal.x = i; __retVal.y = j;}ivec2 __constructor(const int i){ __asm vec4_move __retVal.xy, i;}ivec2 __constructor(const float f){ __asm vec4_to_ivec4 __retVal.xy, f;}ivec2 __constructor(const bool b){ __asm vec4_to_ivec4 __retVal.xy, b;}//// ivec3 constructorsivec3 __constructor(const int i, const int j, const int k){ __retVal.x = i; __retVal.y = j; __retVal.z = k;}ivec3 __constructor(const int i){ __asm vec4_move __retVal.xyz, i;}ivec3 __constructor(const float f){ __asm vec4_to_ivec4 __retVal.xyz, f;}ivec3 __constructor(const bool b){ __asm vec4_move __retVal.xyz, b;}//// ivec4 constructorsivec4 __constructor(const int x, const int y, const int z, const int w){ __retVal.x = x; __retVal.y = y; __retVal.z = z; __retVal.w = w;}ivec4 __constructor(const int i){ __asm vec4_move __retVal, i;}ivec4 __constructor(const float f){ __asm vec4_to_ivec4 __retVal, f;}ivec4 __constructor(const bool b){ __asm vec4_to_ivec4 __retVal, b;}//// bvec2 constructorsbvec2 __constructor(const bool b1, const bool b2){ __retVal.x = b1; __retVal.y = b2;}bvec2 __constructor(const int i1, const int i2){ __asm vec4_sne __retVal.x, i1, 0.0; __asm vec4_sne __retVal.y, i2, 0.0;}bvec2 __constructor(const bool b){ __asm vec4_move __retVal.xy, b;}bvec2 __constructor(const float f){ __asm vec4_sne __retVal.xy, f, 0.0;}bvec2 __constructor(const int i){ __asm vec4_sne __retVal.xy, i, 0.0;}bvec2 __constructor(const vec2 v){ __asm vec4_sne __retVal.xy, v, 0.0;}bvec2 __constructor(const ivec2 v){ __asm vec4_sne __retVal.xy, v, 0.0;}//// bvec3 constructorsbvec3 __constructor(const bool b1, const bool b2, const bool b3){ __retVal.x = b1; __retVal.y = b2; __retVal.z = b3;}bvec3 __constructor(const float f1, const float f2, const float f3){ __asm vec4_sne __retVal.x, f1, 0.0; __asm vec4_sne __retVal.y, f2, 0.0; __asm vec4_sne __retVal.z, f3, 0.0;}bvec3 __constructor(const bool b){ __asm vec4_move __retVal.xyz, b;}bvec3 __constructor(const float f){ __asm vec4_sne __retVal.xyz, f, 0.0;}bvec3 __constructor(const int i){ __asm vec4_sne __retVal.xyz, i, 0.0;}bvec3 __constructor(const vec3 v){ __asm vec4_sne __retVal.xyz, v, 0.0;}bvec3 __constructor(const ivec3 v){ __asm vec4_sne __retVal.xyz, v, 0.0;}//// bvec4 constructorsbvec4 __constructor(const bool b1, const bool b2, const bool b3, const bool b4){ __retVal.x = b1; __retVal.y = b2; __retVal.z = b3; __retVal.w = b4;}bvec4 __constructor(const float f1, const float f2, const float f3, const float f4){ const float zero = 0.0; __asm vec4_sne __retVal.x, f1, zero; __asm vec4_sne __retVal.y, f2, zero; __asm vec4_sne __retVal.z, f3, zero; __asm vec4_sne __retVal.w, f4, zero; }bvec4 __constructor(const bool b){ __asm vec4_move __retVal.xyzw, b;}bvec4 __constructor(const float f){ __asm vec4_sne __retVal.xyzw, f, 0.0;}bvec4 __constructor(const int i){ __asm vec4_sne __retVal.xyzw, i, 0.0;}bvec4 __constructor(const vec4 v){ __asm vec4_sne __retVal.xyzw, v, 0.0;}bvec4 __constructor(const ivec4 v){ __asm vec4_sne __retVal.xyzw, v, 0.0;}//// mat2 constructorsmat2 __constructor(const float m00, const float m10, const float m01, const float m11){ __retVal[0].x = m00; __retVal[0].y = m10; __retVal[1].x = m01; __retVal[1].y = m11;}mat2 __constructor(const float f){ __retVal[0].x = f; __retVal[0].y = 0.0; __retVal[1].x = 0.0; __retVal[1].y = f;}mat2 __constructor(const int i){ return mat2(float(i));}mat2 __constructor(const bool b){ return mat2(float(b));}mat2 __constructor(const vec2 c0, const vec2 c1){ __retVal[0] = c0; __retVal[1] = c1;}//// mat3 constructorsmat3 __constructor(const float m00, const float m10, const float m20, const float m01, const float m11, const float m21, const float m02, const float m12, const float m22){ __retVal[0].x = m00; __retVal[0].y = m10; __retVal[0].z = m20; __retVal[1].x = m01; __retVal[1].y = m11; __retVal[1].z = m21; __retVal[2].x = m02; __retVal[2].y = m12; __retVal[2].z = m22;}mat3 __constructor(const float f){ vec2 v = vec2(f, 0.0); __retVal[0] = v.xyy; __retVal[1] = v.yxy; __retVal[2] = v.yyx;}mat3 __constructor(const int i){ return mat3(float(i));}mat3 __constructor(const bool b){ return mat3(float(b));}mat3 __constructor(const vec3 c0, const vec3 c1, const vec3 c2){ __retVal[0] = c0; __retVal[1] = c1; __retVal[2] = c2;}//// mat4 constructorsmat4 __constructor(const float m00, const float m10, const float m20, const float m30, const float m01, const float m11, const float m21, const float m31, const float m02, const float m12, const float m22, const float m32, const float m03, const float m13, const float m23, const float m33){ __retVal[0].x = m00; __retVal[0].y = m10; __retVal[0].z = m20; __retVal[0].w = m30; __retVal[1].x = m01; __retVal[1].y = m11; __retVal[1].z = m21; __retVal[1].w = m31; __retVal[2].x = m02; __retVal[2].y = m12; __retVal[2].z = m22; __retVal[2].w = m32; __retVal[3].x = m03; __retVal[3].y = m13; __retVal[3].z = m23; __retVal[3].w = m33;}mat4 __constructor(const float f){ vec2 v = vec2(f, 0.0); __retVal[0] = v.xyyy; __retVal[1] = v.yxyy; __retVal[2] = v.yyxy; __retVal[3] = v.yyyx;}mat4 __constructor(const int i){ return mat4(float(i));}mat4 __constructor(const bool b){ return mat4(float(b));}mat4 __constructor(const vec4 c0, const vec4 c1, const vec4 c2, const vec4 c3){ __retVal[0] = c0; __retVal[1] = c1; __retVal[2] = c2; __retVal[3] = c3;}//// Basic int operatorsint __operator + (const int a, const int b){ __asm vec4_add __retVal, a, b;}int __operator - (const int a, const int b){ __asm vec4_subtract __retVal, a, b;}int __operator * (const int a, const int b){ __asm vec4_multiply __retVal, a, b;}int __operator / (const int a, const int b){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -