Skip to content

Commit

Permalink
Optimize setUniforms (#7969)
Browse files Browse the repository at this point in the history
* optimize setUniforms

* make sure to use the proper binder
  • Loading branch information
mourner committed Mar 4, 2019
1 parent d417470 commit d85afad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
28 changes: 16 additions & 12 deletions src/data/program_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
Uniform1f,
UniformColor,
Uniform4f,
type UniformBindings,
type UniformLocations
} from '../render/uniform_binding';

Expand All @@ -34,6 +33,12 @@ import type {
import type {PossiblyEvaluated} from '../style/properties';
import type {FeatureStates} from '../source/source_state';

export type BinderUniform = {
name: string,
property: string,
binding: Uniform<any>
};

function packColor(color: Color): [number, number] {
return [
packUint8ToFloat(255 * color.r, 255 * color.g),
Expand Down Expand Up @@ -659,26 +664,25 @@ export default class ProgramConfiguration {
return this._buffers;
}
getUniforms(context: Context, locations: UniformLocations): UniformBindings {
const result = {};
getUniforms(context: Context, locations: UniformLocations): Array<BinderUniform> {
const uniforms = [];
for (const property in this.binders) {
const binder = this.binders[property];
for (const name of binder.uniformNames) {
result[name] = binder.getBinding(context, locations[name]);
if (locations[name]) {
const binding = binder.getBinding(context, locations[name]);
uniforms.push({name, property, binding});
}
}
}
return result;
return uniforms;
}
setUniforms<Properties: Object>(context: Context, uniformBindings: UniformBindings, properties: PossiblyEvaluated<Properties>, globals: GlobalProperties) {
setUniforms<Properties: Object>(context: Context, binderUniforms: Array<BinderUniform>, properties: PossiblyEvaluated<Properties>, globals: GlobalProperties) {
// Uniform state bindings are owned by the Program, but we set them
// from within the ProgramConfiguraton's binder members.

for (const property in this.binders) {
const binder = this.binders[property];
for (const uniformName of binder.uniformNames) {
binder.setUniforms(context, uniformBindings[uniformName], globals, properties.get(property), uniformName);
}
for (const {name, property, binding} of binderUniforms) {
this.binders[property].setUniforms(context, binding, globals, properties.get(property), name);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/render/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type StencilMode from '../gl/stencil_mode';
import type ColorMode from '../gl/color_mode';
import type CullFaceMode from '../gl/cull_face_mode';
import type {UniformBindings, UniformValues, UniformLocations} from './uniform_binding';
import type {BinderUniform} from '../data/program_configuration';

export type DrawMode =
| $PropertyType<WebGLRenderingContext, 'LINES'>
Expand All @@ -27,7 +28,7 @@ class Program<Us: UniformBindings> {
attributes: {[string]: number};
numAttributes: number;
fixedUniforms: Us;
binderUniforms: UniformBindings;
binderUniforms: Array<BinderUniform>;

constructor(context: Context,
source: {fragmentSource: string, vertexSource: string},
Expand Down

0 comments on commit d85afad

Please sign in to comment.