1 // Copyright (C) 2011-2012 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma version(1) 16 17 #pragma rs java_package_name(com.android.scenegraph) 18 19 #ifndef _TRANSFORM_DEF_ 20 #define _TRANSFORM_DEF_ 21 22 #include "rs_graphics.rsh" 23 24 #define TRANSFORM_NONE 0 25 #define TRANSFORM_TRANSLATE 1 26 #define TRANSFORM_ROTATE 2 27 #define TRANSFORM_SCALE 3 28 29 #define CULL_FRUSTUM 0 30 #define CULL_ALWAYS 2 31 32 #define LIGHT_POINT 0 33 #define LIGHT_DIRECTIONAL 1 34 35 // Shader params that involve only data 36 #define SHADER_PARAM_DATA_ONLY 10000 37 #define SHADER_PARAM_FLOAT4_DATA 10001 38 #define SHADER_PARAM_TRANSFORM_DATA 10002 39 #define SHADER_PARAM_TRANSFORM_MODEL 10003 40 41 // Shader params that involve camera 42 #define SHADER_PARAM_CAMERA 1000 43 #define SHADER_PARAM_FLOAT4_CAMERA_POS 1001 44 #define SHADER_PARAM_FLOAT4_CAMERA_DIR 1002 45 #define SHADER_PARAM_TRANSFORM_VIEW 1003 46 #define SHADER_PARAM_TRANSFORM_PROJ 1004 47 #define SHADER_PARAM_TRANSFORM_VIEW_PROJ 1005 48 #define SHADER_PARAM_TRANSFORM_MODEL_VIEW 1006 49 #define SHADER_PARAM_TRANSFORM_MODEL_VIEW_PROJ 1007 50 51 // Shader Params that only involve lights 52 #define SHADER_PARAM_LIGHT 100 53 #define SHADER_PARAM_FLOAT4_LIGHT_COLOR 103 54 #define SHADER_PARAM_FLOAT4_LIGHT_POS 104 55 #define SHADER_PARAM_FLOAT4_LIGHT_DIR 105 56 57 #define SHADER_PARAM_TEXTURE 10 58 59 #define TEXTURE_NONE 0 60 #define TEXTURE_2D 1 61 #define TEXTURE_CUBE 2 62 #define TEXTURE_RENDER_TARGET 3 63 64 typedef struct TransformComponent_s { 65 float4 value; 66 int type; 67 rs_allocation name; 68 } SgTransformComponent; 69 70 typedef struct __attribute__((packed, aligned(4))) SgTransform { 71 rs_matrix4x4 globalMat; 72 rs_matrix4x4 localMat; 73 74 rs_allocation components; 75 int isDirty; 76 77 rs_allocation children; 78 rs_allocation name; 79 80 // Used to check whether transform params need to be updated 81 uint32_t timestamp; 82 } SgTransform; 83 84 typedef struct VertexShader_s { 85 rs_program_vertex program; 86 // Buffer with vertex constant data 87 rs_allocation shaderConst; 88 // ShaderParam's that populate data 89 rs_allocation shaderConstParams; 90 // location of the per object constants on the buffer 91 int objectConstIndex; 92 } SgVertexShader; 93 94 typedef struct FragmentShader_s { 95 rs_program_fragment program; 96 // Buffer with vertex constant data 97 rs_allocation shaderConst; 98 // ShaderParam's that populate data 99 rs_allocation shaderConstParams; 100 // ShaderParam's that set textures 101 rs_allocation shaderTextureParams; 102 // location of the per object constants on the buffer 103 int objectConstIndex; 104 } SgFragmentShader; 105 106 typedef struct RenderState_s { 107 rs_allocation pv; // VertexShader struct 108 rs_allocation pf; // FragmentShader struct 109 rs_program_store ps; 110 rs_program_raster pr; 111 } SgRenderState; 112 113 typedef struct Renderable_s { 114 rs_allocation render_state; 115 // Buffer with vertex constant data 116 rs_allocation pv_const; 117 // ShaderParam's that populate data 118 rs_allocation pv_constParams; 119 // Buffer with fragment constant data 120 rs_allocation pf_const; 121 // ShaderParam's that populate data 122 rs_allocation pf_constParams; 123 rs_allocation pf_textures[8]; 124 int pf_num_textures; 125 rs_mesh mesh; 126 int meshIndex; 127 rs_allocation transformMatrix; 128 rs_allocation name; 129 float4 boundingSphere; 130 float4 worldBoundingSphere; 131 int bVolInitialized; 132 int cullType; // specifies whether to frustum cull 133 int isVisible; 134 } SgRenderable; 135 136 typedef struct RenderPass_s { 137 rs_allocation color_target; 138 rs_allocation depth_target; 139 rs_allocation camera; 140 rs_allocation objects; 141 142 float4 clear_color; 143 float clear_depth; 144 bool should_clear_color; 145 bool should_clear_depth; 146 } SgRenderPass; 147 148 typedef struct Camera_s { 149 rs_matrix4x4 proj; 150 rs_matrix4x4 view; 151 rs_matrix4x4 viewProj; 152 float4 position; 153 float near; 154 float far; 155 float horizontalFOV; 156 float aspect; 157 rs_allocation name; 158 rs_allocation transformMatrix; 159 float4 frustumPlanes[6]; 160 161 int isDirty; 162 // Timestamp of the camera itself to signal params if anything changes 163 uint32_t timestamp; 164 // Timestamp of our transform 165 uint32_t transformTimestamp; 166 } SgCamera; 167 168 typedef struct Light_s { 169 float4 position; 170 float4 color; 171 float intensity; 172 int type; 173 rs_allocation name; 174 rs_allocation transformMatrix; 175 } SgLight; 176 177 // This represents the shader parameter data needed to set a float or transform data 178 typedef struct ShaderParamData_s { 179 int type; 180 float4 float_value; 181 uint32_t timestamp; 182 rs_allocation paramName; 183 rs_allocation camera; 184 rs_allocation light; 185 rs_allocation transform; 186 rs_allocation texture; 187 } SgShaderParamData; 188 189 // This represents a shader parameter that knows how to update itself for a given 190 // renderable or shader and contains a timestamp for the last time this buffer was updated 191 typedef struct ShaderParam_s { 192 // Used to check whether transform params need to be updated 193 uint32_t transformTimestamp; 194 // Used to check whether data params need to be updated 195 // These are used when somebody set the matrix of float value directly in java 196 uint32_t dataTimestamp; 197 // Specifies where in the constant buffer data gets written to 198 int bufferOffset; 199 // An instance of SgShaderParamData that could be shared by multiple objects 200 rs_allocation data; 201 // How many components of the vector we need to write 202 int float_vecSize; 203 } SgShaderParam; 204 205 // This represents a texture object 206 typedef struct Texture_s { 207 uint32_t type; 208 rs_allocation texture; 209 } SgTexture; 210 211 static void printName(rs_allocation name) { 212 if (!rsIsObject(name)) { 213 rsDebug("no name", 0); 214 return; 215 } 216 217 rsDebug((const char*)rsGetElementAt(name, 0), 0); 218 } 219 220 static void printCameraInfo(const SgCamera *cam) { 221 rsDebug("***** Camera information. ptr:", cam); 222 printName(cam->name); 223 const SgTransform *camTransform = (const SgTransform *)rsGetElementAt(cam->transformMatrix, 0); 224 rsDebug("Transform name:", camTransform); 225 printName(camTransform->name); 226 227 rsDebug("Aspect: ", cam->aspect); 228 rsDebug("Near: ", cam->near); 229 rsDebug("Far: ", cam->far); 230 rsDebug("Fov: ", cam->horizontalFOV); 231 rsDebug("Position: ", cam->position); 232 rsDebug("Proj: ", &cam->proj); 233 rsDebug("View: ", &cam->view); 234 } 235 236 static void printLightInfo(const SgLight *light) { 237 rsDebug("***** Light information. ptr:", light); 238 printName(light->name); 239 const SgTransform *lTransform = (const SgTransform *)rsGetElementAt(light->transformMatrix, 0); 240 rsDebug("Transform name:", lTransform); 241 printName(lTransform->name); 242 243 rsDebug("Position: ", light->position); 244 rsDebug("Color : ", light->color); 245 rsDebug("Intensity: ", light->intensity); 246 rsDebug("Type: ", light->type); 247 } 248 249 static void getCameraRay(const SgCamera *cam, int screenX, int screenY, float3 *pnt, float3 *vec) { 250 rsDebug("=================================", screenX); 251 rsDebug("Point X", screenX); 252 rsDebug("Point Y", screenY); 253 254 rs_matrix4x4 mvpInv; 255 rsMatrixLoad(&mvpInv, &cam->viewProj); 256 rsMatrixInverse(&mvpInv); 257 258 float width = (float)rsgGetWidth(); 259 float height = (float)rsgGetHeight(); 260 261 float4 pos = {(float)screenX, height - (float)screenY, 0.0f, 1.0f}; 262 263 pos.x /= width; 264 pos.y /= height; 265 266 rsDebug("Pre Norm X", pos.x); 267 rsDebug("Pre Norm Y", pos.y); 268 269 pos.xy = pos.xy * 2.0f - 1.0f; 270 271 rsDebug("Norm X", pos.x); 272 rsDebug("Norm Y", pos.y); 273 274 pos = rsMatrixMultiply(&mvpInv, pos); 275 float oneOverW = 1.0f / pos.w; 276 pos.xyz *= oneOverW; 277 278 rsDebug("World X", pos.x); 279 rsDebug("World Y", pos.y); 280 rsDebug("World Z", pos.z); 281 282 rsDebug("Cam X", cam->position.x); 283 rsDebug("Cam Y", cam->position.y); 284 rsDebug("Cam Z", cam->position.z); 285 286 *vec = normalize(pos.xyz - cam->position.xyz); 287 rsDebug("Vec X", vec->x); 288 rsDebug("Vec Y", vec->y); 289 rsDebug("Vec Z", vec->z); 290 *pnt = cam->position.xyz; 291 } 292 293 static bool intersect(const SgRenderable *obj, float3 pnt, float3 vec) { 294 // Solving for t^2 + Bt + C = 0 295 float3 originMinusCenter = pnt - obj->worldBoundingSphere.xyz; 296 float B = dot(originMinusCenter, vec) * 2.0f; 297 float C = dot(originMinusCenter, originMinusCenter) - 298 obj->worldBoundingSphere.w * obj->worldBoundingSphere.w; 299 300 float discriminant = B * B - 4.0f * C; 301 if (discriminant < 0.0f) { 302 return false; 303 } 304 discriminant = sqrt(discriminant); 305 306 float t0 = (-B - discriminant) * 0.5f; 307 float t1 = (-B + discriminant) * 0.5f; 308 309 if (t0 > t1) { 310 float temp = t0; 311 t0 = t1; 312 t1 = temp; 313 } 314 315 // The sphere is behind us 316 if (t1 < 0.0f) { 317 return false; 318 } 319 return true; 320 } 321 322 323 #endif // _TRANSFORM_DEF_ 324