精品国产一区在线_av无码中文字幕无码王_天海翼三点刺激高潮不停_好硬好大好爽视频_欧美高清一区三区在线专区_香蕉黄色片

  • 回復(fù)
  • 收藏
  • 點(diǎn)贊
  • 分享
  • 發(fā)新帖

【基礎(chǔ)】Android RenderScript 使用 Struct 及其下標(biāo)的賦值

這是一個關(guān)于RenderScript如何使用 Struct 的文章,是學(xué)習(xí)RenderScript 一個必須要掌握的基礎(chǔ)知識點(diǎn)。
大綱
如何定義Struct
如何得到指針長度并循環(huán)為指針賦值
整體DEMO代碼
如何定義Struct
RenderScript 里面定義結(jié)構(gòu)有兩種定義方法,參考如下:
1.
typedef struct tempArray
{
    float2 position;
    float size;
} Array_T;

Array_T *myArray;


2.
//定義一個struct
typedef struct __attribute__((packed, aligned(4))) tempArray {
     int temp;
} Array_T;
Array_T *myArray;


RenderScript 定義Struct 成功后,會自動生成一個java文件,如上面的tempArray名稱的結(jié)構(gòu),會生產(chǎn)這個文件:ScriptField_tempArray,代碼如下:
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* This file is auto-generated. DO NOT MODIFY!
* The source Renderscript file: /home/terry/workspace/RenderScriptsArray/src/com/xuzhi/renderScriptArray/array.rs
*/
package com.xuzhi.renderScriptArray;

import android.renderscript.*;
import android.content.res.Resources;

/**
* @hide
*/
public class ScriptField_tempArray extends android.renderscript.Script.FieldBase {
    static public class Item {
        public static final int sizeof = 4;

        int temp;

        Item() {
        }

    }

    private Item mItemArray[];
    private FieldPacker mIOBuffer;
    public static Element createElement(RenderScript rs) {
        Element.Builder eb = new Element.Builder(rs);
        eb.add(Element.I32(rs), "temp");
        return eb.create();
    }

    public  ScriptField_tempArray(RenderScript rs, int count) {
        mItemArray = null;
        mIOBuffer = null;
        mElement = createElement(rs);
        init(rs, count);
    }

    public  ScriptField_tempArray(RenderScript rs, int count, int usages) {
        mItemArray = null;
        mIOBuffer = null;
        mElement = createElement(rs);
        init(rs, count, usages);
    }

    private void copyToArrayLocal(Item i, FieldPacker fp) {
        fp.addI32(i.temp);
    }

    private void copyToArray(Item i, int index) {
        if (mIOBuffer == null) mIOBuffer = new FieldPacker(Item.sizeof * getType().getX()/* count */);
        mIOBuffer.reset(index * Item.sizeof);
        copyToArrayLocal(i, mIOBuffer);
    }

    public synchronized void set(Item i, int index, boolean copyNow) {
        if (mItemArray == null) mItemArray = new Item[getType().getX() /* count */];
        mItemArray[index] = i;
        if (copyNow)  {
            copyToArray(i, index);
            FieldPacker fp = new FieldPacker(Item.sizeof);
            copyToArrayLocal(i, fp);
            mAllocation.setFromFieldPacker(index, fp);
        }

    }

    public synchronized Item get(int index) {
        if (mItemArray == null) return null;
        return mItemArray[index];
    }

    public synchronized void set_temp(int index, int v, boolean copyNow) {
        if (mIOBuffer == null) mIOBuffer = new FieldPacker(Item.sizeof * getType().getX()/* count */);
        if (mItemArray == null) mItemArray = new Item[getType().getX() /* count */];
        if (mItemArray[index] == null) mItemArray[index] = new Item();
        mItemArray[index].temp = v;
        if (copyNow)  {
            mIOBuffer.reset(index * Item.sizeof);
            mIOBuffer.addI32(v);
            FieldPacker fp = new FieldPacker(4);
            fp.addI32(v);
            mAllocation.setFromFieldPacker(index, 0, fp);
        }

    }

    public synchronized int get_temp(int index) {
        if (mItemArray == null) return 0;
        return mItemArray[index].temp;
    }

    public synchronized void copyAll() {
        for (int ct = 0; ct < mItemArray.length; ct++) copyToArray(mItemArray[ct], ct);
        mAllocation.setFromFieldPacker(0, mIOBuffer);
    }

    public synchronized void resize(int newSize) {
        if (mItemArray != null)  {
            int oldSize = mItemArray.length;
            int copySize = Math.min(oldSize, newSize);
            if (newSize == oldSize) return;
            Item ni[] = new Item[newSize];
            System.arraycopy(mItemArray, 0, ni, 0, copySize);
            mItemArray = ni;
        }

        mAllocation.resize(newSize);
        if (mIOBuffer != null) mIOBuffer = new FieldPacker(Item.sizeof * getType().getX()/* count */);
    }
}

生成的代碼是提供給你做內(nèi)存分配和操作類似數(shù)組的功能。
如何得到指針長度并循環(huán)為指針賦值
RenderScript 有兩個函數(shù)是專門用來獲取指針長度的:
rsGetAllocation: 返回一個己經(jīng)分配過地址的指針
rsAllocationGetDimX :獲取返回指針的長度
通過將這兩個函數(shù)做組合使用可以返回指針長度,代碼如下:
const int size = rsAllocationGetDimX(rsGetAllocation(myArray));

取得了長度即可以為指針內(nèi)部變量賦值,代碼如下:
for(int i=0;itemp=i;    //循環(huán)賦值

    rsDebug("current value is ====>", array->temp);    //打印當(dāng)前值
    //指向下個指針
        array++;

    }

整體DEMO代碼
本DEMO沒有任何界面 ,只是演示如何使用struct并打印出指針下標(biāo)原素的值,如此簡單, 涉及的rs文件代碼如下:
#pragma version(1)
#pragma rs java_package_name(com.xuzhi.renderScriptArray)

#include "rs_graphics.rsh"

static int initialized = 0;

//定義一個struct
typedef struct __attribute__((packed, aligned(4))) tempArray {
     int temp;
} Array_T;
Array_T *myArray;



static void  initArray(){
    Array_T *array=myArray;
    //得到struct長度
    //1.返回一個己經(jīng)分配過地址的指針
    //2.獲取返回指針的長度
    const int size = rsAllocationGetDimX(rsGetAllocation(myArray));
    for(int i=0;itemp=i;    //循環(huán)賦值

    rsDebug("current value is ====>", array->temp);    //打印當(dāng)前值
    //指向下個指針
        array++;
    }
}


int root(){
rsgClearColor(0.0f, 1.0f, 0.0f, 1.0f); 
if(initialized==0){
    initArray(); 
    initialized=1;
    }

    return 16;

} 


java 代碼如下:
package com.xuzhi.renderScriptArray;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScriptGL;

public class RenderScriptsArrayActivity extends Activity {



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new renderScriptView(this)); 
    }


    public class renderScriptRS { 

        private ScriptC_array mScript;

        private ScriptField_tempArray array;

        RenderScriptGL mRS;

        public renderScriptRS(RenderScriptGL rs,Resources resource) {
            // TODO Auto-generated constructor stub
            mRS=rs;
            //初始化struct 并為其指定有多少個下標(biāo)
            array=new ScriptField_tempArray(mRS, 10,Allocation.USAGE_SCRIPT|Allocation.USAGE_GRAPHICS_VERTEX);

            //實(shí)始化CcriptC
            mScript=new ScriptC_array(mRS, resource, R.raw.array);
            //綁定struct
            mScript.bind_myArray(array);
            //綁定腳本
            mRS.bindRootScript(mScript);

        }
    }



    public class renderScriptView extends RSSurfaceView {
        private RenderScriptGL mRS;
        private renderScriptRS mRender;

        public renderScriptView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }


        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
            android.util.Log.e("rs", "onAttachedToWindow");
            if (mRS == null) {
                RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
                mRS = createRenderScriptGL(sc);
                mRender = new renderScriptRS(mRS, getResources()); 
            }
        }

        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow(); 
            if (mRS != null) {
                mRS = null;
                destroyRenderScriptGL();
            }
        }

    }
}

都加了注釋了,最主要的四段代碼標(biāo)注了紅色,要注意。
運(yùn)行結(jié)果:
03-09 15:47:50.492: D/RenderScript(2298): current value is ====> 0  0x0
03-09 15:47:50.507: D/RenderScript(2298): current value is ====> 1  0x1
03-09 15:47:50.507: D/RenderScript(2298): current value is ====> 2  0x2
03-09 15:47:50.507: D/RenderScript(2298): current value is ====> 3  0x3
03-09 15:47:50.507: D/RenderScript(2298): current value is ====> 4  0x4
03-09 15:47:50.507: D/RenderScript(2298): current value is ====> 5  0x5
03-09 15:47:50.507: D/RenderScript(2298): current value is ====> 6  0x6
03-09 15:47:50.507: D/RenderScript(2298): current value is ====> 7  0x7
03-09 15:47:50.507: D/RenderScript(2298): current value is ====> 8  0x8
03-09 15:47:50.507: D/RenderScript(2298): current value is ====> 9  0x9
全部回復(fù)(0)
正序查看
倒序查看
現(xiàn)在還沒有回復(fù)呢,說說你的想法
發(fā)
主站蜘蛛池模板: 亚洲一区网址 | 毛片在线网 | 久久久99精品 | 成人在线国产精品 | 亚洲乱码一区二区三区在线观看 | 91精品国产综合久久福利不卡 | 亚洲日本一区二区一本一道 | 久久AV无码精品人妻系列试探 | 日韩一卡二卡三卡四卡免费观在线 | 久久丁香综合 | 久草在线观看资源 | 成人444kkkk在线观看 | 爆乳上司julia中文字幕 | 亚洲少妇最新在线视频 | 亚洲日韩成人性av网站 | 国产农村女人一级毛片 | 久久香蕉综合 | 日韩精品在线观看网站 | av播放网站 | 日韩性欧美 | 在线播放www | 把女人弄爽特黄a大片 | 国产成人www免费人成看片 | 亚洲午夜无码片在线观看影院百度 | 欧美一区二区三区爽大粗免费 | 在线亚洲97SE亚洲综合在线 | 国产无套内射普通话对白 | 欧美精品综合A片在线观看 区一区二在线观看 | 中文字幕第一 | 日本不卡免费在线 | 日本aⅴ毛片成人偷拍 | caoporn国产 | 日本xxx性 | 黄色成年人在线观看 | 性高爱久久久久久久久 | h333.tv免费看片| 国产精品免费久久久久软件 | 狠狠色噜噜狠狠狠色综合 | 国产成人免费在线视频 | 99在线精品视频播放免费观看 | 在线日韩中文字幕 |