rsdShader.cpp revision cf27eb46f97cff087ebfc5b81fe998eabe0569cf
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <GLES2/gl2.h>
18#include <GLES2/gl2ext.h>
19
20#include <rs_hal.h>
21#include <rsContext.h>
22#include <rsProgram.h>
23
24#include "rsdCore.h"
25#include "rsdAllocation.h"
26#include "rsdShader.h"
27#include "rsdShaderCache.h"
28
29using namespace android;
30using namespace android::renderscript;
31
32RsdShader::RsdShader(const Program *p, uint32_t type,
33                       const char * shaderText, uint32_t shaderLength) {
34
35    mUserShader.setTo(shaderText, shaderLength);
36    mRSProgram = p;
37    mType = type;
38    initMemberVars();
39    initAttribAndUniformArray();
40    init();
41}
42
43RsdShader::~RsdShader() {
44    if (mShaderID) {
45        glDeleteShader(mShaderID);
46    }
47
48    delete[] mAttribNames;
49    delete[] mUniformNames;
50    delete[] mUniformArraySizes;
51    delete[] mTextureTargets;
52}
53
54void RsdShader::initMemberVars() {
55    mDirty = true;
56    mShaderID = 0;
57    mAttribCount = 0;
58    mUniformCount = 0;
59
60    mAttribNames = NULL;
61    mUniformNames = NULL;
62    mUniformArraySizes = NULL;
63    mTextureTargets = NULL;
64
65    mIsValid = false;
66}
67
68void RsdShader::init() {
69    uint32_t attribCount = 0;
70    uint32_t uniformCount = 0;
71    for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) {
72        initAddUserElement(mRSProgram->mHal.state.inputElements[ct], mAttribNames, NULL, &attribCount, RS_SHADER_ATTR);
73    }
74    for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) {
75        initAddUserElement(mRSProgram->mHal.state.constantTypes[ct]->getElement(), mUniformNames, mUniformArraySizes, &uniformCount, RS_SHADER_UNI);
76    }
77
78    mTextureUniformIndexStart = uniformCount;
79    char buf[256];
80    for (uint32_t ct=0; ct < mRSProgram->mHal.state.texturesCount; ct++) {
81        snprintf(buf, sizeof(buf), "UNI_Tex%i", ct);
82        mUniformNames[uniformCount].setTo(buf);
83        mUniformArraySizes[uniformCount] = 1;
84        uniformCount++;
85    }
86
87}
88
89String8 RsdShader::getGLSLInputString() const {
90    String8 s;
91    for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) {
92        const Element *e = mRSProgram->mHal.state.inputElements[ct];
93        for (uint32_t field=0; field < e->getFieldCount(); field++) {
94            const Element *f = e->getField(field);
95
96            // Cannot be complex
97            rsAssert(!f->getFieldCount());
98            switch (f->getComponent().getVectorSize()) {
99            case 1: s.append("attribute float ATTRIB_"); break;
100            case 2: s.append("attribute vec2 ATTRIB_"); break;
101            case 3: s.append("attribute vec3 ATTRIB_"); break;
102            case 4: s.append("attribute vec4 ATTRIB_"); break;
103            default:
104                rsAssert(0);
105            }
106
107            s.append(e->getFieldName(field));
108            s.append(";\n");
109        }
110    }
111    return s;
112}
113
114void RsdShader::appendAttributes() {
115    for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) {
116        const Element *e = mRSProgram->mHal.state.inputElements[ct];
117        for (uint32_t field=0; field < e->getFieldCount(); field++) {
118            const Element *f = e->getField(field);
119            const char *fn = e->getFieldName(field);
120
121            if (fn[0] == '#') {
122                continue;
123            }
124
125            // Cannot be complex
126            rsAssert(!f->getFieldCount());
127            switch (f->getComponent().getVectorSize()) {
128            case 1: mShader.append("attribute float ATTRIB_"); break;
129            case 2: mShader.append("attribute vec2 ATTRIB_"); break;
130            case 3: mShader.append("attribute vec3 ATTRIB_"); break;
131            case 4: mShader.append("attribute vec4 ATTRIB_"); break;
132            default:
133                rsAssert(0);
134            }
135
136            mShader.append(fn);
137            mShader.append(";\n");
138        }
139    }
140}
141
142void RsdShader::appendTextures() {
143    char buf[256];
144    for (uint32_t ct=0; ct < mRSProgram->mHal.state.texturesCount; ct++) {
145        if (mRSProgram->mHal.state.textureTargets[ct] == RS_TEXTURE_2D) {
146            Allocation *a = mRSProgram->mHal.state.textures[ct];
147            if (a && a->mHal.state.surfaceTextureID) {
148                snprintf(buf, sizeof(buf), "uniform samplerExternalOES UNI_Tex%i;\n", ct);
149            } else {
150                snprintf(buf, sizeof(buf), "uniform sampler2D UNI_Tex%i;\n", ct);
151            }
152            mTextureTargets[ct] = GL_TEXTURE_2D;
153        } else {
154            snprintf(buf, sizeof(buf), "uniform samplerCube UNI_Tex%i;\n", ct);
155            mTextureTargets[ct] = GL_TEXTURE_CUBE_MAP;
156        }
157        mShader.append(buf);
158    }
159}
160
161bool RsdShader::createShader() {
162
163    if (mType == GL_FRAGMENT_SHADER) {
164        mShader.append("precision mediump float;\n");
165    }
166    appendUserConstants();
167    appendAttributes();
168    appendTextures();
169
170    mShader.append(mUserShader);
171
172    return true;
173}
174
175bool RsdShader::loadShader(const Context *rsc) {
176    mShaderID = glCreateShader(mType);
177    rsAssert(mShaderID);
178
179    if (rsc->props.mLogShaders) {
180        ALOGV("Loading shader type %x, ID %i", mType, mShaderID);
181        ALOGV("%s", mShader.string());
182    }
183
184    if (mShaderID) {
185        const char * ss = mShader.string();
186        RSD_CALL_GL(glShaderSource, mShaderID, 1, &ss, NULL);
187        RSD_CALL_GL(glCompileShader, mShaderID);
188
189        GLint compiled = 0;
190        RSD_CALL_GL(glGetShaderiv, mShaderID, GL_COMPILE_STATUS, &compiled);
191        if (!compiled) {
192            GLint infoLen = 0;
193            RSD_CALL_GL(glGetShaderiv, mShaderID, GL_INFO_LOG_LENGTH, &infoLen);
194            if (infoLen) {
195                char* buf = (char*) malloc(infoLen);
196                if (buf) {
197                    RSD_CALL_GL(glGetShaderInfoLog, mShaderID, infoLen, NULL, buf);
198                    ALOGE("Could not compile shader \n%s\n", buf);
199                    free(buf);
200                }
201                RSD_CALL_GL(glDeleteShader, mShaderID);
202                mShaderID = 0;
203                rsc->setError(RS_ERROR_BAD_SHADER, "Error returned from GL driver loading shader text,");
204                return false;
205            }
206        }
207    }
208
209    if (rsc->props.mLogShaders) {
210        ALOGV("--Shader load result %x ", glGetError());
211    }
212    mIsValid = true;
213    return true;
214}
215
216void RsdShader::appendUserConstants() {
217    for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) {
218        const Element *e = mRSProgram->mHal.state.constantTypes[ct]->getElement();
219        for (uint32_t field=0; field < e->getFieldCount(); field++) {
220            const Element *f = e->getField(field);
221            const char *fn = e->getFieldName(field);
222
223            if (fn[0] == '#') {
224                continue;
225            }
226
227            // Cannot be complex
228            rsAssert(!f->getFieldCount());
229            if (f->getType() == RS_TYPE_MATRIX_4X4) {
230                mShader.append("uniform mat4 UNI_");
231            } else if (f->getType() == RS_TYPE_MATRIX_3X3) {
232                mShader.append("uniform mat3 UNI_");
233            } else if (f->getType() == RS_TYPE_MATRIX_2X2) {
234                mShader.append("uniform mat2 UNI_");
235            } else {
236                switch (f->getComponent().getVectorSize()) {
237                case 1: mShader.append("uniform float UNI_"); break;
238                case 2: mShader.append("uniform vec2 UNI_"); break;
239                case 3: mShader.append("uniform vec3 UNI_"); break;
240                case 4: mShader.append("uniform vec4 UNI_"); break;
241                default:
242                    rsAssert(0);
243                }
244            }
245
246            mShader.append(fn);
247            if (e->getFieldArraySize(field) > 1) {
248                mShader.appendFormat("[%d]", e->getFieldArraySize(field));
249            }
250            mShader.append(";\n");
251        }
252    }
253}
254
255void RsdShader::logUniform(const Element *field, const float *fd, uint32_t arraySize ) {
256    RsDataType dataType = field->getType();
257    uint32_t elementSize = field->getSizeBytes() / sizeof(float);
258    for (uint32_t i = 0; i < arraySize; i ++) {
259        if (arraySize > 1) {
260            ALOGV("Array Element [%u]", i);
261        }
262        if (dataType == RS_TYPE_MATRIX_4X4) {
263            ALOGV("Matrix4x4");
264            ALOGV("{%f, %f, %f, %f",  fd[0], fd[4], fd[8], fd[12]);
265            ALOGV(" %f, %f, %f, %f",  fd[1], fd[5], fd[9], fd[13]);
266            ALOGV(" %f, %f, %f, %f",  fd[2], fd[6], fd[10], fd[14]);
267            ALOGV(" %f, %f, %f, %f}", fd[3], fd[7], fd[11], fd[15]);
268        } else if (dataType == RS_TYPE_MATRIX_3X3) {
269            ALOGV("Matrix3x3");
270            ALOGV("{%f, %f, %f",  fd[0], fd[3], fd[6]);
271            ALOGV(" %f, %f, %f",  fd[1], fd[4], fd[7]);
272            ALOGV(" %f, %f, %f}", fd[2], fd[5], fd[8]);
273        } else if (dataType == RS_TYPE_MATRIX_2X2) {
274            ALOGV("Matrix2x2");
275            ALOGV("{%f, %f",  fd[0], fd[2]);
276            ALOGV(" %f, %f}", fd[1], fd[3]);
277        } else {
278            switch (field->getComponent().getVectorSize()) {
279            case 1:
280                ALOGV("Uniform 1 = %f", fd[0]);
281                break;
282            case 2:
283                ALOGV("Uniform 2 = %f %f", fd[0], fd[1]);
284                break;
285            case 3:
286                ALOGV("Uniform 3 = %f %f %f", fd[0], fd[1], fd[2]);
287                break;
288            case 4:
289                ALOGV("Uniform 4 = %f %f %f %f", fd[0], fd[1], fd[2], fd[3]);
290                break;
291            default:
292                rsAssert(0);
293            }
294        }
295        ALOGE("Element size %u data=%p", elementSize, fd);
296        fd += elementSize;
297        ALOGE("New data=%p", fd);
298    }
299}
300
301void RsdShader::setUniform(const Context *rsc, const Element *field, const float *fd,
302                         int32_t slot, uint32_t arraySize ) {
303    RsDataType dataType = field->getType();
304    if (dataType == RS_TYPE_MATRIX_4X4) {
305        RSD_CALL_GL(glUniformMatrix4fv, slot, arraySize, GL_FALSE, fd);
306    } else if (dataType == RS_TYPE_MATRIX_3X3) {
307        RSD_CALL_GL(glUniformMatrix3fv, slot, arraySize, GL_FALSE, fd);
308    } else if (dataType == RS_TYPE_MATRIX_2X2) {
309        RSD_CALL_GL(glUniformMatrix2fv, slot, arraySize, GL_FALSE, fd);
310    } else {
311        switch (field->getComponent().getVectorSize()) {
312        case 1:
313            RSD_CALL_GL(glUniform1fv, slot, arraySize, fd);
314            break;
315        case 2:
316            RSD_CALL_GL(glUniform2fv, slot, arraySize, fd);
317            break;
318        case 3:
319            RSD_CALL_GL(glUniform3fv, slot, arraySize, fd);
320            break;
321        case 4:
322            RSD_CALL_GL(glUniform4fv, slot, arraySize, fd);
323            break;
324        default:
325            rsAssert(0);
326        }
327    }
328}
329
330void RsdShader::setupSampler(const Context *rsc, const Sampler *s, const Allocation *tex) {
331    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
332
333    GLenum trans[] = {
334        GL_NEAREST, //RS_SAMPLER_NEAREST,
335        GL_LINEAR, //RS_SAMPLER_LINEAR,
336        GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
337        GL_REPEAT, //RS_SAMPLER_WRAP,
338        GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
339        GL_LINEAR_MIPMAP_NEAREST, //RS_SAMPLER_LINEAR_MIP_NEAREST
340    };
341
342    GLenum transNP[] = {
343        GL_NEAREST, //RS_SAMPLER_NEAREST,
344        GL_LINEAR, //RS_SAMPLER_LINEAR,
345        GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
346        GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
347        GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
348        GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_NEAREST,
349    };
350
351    // This tells us the correct texture type
352    DrvAllocation *drvTex = (DrvAllocation *)tex->mHal.drv;
353    const GLenum target = drvTex->glTarget;
354
355    if (!dc->gl.gl.OES_texture_npot && tex->getType()->getIsNp2()) {
356        if (tex->getHasGraphicsMipmaps() &&
357            (dc->gl.gl.NV_texture_npot_2D_mipmap || dc->gl.gl.IMG_texture_npot)) {
358            if (dc->gl.gl.NV_texture_npot_2D_mipmap) {
359                RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
360                            trans[s->mHal.state.minFilter]);
361            } else {
362                switch (trans[s->mHal.state.minFilter]) {
363                case GL_LINEAR_MIPMAP_LINEAR:
364                    RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
365                                GL_LINEAR_MIPMAP_NEAREST);
366                    break;
367                default:
368                    RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
369                                trans[s->mHal.state.minFilter]);
370                    break;
371                }
372            }
373        } else {
374            RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
375                        transNP[s->mHal.state.minFilter]);
376        }
377        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MAG_FILTER,
378                    transNP[s->mHal.state.magFilter]);
379        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_S, transNP[s->mHal.state.wrapS]);
380        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_T, transNP[s->mHal.state.wrapT]);
381    } else {
382        if (tex->getHasGraphicsMipmaps()) {
383            RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
384                        trans[s->mHal.state.minFilter]);
385        } else {
386            RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MIN_FILTER,
387                        transNP[s->mHal.state.minFilter]);
388        }
389        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_MAG_FILTER, trans[s->mHal.state.magFilter]);
390        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_S, trans[s->mHal.state.wrapS]);
391        RSD_CALL_GL(glTexParameteri, target, GL_TEXTURE_WRAP_T, trans[s->mHal.state.wrapT]);
392    }
393
394    float anisoValue = rsMin(dc->gl.gl.EXT_texture_max_aniso, s->mHal.state.aniso);
395    if (dc->gl.gl.EXT_texture_max_aniso > 1.0f) {
396        RSD_CALL_GL(glTexParameterf, target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
397    }
398
399    rsdGLCheckError(rsc, "Sampler::setup tex env");
400}
401
402void RsdShader::setupTextures(const Context *rsc, RsdShaderCache *sc) {
403    if (mRSProgram->mHal.state.texturesCount == 0) {
404        return;
405    }
406
407    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
408
409    uint32_t numTexturesToBind = mRSProgram->mHal.state.texturesCount;
410    uint32_t numTexturesAvailable = dc->gl.gl.maxFragmentTextureImageUnits;
411    if (numTexturesToBind >= numTexturesAvailable) {
412        ALOGE("Attempting to bind %u textures on shader id %u, but only %u are available",
413             mRSProgram->mHal.state.texturesCount, (uint32_t)this, numTexturesAvailable);
414        rsc->setError(RS_ERROR_BAD_SHADER, "Cannot bind more textuers than available");
415        numTexturesToBind = numTexturesAvailable;
416    }
417
418    for (uint32_t ct=0; ct < numTexturesToBind; ct++) {
419        RSD_CALL_GL(glActiveTexture, GL_TEXTURE0 + ct);
420        RSD_CALL_GL(glUniform1i, sc->fragUniformSlot(mTextureUniformIndexStart + ct), ct);
421
422        if (!mRSProgram->mHal.state.textures[ct]) {
423            // if nothing is bound, reset to default GL texture
424            RSD_CALL_GL(glBindTexture, mTextureTargets[ct], 0);
425            continue;
426        }
427
428        DrvAllocation *drvTex = (DrvAllocation *)mRSProgram->mHal.state.textures[ct]->mHal.drv;
429        if (drvTex->glTarget != GL_TEXTURE_2D && drvTex->glTarget != GL_TEXTURE_CUBE_MAP) {
430            ALOGE("Attempting to bind unknown texture to shader id %u, texture unit %u", (uint)this, ct);
431            rsc->setError(RS_ERROR_BAD_SHADER, "Non-texture allocation bound to a shader");
432        }
433        RSD_CALL_GL(glBindTexture, drvTex->glTarget, drvTex->textureID);
434        rsdGLCheckError(rsc, "ProgramFragment::setup tex bind");
435        if (mRSProgram->mHal.state.samplers[ct]) {
436            setupSampler(rsc, mRSProgram->mHal.state.samplers[ct],
437                         mRSProgram->mHal.state.textures[ct]);
438        } else {
439            RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
440            RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
441            RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
442            RSD_CALL_GL(glTexParameteri, drvTex->glTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
443            rsdGLCheckError(rsc, "ProgramFragment::setup tex env");
444        }
445        rsdGLCheckError(rsc, "ProgramFragment::setup uniforms");
446    }
447
448    RSD_CALL_GL(glActiveTexture, GL_TEXTURE0);
449    mDirty = false;
450    rsdGLCheckError(rsc, "ProgramFragment::setup");
451}
452
453void RsdShader::setupUserConstants(const Context *rsc, RsdShaderCache *sc, bool isFragment) {
454    uint32_t uidx = 0;
455    for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) {
456        Allocation *alloc = mRSProgram->mHal.state.constants[ct];
457        if (!alloc) {
458            ALOGE("Attempting to set constants on shader id %u, but alloc at slot %u is not set",
459                 (uint32_t)this, ct);
460            rsc->setError(RS_ERROR_BAD_SHADER, "No constant allocation bound");
461            continue;
462        }
463
464        const uint8_t *data = static_cast<const uint8_t *>(alloc->getPtr());
465        const Element *e = mRSProgram->mHal.state.constantTypes[ct]->getElement();
466        for (uint32_t field=0; field < e->getFieldCount(); field++) {
467            const Element *f = e->getField(field);
468            const char *fieldName = e->getFieldName(field);
469            // If this field is padding, skip it
470            if (fieldName[0] == '#') {
471                continue;
472            }
473
474            uint32_t offset = e->getFieldOffsetBytes(field);
475            const float *fd = reinterpret_cast<const float *>(&data[offset]);
476
477            int32_t slot = -1;
478            uint32_t arraySize = 1;
479            if (!isFragment) {
480                slot = sc->vtxUniformSlot(uidx);
481                arraySize = sc->vtxUniformSize(uidx);
482            } else {
483                slot = sc->fragUniformSlot(uidx);
484                arraySize = sc->fragUniformSize(uidx);
485            }
486            if (rsc->props.mLogShadersUniforms) {
487                ALOGV("Uniform  slot=%i, offset=%i, constant=%i, field=%i, uidx=%i, name=%s",
488                     slot, offset, ct, field, uidx, fieldName);
489            }
490            uidx ++;
491            if (slot < 0) {
492                continue;
493            }
494
495            if (rsc->props.mLogShadersUniforms) {
496                logUniform(f, fd, arraySize);
497            }
498            setUniform(rsc, f, fd, slot, arraySize);
499        }
500    }
501}
502
503void RsdShader::setup(const android::renderscript::Context *rsc, RsdShaderCache *sc) {
504
505    setupUserConstants(rsc, sc, mType == GL_FRAGMENT_SHADER);
506    setupTextures(rsc, sc);
507}
508
509void RsdShader::initAttribAndUniformArray() {
510    mAttribCount = 0;
511    for (uint32_t ct=0; ct < mRSProgram->mHal.state.inputElementsCount; ct++) {
512        const Element *elem = mRSProgram->mHal.state.inputElements[ct];
513        for (uint32_t field=0; field < elem->getFieldCount(); field++) {
514            if (elem->getFieldName(field)[0] != '#') {
515                mAttribCount ++;
516            }
517        }
518    }
519
520    mUniformCount = 0;
521    for (uint32_t ct=0; ct < mRSProgram->mHal.state.constantsCount; ct++) {
522        const Element *elem = mRSProgram->mHal.state.constantTypes[ct]->getElement();
523
524        for (uint32_t field=0; field < elem->getFieldCount(); field++) {
525            if (elem->getFieldName(field)[0] != '#') {
526                mUniformCount ++;
527            }
528        }
529    }
530    mUniformCount += mRSProgram->mHal.state.texturesCount;
531
532    if (mAttribCount) {
533        mAttribNames = new String8[mAttribCount];
534    }
535    if (mUniformCount) {
536        mUniformNames = new String8[mUniformCount];
537        mUniformArraySizes = new uint32_t[mUniformCount];
538    }
539
540    mTextureCount = mRSProgram->mHal.state.texturesCount;
541    if (mTextureCount) {
542        mTextureTargets = new uint32_t[mTextureCount];
543    }
544}
545
546void RsdShader::initAddUserElement(const Element *e, String8 *names, uint32_t *arrayLengths,
547                                   uint32_t *count, const char *prefix) {
548    rsAssert(e->getFieldCount());
549    for (uint32_t ct=0; ct < e->getFieldCount(); ct++) {
550        const Element *ce = e->getField(ct);
551        if (ce->getFieldCount()) {
552            initAddUserElement(ce, names, arrayLengths, count, prefix);
553        } else if (e->getFieldName(ct)[0] != '#') {
554            String8 tmp(prefix);
555            tmp.append(e->getFieldName(ct));
556            names[*count].setTo(tmp.string());
557            if (arrayLengths) {
558                arrayLengths[*count] = e->getFieldArraySize(ct);
559            }
560            (*count)++;
561        }
562    }
563}
564