Commit 729a9db9 authored by Gaëtan Renaudeau's avatar Gaëtan Renaudeau

Implement Array uniforms support

parent 60390631
...@@ -12,10 +12,7 @@ ...@@ -12,10 +12,7 @@
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" /> <option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" /> <option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" /> <option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugAndroidTest" />
<option name="COMPILE_JAVA_TEST_TASK_NAME" value="compileDebugAndroidTestSources" />
<afterSyncTasks> <afterSyncTasks>
<task>generateDebugAndroidTestSources</task>
<task>generateDebugSources</task> <task>generateDebugSources</task>
</afterSyncTasks> </afterSyncTasks>
<option name="ALLOW_USER_CONFIGURATION" value="false" /> <option name="ALLOW_USER_CONFIGURATION" value="false" />
...@@ -29,7 +26,7 @@ ...@@ -29,7 +26,7 @@
</component> </component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false"> <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" /> <output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/androidTest/debug" /> <output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" /> <sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />
...@@ -51,6 +48,15 @@ ...@@ -51,6 +48,15 @@
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/debug/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" />
...@@ -58,6 +64,15 @@ ...@@ -58,6 +64,15 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
...@@ -65,17 +80,31 @@ ...@@ -65,17 +80,31 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.facebook.fresco/drawee/0.8.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.facebook.fresco/fbcore/0.8.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.facebook.fresco/fresco/0.8.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.facebook.fresco/imagepipeline-okhttp/0.8.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.facebook.fresco/imagepipeline/0.8.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.facebook.react/react-native/0.20.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" /> <excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" /> <excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content> </content>
...@@ -86,8 +115,8 @@ ...@@ -86,8 +115,8 @@
<orderEntry type="library" exported="" name="okio-1.6.0" level="project" /> <orderEntry type="library" exported="" name="okio-1.6.0" level="project" />
<orderEntry type="library" exported="" name="stetho-okhttp-1.2.0" level="project" /> <orderEntry type="library" exported="" name="stetho-okhttp-1.2.0" level="project" />
<orderEntry type="library" exported="" name="okhttp-2.5.0" level="project" /> <orderEntry type="library" exported="" name="okhttp-2.5.0" level="project" />
<orderEntry type="library" exported="" name="stetho-1.2.0" level="project" />
<orderEntry type="library" exported="" name="jsr305-3.0.0" level="project" /> <orderEntry type="library" exported="" name="jsr305-3.0.0" level="project" />
<orderEntry type="library" exported="" name="stetho-1.2.0" level="project" />
<orderEntry type="library" exported="" name="jackson-core-2.2.3" level="project" /> <orderEntry type="library" exported="" name="jackson-core-2.2.3" level="project" />
<orderEntry type="library" exported="" name="fbcore-0.8.1" level="project" /> <orderEntry type="library" exported="" name="fbcore-0.8.1" level="project" />
<orderEntry type="library" exported="" name="commons-cli-1.2" level="project" /> <orderEntry type="library" exported="" name="commons-cli-1.2" level="project" />
......
...@@ -7,7 +7,9 @@ import java.nio.ByteBuffer; ...@@ -7,7 +7,9 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public class GLShader { public class GLShader {
...@@ -16,10 +18,12 @@ public class GLShader { ...@@ -16,10 +18,12 @@ public class GLShader {
private final String vert; private final String vert;
private final String frag; private final String frag;
private Map<String, Integer> uniformTypes; private Map<String, Integer> uniformTypes;
private List<String> uniformNames;
private int program; // Program of the shader private int program; // Program of the shader
private int buffer[]; // the buffer currently contains 2 static triangles covering the surface private int buffer[]; // the buffer currently contains 2 static triangles covering the surface
private int pointerLoc; // The "pointer" attribute is used to iterate over vertex private int pointerLoc; // The "pointer" attribute is used to iterate over vertex
private Map<String, Integer> uniformLocations; // The uniform locations cache private Map<String, Integer> uniformLocations; // The uniform locations cache
private Map<String, Integer> uniformSizes; // The size of the uniform variable
private Integer id; private Integer id;
private RNGLContext rnglContext; private RNGLContext rnglContext;
...@@ -76,6 +80,7 @@ public class GLShader { ...@@ -76,6 +80,7 @@ public class GLShader {
glUniform1f(uniformLocations.get(name), f); glUniform1f(uniformLocations.get(name), f);
} }
public void setUniform (String name, FloatBuffer buf, int type) { public void setUniform (String name, FloatBuffer buf, int type) {
System.out.println("setUniform "+name);
switch (type) { switch (type) {
case GL_FLOAT_VEC2: case GL_FLOAT_VEC2:
glUniform2fv(uniformLocations.get(name), 1, buf); glUniform2fv(uniformLocations.get(name), 1, buf);
...@@ -125,7 +130,10 @@ public class GLShader { ...@@ -125,7 +130,10 @@ public class GLShader {
public Map<String, Integer> getUniformTypes() { public Map<String, Integer> getUniformTypes() {
return uniformTypes; return uniformTypes;
} }
public Map<String, Integer> getUniformSizes() {
return uniformSizes;
}
public List<String> getUniformNames() { return uniformNames; }
private int compileShader (String code, int shaderType) { private int compileShader (String code, int shaderType) {
int shaderHandle = glCreateShader(shaderType); int shaderHandle = glCreateShader(shaderType);
...@@ -143,19 +151,42 @@ public class GLShader { ...@@ -143,19 +151,42 @@ public class GLShader {
private void computeMeta () { private void computeMeta () {
Map<String, Integer> uniforms = new HashMap<>(); Map<String, Integer> uniforms = new HashMap<>();
Map<String, Integer> locations = new HashMap<>(); Map<String, Integer> locations = new HashMap<>();
Map<String, Integer> sizes = new HashMap<>();
ArrayList<String> uniformNames = new ArrayList<>();
int[] nbUniforms = new int[1]; int[] nbUniforms = new int[1];
int[] type = new int[1]; int[] typePointer = new int[1];
int[] size = new int[1]; int[] sizePointer = new int[1];
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, nbUniforms, 0); glGetProgramiv(program, GL_ACTIVE_UNIFORMS, nbUniforms, 0);
for (int i=0; i < nbUniforms[0]; i++) { for (int i=0; i < nbUniforms[0]; i++) {
String uniformName = glGetActiveUniform(program, i, size, 0, type, 0); String uniformName = glGetActiveUniform(program, i, sizePointer, 0, typePointer, 0);
int location = glGetUniformLocation(program, uniformName); int size = sizePointer[0];
uniforms.put(uniformName, type[0]); int type = typePointer[0];
locations.put(uniformName, location); if (uniformName.contains("[0]")) {
uniformName = uniformName.substring(0, uniformName.length()-3);
}
uniformNames.add(uniformName);
uniforms.put(uniformName, type);
sizes.put(uniformName, size);
if (size == 1) {
int location = glGetUniformLocation(program, uniformName);
locations.put(uniformName, location);
}
else {
for (int j=0; j<size; j++) {
String uniformIndexName = uniformName+ "[" + j + "]";
int location = glGetUniformLocation(program, uniformIndexName);
locations.put(uniformIndexName, location);
uniforms.put(uniformIndexName, type);
sizes.put(uniformIndexName, 1);
}
}
} }
this.uniformTypes = uniforms; this.uniformTypes = uniforms;
this.uniformLocations = locations; this.uniformLocations = locations;
this.uniformSizes = sizes;
this.uniformNames = uniformNames;
} }
private void makeProgram () throws GLShaderCompilationFailed { private void makeProgram () throws GLShaderCompilationFailed {
......
This diff is collapsed.
...@@ -5,7 +5,7 @@ buildscript { ...@@ -5,7 +5,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:1.3.1' classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
......
#Mon Jun 13 21:59:23 CEST 2016
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
import React, {Component} from "react";
import {View, Animated} from "react-native";
import {Surface} from "gl-react-native";
import GL from "gl-react";
const stateForTime = t => ({
colors: [
[ Math.cos(0.002*t), Math.sin(0.002*t), 0.2, 1 ],
[ Math.sin(0.002*t), -Math.cos(0.002*t), 0.1, 1 ],
[ 0.3, Math.sin(3+0.002*t), Math.cos(1+0.003*t), 1 ]
],
particles: [
[ 0.3, 0.3 ],
[ 0.7, 0.5 ],
[ 0.4, 0.9 ]
]
});
export default class AnimatedExample extends Component {
state = stateForTime(0);
componentWillMount () {
const begin = Date.now();
this.interval = setInterval(() => this.setState(
stateForTime(Date.now() - begin)
));
}
componentWillUnmount () {
clearInterval(this.interval);
}
render () {
const { colors, particles } = this.state;
return <View style={{ paddingTop: 60, alignItems: "center" }}>
<Surface width={300} height={300}>
<GL.Node
uniforms={{
colors,
particles,
}}
shader={{// inline example
frag: `
precision highp float;
varying vec2 uv;
uniform vec4 colors[3];
uniform vec2 particles[3]; // N.B. don't abuse these technique. it's not meant to be used with lot of objects.
void main () {
vec4 sum = vec4(0.0);
for (int i=0; i<3; i++) {
vec4 c = colors[i];
vec2 p = particles[i];
float d = c.a * smoothstep(0.6, 0.2, distance(p, uv));
sum += d * vec4(c.a * c.rgb, c.a);
}
if (sum.a > 1.0) {
sum.rgb /= sum.a;
sum.a = 1.0;
}
gl_FragColor = vec4(sum.a * sum.rgb, 1.0);
}
`
}}
/>
</Surface>
</View>;
}
}
...@@ -6,22 +6,15 @@ import AdvancedEffects from "./AdvancedEffects"; ...@@ -6,22 +6,15 @@ import AdvancedEffects from "./AdvancedEffects";
import Hearts from "./Hearts"; import Hearts from "./Hearts";
import Tests from "./Tests"; import Tests from "./Tests";
import Animated from "./Animated"; import Animated from "./Animated";
import Particles from "./Particles";
const screens = { Simple, AdvancedEffects, Hearts, Tests, Animated }; const screens = { Simple, AdvancedEffects, Hearts, Tests, Animated, Particles };
const homeRoute = { const homeRoute = {
id: "home", id: "home",
title: "gl-react Showcase" title: "gl-react Showcase"
}; };
const routes = [
{ id: "Simple" },
{ id: "AdvancedEffects" },
{ id: "Hearts" },
{ id: "Tests" },
{ id: "Animated" },
];
const styles = StyleSheet.create({ const styles = StyleSheet.create({
root: { root: {
backgroundColor: "#333", backgroundColor: "#333",
...@@ -62,9 +55,9 @@ class Home extends Component { ...@@ -62,9 +55,9 @@ class Home extends Component {
render () { render () {
const { openScreen } = this.props; const { openScreen } = this.props;
return <View style={styles.home}> return <View style={styles.home}>
{routes.map(route => {Object.keys(screens).map(id =>
<TouchableOpacity style={styles.homeLink} key={route.id} onPress={() => openScreen(route)}> <TouchableOpacity style={styles.homeLink} key={id} onPress={() => openScreen({ id })}>
<Text style={styles.homeText}>{route.id}</Text> <Text style={styles.homeText}>{id}</Text>
</TouchableOpacity>)} </TouchableOpacity>)}
</View>; </View>;
} }
......
...@@ -283,7 +283,7 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -283,7 +283,7 @@ RCT_NOT_IMPLEMENTED(-init)
RCTLogError(@"Maximum number of texture reach. got %i >= max %i", units, maxTextureUnits); RCTLogError(@"Maximum number of texture reach. got %i >= max %i", units, maxTextureUnits);
} }
for (NSString *uniformName in shader.uniformTypes) { for (NSString *uniformName in shader.uniformNames) {
if (uniforms[uniformName] == nil) { if (uniforms[uniformName] == nil) {
RCTLogError(@"All defined uniforms must be provided. Missing '%@'", uniformName); RCTLogError(@"All defined uniforms must be provided. Missing '%@'", uniformName);
} }
......
...@@ -14,6 +14,7 @@ NS_ENUM(NSInteger) { ...@@ -14,6 +14,7 @@ NS_ENUM(NSInteger) {
@property EAGLContext *context; @property EAGLContext *context;
@property NSString *vert; @property NSString *vert;
@property NSString *frag; @property NSString *frag;
@property NSArray *uniformNames;
@property NSDictionary *uniformTypes; @property NSDictionary *uniformTypes;
/** /**
......
...@@ -40,8 +40,10 @@ GLuint compileShader (NSString* shaderName, NSString* shaderString, GLenum shade ...@@ -40,8 +40,10 @@ GLuint compileShader (NSString* shaderName, NSString* shaderString, GLenum shade
GLuint program; // Program of the shader GLuint program; // Program of the shader
GLuint buffer; // the buffer currently contains 2 static triangles covering the surface GLuint buffer; // the buffer currently contains 2 static triangles covering the surface
GLint pointerLoc; // The "pointer" attribute is used to iterate over vertex GLint pointerLoc; // The "pointer" attribute is used to iterate over vertex
NSDictionary *_uniformTypes; // The types of the GLSL uniforms (N.B: array are not supported) NSDictionary *_uniformTypes; // The types of the GLSL uniforms
NSDictionary *_uniformSizes; // The size of the uniform variable
NSDictionary *_uniformLocations; // The uniform locations cache NSDictionary *_uniformLocations; // The uniform locations cache
NSArray *__uniformNames; // The uniforms names
NSError *_error; NSError *_error;
} }
...@@ -95,13 +97,28 @@ GLuint compileShader (NSString* shaderName, NSString* shaderString, GLenum shade ...@@ -95,13 +97,28 @@ GLuint compileShader (NSString* shaderName, NSString* shaderString, GLenum shade
- (void) setUniform: (NSString *)name withValue:(id)value - (void) setUniform: (NSString *)name withValue:(id)value
{ {
GLint size = [_uniformSizes[name] intValue];
GLenum type = [_uniformTypes[name] intValue];
if (size != 1) {
NSArray *v = [RCTConvert NSArray:value];
if (!v || [v count]!=size) {
RCTLogError(@"Shader '%@': uniform '%@' should be an array of %i elements", _name, name, size);
return;
}
for (int i=0; i<size; i++) {
NSString *indexName = [NSString stringWithFormat:@"%@[%i]", name, i];
id indexValue = v[i];
[self setUniform:indexName withValue:indexValue];
}
return;
}
if ([_uniformLocations objectForKey:name] == nil) { if ([_uniformLocations objectForKey:name] == nil) {
RCTLogError(@"Shader '%@': uniform '%@' does not exist", _name, name); RCTLogError(@"Shader '%@': uniform '%@' does not exist", _name, name);
return; return;
} }
GLint location = [_uniformLocations[name] intValue]; GLint location = [_uniformLocations[name] intValue];
GLenum type = [_uniformTypes[name] intValue];
switch (type) switch (type)
{ {
case GL_FLOAT: { case GL_FLOAT: {
...@@ -318,8 +335,10 @@ GLuint compileShader (NSString* shaderName, NSString* shaderString, GLenum shade ...@@ -318,8 +335,10 @@ GLuint compileShader (NSString* shaderName, NSString* shaderString, GLenum shade
- (void) computeMeta - (void) computeMeta
{ {
NSMutableDictionary *uniforms = @{}.mutableCopy; NSMutableArray *uniformNames = [[NSMutableArray alloc] init];
NSMutableDictionary *uniformTypes = @{}.mutableCopy;
NSMutableDictionary *locations = @{}.mutableCopy; NSMutableDictionary *locations = @{}.mutableCopy;
NSMutableDictionary *sizes = @{}.mutableCopy;
int nbUniforms; int nbUniforms;
GLchar name[256]; GLchar name[256];
GLenum type; GLenum type;
...@@ -328,14 +347,33 @@ GLuint compileShader (NSString* shaderName, NSString* shaderString, GLenum shade ...@@ -328,14 +347,33 @@ GLuint compileShader (NSString* shaderName, NSString* shaderString, GLenum shade
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &nbUniforms); glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &nbUniforms);
for (int i=0; i<nbUniforms; i++) { for (int i=0; i<nbUniforms; i++) {
glGetActiveUniform(program, i, sizeof(name), &length, &size, &type, name); glGetActiveUniform(program, i, sizeof(name), &length, &size, &type, name);
GLint location = glGetUniformLocation(program, name);
NSString *uniformName = [NSString stringWithUTF8String:name]; NSString *uniformName = [NSString stringWithUTF8String:name];
uniforms[uniformName] = [NSNumber numberWithInt:type]; if ([uniformName containsString:@"[0]"]) {
locations[uniformName] = [NSNumber numberWithInt:location]; uniformName = [uniformName substringToIndex:[uniformName length]-3];
}
uniformTypes[uniformName] = [NSNumber numberWithInt:type];
sizes[uniformName] = [NSNumber numberWithInt:size];
[uniformNames addObject:uniformName];
if (size == 1) {
GLint location = glGetUniformLocation(program, name);
locations[uniformName] = [NSNumber numberWithInt:location];
}
else {
for (int j=0; j<size; j++) {
NSString *uniformIndexName = [NSString stringWithFormat:@"%@[%i]", uniformName, j];
GLint location = glGetUniformLocation(program, [uniformIndexName UTF8String]);
locations[uniformIndexName] = [NSNumber numberWithInt:location];
uniformTypes[uniformIndexName] = [NSNumber numberWithInt:type];
sizes[uniformIndexName] = [NSNumber numberWithInt:1];
}
}
} }
_uniformTypes = uniforms;
_uniformTypes = uniformTypes;
_uniformLocations = locations; _uniformLocations = locations;
_uniformSizes = sizes;
_uniformNames = uniformNames;
} }
- (bool) ensureCompiles: (NSError **)error - (bool) ensureCompiles: (NSError **)error
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
"license": "MIT", "license": "MIT",
"peerDependencies": { "peerDependencies": {
"react-native": "*", "react-native": "*",
"gl-react": "^2.2.0" "gl-react": "^2.2.3"
}, },
"dependencies": { "dependencies": {
"invariant": "2.2.0", "invariant": "2.2.0",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment