void FontState::initTextTexture() {
    ObjectBaseRef<const Element> alphaElem = Element::createRef(mRSC, RS_TYPE_UNSIGNED_8,
                                                                RS_KIND_PIXEL_A, true, 1);

    // We will allocate a texture to initially hold 32 character bitmaps
    mCacheHeight = 256;
    mCacheWidth = 1024;
    ObjectBaseRef<Type> texType = Type::getTypeRef(mRSC, alphaElem.get(), mCacheWidth, mCacheHeight);

    mCacheBuffer = new uint8_t[mCacheWidth * mCacheHeight];


    Allocation *cacheAlloc = Allocation::createAllocation(mRSC, texType.get(),
                                RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE);
    mTextTexture.set(cacheAlloc);

    // Split up our cache texture into lines of certain widths
    int32_t nextLine = 0;
    mCacheLines.push(new CacheTextureLine(16, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(24, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(24, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(32, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(32, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(40, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(texType->getDimY() - nextLine, texType->getDimX(), nextLine, 0));
}
void ProgramFragmentState::init(Context *rsc) {
    const char *shaderString =
            RS_SHADER_INTERNAL
            "varying lowp vec4 varColor;\n"
            "varying vec2 varTex0;\n"
            "void main() {\n"
            "  lowp vec4 col = UNI_Color;\n"
            "  gl_FragColor = col;\n"
            "}\n";

    ObjectBaseRef<const Element> colorElem = Element::createRef(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);

    const char *enames[] = { "Color" };
    const Element *eins[] = {colorElem.get()};
    ObjectBaseRef<const Element> constInput = Element::create(rsc, 1, eins, enames);

    ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1);

    uintptr_t tmp[2];
    tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
    tmp[1] = (uintptr_t)inputType.get();

    Allocation *constAlloc = Allocation::createAllocation(rsc, inputType.get(),
                              RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
    ProgramFragment *pf = new ProgramFragment(rsc, shaderString, strlen(shaderString),
                                              nullptr, 0, nullptr, tmp, 2);
    pf->bindAllocation(rsc, constAlloc, 0);
    pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);

    mDefault.set(pf);
}
void ProgramFragmentState::init(Context *rsc) {
    String8 shaderString(RS_SHADER_INTERNAL);
    shaderString.append("varying lowp vec4 varColor;\n");
    shaderString.append("varying vec2 varTex0;\n");
    shaderString.append("void main() {\n");
    shaderString.append("  lowp vec4 col = UNI_Color;\n");
    shaderString.append("  gl_FragColor = col;\n");
    shaderString.append("}\n");

    ObjectBaseRef<const Element> colorElem = Element::createRef(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
    Element::Builder builder;
    builder.add(colorElem.get(), "Color", 1);
    ObjectBaseRef<const Element> constInput = builder.create(rsc);

    ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false);

    uint32_t tmp[2];
    tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
    tmp[1] = (uint32_t)inputType.get();

    Allocation *constAlloc = Allocation::createAllocation(rsc, inputType.get(),
                              RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
    ProgramFragment *pf = new ProgramFragment(rsc, shaderString.string(),
                                              shaderString.length(), tmp, 2);
    pf->bindAllocation(rsc, constAlloc, 0);
    pf->setConstantColor(rsc, 1.0f, 1.0f, 1.0f, 1.0f);

    mDefault.set(pf);
}
void FontState::initRenderState() {
    const char *shaderString = "varying vec2 varTex0;\n"
                               "void main() {\n"
                               "  lowp vec4 col = UNI_Color;\n"
                               "  col.a = texture2D(UNI_Tex0, varTex0.xy).a;\n"
                               "  col.a = pow(col.a, UNI_Gamma);\n"
                               "  gl_FragColor = col;\n"
                               "}\n";

    const char *textureNames[] = { "Tex0" };
    const size_t textureNamesLengths[] = { 4 };
    size_t numTextures = sizeof(textureNamesLengths)/sizeof(*textureNamesLengths);

    ObjectBaseRef<const Element> colorElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32,
                                                                RS_KIND_USER, false, 4);
    ObjectBaseRef<const Element> gammaElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32,
                                                                RS_KIND_USER, false, 1);

    const char *ebn1[] = { "Color", "Gamma" };
    const Element *ebe1[] = {colorElem.get(), gammaElem.get()};
    ObjectBaseRef<const Element> constInput = Element::create(mRSC, 2, ebe1, ebn1);

    ObjectBaseRef<Type> inputType = Type::getTypeRef(mRSC, constInput.get(), 1, 0, 0, false, false, 0);

    uintptr_t tmp[4];
    tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
    tmp[1] = (uintptr_t)inputType.get();
    tmp[2] = RS_PROGRAM_PARAM_TEXTURE_TYPE;
    tmp[3] = RS_TEXTURE_2D;

    mFontShaderFConstant.set(Allocation::createAllocation(mRSC, inputType.get(),
                                                          RS_ALLOCATION_USAGE_SCRIPT |
                                                          RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS));
    ProgramFragment *pf = new ProgramFragment(mRSC, shaderString, strlen(shaderString),
                                              textureNames, numTextures, textureNamesLengths,
                                              tmp, 4);
    mFontShaderF.set(pf);
    mFontShaderF->bindAllocation(mRSC, mFontShaderFConstant.get(), 0);

    mFontSampler.set(Sampler::getSampler(mRSC, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST,
                                         RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP,
                                         RS_SAMPLER_CLAMP).get());
    mFontShaderF->bindSampler(mRSC, 0, mFontSampler.get());

    mFontProgramStore.set(ProgramStore::getProgramStore(mRSC, true, true, true, true,
                                                        false, false,
                                                        RS_BLEND_SRC_SRC_ALPHA,
                                                        RS_BLEND_DST_ONE_MINUS_SRC_ALPHA,
                                                        RS_DEPTH_FUNC_ALWAYS).get());
    mFontProgramStore->init();
}
void Allocation::resize1D(Context *rsc, uint32_t dimX) {
    uint32_t oldDimX = mHal.state.dimensionX;
    if (dimX == oldDimX) {
        return;
    }

    ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX);
    if (dimX < oldDimX) {
        decRefs(getPtr(), oldDimX - dimX, dimX);
    }
    rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences);
    setType(t.get());
    updateCache();
}
Example #6
0
RsSampler rsi_SamplerCreate(Context * rsc,
                            RsSamplerValue magFilter,
                            RsSamplerValue minFilter,
                            RsSamplerValue wrapS,
                            RsSamplerValue wrapT,
                            RsSamplerValue wrapR,
                            float aniso) {
    ObjectBaseRef<Sampler> s = Sampler::getSampler(rsc, magFilter, minFilter,
                                                   wrapS, wrapT, wrapR, aniso);
    s->incUserRef();
    return s.get();
}
void ProgramVertexState::init(Context *rsc) {
    ObjectBaseRef<const Element> matrixElem = Element::createRef(rsc, RS_TYPE_MATRIX_4X4,
                                                                 RS_KIND_USER, false, 1);
    ObjectBaseRef<const Element> f2Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
                                                             RS_KIND_USER, false, 2);
    ObjectBaseRef<const Element> f3Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
                                                             RS_KIND_USER, false, 3);
    ObjectBaseRef<const Element> f4Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
                                                             RS_KIND_USER, false, 4);

    const char *ebn1[] = { "MV", "P", "TexMatrix", "MVP" };
    const Element *ebe1[] = {matrixElem.get(), matrixElem.get(),
            matrixElem.get(), matrixElem.get()};
    ObjectBaseRef<const Element> constInput = Element::create(rsc, 4, ebe1, ebn1);

    const char *ebn2[] = { "position", "color", "normal", "texture0" };
    const Element *ebe2[] = {f4Elem.get(), f4Elem.get(), f3Elem.get(), f2Elem.get()};
    ObjectBaseRef<const Element> attrElem = Element::create(rsc, 4, ebe2, ebn2);

    ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false, 0);

    const char *shaderString =
            RS_SHADER_INTERNAL
            "varying vec4 varColor;\n"
            "varying vec2 varTex0;\n"
            "void main() {\n"
            "  gl_Position = UNI_MVP * ATTRIB_position;\n"
            "  gl_PointSize = 1.0;\n"
            "  varColor = ATTRIB_color;\n"
            "  varTex0 = ATTRIB_texture0;\n"
            "}\n";

    uintptr_t tmp[4];
    tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
    tmp[1] = (uintptr_t)inputType.get();
    tmp[2] = RS_PROGRAM_PARAM_INPUT;
    tmp[3] = (uintptr_t)attrElem.get();

    ProgramVertex *pv = new ProgramVertex(rsc, shaderString, strlen(shaderString),
                                          NULL, 0, NULL, tmp, 4);
    Allocation *alloc = Allocation::createAllocation(rsc, inputType.get(),
                              RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
    pv->bindAllocation(rsc, alloc, 0);

    mDefaultAlloc.set(alloc);
    mDefault.set(pv);

    updateSize(rsc);
}
void ProgramVertexState::init(Context *rsc) {
    ObjectBaseRef<const Element> matrixElem = Element::createRef(rsc, RS_TYPE_MATRIX_4X4,
                                                                 RS_KIND_USER, false, 1);
    ObjectBaseRef<const Element> f2Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
                                                             RS_KIND_USER, false, 2);
    ObjectBaseRef<const Element> f3Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
                                                             RS_KIND_USER, false, 3);
    ObjectBaseRef<const Element> f4Elem = Element::createRef(rsc, RS_TYPE_FLOAT_32,
                                                             RS_KIND_USER, false, 4);

    Element::Builder constBuilder;
    constBuilder.add(matrixElem.get(), "MV", 1);
    constBuilder.add(matrixElem.get(), "P", 1);
    constBuilder.add(matrixElem.get(), "TexMatrix", 1);
    constBuilder.add(matrixElem.get(), "MVP", 1);
    ObjectBaseRef<const Element> constInput = constBuilder.create(rsc);

    Element::Builder inputBuilder;
    inputBuilder.add(f4Elem.get(), "position", 1);
    inputBuilder.add(f4Elem.get(), "color", 1);
    inputBuilder.add(f3Elem.get(), "normal", 1);
    inputBuilder.add(f2Elem.get(), "texture0", 1);
    ObjectBaseRef<const Element> attrElem = inputBuilder.create(rsc);

    ObjectBaseRef<Type> inputType = Type::getTypeRef(rsc, constInput.get(), 1, 0, 0, false, false);

    String8 shaderString(RS_SHADER_INTERNAL);
    shaderString.append("varying vec4 varColor;\n");
    shaderString.append("varying vec2 varTex0;\n");
    shaderString.append("void main() {\n");
    shaderString.append("  gl_Position = UNI_MVP * ATTRIB_position;\n");
    shaderString.append("  gl_PointSize = 1.0;\n");
    shaderString.append("  varColor = ATTRIB_color;\n");
    shaderString.append("  varTex0 = ATTRIB_texture0;\n");
    shaderString.append("}\n");

    uint32_t tmp[4];
    tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
    tmp[1] = (uint32_t)inputType.get();
    tmp[2] = RS_PROGRAM_PARAM_INPUT;
    tmp[3] = (uint32_t)attrElem.get();

    ProgramVertex *pv = new ProgramVertex(rsc, shaderString.string(),
                                          shaderString.length(), tmp, 4);
    Allocation *alloc = Allocation::createAllocation(rsc, inputType.get(),
                              RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_GRAPHICS_CONSTANTS);
    pv->bindAllocation(rsc, alloc, 0);

    mDefaultAlloc.set(alloc);
    mDefault.set(pv);

    updateSize(rsc);
}
// Avoid having to reallocate memory and render quad by quad
void FontState::initVertexArrayBuffers() {
    // Now lets write index data
    ObjectBaseRef<const Element> indexElem = Element::createRef(mRSC, RS_TYPE_UNSIGNED_16, RS_KIND_USER, false, 1);
    uint32_t numIndicies = mMaxNumberOfQuads * 6;
    ObjectBaseRef<Type> indexType = Type::getTypeRef(mRSC, indexElem.get(), numIndicies, 0, 0, false, false, 0);

    Allocation *indexAlloc = Allocation::createAllocation(mRSC, indexType.get(),
                                                          RS_ALLOCATION_USAGE_SCRIPT |
                                                          RS_ALLOCATION_USAGE_GRAPHICS_VERTEX);
    uint16_t *indexPtr = (uint16_t*)mRSC->mHal.funcs.allocation.lock1D(mRSC, indexAlloc);

    // Four verts, two triangles , six indices per quad
    for (uint32_t i = 0; i < mMaxNumberOfQuads; i ++) {
        int32_t i6 = i * 6;
        int32_t i4 = i * 4;

        indexPtr[i6 + 0] = i4 + 0;
        indexPtr[i6 + 1] = i4 + 1;
        indexPtr[i6 + 2] = i4 + 2;

        indexPtr[i6 + 3] = i4 + 0;
        indexPtr[i6 + 4] = i4 + 2;
        indexPtr[i6 + 5] = i4 + 3;
    }

    indexAlloc->sendDirty(mRSC);

    ObjectBaseRef<const Element> posElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 3);
    ObjectBaseRef<const Element> texElem = Element::createRef(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 2);

    const char *ebn1[] = { "position", "texture0" };
    const Element *ebe1[] = {posElem.get(), texElem.get()};
    ObjectBaseRef<const Element> vertexDataElem = Element::create(mRSC, 2, ebe1, ebn1);

    ObjectBaseRef<Type> vertexDataType = Type::getTypeRef(mRSC, vertexDataElem.get(),
                                                          mMaxNumberOfQuads * 4,
                                                          0, 0, false, false, 0);

    Allocation *vertexAlloc = Allocation::createAllocation(mRSC, vertexDataType.get(),
                                                           RS_ALLOCATION_USAGE_SCRIPT);
    mTextMeshPtr = (float*)mRSC->mHal.funcs.allocation.lock1D(mRSC, vertexAlloc);

    mMesh.set(new Mesh(mRSC, 1, 1));
    mMesh->setVertexBuffer(vertexAlloc, 0);
    mMesh->setPrimitive(indexAlloc, RS_PRIMITIVE_TRIANGLE, 0);
    mMesh->init();
    mRSC->mHal.funcs.allocation.unlock1D(mRSC, indexAlloc);
    mRSC->mHal.funcs.allocation.unlock1D(mRSC, vertexAlloc);
}
RsProgramRaster rsi_ProgramRasterCreate(Context * rsc, bool pointSprite, RsCullMode cull) {
    ObjectBaseRef<ProgramRaster> pr = ProgramRaster::getProgramRaster(rsc, pointSprite, cull);
    pr->incUserRef();
    return pr.get();
}